Skip to content

Commit c97ea75

Browse files
authored
Introduce Concept of modes (#69)
Signed-off-by: Will Dixon <[email protected]>
1 parent 47afd98 commit c97ea75

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

settings.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ import (
1111
)
1212

1313
type List struct {
14-
Files []string `json:"files" yaml:"files" toml:"files" mapstructure:"files"`
15-
Allow []string `json:"allow" yaml:"allow" toml:"allow" mapstructure:"allow"`
16-
Deny map[string]string `json:"deny" yaml:"deny" toml:"deny" mapstructure:"deny"`
14+
ListMode string `json:"listMode" yaml:"listMode" toml:"listMode" mapstructure:"listMode"`
15+
Files []string `json:"files" yaml:"files" toml:"files" mapstructure:"files"`
16+
Allow []string `json:"allow" yaml:"allow" toml:"allow" mapstructure:"allow"`
17+
Deny map[string]string `json:"deny" yaml:"deny" toml:"deny" mapstructure:"deny"`
1718
}
1819

20+
type listMode int
21+
22+
const (
23+
listModeStrict listMode = iota
24+
)
25+
1926
type list struct {
27+
listMode listMode
2028
name string
2129
files []glob.Glob
2230
negFiles []glob.Glob
@@ -33,6 +41,16 @@ func (l *List) compile() (*list, error) {
3341
var errs utils.MultiError
3442
var err error
3543

44+
// Determine List Mode
45+
switch strings.ToLower(l.ListMode) {
46+
case "":
47+
li.listMode = listModeStrict
48+
case "strict":
49+
li.listMode = listModeStrict
50+
default:
51+
errs = append(errs, fmt.Errorf("%s is not a known list mode", l.ListMode))
52+
}
53+
3654
// Compile Files
3755
for _, f := range l.Files {
3856
var negate bool

settings_test.go

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,48 @@ var (
159159
suggestions: []string{"Don't use Reflect"},
160160
},
161161
},
162+
{
163+
name: "Strict Mode Default",
164+
list: &List{
165+
Allow: []string{"os"},
166+
Deny: map[string]string{
167+
"reflect": "Don't use Reflect",
168+
},
169+
},
170+
exp: &list{
171+
listMode: listModeStrict,
172+
allow: []string{"os"},
173+
deny: []string{"reflect"},
174+
suggestions: []string{"Don't use Reflect"},
175+
},
176+
},
177+
{
178+
name: "Set Strict Mode",
179+
list: &List{
180+
ListMode: "sTriCT",
181+
Allow: []string{"os"},
182+
Deny: map[string]string{
183+
"reflect": "Don't use Reflect",
184+
},
185+
},
186+
exp: &list{
187+
listMode: listModeStrict,
188+
allow: []string{"os"},
189+
deny: []string{"reflect"},
190+
suggestions: []string{"Don't use Reflect"},
191+
},
192+
},
193+
{
194+
name: "Unknown List Mode",
195+
list: &List{
196+
ListMode: "MiddleOut",
197+
Allow: []string{"os"},
198+
Deny: map[string]string{
199+
"reflect": "Don't use Reflect",
200+
},
201+
},
202+
expErr: errors.New("MiddleOut is not a known list mode"),
203+
},
162204
}
163205
settingsCompileScenarios = []*settingsCompileScenario{
164206
{
@@ -259,6 +301,7 @@ func init() {
259301
// Only doing this so I have a controlled list of expansions for packages
260302
utils.PackageExpandable["$gostd"] = &expanderTest{}
261303
}
304+
262305
func TestListCompile(t *testing.T) {
263306
for _, s := range listCompileScenarios {
264307
t.Run(s.name, testListCompile(s))
@@ -521,27 +564,45 @@ var listImportAllowedScenarios = []*listImportAllowedScenario{
521564
},
522565
},
523566
{
524-
name: "Both only allows what is in allow and not in deny",
567+
name: "Both in Strict mode allows what is in allow and not in deny",
525568
setup: &list{
526-
allow: []string{"some/pkg/a"},
527-
deny: []string{"some/pkg/a/foo"},
528-
suggestions: []string{"because I said so"},
569+
listMode: listModeStrict,
570+
allow: []string{"some/pkg/a/foo", "some/pkg/b", "some/pkg/c"},
571+
deny: []string{"some/pkg/a", "some/pkg/b/foo"},
572+
suggestions: []string{"because I said so", "really don't use"},
529573
},
530574
tests: []*listImportAllowedScenarioInner{
531575
{
532576
name: "in allow but not in deny",
533-
input: "some/pkg/a/bar",
577+
input: "some/pkg/c/alpha",
534578
expected: true,
535579
},
536580
{
537-
name: "in allow and in deny",
581+
name: "subpackage allowed but root denied",
538582
input: "some/pkg/a/foo/bar",
539583
expected: false,
540584
suggestion: "because I said so",
541585
},
586+
{
587+
name: "subpackage not in allowed but root denied",
588+
input: "some/pkg/a/baz",
589+
expected: false,
590+
suggestion: "because I said so",
591+
},
592+
{
593+
name: "subpackage denied but root allowed",
594+
input: "some/pkg/b/foo/bar",
595+
expected: false,
596+
suggestion: "really don't use",
597+
},
598+
{
599+
name: "subpackage not denied but root allowed",
600+
input: "some/pkg/b/baz",
601+
expected: true,
602+
},
542603
{
543604
name: "not in allow nor in deny",
544-
input: "some/pkg/b/foo/bar",
605+
input: "some/pkg/d/alpha",
545606
expected: false,
546607
},
547608
},
@@ -564,7 +625,6 @@ func TestListImportAllowed(t *testing.T) {
564625
}
565626
})
566627
}
567-
568628
}
569629

570630
type linterSettingsWhichListsScenario struct {

0 commit comments

Comments
 (0)