Skip to content

Commit a782a77

Browse files
authoredJun 7, 2023
feat(ginkgo/generators): add --tags flag (#1216)
Signed-off-by: Alessio Greggi <ale_grey_91@hotmail.it>
1 parent 548d78e commit a782a77

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed
 

‎ginkgo/generators/generate_command.go

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func BuildGenerateCommand() command.Command {
3232
{Name: "template-data", KeyPath: "CustomTemplateData",
3333
UsageArgument: "template-data-file",
3434
Usage: "If specified, generate will use the contents of the file passed as data to be rendered in the test file template"},
35+
{Name: "tags", KeyPath: "Tags",
36+
UsageArgument: "build-tags",
37+
Usage: "If specified, generate will create a test file that uses the given build tags (i.e. `--tags e2e,!unit` will add `//go:build e2e,!unit`)"},
3538
},
3639
&conf,
3740
types.GinkgoFlagSections{},
@@ -59,6 +62,7 @@ You can also pass a <filename> of the form "file.go" and generate will emit "fil
5962
}
6063

6164
type specData struct {
65+
BuildTags string
6266
Package string
6367
Subject string
6468
PackageImportPath string
@@ -93,6 +97,7 @@ func generateTestFileForSubject(subject string, conf GeneratorsConfig) {
9397
}
9498

9599
data := specData{
100+
BuildTags: getBuildTags(conf.Tags),
96101
Package: determinePackageName(packageName, conf.Internal),
97102
Subject: formattedName,
98103
PackageImportPath: getPackageImportPath(),

‎ginkgo/generators/generate_templates.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generators
22

3-
var specText = `package {{.Package}}
3+
var specText = `{{.BuildTags}}
4+
package {{.Package}}
45
56
import (
67
{{.GinkgoImport}}
@@ -14,7 +15,8 @@ var _ = {{.GinkgoPackage}}Describe("{{.Subject}}", func() {
1415
})
1516
`
1617

17-
var agoutiSpecText = `package {{.Package}}
18+
var agoutiSpecText = `{{.BuildTags}}
19+
package {{.Package}}
1820
1921
import (
2022
{{.GinkgoImport}}

‎ginkgo/generators/generators_common.go

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generators
22

33
import (
4+
"fmt"
45
"go/build"
56
"os"
67
"path/filepath"
@@ -14,6 +15,7 @@ type GeneratorsConfig struct {
1415
Agouti, NoDot, Internal bool
1516
CustomTemplate string
1617
CustomTemplateData string
18+
Tags string
1719
}
1820

1921
func getPackageAndFormattedName() (string, string, string) {
@@ -62,3 +64,13 @@ func determinePackageName(name string, internal bool) string {
6264

6365
return name + "_test"
6466
}
67+
68+
// getBuildTags returns the resultant string to be added.
69+
// If the input string is not empty, then returns a `//go:build {}` string,
70+
// otherwise returns an empty string.
71+
func getBuildTags(tags string) string {
72+
if tags != "" {
73+
return fmt.Sprintf("//go:build %s\n", tags)
74+
}
75+
return ""
76+
}

0 commit comments

Comments
 (0)
Please sign in to comment.