Skip to content

Commit

Permalink
added -ignore-tests flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Laszczak committed Oct 24, 2017
1 parent 4110b71 commit ee84b70
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 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) (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 {
Expand All @@ -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
Expand Down
22 changes: 13 additions & 9 deletions cleanarch/cleanarch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package app

type Product struct {}

func NewProduct() *Product {
return &Product{}
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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) {
// ...
}
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit ee84b70

Please sign in to comment.