2019-05-27 19:20:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-12-02 18:36:43 +00:00
|
|
|
"os"
|
2020-09-30 20:32:14 +00:00
|
|
|
"path/filepath"
|
2019-05-27 19:20:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-09-30 20:32:14 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
2019-05-27 19:20:54 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_PrintFunctionsRespectsGlobalStdout(t *testing.T) {
|
|
|
|
gopts := globalOptions
|
|
|
|
defer func() {
|
|
|
|
globalOptions = gopts
|
|
|
|
}()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
globalOptions.stdout = buf
|
|
|
|
|
|
|
|
for _, p := range []func(){
|
|
|
|
func() { Println("message") },
|
|
|
|
func() { Print("message\n") },
|
|
|
|
func() { Printf("mes%s\n", "sage") },
|
|
|
|
} {
|
|
|
|
p()
|
|
|
|
rtest.Equals(t, "message\n", buf.String())
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 20:32:14 +00:00
|
|
|
|
|
|
|
func TestReadRepo(t *testing.T) {
|
2022-12-09 12:42:33 +00:00
|
|
|
tempDir := test.TempDir(t)
|
2020-09-30 20:32:14 +00: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 18:36:43 +00:00
|
|
|
err = os.WriteFile(foo, []byte(tempDir+"\n"), 0666)
|
2020-09-30 20:32:14 +00: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 19:43:53 +00:00
|
|
|
_, err = ReadRepo(opts3)
|
2020-09-30 20:32:14 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("must not read repository path from invalid file path")
|
|
|
|
}
|
|
|
|
}
|