package platform import ( "os" "path/filepath" "runtime" "strings" "testing" ) func TestExpandPathWindowsStyleVariables(t *testing.T) { t.Setenv("DEPLOYR_TEST_DIR", filepath.Join("C:", "Apps")) got := ExpandPath("%DEPLOYR_TEST_DIR%/tool/bin.exe") want := filepath.Clean(filepath.Join("C:", "Apps", "tool", "bin.exe")) if got != want { t.Errorf("ExpandPath = %q, want %q", got, want) } } func TestExpandPathUnixStyleVariables(t *testing.T) { t.Setenv("DEPLOYR_TEST_DIR", filepath.Join("/opt", "apps")) got := ExpandPath("$DEPLOYR_TEST_DIR/tool") want := filepath.Clean(filepath.Join("/opt", "apps", "tool")) if got != want { t.Errorf("ExpandPath = %q, want %q", got, want) } } // Variable names with parentheses are common on Windows -- %ProgramFiles(x86)% // is the one that matters -- so the expander has to accept them. func TestExpandPathHandlesParenthesisedVariables(t *testing.T) { t.Setenv("DEPLOYR_TEST(x86)", "/legacy") got := ExpandPath("%DEPLOYR_TEST(x86)%/app") if !strings.Contains(got, "legacy") { t.Errorf("ExpandPath = %q, want it to contain the expanded value", got) } } func TestExpandPathTilde(t *testing.T) { home, err := os.UserHomeDir() if err != nil { t.Skip("no home directory in this environment") } if got := ExpandPath("~/deployr"); got != filepath.Clean(filepath.Join(home, "deployr")) { t.Errorf("ExpandPath = %q", got) } } func TestExpandPathEmpty(t *testing.T) { if got := ExpandPath(""); got != "" { t.Errorf("ExpandPath(\"\") = %q, want an empty string", got) } } func TestPathExists(t *testing.T) { dir := t.TempDir() file := filepath.Join(dir, "present.txt") if err := os.WriteFile(file, []byte("x"), 0o644); err != nil { t.Fatal(err) } if !PathExists(file) { t.Error("an existing file should be detected") } if PathExists(filepath.Join(dir, "absent.txt")) { t.Error("a missing file should not be detected") } if !PathExists(filepath.Join(dir, "*.txt")) { t.Error("a glob matching a file should be detected") } if PathExists(filepath.Join(dir, "*.log")) { t.Error("a glob matching nothing should not be detected") } if PathExists("") { t.Error("an empty path should not be detected") } } func TestDirectoriesAreAbsolute(t *testing.T) { for name, dir := range map[string]string{ "data": DataDir(), "logs": LogDir(), "cache": CacheDir(), } { if !filepath.IsAbs(dir) { t.Errorf("%s dir %q is not absolute", name, dir) } } if len(CatalogDirs()) == 0 { t.Error("there should be at least one catalog overlay directory") } } func TestSupported(t *testing.T) { if !Supported() { t.Errorf("%s should be a supported platform", runtime.GOOS) } }