|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestGetChangedGoPackagesFromDiff(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + out string |
| 14 | + projectPath string |
| 15 | + excludes []string |
| 16 | + fileMap map[string][]string |
| 17 | + expected []string |
| 18 | + expectError bool |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "Basic Case", |
| 22 | + out: "pkg1/file1.go\npkg2/file2.go\n", |
| 23 | + excludes: []string{}, |
| 24 | + fileMap: map[string][]string{ |
| 25 | + "pkg1/file1.go": {"pkg1"}, |
| 26 | + "pkg2/file2.go": {"pkg2"}, |
| 27 | + }, |
| 28 | + expected: []string{"pkg1", "pkg2"}, |
| 29 | + expectError: false, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Empty Input", |
| 33 | + out: "", |
| 34 | + excludes: []string{}, |
| 35 | + fileMap: map[string][]string{}, |
| 36 | + expected: []string{}, |
| 37 | + expectError: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Non-Go Files Ignored", |
| 41 | + out: "pkg1/file1.txt\npkg2/file2.go\n", |
| 42 | + excludes: []string{}, |
| 43 | + fileMap: map[string][]string{ |
| 44 | + "pkg2/file2.go": {"pkg2"}, |
| 45 | + }, |
| 46 | + expected: []string{"pkg2"}, |
| 47 | + expectError: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Exclusions Applied", |
| 51 | + out: "pkg1/file1.go\npkg2/file2.go\npkg3/file3.go\n", |
| 52 | + excludes: []string{"pkg2"}, |
| 53 | + fileMap: map[string][]string{ |
| 54 | + "pkg1/file1.go": {"pkg1"}, |
| 55 | + "pkg2/file2.go": {"pkg2"}, |
| 56 | + "pkg3/file3.go": {"pkg3"}, |
| 57 | + }, |
| 58 | + expected: []string{"pkg1", "pkg3"}, |
| 59 | + expectError: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Multiple Imports", |
| 63 | + out: "pkg1/file1.go\n", |
| 64 | + excludes: []string{}, |
| 65 | + fileMap: map[string][]string{ |
| 66 | + "pkg1/file1.go": {"pkg1", "pkg1/subpkg"}, |
| 67 | + }, |
| 68 | + expected: []string{"pkg1", "pkg1/subpkg"}, |
| 69 | + expectError: false, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "Duplicate Packages", |
| 73 | + out: "pkg1/file1.go\npkg1/file1.go\n", |
| 74 | + excludes: []string{}, |
| 75 | + fileMap: map[string][]string{ |
| 76 | + "pkg1/file1.go": {"pkg1"}, |
| 77 | + }, |
| 78 | + expected: []string{"pkg1"}, |
| 79 | + expectError: false, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + outBuffer := bytes.Buffer{} |
| 86 | + outBuffer.WriteString(tt.out) |
| 87 | + result, err := GetChangedGoPackagesFromDiff(outBuffer, tt.projectPath, tt.excludes, tt.fileMap) |
| 88 | + if tt.expectError { |
| 89 | + assert.Error(t, err) |
| 90 | + } else { |
| 91 | + assert.NoError(t, err) |
| 92 | + assert.ElementsMatch(t, tt.expected, result) |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments