2019-05-27 21:20:54 +02:00
package main
import (
2024-05-18 18:59:29 +02:00
"context"
2022-12-02 19:36:43 +01:00
"os"
2020-09-30 22:32:14 +02:00
"path/filepath"
2024-05-18 18:59:29 +02:00
"strings"
2019-05-27 21:20:54 +02:00
"testing"
rtest "github.com/restic/restic/internal/test"
)
func Test_PrintFunctionsRespectsGlobalStdout ( t * testing . T ) {
for _ , p := range [ ] func ( ) {
func ( ) { Println ( "message" ) } ,
func ( ) { Print ( "message\n" ) } ,
func ( ) { Printf ( "mes%s\n" , "sage" ) } ,
} {
2023-05-07 22:06:39 +02:00
buf , _ := withCaptureStdout ( func ( ) error {
p ( )
return nil
} )
2019-05-27 21:20:54 +02:00
rtest . Equals ( t , "message\n" , buf . String ( ) )
}
}
2020-09-30 22:32:14 +02:00
func TestReadRepo ( t * testing . T ) {
2023-05-18 17:44:56 +02:00
tempDir := rtest . TempDir ( t )
2020-09-30 22:32:14 +02:00
// test --repo option
var opts GlobalOptions
opts . Repo = tempDir
repo , err := ReadRepo ( opts )
rtest . OK ( t , err )
rtest . Equals ( t , tempDir , repo )
// test --repository-file option
foo := filepath . Join ( tempDir , "foo" )
2022-12-02 19:36:43 +01:00
err = os . WriteFile ( foo , [ ] byte ( tempDir + "\n" ) , 0666 )
2020-09-30 22:32:14 +02:00
rtest . OK ( t , err )
var opts2 GlobalOptions
opts2 . RepositoryFile = foo
repo , err = ReadRepo ( opts2 )
rtest . OK ( t , err )
rtest . Equals ( t , tempDir , repo )
var opts3 GlobalOptions
opts3 . RepositoryFile = foo + "-invalid"
2021-01-30 20:43:53 +01:00
_ , err = ReadRepo ( opts3 )
2020-09-30 22:32:14 +02:00
if err == nil {
t . Fatal ( "must not read repository path from invalid file path" )
}
}
2024-05-18 18:59:29 +02:00
func TestReadEmptyPassword ( t * testing . T ) {
opts := GlobalOptions { InsecureNoPassword : true }
password , err := ReadPassword ( context . TODO ( ) , opts , "test" )
rtest . OK ( t , err )
rtest . Equals ( t , "" , password , "got unexpected password" )
opts . password = "invalid"
_ , err = ReadPassword ( context . TODO ( ) , opts , "test" )
rtest . Assert ( t , strings . Contains ( err . Error ( ) , "must not be specified together with providing a password via a cli option or environment variable" ) , "unexpected error message, got %v" , err )
}