|
| 1 | +package extractor |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/ast" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "golang.org/x/tools/go/packages" |
| 8 | +) |
| 9 | + |
| 10 | +func TestIsExactTestPackage(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + pkgID string |
| 14 | + pkgPath string |
| 15 | + expected bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "exact test package", |
| 19 | + pkgID: "github.com/foo/bar [github.com/foo/bar.test]", |
| 20 | + pkgPath: "github.com/foo/bar", |
| 21 | + expected: true, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "nested test package", |
| 25 | + pkgID: "github.com/foo/bar [github.com/foo/bar/nested.test]", |
| 26 | + pkgPath: "github.com/foo/bar", |
| 27 | + expected: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "deeply nested test package", |
| 31 | + pkgID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]", |
| 32 | + pkgPath: "github.com/go-git/go-git/v6", |
| 33 | + expected: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "exact test package with version", |
| 37 | + pkgID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]", |
| 38 | + pkgPath: "github.com/go-git/go-git/v6", |
| 39 | + expected: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "non-test package", |
| 43 | + pkgID: "github.com/foo/bar", |
| 44 | + pkgPath: "github.com/foo/bar", |
| 45 | + expected: false, |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + for _, tt := range tests { |
| 50 | + t.Run(tt.name, func(t *testing.T) { |
| 51 | + pkg := &packages.Package{ |
| 52 | + ID: tt.pkgID, |
| 53 | + PkgPath: tt.pkgPath, |
| 54 | + } |
| 55 | + result := isExactTestPackage(pkg) |
| 56 | + if result != tt.expected { |
| 57 | + t.Errorf("isExactTestPackage(%q) = %v, want %v", tt.pkgID, result, tt.expected) |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestIsBetterPackage(t *testing.T) { |
| 64 | + // Helper to create a package with specified properties |
| 65 | + makePkg := func(id, path string, syntaxCount int) *packages.Package { |
| 66 | + syntax := make([]*ast.File, syntaxCount) |
| 67 | + return &packages.Package{ |
| 68 | + ID: id, |
| 69 | + PkgPath: path, |
| 70 | + Syntax: syntax, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + tests := []struct { |
| 75 | + name string |
| 76 | + pkg *packages.Package |
| 77 | + current *packages.Package |
| 78 | + expected bool // true if pkg is better than current |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "exact test package beats nested test package", |
| 82 | + pkg: makePkg( |
| 83 | + "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]", |
| 84 | + "github.com/go-git/go-git/v6", |
| 85 | + 39, // 19 production + 20 test files |
| 86 | + ), |
| 87 | + current: makePkg( |
| 88 | + "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]", |
| 89 | + "github.com/go-git/go-git/v6", |
| 90 | + 19, // production files only |
| 91 | + ), |
| 92 | + expected: true, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "nested test package loses to exact test package", |
| 96 | + pkg: makePkg( |
| 97 | + "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]", |
| 98 | + "github.com/go-git/go-git/v6", |
| 99 | + 19, |
| 100 | + ), |
| 101 | + current: makePkg( |
| 102 | + "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]", |
| 103 | + "github.com/go-git/go-git/v6", |
| 104 | + 39, |
| 105 | + ), |
| 106 | + expected: false, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "more syntax nodes wins when both are exact tests", |
| 110 | + pkg: makePkg( |
| 111 | + "github.com/foo/bar [github.com/foo/bar.test]", |
| 112 | + "github.com/foo/bar", |
| 113 | + 50, |
| 114 | + ), |
| 115 | + current: makePkg( |
| 116 | + "github.com/foo/bar [github.com/foo/bar.test]", |
| 117 | + "github.com/foo/bar", |
| 118 | + 30, |
| 119 | + ), |
| 120 | + expected: true, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "fewer syntax nodes loses when both are exact tests", |
| 124 | + pkg: makePkg( |
| 125 | + "github.com/foo/bar [github.com/foo/bar.test]", |
| 126 | + "github.com/foo/bar", |
| 127 | + 30, |
| 128 | + ), |
| 129 | + current: makePkg( |
| 130 | + "github.com/foo/bar [github.com/foo/bar.test]", |
| 131 | + "github.com/foo/bar", |
| 132 | + 50, |
| 133 | + ), |
| 134 | + expected: false, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "more syntax nodes wins when both are nested tests", |
| 138 | + pkg: makePkg( |
| 139 | + "github.com/foo/bar [github.com/foo/bar/pkg1.test]", |
| 140 | + "github.com/foo/bar", |
| 141 | + 25, |
| 142 | + ), |
| 143 | + current: makePkg( |
| 144 | + "github.com/foo/bar [github.com/foo/bar/pkg2.test]", |
| 145 | + "github.com/foo/bar", |
| 146 | + 20, |
| 147 | + ), |
| 148 | + expected: true, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "longer ID wins when same syntax count", |
| 152 | + pkg: makePkg( |
| 153 | + "github.com/foo/bar [github.com/foo/bar/verylongpackagename.test]", |
| 154 | + "github.com/foo/bar", |
| 155 | + 20, |
| 156 | + ), |
| 157 | + current: makePkg( |
| 158 | + "github.com/foo/bar [github.com/foo/bar/short.test]", |
| 159 | + "github.com/foo/bar", |
| 160 | + 20, |
| 161 | + ), |
| 162 | + expected: true, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "test package beats non-test with same syntax count", |
| 166 | + pkg: makePkg( |
| 167 | + "github.com/foo/bar [github.com/foo/bar.test]", |
| 168 | + "github.com/foo/bar", |
| 169 | + 20, |
| 170 | + ), |
| 171 | + current: makePkg( |
| 172 | + "github.com/foo/bar", |
| 173 | + "github.com/foo/bar", |
| 174 | + 20, |
| 175 | + ), |
| 176 | + expected: true, |
| 177 | + }, |
| 178 | + } |
| 179 | + |
| 180 | + for _, tt := range tests { |
| 181 | + t.Run(tt.name, func(t *testing.T) { |
| 182 | + result := isBetterPackage(tt.pkg, tt.current) |
| 183 | + if result != tt.expected { |
| 184 | + t.Errorf("isBetterPackage() = %v, want %v\n pkg: %q (%d syntax nodes)\n current: %q (%d syntax nodes)", |
| 185 | + result, tt.expected, |
| 186 | + tt.pkg.ID, len(tt.pkg.Syntax), |
| 187 | + tt.current.ID, len(tt.current.Syntax)) |
| 188 | + } |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +// TestPackageSelectionRealWorld simulates the real-world go-git scenario |
| 194 | +func TestPackageSelectionRealWorld(t *testing.T) { |
| 195 | + // Simulate the actual packages.Load result for go-git repository |
| 196 | + // when EXTRACT_TESTS=true |
| 197 | + pkgs := []*packages.Package{ |
| 198 | + // Production package only |
| 199 | + { |
| 200 | + ID: "github.com/go-git/go-git/v6", |
| 201 | + PkgPath: "github.com/go-git/go-git/v6", |
| 202 | + Syntax: make([]*ast.File, 19), // 19 production files |
| 203 | + }, |
| 204 | + // Root test package - this is what we want! |
| 205 | + { |
| 206 | + ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]", |
| 207 | + PkgPath: "github.com/go-git/go-git/v6", |
| 208 | + Syntax: make([]*ast.File, 39), // 19 production + 20 test files |
| 209 | + }, |
| 210 | + // Nested test dependency 1 |
| 211 | + { |
| 212 | + ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]", |
| 213 | + PkgPath: "github.com/go-git/go-git/v6", |
| 214 | + Syntax: make([]*ast.File, 19), // production files only (dependency) |
| 215 | + }, |
| 216 | + // Nested test dependency 2 |
| 217 | + { |
| 218 | + ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/object.test]", |
| 219 | + PkgPath: "github.com/go-git/go-git/v6", |
| 220 | + Syntax: make([]*ast.File, 19), // production files only (dependency) |
| 221 | + }, |
| 222 | + } |
| 223 | + |
| 224 | + // Simulate the bestPackageIds selection logic |
| 225 | + bestPackageIds := make(map[string]*packages.Package) |
| 226 | + for _, pkg := range pkgs { |
| 227 | + if bestSoFar, present := bestPackageIds[pkg.PkgPath]; present { |
| 228 | + if isBetterPackage(pkg, bestSoFar) { |
| 229 | + bestPackageIds[pkg.PkgPath] = pkg |
| 230 | + } |
| 231 | + } else { |
| 232 | + bestPackageIds[pkg.PkgPath] = pkg |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + // Verify the correct package was selected |
| 237 | + selected := bestPackageIds["github.com/go-git/go-git/v6"] |
| 238 | + expectedID := "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]" |
| 239 | + expectedSyntaxCount := 39 |
| 240 | + |
| 241 | + if selected.ID != expectedID { |
| 242 | + t.Errorf("Wrong package selected!\n got: %q (%d syntax nodes)\n want: %q (%d syntax nodes)", |
| 243 | + selected.ID, len(selected.Syntax), |
| 244 | + expectedID, expectedSyntaxCount) |
| 245 | + } |
| 246 | + |
| 247 | + if len(selected.Syntax) != expectedSyntaxCount { |
| 248 | + t.Errorf("Wrong syntax count: got %d, want %d", len(selected.Syntax), expectedSyntaxCount) |
| 249 | + } |
| 250 | + |
| 251 | + // Verify it's recognized as an exact test package |
| 252 | + if !isExactTestPackage(selected) { |
| 253 | + t.Errorf("Selected package %q should be recognized as exact test package", selected.ID) |
| 254 | + } |
| 255 | +} |
0 commit comments