From 0514c73141f576ad1a1d28a6b363cc52b48e009e Mon Sep 17 00:00:00 2001 From: rgodden Date: Thu, 16 May 2024 15:02:46 +0100 Subject: [PATCH] simplify as we can only concat lists --- eval/eval.go | 37 +++++++++---------------------------- eval/eval_test.go | 12 +----------- glob/glob.go | 8 ++++++-- glob/glob_test.go | 4 ++-- 4 files changed, 18 insertions(+), 43 deletions(-) diff --git a/eval/eval.go b/eval/eval.go index 7f42a2d..14f0302 100644 --- a/eval/eval.go +++ b/eval/eval.go @@ -1,6 +1,7 @@ package eval import ( + "fmt" "strings" "github.com/please-build/buildtools/build" @@ -31,18 +32,10 @@ func LookLikeBuildLabel(l string) bool { } func (e *Eval) EvalGlobs(dir string, rule *build.Rule, attrName string) ([]string, error) { - files, err := e.evalGlobs(dir, rule.Attr(attrName)) - if err != nil { - return nil, err - } - ret := make([]string, 0, len(files)) - for f := range files { - ret = append(ret, f) - } - return ret, nil + return e.evalGlobs(dir, rule.Attr(attrName)) } -func (e *Eval) evalGlobs(dir string, val build.Expr) (map[string]struct{}, error) { +func (e *Eval) evalGlobs(dir string, val build.Expr) ([]string, error) { switch expr := val.(type) { case *build.CallExpr: globArgs := parseGlob(expr) @@ -51,7 +44,10 @@ func (e *Eval) evalGlobs(dir string, val build.Expr) (map[string]struct{}, error } return e.globber.Glob(dir, globArgs) case *build.BinaryExpr: - ret, err := e.evalGlobs(dir, expr.X) + if expr.Op != "+" { + return nil, fmt.Errorf("encountered a binary expression with operation %s. Only + is supported", expr.Op) + } + x, err := e.evalGlobs(dir, expr.X) if err != nil { return nil, err } @@ -59,24 +55,9 @@ func (e *Eval) evalGlobs(dir string, val build.Expr) (map[string]struct{}, error if err != nil { return nil, err } - switch expr.Op { - case "+": - for f := range y { - ret[f] = struct{}{} - } - case "-": - for f := range y { - delete(ret, f) - } - } - return ret, nil + return append(x, y...), nil default: - str := build.Strings(expr) - ret := make(map[string]struct{}, len(str)) - for _, s := range str { - ret[s] = struct{}{} - } - return ret, nil + return build.Strings(expr), nil } } diff --git a/eval/eval_test.go b/eval/eval_test.go index f1d4d31..bf14d28 100644 --- a/eval/eval_test.go +++ b/eval/eval_test.go @@ -35,7 +35,6 @@ func TestParseGlob(t *testing.T) { include: []string{"*.go"}, exclude: []string{"*_test.go"}, }, - { name: "ignores other args", // Please has some other stuff we don't care about. @@ -85,22 +84,13 @@ func TestEvalGlob(t *testing.T) { code: `glob(["mai*.go"]) + ["bar.go"]`, expected: []string{"main.go", "bar.go"}, }, - { - name: "glob - strings", - code: `glob(["*.go"]) - ["bar.go"]`, - expected: []string{"main.go", "bar_test.go"}, - }, } for _, test := range testCases { t.Run(test.name, func(t *testing.T) { file, err := build.ParseBuild(test.name, []byte(test.code)) require.NoError(t, err) require.Len(t, file.Stmt, 1) - ret, err := e.evalGlobs("test_project", file.Stmt[0]) - got := make([]string, 0, len(ret)) - for f := range ret { - got = append(got, f) - } + got, err := e.evalGlobs("test_project", file.Stmt[0]) require.NoError(t, err) assert.ElementsMatch(t, test.expected, got) }) diff --git a/glob/glob.go b/glob/glob.go index ad51260..4f5932f 100644 --- a/glob/glob.go +++ b/glob/glob.go @@ -25,7 +25,7 @@ func New() *Globber { // 1) globs should only match .go files as they're being used in go rules // 2) go rules will never depend on files outside the package dir, so we don't need to support ** // 3) we don't want symlinks, directories and other non-regular files -func (g *Globber) Glob(dir string, args *Args) (map[string]struct{}, error) { +func (g *Globber) Glob(dir string, args *Args) ([]string, error) { inc := map[string]struct{}{} for _, i := range args.Include { fs, err := g.glob(dir, i) @@ -49,7 +49,11 @@ func (g *Globber) Glob(dir string, args *Args) (map[string]struct{}, error) { } } - return inc, nil + ret := make([]string, 0, len(inc)) + for i := range inc { + ret = append(ret, i) + } + return ret, nil } // glob matches all regular files in a directory based on a glob pattern diff --git a/glob/glob_test.go b/glob/glob_test.go index e2ba33c..e52bbae 100644 --- a/glob/glob_test.go +++ b/glob/glob_test.go @@ -15,7 +15,7 @@ func TestGlob(t *testing.T) { }) require.NoError(t, err) - assert.Equal(t, map[string]struct{}{"bar_test.go": {}}, files) + assert.ElementsMatch(t, []string{"bar_test.go"}, files) }) t.Run("excludes pattern", func(t *testing.T) { @@ -25,6 +25,6 @@ func TestGlob(t *testing.T) { }) require.NoError(t, err) - assert.Equal(t, map[string]struct{}{"main.go": {}, "bar.go": {}}, files) + assert.ElementsMatch(t, []string{"main.go", "bar.go"}, files) }) }