-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement(processor): improve detection if package is in a module
Improve code layout for reading and add stricter detection if a package is in a module by checking each path part. Issue: GH-30 Signed-off-by: Ryan Currah <ryan@currah.ca>
- Loading branch information
1 parent
349d1ba
commit 5295705
Showing
28 changed files
with
838 additions
and
609 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package gomodguard | ||
|
||
import "strings" | ||
|
||
// Allowed is a list of modules and module | ||
// domains that are allowed to be used. | ||
type Allowed struct { | ||
Modules []string `yaml:"modules"` | ||
Domains []string `yaml:"domains"` | ||
} | ||
|
||
// IsAllowedModule returns true if the given module | ||
// name is in the allowed modules list. | ||
func (a *Allowed) IsAllowedModule(moduleName string) bool { | ||
allowedModules := a.Modules | ||
|
||
for i := range allowedModules { | ||
if strings.TrimSpace(moduleName) == strings.TrimSpace(allowedModules[i]) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// IsAllowedModuleDomain returns true if the given modules domain is | ||
// in the allowed module domains list. | ||
func (a *Allowed) IsAllowedModuleDomain(moduleName string) bool { | ||
allowedDomains := a.Domains | ||
|
||
for i := range allowedDomains { | ||
if strings.HasPrefix(strings.TrimSpace(strings.ToLower(moduleName)), | ||
strings.TrimSpace(strings.ToLower(allowedDomains[i]))) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package gomodguard_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/ryancurrah/gomodguard" | ||
) | ||
|
||
func TestAllowedIsAllowedModule(t *testing.T) { | ||
var tests = []struct { | ||
testName string | ||
allowedModules gomodguard.Allowed | ||
lintedModuleName string | ||
wantIsAllowedModule bool | ||
}{ | ||
{ | ||
"module is allowed", | ||
gomodguard.Allowed{Modules: []string{"github.com/someallowed/module"}}, | ||
"github.com/someallowed/module", | ||
true, | ||
}, | ||
{ | ||
"module not allowed", | ||
gomodguard.Allowed{}, | ||
"github.com/someblocked/module", | ||
false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.testName, func(t *testing.T) { | ||
isAllowedModule := tt.allowedModules.IsAllowedModule(tt.lintedModuleName) | ||
if !reflect.DeepEqual(isAllowedModule, tt.wantIsAllowedModule) { | ||
t.Errorf("got '%v' want '%v'", isAllowedModule, tt.wantIsAllowedModule) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAllowedIsAllowedModuleDomain(t *testing.T) { | ||
var tests = []struct { | ||
testName string | ||
allowedModules gomodguard.Allowed | ||
lintedModuleName string | ||
wantIsAllowedModuleDomain bool | ||
}{ | ||
{ | ||
"module is allowed", | ||
gomodguard.Allowed{Domains: []string{"github.com"}}, | ||
"github.com/someallowed/module", | ||
true, | ||
}, | ||
{ | ||
"module not allowed", | ||
gomodguard.Allowed{}, | ||
"github.com/someblocked/module", | ||
false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.testName, func(t *testing.T) { | ||
isAllowedModuleDomain := tt.allowedModules.IsAllowedModuleDomain(tt.lintedModuleName) | ||
if !reflect.DeepEqual(isAllowedModuleDomain, tt.wantIsAllowedModuleDomain) { | ||
t.Errorf("got '%v' want '%v'", isAllowedModuleDomain, tt.wantIsAllowedModuleDomain) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.