github.com/ldez/gomoddirectives@v0.2.4/gomoddirectives_test.go (about) 1 package gomoddirectives 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 "golang.org/x/mod/modfile" 11 ) 12 13 func TestAnalyze(t *testing.T) { 14 wd, err := os.Getwd() 15 require.NoError(t, err) 16 defer func() { 17 _ = os.Chdir(wd) 18 }() 19 20 err = os.Chdir("./testdata/a/") 21 require.NoError(t, err) 22 23 results, err := Analyze(Options{}) 24 require.NoError(t, err) 25 26 assert.Len(t, results, 2) 27 } 28 29 func TestAnalyzeFile(t *testing.T) { 30 testCases := []struct { 31 desc string 32 modulePath string 33 opts Options 34 expected int 35 }{ 36 { 37 desc: "replace: allow nothing", 38 modulePath: "a/go.mod", 39 opts: Options{}, 40 expected: 2, 41 }, 42 { 43 desc: "replace: allow a replace", 44 modulePath: "a/go.mod", 45 opts: Options{ 46 ReplaceAllowList: []string{ 47 "github.com/gorilla/mux", 48 }, 49 }, 50 expected: 1, 51 }, 52 { 53 desc: "replace: allow local", 54 modulePath: "a/go.mod", 55 opts: Options{ 56 ReplaceAllowLocal: true, 57 }, 58 expected: 1, 59 }, 60 { 61 desc: "replace: exclude all", 62 modulePath: "a/go.mod", 63 opts: Options{ 64 ReplaceAllowLocal: true, 65 ReplaceAllowList: []string{ 66 "github.com/ldez/grignotin", 67 "github.com/gorilla/mux", 68 }, 69 }, 70 expected: 0, 71 }, 72 { 73 desc: "replace: allow list doesn't override allow local", 74 modulePath: "a/go.mod", 75 opts: Options{ 76 ReplaceAllowLocal: false, 77 ReplaceAllowList: []string{ 78 "github.com/ldez/grignotin", 79 }, 80 }, 81 expected: 2, 82 }, 83 { 84 desc: "replace: duplicate replacement", 85 modulePath: "e/go.mod", 86 opts: Options{ 87 ReplaceAllowLocal: true, 88 ReplaceAllowList: []string{ 89 "github.com/gorilla/mux", 90 "github.com/ldez/grignotin", 91 }, 92 }, 93 expected: 2, 94 }, 95 { 96 desc: "replace: replaced by the same", 97 modulePath: "f/go.mod", 98 opts: Options{ 99 ReplaceAllowLocal: true, 100 ReplaceAllowList: []string{ 101 "github.com/gorilla/mux", 102 "github.com/ldez/grignotin", 103 }, 104 }, 105 expected: 1, 106 }, 107 { 108 desc: "replace: duplicate replacement but for the different versions", 109 modulePath: "g/go.mod", 110 opts: Options{ 111 ReplaceAllowLocal: true, 112 ReplaceAllowList: []string{ 113 "github.com/gorilla/mux", 114 "github.com/ldez/grignotin", 115 }, 116 }, 117 expected: 0, 118 }, 119 { 120 desc: "retract: allow no explanation", 121 modulePath: "c/go.mod", 122 opts: Options{ 123 RetractAllowNoExplanation: true, 124 }, 125 expected: 0, 126 }, 127 { 128 desc: "retract: explanation is require", 129 modulePath: "c/go.mod", 130 opts: Options{ 131 RetractAllowNoExplanation: false, 132 }, 133 expected: 1, 134 }, 135 { 136 desc: "exclude: don't allow", 137 modulePath: "d/go.mod", 138 opts: Options{ 139 ExcludeForbidden: true, 140 }, 141 expected: 2, 142 }, 143 { 144 desc: "exclude: allow", 145 modulePath: "d/go.mod", 146 opts: Options{ 147 ExcludeForbidden: false, 148 }, 149 expected: 0, 150 }, 151 } 152 153 for _, test := range testCases { 154 test := test 155 t.Run(test.desc, func(t *testing.T) { 156 t.Parallel() 157 158 raw, err := os.ReadFile(filepath.FromSlash("./testdata/" + test.modulePath)) 159 require.NoError(t, err) 160 161 file, err := modfile.Parse("go.mod", raw, nil) 162 require.NoError(t, err) 163 164 results := AnalyzeFile(file, test.opts) 165 166 assert.Len(t, results, test.expected) 167 }) 168 } 169 }