From ee84b7087f18c7688bb70e351fc2fea7cf8e3987 Mon Sep 17 00:00:00 2001 From: Robert Laszczak Date: Tue, 24 Oct 2017 13:07:36 +0200 Subject: [PATCH] added -ignore-tests flag --- README.md | 7 ++++++ cleanarch/cleanarch.go | 6 ++++- cleanarch/cleanarch_test.go | 22 +++++++++++-------- .../app/product.go | 7 ++++++ .../app/product_test.go | 14 ++++++++++++ .../infrastructure/product.go | 10 +++++++++ main.go | 6 ++++- 7 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 examples/invalid-infrastructure-to-app-import-in-tests/app/product.go create mode 100644 examples/invalid-infrastructure-to-app-import-in-tests/app/product_test.go create mode 100644 examples/invalid-infrastructure-to-app-import-in-tests/infrastructure/product.go diff --git a/README.md b/README.md index 8f22296..93ff735 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,13 @@ To run in provided directory Process will exit with code `1` if architecture is not valid, otherwise it will exit with `0`. +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. + ## Running the tests make test diff --git a/cleanarch/cleanarch.go b/cleanarch/cleanarch.go index 8e3a8d9..5c5ae43 100644 --- a/cleanarch/cleanarch.go +++ b/cleanarch/cleanarch.go @@ -80,7 +80,7 @@ type Validator struct { } // Validate validates provided path for Clean Architecture rules. -func (v *Validator) Validate(root string) (bool, []ValidationError, error) { +func (v *Validator) Validate(root string, ignoreTests bool) (bool, []ValidationError, error) { errors := []ValidationError{} err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error { @@ -92,6 +92,10 @@ func (v *Validator) Validate(root string) (bool, []ValidationError, error) { return nil } + if ignoreTests && strings.HasSuffix(path, "_test.go") { + return nil + } + if strings.Contains(path, "/vendor/") { // todo - better check and flag return nil diff --git a/cleanarch/cleanarch_test.go b/cleanarch/cleanarch_test.go index 9622bd4..3a4f36f 100644 --- a/cleanarch/cleanarch_test.go +++ b/cleanarch/cleanarch_test.go @@ -10,21 +10,25 @@ func TestValidator_Validate(t *testing.T) { testCases := []struct { Path string IsValid bool + IgnoreTests bool }{ - {"../examples/valid-simple", true}, - {"../examples/invalid-infra-in-domain-import", false}, - {"../examples/invalid-app-to-domain-import", false}, - {"../examples/invalid-cross-module-deps", false}, - {"../examples/valid-cross-module-deps", true}, - {"../examples/valid-imports-inside-module", true}, - {"../examples/invalid-imports-between-submodules", false}, - {"../examples/ignored-dirs", true}, + {"../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/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}, } for _, c := range testCases { t.Run(c.Path, func(t *testing.T) { validator := cleanarch.NewValidator() - valid, errors, err := validator.Validate(c.Path) + valid, errors, err := validator.Validate(c.Path, c.IgnoreTests) if err != nil { t.Fatal(err) } diff --git a/examples/invalid-infrastructure-to-app-import-in-tests/app/product.go b/examples/invalid-infrastructure-to-app-import-in-tests/app/product.go new file mode 100644 index 0000000..a91b8ea --- /dev/null +++ b/examples/invalid-infrastructure-to-app-import-in-tests/app/product.go @@ -0,0 +1,7 @@ +package app + +type Product struct {} + +func NewProduct() *Product { + return &Product{} +} diff --git a/examples/invalid-infrastructure-to-app-import-in-tests/app/product_test.go b/examples/invalid-infrastructure-to-app-import-in-tests/app/product_test.go new file mode 100644 index 0000000..5713dc8 --- /dev/null +++ b/examples/invalid-infrastructure-to-app-import-in-tests/app/product_test.go @@ -0,0 +1,14 @@ +package app_test + +import ( + "testing" + "github.com/roblaszczak/go-cleanarch/examples/invalid-infrastructure-to-app-import-in-tests/infrastructure" + "github.com/roblaszczak/go-cleanarch/examples/invalid-infrastructure-to-app-import-in-tests/app" +) + +func TestNewProduct(t *testing.T) { + repo := infrastructure.ProductMemoryRepo{} + + product := app.NewProduct() + repo.Add(product) +} diff --git a/examples/invalid-infrastructure-to-app-import-in-tests/infrastructure/product.go b/examples/invalid-infrastructure-to-app-import-in-tests/infrastructure/product.go new file mode 100644 index 0000000..6f86f22 --- /dev/null +++ b/examples/invalid-infrastructure-to-app-import-in-tests/infrastructure/product.go @@ -0,0 +1,10 @@ +package infrastructure + +import "github.com/roblaszczak/go-cleanarch/examples/invalid-infrastructure-to-app-import-in-tests/app" + +// ProductMemoryRepo is imported in domain.Product tests. +type ProductMemoryRepo struct {} + +func (p *ProductMemoryRepo) Add(product *app.Product) { + // ... +} diff --git a/main.go b/main.go index c9eec93..f9ae842 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,10 @@ import ( "path/filepath" ) +var ( + ignoreTests = flag.Bool("ignore-tests", false, "if flag is passed *_test.go files will be not checked") +) + func main() { flag.Parse() var path string @@ -25,7 +29,7 @@ func main() { fmt.Printf("[cleanarch] checking %s\n", path) validator := cleanarch.NewValidator() - isValid, errors, err := validator.Validate(path) + isValid, errors, err := validator.Validate(path, *ignoreTests) if err != nil { panic(err) }