-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements formatting of constraints according to the current Go version supported syntax. This is achieved by delegating to go/format and extracting out the resulting comments. Also provides functions to query for constraint syntax support, which are mainly intended for writing version-dependent tests. Updates #183
- Loading branch information
1 parent
a548039
commit 55d98cc
Showing
5 changed files
with
143 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,45 @@ | ||
package buildtags | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"go/format" | ||
"strings" | ||
) | ||
|
||
// PlusBuildSyntaxSupported reports whether the current Go version supports the | ||
// "// +build" constraint syntax. | ||
func PlusBuildSyntaxSupported() bool { return plusbuild } | ||
|
||
// GoBuildSyntaxSupported reports whether the current Go version supports the | ||
// "//go:build" constraint syntax. | ||
func GoBuildSyntaxSupported() bool { return gobuild } | ||
|
||
// Format constraints according to the syntax supported by the current Go version. | ||
func Format(t ConstraintsConvertable) (string, error) { | ||
// Print build tags to minimal Go source that can be passed to go/format. | ||
src := t.ToConstraints().GoString() + "\npackage stub" | ||
|
||
// Format them. | ||
formatted, err := format.Source([]byte(src)) | ||
if err != nil { | ||
return "", fmt.Errorf("format build constraints: %w", err) | ||
} | ||
|
||
// Extract the comment lines. | ||
buf := bytes.NewReader(formatted) | ||
scanner := bufio.NewScanner(buf) | ||
output := "" | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.HasPrefix(line, "//") { | ||
output += line + "\n" | ||
} | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return "", fmt.Errorf("parse formatted build constraints: %w", err) | ||
} | ||
|
||
return output, nil | ||
} |
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,9 @@ | ||
//go:build !go1.17 | ||
// +build !go1.17 | ||
|
||
package buildtags | ||
|
||
const ( | ||
plusbuild = true | ||
gobuild = false | ||
) |
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,9 @@ | ||
//go:build go1.17 && !go1.18 | ||
// +build go1.17,!go1.18 | ||
|
||
package buildtags | ||
|
||
const ( | ||
plusbuild = true | ||
gobuild = true | ||
) |
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,9 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package buildtags | ||
|
||
const ( | ||
plusbuild = false | ||
gobuild = true | ||
) |
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,71 @@ | ||
package buildtags | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
cases := []struct { | ||
PlusBuild []string | ||
GoBuild string | ||
}{ | ||
{ | ||
PlusBuild: []string{"amd64"}, | ||
GoBuild: "amd64", | ||
}, | ||
{ | ||
PlusBuild: []string{ | ||
"linux darwin", | ||
"amd64 arm64 mips64x ppc64x", | ||
}, | ||
GoBuild: "(linux || darwin) && (amd64 || arm64 || mips64x || ppc64x)", | ||
}, | ||
{ | ||
PlusBuild: []string{"!linux,!darwin !amd64,!arm64,!mips64x,!ppc64x"}, | ||
GoBuild: "(!linux && !darwin) || (!amd64 && !arm64 && !mips64x && !ppc64x)", | ||
}, | ||
{ | ||
PlusBuild: []string{ | ||
"linux,386 darwin,!cgo", | ||
"!noasm", | ||
}, | ||
GoBuild: "((linux && 386) || (darwin && !cgo)) && !noasm", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
// Parse constraints. | ||
var cs Constraints | ||
for _, expr := range c.PlusBuild { | ||
constraint, err := ParseConstraint(expr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
cs = append(cs, constraint) | ||
} | ||
|
||
// Build expected output. | ||
var buf bytes.Buffer | ||
if GoBuildSyntaxSupported() { | ||
fmt.Fprintf(&buf, "//go:build %s\n", c.GoBuild) | ||
} | ||
if PlusBuildSyntaxSupported() { | ||
for _, expr := range c.PlusBuild { | ||
fmt.Fprintf(&buf, "// +build %s\n", expr) | ||
} | ||
} | ||
expect := buf.String() | ||
|
||
// Format and check. | ||
got, err := Format(cs) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if got != expect { | ||
t.Errorf("got=\n%s\nexpect=\n%s\n", got, expect) | ||
} | ||
} | ||
} |