-
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
2 changed files
with
108 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package that | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/omissis/goarkitect/internal/arch/file" | ||
"github.com/omissis/goarkitect/internal/arch/rule" | ||
) | ||
|
||
func StartWith(s string) *StartWithExpression { | ||
return &StartWithExpression{ | ||
prefix: s, | ||
} | ||
} | ||
|
||
type StartWithExpression struct { | ||
prefix string | ||
|
||
errors []error | ||
} | ||
|
||
func (e *StartWithExpression) GetErrors() []error { | ||
return e.errors | ||
} | ||
|
||
func (e *StartWithExpression) Evaluate(rb rule.Builder) { | ||
frb, ok := rb.(*file.RuleBuilder) | ||
if !ok { | ||
e.errors = append(e.errors, file.ErrInvalidRuleBuilder) | ||
|
||
return | ||
} | ||
|
||
files := make([]string, 0) | ||
|
||
for _, f := range frb.GetFiles() { | ||
if strings.HasPrefix(filepath.Base(f), e.prefix) { | ||
files = append(files, f) | ||
} | ||
} | ||
|
||
frb.SetFiles(files) | ||
} |
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,64 @@ | ||
package that_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
|
||
"github.com/omissis/goarkitect/internal/arch/file" | ||
"github.com/omissis/goarkitect/internal/arch/file/that" | ||
"github.com/omissis/goarkitect/internal/arch/rule" | ||
) | ||
|
||
func Test_StartWith(t *testing.T) { | ||
t.Parallel() | ||
|
||
rb := func() *file.RuleBuilder { | ||
rb := file.All() | ||
rb.SetFiles([]string{"Dockerfile", "Makefile", "foo/bar.go"}) | ||
|
||
return rb | ||
} | ||
|
||
testCases := []struct { | ||
desc string | ||
ruleBuilder *file.RuleBuilder | ||
prefix string | ||
want []string | ||
}{ | ||
{ | ||
desc: "files starting with 'foo'", | ||
ruleBuilder: rb(), | ||
prefix: "foo", | ||
want: nil, | ||
}, | ||
{ | ||
desc: "files starting with 'Make'", | ||
ruleBuilder: rb(), | ||
prefix: "Make", | ||
want: []string{"Makefile"}, | ||
}, | ||
{ | ||
desc: "files in a subdirectory starting with 'bar", | ||
ruleBuilder: rb(), | ||
prefix: "bar", | ||
want: []string{"foo/bar.go"}, | ||
}, | ||
} | ||
for _, tC := range testCases { | ||
tC := tC | ||
|
||
t.Run(tC.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ew := that.StartWith(tC.prefix) | ||
ew.Evaluate(tC.ruleBuilder) | ||
|
||
got := tC.ruleBuilder.GetFiles() | ||
if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) { | ||
t.Errorf("want = %+v, got = %+v", tC.want, got) | ||
} | ||
}) | ||
} | ||
} |