Skip to content

Commit

Permalink
feat: parallel [#52] (#54)
Browse files Browse the repository at this point in the history
* Add parallel flag

* go 1.18.4

* parallel
  • Loading branch information
hedhyw authored Jul 24, 2022
1 parent bff3ff1 commit 7da4ac2
Show file tree
Hide file tree
Showing 57 changed files with 2,637 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.18.3'
go-version: '1.18.4'
id: go

- name: Check out code into the Go module directory
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.18.3'
go-version: '1.18.4'

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG GOLANG_DOCKER_TAG=1.18.3-alpine3.15
ARG GOLANG_DOCKER_TAG=1.18.4-alpine3.15
ARG ALPINE_DOCKER_TAG=3.15

FROM golang:$GOLANG_DOCKER_TAG as builder
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GOLANG_CI_LINT_VER:=v1.46.2
GOLANG_CI_LINT_VER:=v1.47.2
OUT_BIN?=${PWD}/bin/gherkingen
COVER_PACKAGES=./...
VERSION?=${shell git describe --tags}
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ gherkingen --help
Usage of gherkingen [FEATURE_FILE]:
-format string
output format: json, go, raw (default "go")
output format: autodetect, json, go, raw (default "autodetect")
-go-parallel
add parallel mark
-help
print usage
-list
Expand All @@ -193,6 +195,8 @@ Usage of gherkingen [FEATURE_FILE]:
The same calls to the generator always produces the same output
-template string
template file (default "@/std.struct.v1.go.tmpl")
-version
print version
```

## Running in docker
Expand Down
10 changes: 10 additions & 0 deletions internal/app/app.feature
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ Feature: Application command line tool
Examples:
| <feature> | <template> |
| app.feature | not_found |

Scenario: User wants to run tests in parallel
When `-go-parallel` is provided
And `scenario.feature` is given
Then generated code contains `t.Parallel()`

Scenario: User wants to run tests sequentially
When `-go-parallel` is not provided
And `scenario.feature` is given
Then generated code doesn't contain `t.Parallel()`
20 changes: 13 additions & 7 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func Run(arguments []string, out io.Writer, version string) (err error) {
false,
"print usage",
)
goParallel := flag.Bool(
"go-parallel",
false,
"add parallel mark",
)
listCmd := flag.Bool(
"list",
false,
Expand Down Expand Up @@ -82,12 +87,13 @@ func Run(arguments []string, out io.Writer, version string) (err error) {
case *helpCmd, inputFile == "":
return runHelp()
default:
return runGenerator(
out,
model.Format(*outputFormat),
*templateFile,
inputFile,
*packageName,
)
return runGenerator(appArgs{
Output: out,
OutputFormat: model.Format(*outputFormat),
TemplateFile: *templateFile,
InputFile: inputFile,
PackageName: *packageName,
GoParallel: *goParallel,
})
}
}
26 changes: 26 additions & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,32 @@ func TestApplicationCommandLineTool(t *testing.T) {
})
})
})

f.Scenario("User wants to run tests in parallel", func(t *testing.T, f *bdd.Feature) {
arguments := []string{}
f.When("`-go-parallel` is provided", func() {
arguments = append(arguments, "-go-parallel")
})
f.And("`app.feature` is given", func() {
arguments = append(arguments, "../generator/examples/scenario.feature")
})
f.Then("generated code contains `t.Parallel()`", func() {
assert.Contains(t, runApp(t, arguments, true), "t.Parallel()")
})
})

f.Scenario("User wants to run tests sequentially", func(t *testing.T, f *bdd.Feature) {
arguments := []string{}
f.When("`-go-parallel` is provided", func() {
// Go on.
})
f.And("`app.feature` is given", func() {
arguments = append(arguments, "../generator/examples/scenario.feature")
})
f.Then("generated code doesn't contain `t.Parallel()`", func() {
assert.NotContains(t, runApp(t, arguments, true), "t.Parallel()")
})
})
}

func runApp(tb testing.TB, arguments []string, ok bool) string {
Expand Down
36 changes: 23 additions & 13 deletions internal/app/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,49 @@ import (
"github.com/hedhyw/gherkingen/v2/internal/model"
)

// appArgs contains required arguments for runGenerator.
type appArgs struct {
Output io.Writer
OutputFormat model.Format
TemplateFile string
InputFile string
PackageName string
GoParallel bool
}

func runGenerator(
out io.Writer,
outputFormat model.Format,
templateFile string,
inputFile string,
packageName string,
args appArgs,
) (err error) {
templateSource, err := readTemplate(templateFile)
templateSource, err := readTemplate(args.TemplateFile)
if err != nil {
return err
}

if outputFormat == model.FormatAutoDetect {
outputFormat = detectFormat(templateFile)
if args.OutputFormat == model.FormatAutoDetect {
args.OutputFormat = detectFormat(args.TemplateFile)
}

inputSource, err := readInput(inputFile)
inputSource, err := readInput(args.InputFile)
if err != nil {
return err
}

goPlugin := goplugin.New(goplugin.Args{
Parallel: args.GoParallel,
})

data, err := generator.Generate(generator.Args{
Format: outputFormat,
Format: args.OutputFormat,
InputSource: inputSource,
TemplateSource: templateSource,
PackageName: packageName,
Plugin: multiplugin.New(goplugin.New()),
PackageName: args.PackageName,
Plugin: multiplugin.New(goPlugin),
})
if err != nil {
return err
}

fmt.Fprint(out, string(data))
fmt.Fprint(args.Output, string(data))

return nil
}
Expand Down
24 changes: 22 additions & 2 deletions internal/assets/std.struct.v1.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ import (
{{- $Scenario := . -}}
f.{{ $Scenario.PluginData.GoName }}({{ $Scenario.PluginData.GoValue }}, func({{- /*
t is usualy unused if there are no examples
*/ -}}{{- if $Scenario.Examples -}}_{{- else -}}t{{- end -}} *testing.T, f *bdd.Feature) {
*/ -}}{{- if and $Scenario.Examples (not $Scenario.PluginData.GoParallel) -}}_{{- else -}}t{{- end -}} *testing.T, f *bdd.Feature) {
{{- range $Scenario.Examples }}
{{- if $Scenario.PluginData.GoParallel }}
t.Parallel()

{{ end -}}

{{- /* Define test case struct. */ -}}

Expand All @@ -49,6 +53,10 @@ import (
}

f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
{{- if $Scenario.PluginData.GoParallel }}
t.Parallel()

{{ end -}}
{{- if $Scenario.PluginData.GoHasBackground }}
_ = background(t, f)
{{- end -}}
Expand All @@ -60,6 +68,10 @@ import (
{{- end }}
})
{{- else }}
{{- if $Scenario.PluginData.GoParallel }}
t.Parallel()

{{ end -}}
{{- range $Scenario.Steps }}
f.{{ .PluginData.GoName }}({{.PluginData.GoValue}}, func() {
{{- if $Scenario.PluginData.GoHasBackground }}
Expand All @@ -74,7 +86,11 @@ import (

{{ define "Rule" }}
{{ $Rule := . }}
f.{{ $Rule.PluginData.GoName }}({{ $Rule.PluginData.GoValue }}, func(_ *testing.T, f *bdd.Feature) {
f.{{ $Rule.PluginData.GoName }}({{ $Rule.PluginData.GoValue }}, func({{- if $Rule.PluginData.GoParallel -}}t{{- else -}}_{{- end -}} *testing.T, f *bdd.Feature) {
{{- if $Rule.PluginData.GoParallel }}
t.Parallel()

{{ end -}}
{{- range $Rule.Children -}}

{{- if .Background }}
Expand All @@ -90,6 +106,10 @@ import (
{{ end }}

func Test{{ .Feature.PluginData.GoName }}(t *testing.T) {
{{- if .Feature.PluginData.GoParallel }}
t.Parallel()

{{ end -}}
f := bdd.NewFeature(t, {{ .Feature.PluginData.GoValue }})
{{ if .Feature.PluginData.GoComment }}
/* {{ .Feature.PluginData.GoComment }} */
Expand Down
15 changes: 14 additions & 1 deletion internal/docplugin/goplugin/goplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,31 @@ const (
dataFieldGoName = "GoName"
dataFieldGoComment = "GoComment"
dataFieldGoBackground = "GoHasBackground"
dataFieldGoparallel = "GoParallel"
)

// GoPlugin injects golang specific information: go types, aliases.
type GoPlugin struct {
aliaser *goaliaser.Aliaser
exampleNameReplacer *strings.Replacer
usedExampleNames map[string]struct{}

args Args
}

// Args contains optional arguments for GoPlugin.
type Args struct {
Parallel bool
}

// New initializes a new go plugin.
func New() *GoPlugin {
func New(args Args) *GoPlugin {
return &GoPlugin{
aliaser: goaliaser.New(),
exampleNameReplacer: strings.NewReplacer(" ", "_", "\t", "_"),
usedExampleNames: make(map[string]struct{}),

args: args,
}
}

Expand Down Expand Up @@ -150,16 +160,19 @@ func (p GoPlugin) handleStruct(
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name)
val.PluginData[dataFieldGoType] = string(goTypeString)
val.PluginData[dataFieldGoComment] = p.prepareFeatureDescription(val.Description)
val.PluginData[dataFieldGoparallel] = p.args.Parallel
p.processFeatureBackground(val)
case model.Rule:
val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(val.Keyword)
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name)
val.PluginData[dataFieldGoType] = string(goTypeString)
val.PluginData[dataFieldGoparallel] = p.args.Parallel
p.processRuleBackground(val)
case model.Scenario:
val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(val.Keyword)
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name)
val.PluginData[dataFieldGoType] = string(goTypeString)
val.PluginData[dataFieldGoparallel] = p.args.Parallel
case model.Step:
val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(val.Keyword)
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Text)
Expand Down
Loading

0 comments on commit 7da4ac2

Please sign in to comment.