Skip to content

Commit

Permalink
Add start_with that rule
Browse files Browse the repository at this point in the history
  • Loading branch information
git-audo authored and omissis committed Nov 4, 2024
1 parent 4deab04 commit fe91c34
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
44 changes: 44 additions & 0 deletions internal/arch/file/that/start_with.go
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)
}
64 changes: 64 additions & 0 deletions internal/arch/file/that/start_with_test.go
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)
}
})
}
}

0 comments on commit fe91c34

Please sign in to comment.