|
7 | 7 | "testing" |
8 | 8 |
|
9 | 9 | "github.com/spf13/viper" |
| 10 | + |
| 11 | + "github.com/imtaebin/code-context-graph/internal/model" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | func TestResolveExcludes_MergesConfigAndFlag(t *testing.T) { |
@@ -84,6 +86,103 @@ func TestResolveOutDir_PrefersFlagOverConfig(t *testing.T) { |
84 | 86 | viper.Reset() |
85 | 87 | } |
86 | 88 |
|
| 89 | +func TestResolveIncludePaths_MergesConfigAndFlag(t *testing.T) { |
| 90 | + viper.Reset() |
| 91 | + viper.Set("include_paths", []string{"src/api"}) |
| 92 | + |
| 93 | + result := resolveIncludePaths([]string{"src/auth"}) |
| 94 | + |
| 95 | + if len(result) != 2 { |
| 96 | + t.Fatalf("expected 2 paths, got %d: %v", len(result), result) |
| 97 | + } |
| 98 | + |
| 99 | + has := func(s string) bool { |
| 100 | + for _, r := range result { |
| 101 | + if r == s { |
| 102 | + return true |
| 103 | + } |
| 104 | + } |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + for _, want := range []string{"src/api", "src/auth"} { |
| 109 | + if !has(want) { |
| 110 | + t.Errorf("expected %q in result %v", want, result) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + viper.Reset() |
| 115 | +} |
| 116 | + |
| 117 | +func TestResolveIncludePaths_EmptyFlagUsesConfig(t *testing.T) { |
| 118 | + viper.Reset() |
| 119 | + viper.Set("include_paths", []string{"src/core", "src/api"}) |
| 120 | + |
| 121 | + result := resolveIncludePaths(nil) |
| 122 | + if len(result) != 2 { |
| 123 | + t.Fatalf("expected 2 paths, got %d: %v", len(result), result) |
| 124 | + } |
| 125 | + |
| 126 | + viper.Reset() |
| 127 | +} |
| 128 | + |
| 129 | +func TestResolveIncludePaths_EmptyBothReturnsNil(t *testing.T) { |
| 130 | + viper.Reset() |
| 131 | + |
| 132 | + result := resolveIncludePaths(nil) |
| 133 | + if len(result) != 0 { |
| 134 | + t.Fatalf("expected empty, got %v", result) |
| 135 | + } |
| 136 | + |
| 137 | + viper.Reset() |
| 138 | +} |
| 139 | + |
| 140 | +func TestBuildCommand_PathFromConfig(t *testing.T) { |
| 141 | + deps, stdout, stderr, db := setupBuildTest(t) |
| 142 | + |
| 143 | + dir := t.TempDir() |
| 144 | + |
| 145 | + apiDir := filepath.Join(dir, "src", "api") |
| 146 | + os.MkdirAll(apiDir, 0755) |
| 147 | + writeGoFile(t, apiDir, "handler.go", `package api |
| 148 | +func Handler() {} |
| 149 | +`) |
| 150 | + |
| 151 | + otherDir := filepath.Join(dir, "src", "other") |
| 152 | + os.MkdirAll(otherDir, 0755) |
| 153 | + writeGoFile(t, otherDir, "other.go", `package other |
| 154 | +func Other() {} |
| 155 | +`) |
| 156 | + |
| 157 | + viper.Reset() |
| 158 | + viper.Set("include_paths", []string{"src/api"}) |
| 159 | + defer viper.Reset() |
| 160 | + |
| 161 | + err := executeCmd(deps, stdout, stderr, "build", dir) |
| 162 | + if err != nil { |
| 163 | + t.Fatalf("unexpected error: %v", err) |
| 164 | + } |
| 165 | + |
| 166 | + var nodes []model.Node |
| 167 | + db.Find(&nodes) |
| 168 | + |
| 169 | + for _, n := range nodes { |
| 170 | + if n.Name == "Other" { |
| 171 | + t.Error("expected Other NOT to be parsed when config has include_paths=[src/api]") |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + foundHandler := false |
| 176 | + for _, n := range nodes { |
| 177 | + if n.Name == "Handler" { |
| 178 | + foundHandler = true |
| 179 | + } |
| 180 | + } |
| 181 | + if !foundHandler { |
| 182 | + t.Error("expected Handler to be parsed (in config include_paths)") |
| 183 | + } |
| 184 | +} |
| 185 | + |
87 | 186 | func TestConfigFlag_LoadsYAML(t *testing.T) { |
88 | 187 | cfgFile := filepath.Join(t.TempDir(), ".ccg.yaml") |
89 | 188 | if err := os.WriteFile(cfgFile, []byte("exclude:\n - vendor\n - \"*.pb.go\"\n"), 0o644); err != nil { |
|
0 commit comments