Skip to content

Commit eb1a3df

Browse files
jawnsyryancurrah
authored andcommitted
Wrap long lines and suppress funlen warnings
1 parent 011d855 commit eb1a3df

File tree

3 files changed

+102
-28
lines changed

3 files changed

+102
-28
lines changed

cmd.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
)
2929

3030
// Run the gomodguard linter. Returns the exit code to use.
31+
//nolint:funlen
3132
func Run() int {
3233
var (
3334
args []string
@@ -43,7 +44,8 @@ func Run() int {
4344
flag.BoolVar(&help, "help", false, "")
4445
flag.BoolVar(&noTest, "n", false, "Don't lint test files")
4546
flag.BoolVar(&noTest, "no-test", false, "")
46-
flag.StringVar(&report, "r", "", "Report results to one of the following formats: checkstyle. A report file destination must also be specified")
47+
flag.StringVar(&report, "r", "", "Report results to one of the following formats: checkstyle. "+
48+
"A report file destination must also be specified")
4749
flag.StringVar(&report, "report", "", "")
4850
flag.StringVar(&reportFile, "f", "", "Report results to the specified file. A report type must also be specified")
4951
flag.StringVar(&reportFile, "file", "", "")
@@ -202,7 +204,8 @@ func WriteCheckstyle(checkstyleFilePath string, results []Issue) error {
202204

203205
for i := range results {
204206
file := check.EnsureFile(results[i].FileName)
205-
file.AddError(checkstyle.NewError(results[i].LineNumber, 1, checkstyle.SeverityError, results[i].Reason, "gomodguard"))
207+
file.AddError(checkstyle.NewError(results[i].LineNumber, 1, checkstyle.SeverityError, results[i].Reason,
208+
"gomodguard"))
206209
}
207210

208211
checkstyleXML := fmt.Sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n%s", check.String())

gomodguard.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ const (
2323
)
2424

2525
var (
26-
blockReasonNotInAllowedList = "import of package `%s` is blocked because the module is not in the allowed modules list."
27-
blockReasonInBlockedList = "import of package `%s` is blocked because the module is in the blocked modules list."
28-
blockReasonHasLocalReplaceDirective = "import of package `%s` is blocked because the module has a local replace directive."
26+
blockReasonNotInAllowedList = "import of package `%s` is blocked because the module is not in the " +
27+
"allowed modules list."
28+
blockReasonInBlockedList = "import of package `%s` is blocked because the module is in the " +
29+
"blocked modules list."
30+
blockReasonHasLocalReplaceDirective = "import of package `%s` is blocked because the module has a " +
31+
"local replace directive."
2932
)
3033

3134
// BlockedVersion has a version constraint a reason why the the module version is blocked.
@@ -61,7 +64,8 @@ func (r *BlockedVersion) Message(lintedModuleVersion string) string {
6164
msg := ""
6265

6366
// Add version contraint to message.
64-
msg += fmt.Sprintf("version `%s` is blocked because it does not meet the version constraint `%s`.", lintedModuleVersion, r.Version)
67+
msg += fmt.Sprintf("version `%s` is blocked because it does not meet the version constraint `%s`.",
68+
lintedModuleVersion, r.Version)
6569

6670
if r.Reason == "" {
6771
return msg
@@ -227,7 +231,8 @@ func (a *Allowed) IsAllowedModuleDomain(moduleName string) bool {
227231
allowedDomains := a.Domains
228232

229233
for i := range allowedDomains {
230-
if strings.HasPrefix(strings.TrimSpace(strings.ToLower(moduleName)), strings.TrimSpace(strings.ToLower(allowedDomains[i]))) {
234+
if strings.HasPrefix(strings.TrimSpace(strings.ToLower(moduleName)),
235+
strings.TrimSpace(strings.ToLower(allowedDomains[i]))) {
231236
return true
232237
}
233238
}
@@ -363,7 +368,7 @@ func (p *Processor) addError(fileset *token.FileSet, pos token.Pos, reason strin
363368
//
364369
// It works by iterating over the dependant modules specified in the require
365370
// directive, checking if the module domain or full name is in the allowed list.
366-
func (p *Processor) SetBlockedModules() { //nolint:gocognit
371+
func (p *Processor) SetBlockedModules() { //nolint:gocognit,funlen
367372
blockedModules := make(map[string][]string, len(p.Modfile.Require))
368373
currentModuleName := p.Modfile.Module.Mod.Path
369374
lintedModules := p.Modfile.Require
@@ -399,11 +404,13 @@ func (p *Processor) SetBlockedModules() { //nolint:gocognit
399404
}
400405

401406
if blockModuleReason != nil && !blockModuleReason.IsCurrentModuleARecommendation(currentModuleName) {
402-
blockedModules[lintedModuleName] = append(blockedModules[lintedModuleName], fmt.Sprintf("%s %s", blockReasonInBlockedList, blockModuleReason.Message()))
407+
blockedModules[lintedModuleName] = append(blockedModules[lintedModuleName],
408+
fmt.Sprintf("%s %s", blockReasonInBlockedList, blockModuleReason.Message()))
403409
}
404410

405411
if blockVersionReason != nil && blockVersionReason.IsLintedModuleVersionBlocked(lintedModuleVersion) {
406-
blockedModules[lintedModuleName] = append(blockedModules[lintedModuleName], fmt.Sprintf("%s %s", blockReasonInBlockedList, blockVersionReason.Message(lintedModuleVersion)))
412+
blockedModules[lintedModuleName] = append(blockedModules[lintedModuleName],
413+
fmt.Sprintf("%s %s", blockReasonInBlockedList, blockVersionReason.Message(lintedModuleVersion)))
407414
}
408415
}
409416

@@ -417,7 +424,8 @@ func (p *Processor) SetBlockedModules() { //nolint:gocognit
417424
replacedModuleNewVersion := strings.TrimSpace(replacedModules[i].New.Version)
418425

419426
if replacedModuleNewName != "" && replacedModuleNewVersion == "" {
420-
blockedModules[replacedModuleOldName] = append(blockedModules[replacedModuleOldName], blockReasonHasLocalReplaceDirective)
427+
blockedModules[replacedModuleOldName] = append(blockedModules[replacedModuleOldName],
428+
blockReasonHasLocalReplaceDirective)
421429
}
422430
}
423431
}

gomodguard_test.go

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,21 @@ func TestBlockedModuleIsAllowed(t *testing.T) {
4747
}{
4848
{
4949
"blocked",
50-
gomodguard.BlockedModule{Recommendations: []string{"github.com/somerecommended/module"}},
50+
gomodguard.BlockedModule{
51+
Recommendations: []string{
52+
"github.com/somerecommended/module",
53+
},
54+
},
5155
"github.com/ryancurrah/gomodguard",
5256
false,
5357
},
5458
{
5559
"allowed",
56-
gomodguard.BlockedModule{Recommendations: []string{"github.com/ryancurrah/gomodguard"}},
60+
gomodguard.BlockedModule{
61+
Recommendations: []string{
62+
"github.com/ryancurrah/gomodguard",
63+
},
64+
},
5765
"github.com/ryancurrah/gomodguard",
5866
true,
5967
},
@@ -72,7 +80,8 @@ func TestBlockedModuleIsAllowed(t *testing.T) {
7280
func TestBlockedModuleMessage(t *testing.T) {
7381
blockedWithNoRecommendation := "Some reason."
7482
blockedWithRecommendation := "`github.com/somerecommended/module` is a recommended module. Some reason."
75-
blockedWithRecommendations := "`github.com/somerecommended/module`, `github.com/someotherrecommended/module` and `github.com/someotherotherrecommended/module` are recommended modules. Some reason."
83+
blockedWithRecommendations := "`github.com/somerecommended/module`, `github.com/someotherrecommended/module` " +
84+
"and `github.com/someotherotherrecommended/module` are recommended modules. Some reason."
7685

7786
var tests = []struct {
7887
testName string
@@ -82,19 +91,34 @@ func TestBlockedModuleMessage(t *testing.T) {
8291
}{
8392
{
8493
"blocked with no recommendation",
85-
gomodguard.BlockedModule{Recommendations: []string{}, Reason: "Some reason."},
94+
gomodguard.BlockedModule{
95+
Recommendations: []string{},
96+
Reason: "Some reason.",
97+
},
8698
"github.com/ryancurrah/gomodguard",
8799
blockedWithNoRecommendation,
88100
},
89101
{
90102
"blocked with recommendation",
91-
gomodguard.BlockedModule{Recommendations: []string{"github.com/somerecommended/module"}, Reason: "Some reason."},
103+
gomodguard.BlockedModule{
104+
Recommendations: []string{
105+
"github.com/somerecommended/module",
106+
},
107+
Reason: "Some reason.",
108+
},
92109
"github.com/ryancurrah/gomodguard",
93110
blockedWithRecommendation,
94111
},
95112
{
96113
"blocked with multiple recommendations",
97-
gomodguard.BlockedModule{Recommendations: []string{"github.com/somerecommended/module", "github.com/someotherrecommended/module", "github.com/someotherotherrecommended/module"}, Reason: "Some reason."},
114+
gomodguard.BlockedModule{
115+
Recommendations: []string{
116+
"github.com/somerecommended/module",
117+
"github.com/someotherrecommended/module",
118+
"github.com/someotherotherrecommended/module",
119+
},
120+
Reason: "Some reason.",
121+
},
98122
"github.com/ryancurrah/gomodguard",
99123
blockedWithRecommendations,
100124
},
@@ -123,7 +147,11 @@ func TestBlockedModuleHasRecommendations(t *testing.T) {
123147
},
124148
{
125149
"does have recommendations",
126-
gomodguard.BlockedModule{Recommendations: []string{"github.com/ryancurrah/gomodguard"}},
150+
gomodguard.BlockedModule{
151+
Recommendations: []string{
152+
"github.com/ryancurrah/gomodguard",
153+
},
154+
},
127155
true,
128156
},
129157
}
@@ -146,7 +174,15 @@ func TestBlockedModulesGet(t *testing.T) {
146174
}{
147175
{
148176
"get all blocked module names",
149-
gomodguard.BlockedModules{{"github.com/someblocked/module": gomodguard.BlockedModule{Recommendations: []string{"github.com/ryancurrah/gomodguard"}}}},
177+
gomodguard.BlockedModules{
178+
{
179+
"github.com/someblocked/module": gomodguard.BlockedModule{
180+
Recommendations: []string{
181+
"github.com/ryancurrah/gomodguard",
182+
},
183+
},
184+
},
185+
},
150186
[]string{"github.com/someblocked/module"},
151187
},
152188
}
@@ -162,8 +198,10 @@ func TestBlockedModulesGet(t *testing.T) {
162198
}
163199

164200
func TestBlockedVersionMessage(t *testing.T) {
165-
blockedWithVersionConstraint := "version `1.0.0` is blocked because it does not meet the version constraint `1.0.0`. Some reason."
166-
blockedWithVersionConstraintNoReason := "version `1.0.0` is blocked because it does not meet the version constraint `<= 1.0.0`."
201+
blockedWithVersionConstraint := "version `1.0.0` is blocked because it does not meet the version constraint " +
202+
"`1.0.0`. Some reason."
203+
blockedWithVersionConstraintNoReason := "version `1.0.0` is blocked because it does not meet the version " +
204+
"constraint `<= 1.0.0`."
167205

168206
var tests = []struct {
169207
testName string
@@ -173,7 +211,10 @@ func TestBlockedVersionMessage(t *testing.T) {
173211
}{
174212
{
175213
"blocked with version constraint",
176-
gomodguard.BlockedVersion{Version: "1.0.0", Reason: "Some reason."},
214+
gomodguard.BlockedVersion{
215+
Version: "1.0.0",
216+
Reason: "Some reason.",
217+
},
177218
"1.0.0",
178219
blockedWithVersionConstraint,
179220
},
@@ -205,14 +246,30 @@ func TestBlockedModulesGetBlockedModule(t *testing.T) {
205246
}{
206247
{
207248
"blocked",
208-
gomodguard.BlockedModules{{"github.com/someblocked/module": gomodguard.BlockedModule{Recommendations: []string{"github.com/someother/module"}}}},
249+
gomodguard.BlockedModules{
250+
{
251+
"github.com/someblocked/module": gomodguard.BlockedModule{
252+
Recommendations: []string{
253+
"github.com/someother/module",
254+
},
255+
},
256+
},
257+
},
209258
"github.com/ryancurrah/gomodguard",
210259
"github.com/someblocked/module",
211260
false,
212261
},
213262
{
214263
"allowed",
215-
gomodguard.BlockedModules{{"github.com/someblocked/module": gomodguard.BlockedModule{Recommendations: []string{"github.com/ryancurrah/gomodguard"}}}},
264+
gomodguard.BlockedModules{
265+
{
266+
"github.com/someblocked/module": gomodguard.BlockedModule{
267+
Recommendations: []string{
268+
"github.com/ryancurrah/gomodguard",
269+
},
270+
},
271+
},
272+
},
216273
"github.com/ryancurrah/gomodguard",
217274
"github.com/someblocked/module",
218275
true,
@@ -223,7 +280,8 @@ func TestBlockedModulesGetBlockedModule(t *testing.T) {
223280
t.Run(tt.testName, func(t *testing.T) {
224281
blockedModule := tt.blockedModules.GetBlockReason(tt.lintedModuleName)
225282
if blockedModule.IsCurrentModuleARecommendation(tt.currentModuleName) != tt.wantIsAllowed {
226-
t.Errorf("got '%+v' want '%+v'", blockedModule.IsCurrentModuleARecommendation(tt.currentModuleName), tt.wantIsAllowed)
283+
t.Errorf("got '%+v' want '%+v'", blockedModule.IsCurrentModuleARecommendation(tt.currentModuleName),
284+
tt.wantIsAllowed)
227285
}
228286
})
229287
}
@@ -337,17 +395,22 @@ func TestProcessorProcessFiles(t *testing.T) {
337395
{
338396
"module blocked because of recommendation",
339397
gomodguard.Processor{Config: config, Modfile: processor.Modfile},
340-
"blocked_example.go:9:1 import of package `github.com/uudashr/go-module` is blocked because the module is in the blocked modules list. `golang.org/x/mod` is a recommended module. `mod` is the official go.mod parser library.",
398+
"blocked_example.go:9:1 import of package `github.com/uudashr/go-module` is blocked because the " +
399+
"module is in the blocked modules list. `golang.org/x/mod` is a recommended module. `mod` " +
400+
"is the official go.mod parser library.",
341401
},
342402
{
343403
"module blocked because of version constraint",
344404
gomodguard.Processor{Config: config, Modfile: processor.Modfile},
345-
"blocked_example.go:7:1 import of package `github.com/mitchellh/go-homedir` is blocked because the module is in the blocked modules list. version `v1.1.0` is blocked because it does not meet the version constraint `<= 1.1.0`. testing if blocked version constraint works.",
405+
"blocked_example.go:7:1 import of package `github.com/mitchellh/go-homedir` is blocked because " +
406+
"the module is in the blocked modules list. version `v1.1.0` is blocked because it does not " +
407+
"meet the version constraint `<= 1.1.0`. testing if blocked version constraint works.",
346408
},
347409
{
348410
"module blocked because of local replace directive",
349411
gomodguard.Processor{Config: config, Modfile: processor.Modfile},
350-
"blocked_example.go:8:1 import of package `github.com/ryancurrah/gomodguard` is blocked because the module has a local replace directive.",
412+
"blocked_example.go:8:1 import of package `github.com/ryancurrah/gomodguard` is blocked because " +
413+
"the module has a local replace directive.",
351414
},
352415
}
353416

0 commit comments

Comments
 (0)