package config_test import ( "os" "path/filepath" "strings" "testing" "git.wntrmute.dev/kyle/crossbar/internal/config" ) // A file that exists but cannot be read is an error, and not a validation error: nothing about // the configuration has been judged. Only a missing file is "absent" (and that is an error too). func TestUnreadableFileIsAnError(t *testing.T) { if os.Geteuid() == 0 { t.Skip("root can read a 000 file") } dir := t.TempDir() path := filepath.Join(dir, "crossbar.toml") good, err := os.ReadFile(filepath.Join("testdata", "good.toml")) if err != nil { t.Fatal(err) } if err := os.WriteFile(path, good, 0o000); err != nil { t.Fatal(err) } _, err = config.Load(path) if err == nil { t.Fatal("Load on an unreadable file must fail") } if _, ok := config.IsError(err); ok { t.Errorf("an unreadable file is not a validation *Error: %v", err) } if !strings.HasPrefix(err.Error(), "config: ") { t.Errorf("Error() = %q, want the config: prefix", err.Error()) } } func TestDirectoryIsAnError(t *testing.T) { _, err := config.Load(t.TempDir()) if err == nil { t.Fatal("Load on a directory must fail") } if _, ok := config.IsError(err); ok { t.Errorf("a directory is not a validation *Error: %v", err) } }