From 7610f75bbcd061386806dc0e084a0e1c2fb54969 Mon Sep 17 00:00:00 2001 From: Charlie Vieth Date: Sat, 28 May 2022 18:39:35 -0400 Subject: [PATCH] buildutil: remove tags field from Constraint The tags field is not needed. --- buildutil.go | 27 +++----------------- buildutil_test.go | 64 ++++------------------------------------------- 2 files changed, 8 insertions(+), 83 deletions(-) diff --git a/buildutil.go b/buildutil.go index 6876a5d..a51be7c 100644 --- a/buildutil.go +++ b/buildutil.go @@ -206,7 +206,6 @@ var emptyConstraint Constraint // A nil Constraint is safe to use and matches any build.Context. type Constraint struct { expr constraint.Expr - tags map[string]bool } // NewConstraint returns a new Constraint for the given constraint.Expr and @@ -215,7 +214,7 @@ func NewConstraint(expr constraint.Expr, tags map[string]bool) *Constraint { if expr == nil && len(tags) == 0 { return &emptyConstraint } - return &Constraint{expr: expr, tags: tags} + return &Constraint{expr: expr} } // Expr returns the Constraint's constraint.Expr. @@ -226,25 +225,13 @@ func (c *Constraint) Expr() constraint.Expr { return c.expr } -// Tags returns a copy of the Constraint's build tags. -func (c *Constraint) Tags() map[string]bool { - if c == nil || len(c.tags) == 0 { - return nil - } - tags := make(map[string]bool, len(c.tags)) - for k, v := range c.tags { - tags[k] = v - } - return tags -} - // Empty returns true if c has no build constraints. An empty Constraint // matches all files. func (c *Constraint) Empty() bool { return c == nil || c.expr == nil } // Eval reports whether build.Context ctxt matches the build constraint. func (c *Constraint) Eval(ctxt *build.Context) bool { - return c.Empty() || eval(ctxt, c.expr, c.tags) + return c.Empty() || eval(ctxt, c.expr, nil) } // ParseConstraint parses the build constraints of a Go source file, if any. @@ -260,19 +247,11 @@ func ParseConstraint(ctxt *build.Context, filename string, src interface{}) (*Co if err != nil { return nil, err } - tags := make(map[string]bool) - goodOSArchFile(ctxt, filepath.Base(filename), tags) - if _, _, err := shouldBuild(ctxt, data, tags); err != nil { - return nil, err - } expr, err := parseBuildConstraint(data) if err != nil { return nil, err } - if len(tags) == 0 { - tags = nil - } - return &Constraint{tags: tags, expr: expr}, nil + return &Constraint{expr: expr}, nil } func openReaderDirName(ctxt *build.Context, dir, name string, src interface{}) (io.ReadCloser, error) { diff --git a/buildutil_test.go b/buildutil_test.go index 612d8cc..07be47d 100644 --- a/buildutil_test.go +++ b/buildutil_test.go @@ -263,21 +263,6 @@ func TestShouldBuild(t *testing.T) { } } -func tagsEqual(m1, m2 map[string]bool) bool { - if len(m1) != len(m2) { - return false - } - if len(m1) == 0 { - return true - } - for k, v := range m1 { - if m2[k] != v { - return false - } - } - return true -} - func TestParseConstraint(t *testing.T) { for _, tt := range shouldBuildTests { t.Run(tt.name, func(t *testing.T) { @@ -287,12 +272,12 @@ func TestParseConstraint(t *testing.T) { t.Fatal(err) } shouldBuild := expr.Eval(ctx) - if shouldBuild != tt.shouldBuild || !tagsEqual(expr.tags, tt.tags) || err != tt.err { + if shouldBuild != tt.shouldBuild || err != tt.err { t.Errorf("mismatch:\n"+ - "have shouldBuild=%v, tags=%v, err=%v\n"+ - "want shouldBuild=%v, tags=%v, err=%v", - shouldBuild, expr.tags, err, - tt.shouldBuild, tt.tags, tt.err) + "have shouldBuild=%v, err=%v\n"+ + "want shouldBuild=%v, err=%v", + shouldBuild, err, + tt.shouldBuild, tt.err) } }) } @@ -301,17 +286,10 @@ func TestParseConstraint(t *testing.T) { ctxt := build.Default ctxt.GOOS = "linux" ctxt.GOARCH = "amd64" - want := map[string]bool{ - "linux": true, - "amd64": true, - } expr, err := ParseConstraint(&ctxt, "x_linux_amd64.go", []byte("package x\n")) if err != nil { t.Fatal(err) } - if !tagsEqual(expr.tags, want) { - t.Errorf("Tags: got: %v want: %v", expr.tags, want) - } if ok := expr.Eval(&ctxt); !ok { t.Errorf("Eval: got: %t want: %t", ok, true) } @@ -323,9 +301,6 @@ func TestParseConstraint(t *testing.T) { if err != nil { t.Fatal(err) } - if expr.tags != nil { - t.Errorf("Tags: got: %v want: %v", expr.tags, nil) - } if expr.expr != nil { t.Errorf("Expr: got: %v want: %v", expr.expr, nil) } @@ -334,32 +309,6 @@ func TestParseConstraint(t *testing.T) { } }) - t.Run("Tags", func(t *testing.T) { - want := map[string]bool{"a": true, "b": true} - expr := NewConstraint(nil, want) - if tags := expr.Tags(); !tagsEqual(tags, want) { - if expr.tags != nil { - t.Errorf("Tags: got: %v want: %v", expr.tags, nil) - } - } - // Make sure we made a copy - tags := expr.Tags() - tags["aa"] = true - if !tagsEqual(expr.tags, want) { - t.Errorf("Tags: got: %v want: %v", expr.tags, nil) - } - }) - - t.Run("EmptyTags", func(t *testing.T) { - expr := NewConstraint(nil, map[string]bool{}) - if expr != &emptyConstraint { - t.Error("Failed to return emptyConstraint when len(tags) == 0") - } - if expr.tags != nil { - t.Errorf("Tags: got: %v want: %v", expr.tags, nil) - } - }) - t.Run("NilConstraint", func(t *testing.T) { // Make sure a nil Constraint can be used var expr *Constraint @@ -369,9 +318,6 @@ func TestParseConstraint(t *testing.T) { if expr.Expr() != nil { t.Errorf("Expr: got: %v want: %v", expr.Expr(), nil) } - if expr.Tags() != nil { - t.Errorf("Tags: got: %v want: %v", expr.Tags(), nil) - } if !expr.Eval(nil) { t.Errorf("Eval: got: %v want: %v", expr.Eval(nil), nil) }