Skip to content

Commit

Permalink
added -ignore-package flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Laszczak committed May 14, 2018
1 parent f4197d2 commit 44674c3
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 23 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,25 @@ To run in provided directory

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

### -ignore-tests

If you need to ignore `*_test.go` files in `go-cleanarch` check you can pass `-ignore-tests`

go-cleanarch -ignore-tests

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

### -ignore-package

If for some reason you need to allow to make forbidden import, for example

`github.com/roblaszczak/go-cleanarch/examples/ignore-package/app` to `github.com/roblaszczak/go-cleanarch/examples/ignore-package/domain`.

you can use

go-cleanarch -ignore-package=github.com/roblaszczak/go-cleanarch/examples/ignore-package/app

## Running the tests

make test
Expand Down
9 changes: 8 additions & 1 deletion cleanarch/cleanarch.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type Validator struct {
}

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

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

ImportsLoop:
for _, imp := range f.Imports {
for _, ignoredPackage := range ignoredPackages {
if strings.Contains(imp.Path.Value, ignoredPackage) {
continue ImportsLoop
}
}

validationErrors := v.validateImport(imp, importerMeta, path)
errors = append(errors, validationErrors...)
}
Expand Down
38 changes: 22 additions & 16 deletions cleanarch/cleanarch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,34 @@ func init() {

func TestValidator_Validate(t *testing.T) {
testCases := []struct {
Path string
IsValid bool
IgnoreTests bool
Path string
IsValid bool
IgnoreTests bool
IgnoredPackages []string
}{
{"../examples/valid-simple", true, false},
{"../examples/invalid-infra-in-domain-import", false, false},
{"../examples/invalid-app-to-domain-import", false, false},
{"../examples/invalid-cross-module-deps", false, false},
{"../examples/valid-cross-module-deps", true, false},
{"../examples/valid-imports-inside-module", true, false},
{"../examples/invalid-imports-between-submodules", false, false},
{"../examples/invalid-imports-between-submodules-2", false, false},
{"../examples/ignored-dirs", true, false},
{"../examples/ignored-dirs", true, false},
{"../examples/invalid-infrastructure-to-app-import-in-tests", true, true},
{"../examples/invalid-infrastructure-to-app-import-in-tests", false, false},
{Path: "../examples/valid-simple", IsValid: true},
{Path: "../examples/invalid-infra-in-domain-import", IsValid: false},
{Path: "../examples/invalid-app-to-domain-import", IsValid: false},
{Path: "../examples/invalid-cross-module-deps", IsValid: false},
{Path: "../examples/valid-cross-module-deps", IsValid: true},
{Path: "../examples/valid-imports-inside-module", IsValid: true},
{Path: "../examples/invalid-imports-between-submodules", IsValid: false},
{Path: "../examples/invalid-imports-between-submodules-2", IsValid: false},
{Path: "../examples/ignored-dirs", IsValid: true},
{Path: "../examples/ignored-dirs", IsValid: true},
{Path: "../examples/invalid-infrastructure-to-app-import-in-tests", IsValid: true, IgnoreTests: true},
{Path: "../examples/invalid-infrastructure-to-app-import-in-tests", IsValid: false},
{
Path: "../examples/ignore-package",
IsValid: true,
IgnoredPackages: []string{"github.com/roblaszczak/go-cleanarch/examples/ignore-package/app"},
},
}

for _, c := range testCases {
t.Run(c.Path, func(t *testing.T) {
validator := cleanarch.NewValidator()
valid, errors, err := validator.Validate(c.Path, c.IgnoreTests)
valid, errors, err := validator.Validate(c.Path, c.IgnoreTests, c.IgnoredPackages)
if err != nil {
t.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions examples/ignore-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Invalid `app` to `domain` import can be ignored with "import-package" flag set to `github.com/roblaszczak/go-cleanarch/examples/ignore-package/app`.
3 changes: 3 additions & 0 deletions examples/ignore-package/app/price.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package app

type Price float64
8 changes: 8 additions & 0 deletions examples/ignore-package/domain/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package domain

import "github.com/roblaszczak/go-cleanarch/examples/ignore-package/app"

// Product imports app.Price, with breaks Dependency Rule.
type Product struct {
Price app.Price
}
14 changes: 14 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "fmt"

type sliceFlag []string

func (s *sliceFlag) String() string {
return fmt.Sprintf("%s", s)
}

func (s *sliceFlag) Set(v string) error {
*s = append(*s, v)
return nil
}
19 changes: 13 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ import (
"github.com/roblaszczak/go-cleanarch/cleanarch"
)

var (
ignoreTests = flag.Bool("ignore-tests", false, "if flag is passed *_test.go files will be not checked")
debug = flag.Bool("debug", false, "debug mode")
)

func main() {
ignoredPackages := sliceFlag{}

ignoreTests := flag.Bool("ignore-tests", false, "if flag is passed *_test.go files will be not checked")
debug := flag.Bool("debug", false, "debug mode")
flag.Var(
&ignoredPackages,
"ignore-package",
"provided packages can be imported to any layer, "+
"for example you can use`-ignore-package github.com/roblaszczak/go-cleanarch/infrastructure` to import "+
"this package to the domain",
)

flag.Parse()
var path string

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

validator := cleanarch.NewValidator()
isValid, errors, err := validator.Validate(path, *ignoreTests)
isValid, errors, err := validator.Validate(path, *ignoreTests, ignoredPackages)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 44674c3

Please sign in to comment.