Skip to content

Commit

Permalink
buildutil: remove tags field from Constraint
Browse files Browse the repository at this point in the history
The tags field is not needed.
  • Loading branch information
charlievieth committed May 28, 2022
1 parent 47d355c commit 7610f75
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 83 deletions.
27 changes: 3 additions & 24 deletions buildutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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) {
Expand Down
64 changes: 5 additions & 59 deletions buildutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
})
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 7610f75

Please sign in to comment.