Skip to content

Commit

Permalink
feat(ginkgo/generators): add --tags flag (#1216)
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <ale_grey_91@hotmail.it>
  • Loading branch information
alegrey91 authored Jun 7, 2023
1 parent 548d78e commit a782a77
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ginkgo/generators/generate_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func BuildGenerateCommand() command.Command {
{Name: "template-data", KeyPath: "CustomTemplateData",
UsageArgument: "template-data-file",
Usage: "If specified, generate will use the contents of the file passed as data to be rendered in the test file template"},
{Name: "tags", KeyPath: "Tags",
UsageArgument: "build-tags",
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`)"},
},
&conf,
types.GinkgoFlagSections{},
Expand Down Expand Up @@ -59,6 +62,7 @@ You can also pass a <filename> of the form "file.go" and generate will emit "fil
}

type specData struct {
BuildTags string
Package string
Subject string
PackageImportPath string
Expand Down Expand Up @@ -93,6 +97,7 @@ func generateTestFileForSubject(subject string, conf GeneratorsConfig) {
}

data := specData{
BuildTags: getBuildTags(conf.Tags),
Package: determinePackageName(packageName, conf.Internal),
Subject: formattedName,
PackageImportPath: getPackageImportPath(),
Expand Down
6 changes: 4 additions & 2 deletions ginkgo/generators/generate_templates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

var specText = `package {{.Package}}
var specText = `{{.BuildTags}}
package {{.Package}}
import (
{{.GinkgoImport}}
Expand All @@ -14,7 +15,8 @@ var _ = {{.GinkgoPackage}}Describe("{{.Subject}}", func() {
})
`

var agoutiSpecText = `package {{.Package}}
var agoutiSpecText = `{{.BuildTags}}
package {{.Package}}
import (
{{.GinkgoImport}}
Expand Down
12 changes: 12 additions & 0 deletions ginkgo/generators/generators_common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"fmt"
"go/build"
"os"
"path/filepath"
Expand All @@ -14,6 +15,7 @@ type GeneratorsConfig struct {
Agouti, NoDot, Internal bool
CustomTemplate string
CustomTemplateData string
Tags string
}

func getPackageAndFormattedName() (string, string, string) {
Expand Down Expand Up @@ -62,3 +64,13 @@ func determinePackageName(name string, internal bool) string {

return name + "_test"
}

// getBuildTags returns the resultant string to be added.
// If the input string is not empty, then returns a `//go:build {}` string,
// otherwise returns an empty string.
func getBuildTags(tags string) string {
if tags != "" {
return fmt.Sprintf("//go:build %s\n", tags)
}
return ""
}

0 comments on commit a782a77

Please sign in to comment.