Skip to content

Commit 44674c3

Browse files
author
Robert Laszczak
committed
added -ignore-package flag
1 parent f4197d2 commit 44674c3

File tree

8 files changed

+81
-23
lines changed

8 files changed

+81
-23
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,25 @@ To run in provided directory
9898

9999
Process will exit with code `1` if architecture is not valid, otherwise it will exit with `0`.
100100

101+
### -ignore-tests
102+
101103
If you need to ignore `*_test.go` files in `go-cleanarch` check you can pass `-ignore-tests`
102104

103105
go-cleanarch -ignore-tests
104106

105107
It is useful when you have memory implementation in infrastructure layer
106108
and you need to test application service which depends of it.
107109

110+
### -ignore-package
111+
112+
If for some reason you need to allow to make forbidden import, for example
113+
114+
`github.com/roblaszczak/go-cleanarch/examples/ignore-package/app` to `github.com/roblaszczak/go-cleanarch/examples/ignore-package/domain`.
115+
116+
you can use
117+
118+
go-cleanarch -ignore-package=github.com/roblaszczak/go-cleanarch/examples/ignore-package/app
119+
108120
## Running the tests
109121

110122
make test

cleanarch/cleanarch.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type Validator struct {
8080
}
8181

8282
// Validate validates provided path for Clean Architecture rules.
83-
func (v *Validator) Validate(root string, ignoreTests bool) (bool, []ValidationError, error) {
83+
func (v *Validator) Validate(root string, ignoreTests bool, ignoredPackages []string) (bool, []ValidationError, error) {
8484
errors := []ValidationError{}
8585

8686
err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
@@ -122,7 +122,14 @@ func (v *Validator) Validate(root string, ignoreTests bool) (bool, []ValidationE
122122
return nil
123123
}
124124

125+
ImportsLoop:
125126
for _, imp := range f.Imports {
127+
for _, ignoredPackage := range ignoredPackages {
128+
if strings.Contains(imp.Path.Value, ignoredPackage) {
129+
continue ImportsLoop
130+
}
131+
}
132+
126133
validationErrors := v.validateImport(imp, importerMeta, path)
127134
errors = append(errors, validationErrors...)
128135
}

cleanarch/cleanarch_test.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,34 @@ func init() {
1515

1616
func TestValidator_Validate(t *testing.T) {
1717
testCases := []struct {
18-
Path string
19-
IsValid bool
20-
IgnoreTests bool
18+
Path string
19+
IsValid bool
20+
IgnoreTests bool
21+
IgnoredPackages []string
2122
}{
22-
{"../examples/valid-simple", true, false},
23-
{"../examples/invalid-infra-in-domain-import", false, false},
24-
{"../examples/invalid-app-to-domain-import", false, false},
25-
{"../examples/invalid-cross-module-deps", false, false},
26-
{"../examples/valid-cross-module-deps", true, false},
27-
{"../examples/valid-imports-inside-module", true, false},
28-
{"../examples/invalid-imports-between-submodules", false, false},
29-
{"../examples/invalid-imports-between-submodules-2", false, false},
30-
{"../examples/ignored-dirs", true, false},
31-
{"../examples/ignored-dirs", true, false},
32-
{"../examples/invalid-infrastructure-to-app-import-in-tests", true, true},
33-
{"../examples/invalid-infrastructure-to-app-import-in-tests", false, false},
23+
{Path: "../examples/valid-simple", IsValid: true},
24+
{Path: "../examples/invalid-infra-in-domain-import", IsValid: false},
25+
{Path: "../examples/invalid-app-to-domain-import", IsValid: false},
26+
{Path: "../examples/invalid-cross-module-deps", IsValid: false},
27+
{Path: "../examples/valid-cross-module-deps", IsValid: true},
28+
{Path: "../examples/valid-imports-inside-module", IsValid: true},
29+
{Path: "../examples/invalid-imports-between-submodules", IsValid: false},
30+
{Path: "../examples/invalid-imports-between-submodules-2", IsValid: false},
31+
{Path: "../examples/ignored-dirs", IsValid: true},
32+
{Path: "../examples/ignored-dirs", IsValid: true},
33+
{Path: "../examples/invalid-infrastructure-to-app-import-in-tests", IsValid: true, IgnoreTests: true},
34+
{Path: "../examples/invalid-infrastructure-to-app-import-in-tests", IsValid: false},
35+
{
36+
Path: "../examples/ignore-package",
37+
IsValid: true,
38+
IgnoredPackages: []string{"github.com/roblaszczak/go-cleanarch/examples/ignore-package/app"},
39+
},
3440
}
3541

3642
for _, c := range testCases {
3743
t.Run(c.Path, func(t *testing.T) {
3844
validator := cleanarch.NewValidator()
39-
valid, errors, err := validator.Validate(c.Path, c.IgnoreTests)
45+
valid, errors, err := validator.Validate(c.Path, c.IgnoreTests, c.IgnoredPackages)
4046
if err != nil {
4147
t.Fatal(err)
4248
}

examples/ignore-package/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Invalid `app` to `domain` import can be ignored with "import-package" flag set to `github.com/roblaszczak/go-cleanarch/examples/ignore-package/app`.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package app
2+
3+
type Price float64
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package domain
2+
3+
import "github.com/roblaszczak/go-cleanarch/examples/ignore-package/app"
4+
5+
// Product imports app.Price, with breaks Dependency Rule.
6+
type Product struct {
7+
Price app.Price
8+
}

flag.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type sliceFlag []string
6+
7+
func (s *sliceFlag) String() string {
8+
return fmt.Sprintf("%s", s)
9+
}
10+
11+
func (s *sliceFlag) Set(v string) error {
12+
*s = append(*s, v)
13+
return nil
14+
}

main.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ import (
99
"github.com/roblaszczak/go-cleanarch/cleanarch"
1010
)
1111

12-
var (
13-
ignoreTests = flag.Bool("ignore-tests", false, "if flag is passed *_test.go files will be not checked")
14-
debug = flag.Bool("debug", false, "debug mode")
15-
)
16-
1712
func main() {
13+
ignoredPackages := sliceFlag{}
14+
15+
ignoreTests := flag.Bool("ignore-tests", false, "if flag is passed *_test.go files will be not checked")
16+
debug := flag.Bool("debug", false, "debug mode")
17+
flag.Var(
18+
&ignoredPackages,
19+
"ignore-package",
20+
"provided packages can be imported to any layer, "+
21+
"for example you can use`-ignore-package github.com/roblaszczak/go-cleanarch/infrastructure` to import "+
22+
"this package to the domain",
23+
)
24+
1825
flag.Parse()
1926
var path string
2027

@@ -35,7 +42,7 @@ func main() {
3542
fmt.Printf("[cleanarch] checking %s\n", path)
3643

3744
validator := cleanarch.NewValidator()
38-
isValid, errors, err := validator.Validate(path, *ignoreTests)
45+
isValid, errors, err := validator.Validate(path, *ignoreTests, ignoredPackages)
3946
if err != nil {
4047
panic(err)
4148
}

0 commit comments

Comments
 (0)