|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// TestFindConfigsDir_CandidatesCovered ensures all candidate paths in |
| 10 | +// findConfigsDir are exercised when configs dir exists in CWD. |
| 11 | +func TestFindConfigsDir_CandidatesCovered(t *testing.T) { |
| 12 | + originalWd, err := os.Getwd() |
| 13 | + if err != nil { |
| 14 | + t.Fatal(err) |
| 15 | + } |
| 16 | + defer os.Chdir(originalWd) |
| 17 | + |
| 18 | + tmpDir := t.TempDir() |
| 19 | + configsDir := filepath.Join(tmpDir, "configs") |
| 20 | + if err := os.Mkdir(configsDir, 0755); err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + if err := os.Chdir(tmpDir); err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + |
| 27 | + dir := findConfigsDir() |
| 28 | + if dir == "" { |
| 29 | + t.Error("expected findConfigsDir to find configs in CWD") |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// TestDefaultBootstrapDataDir_FallbackGetwd covers the os.Getwd fallback |
| 34 | +// path in DefaultBootstrapDataDir when findConfigsDir returns "". |
| 35 | +func TestDefaultBootstrapDataDir_FallbackGetwd(t *testing.T) { |
| 36 | + originalWd, err := os.Getwd() |
| 37 | + if err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + defer os.Chdir(originalWd) |
| 41 | + |
| 42 | + // Move to a dir without configs subdirectory so findConfigsDir returns "" |
| 43 | + tmpDir := t.TempDir() |
| 44 | + if err := os.Chdir(tmpDir); err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + dir := DefaultBootstrapDataDir() |
| 49 | + if dir == "" { |
| 50 | + t.Error("DefaultBootstrapDataDir should never return empty string") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// TestFindConfigsDir_NoMatch covers the case where no candidate matches. |
| 55 | +func TestFindConfigsDir_NoMatch(t *testing.T) { |
| 56 | + originalWd, err := os.Getwd() |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + defer os.Chdir(originalWd) |
| 61 | + |
| 62 | + tmpDir := t.TempDir() |
| 63 | + if err := os.Chdir(tmpDir); err != nil { |
| 64 | + t.Fatal(err) |
| 65 | + } |
| 66 | + |
| 67 | + dir := findConfigsDir() |
| 68 | + // In a fresh temp dir with no configs, this should return "" |
| 69 | + if dir != "" { |
| 70 | + t.Errorf("expected empty from findConfigsDir in empty dir, got %s", dir) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// TestDefaultBootstrapDataDir_AlwaysReturnsConfigs ensures the function |
| 75 | +// always returns a path ending with "configs". |
| 76 | +func TestDefaultBootstrapDataDir_AlwaysReturnsConfigs(t *testing.T) { |
| 77 | + dir := DefaultBootstrapDataDir() |
| 78 | + if filepath.Base(dir) != "configs" { |
| 79 | + t.Errorf("expected path ending with 'configs', got %s", dir) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// TestExecutableDir_ReturnsCleanPath verifies executableDir returns a |
| 84 | +// cleaned absolute path. |
| 85 | +func TestExecutableDir_ReturnsCleanPath(t *testing.T) { |
| 86 | + dir, _ := executableDir() |
| 87 | + if dir != "" && dir != filepath.Clean(dir) { |
| 88 | + t.Errorf("expected cleaned path, got %s", dir) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// TestFindConfigsDir_WithConfigsAboveExe tests that findConfigsDir can |
| 93 | +// locate configs in parent directories relative to executable. |
| 94 | +func TestFindConfigsDir_WithConfigsAboveExe(t *testing.T) { |
| 95 | + // findConfigsDir tries multiple candidate paths; verify it doesn't panic |
| 96 | + dir := findConfigsDir() |
| 97 | + t.Logf("findConfigsDir result: %q", dir) |
| 98 | +} |
| 99 | + |
| 100 | +// TestDefaultBootstrapDataDir_TwoFallbacks covers the two-level fallback |
| 101 | +// chain: findConfigsDir -> executableDir -> os.Getwd -> "configs". |
| 102 | +func TestDefaultBootstrapDataDir_TwoFallbacks(t *testing.T) { |
| 103 | + // This test ensures the function handles the case where configs |
| 104 | + // cannot be found via the normal search paths. |
| 105 | + originalWd, err := os.Getwd() |
| 106 | + if err != nil { |
| 107 | + t.Fatal(err) |
| 108 | + } |
| 109 | + defer os.Chdir(originalWd) |
| 110 | + |
| 111 | + tmpDir := t.TempDir() |
| 112 | + if err := os.Chdir(tmpDir); err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + |
| 116 | + result := DefaultBootstrapDataDir() |
| 117 | + if result == "" { |
| 118 | + t.Error("DefaultBootstrapDataDir returned empty string") |
| 119 | + } |
| 120 | +} |
0 commit comments