-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
286 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package should | ||
|
||
import ( | ||
"fmt" | ||
"goarkitect/internal/arch/rule" | ||
"path/filepath" | ||
) | ||
|
||
func MatchGlob(glob string, basePath string) *Expression { | ||
return &Expression{ | ||
evaluate: func(rb rule.Builder, filePath string) bool { | ||
match, err := filepath.Match(filepath.Join(basePath, glob), filePath) | ||
if err != nil { | ||
rb.AddError(err) | ||
} | ||
|
||
return !match | ||
}, | ||
getViolation: func(filePath string) rule.Violation { | ||
return rule.NewViolation( | ||
fmt.Sprintf( | ||
"file's path '%s' does not match glob pattern '%s'", | ||
filepath.Base(filePath), | ||
glob, | ||
), | ||
) | ||
}, | ||
} | ||
} |
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,67 @@ | ||
package should_test | ||
|
||
import ( | ||
"goarkitect/internal/arch/file" | ||
"goarkitect/internal/arch/file/should" | ||
"goarkitect/internal/arch/rule" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
) | ||
|
||
func Test_MatchGlob(t *testing.T) { | ||
basePath, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
newRuleBuilder := func() *file.RuleBuilder { | ||
rb := file.All() | ||
rb.SetFiles([]string{ | ||
filepath.Join(basePath, "test/project3/baz.go"), | ||
filepath.Join(basePath, "test/project3/quux.go"), | ||
}) | ||
return rb | ||
} | ||
|
||
testCases := []struct { | ||
desc string | ||
ruleBuilder *file.RuleBuilder | ||
glob string | ||
want []rule.Violation | ||
}{ | ||
{ | ||
desc: "project3 matches '*.go'", | ||
ruleBuilder: newRuleBuilder(), | ||
glob: "*/*/*.go", | ||
want: nil, | ||
}, | ||
{ | ||
desc: "project3 matches 'foo/*/*.go'", | ||
ruleBuilder: newRuleBuilder(), | ||
glob: "test/*/*.go", | ||
want: nil, | ||
}, | ||
{ | ||
desc: "project3 does not match '**/*.ts'", | ||
ruleBuilder: newRuleBuilder(), | ||
glob: "**/*.ts", | ||
want: []rule.Violation{ | ||
rule.NewViolation("file's path 'baz.go' does not match glob pattern '**/*.ts'"), | ||
rule.NewViolation("file's path 'quux.go' does not match glob pattern '**/*.ts'"), | ||
}, | ||
}, | ||
} | ||
for _, tC := range testCases { | ||
t.Run(tC.desc, func(t *testing.T) { | ||
mg := should.MatchGlob(tC.glob, basePath) | ||
got := mg.Evaluate(tC.ruleBuilder) | ||
if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) { | ||
t.Errorf("want = %+v, got = %+v", tC.want, got) | ||
} | ||
}) | ||
} | ||
} |
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,5 @@ | ||
package bar | ||
|
||
func baz() string { | ||
return "baz" | ||
} |
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,5 @@ | ||
package bar | ||
|
||
func quux() string { | ||
return "quux" | ||
} |