Files
restic/internal/global/global_test.go

52 lines
1.3 KiB
Go
Raw Normal View History

package global
2019-05-27 21:20:54 +02:00
import (
"context"
"os"
2020-09-30 22:32:14 +02:00
"path/filepath"
"strings"
2019-05-27 21:20:54 +02:00
"testing"
rtest "github.com/restic/restic/internal/test"
)
2020-09-30 22:32:14 +02:00
func TestReadRepo(t *testing.T) {
tempDir := rtest.TempDir(t)
2020-09-30 22:32:14 +02:00
// test --repo option
var gopts Options
gopts.Repo = tempDir
repo, err := readRepo(gopts)
2020-09-30 22:32:14 +02:00
rtest.OK(t, err)
rtest.Equals(t, tempDir, repo)
// test --repository-file option
foo := filepath.Join(tempDir, "foo")
err = os.WriteFile(foo, []byte(tempDir+"\n"), 0666)
2020-09-30 22:32:14 +02:00
rtest.OK(t, err)
var gopts2 Options
gopts2.RepositoryFile = foo
repo, err = readRepo(gopts2)
2020-09-30 22:32:14 +02:00
rtest.OK(t, err)
rtest.Equals(t, tempDir, repo)
var gopts3 Options
gopts3.RepositoryFile = foo + "-invalid"
_, err = readRepo(gopts3)
2020-09-30 22:32:14 +02:00
if err == nil {
t.Fatal("must not read repository path from invalid file path")
}
}
func TestReadEmptyPassword(t *testing.T) {
opts := Options{InsecureNoPassword: true}
password, err := readPassword(context.TODO(), opts, "test")
rtest.OK(t, err)
rtest.Equals(t, "", password, "got unexpected password")
2025-09-28 21:44:40 +02:00
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)
}