From d478d82391ae4b04a52e551c1956135f015d5af3 Mon Sep 17 00:00:00 2001 From: hedhyw Date: Wed, 19 Jun 2024 05:35:58 +0200 Subject: [PATCH] feat: support a new flag to enable go1.22 features --- README.md | 6 +++-- internal/app/app.go | 12 ++++++---- internal/app/generator.go | 4 +++- internal/assets/std.simple.v1.go.tmpl | 2 +- internal/docplugin/goplugin/goplugin.go | 15 ++++++++---- internal/docplugin/goplugin/goplugin_test.go | 23 +++++++++++++++++++ .../simple/background.feature_test.go | 8 ------- .../examples/simple/bool.feature_test.go | 9 +------- .../examples/simple/complex.feature_test.go | 9 +------- .../examples/simple/issue_26.feature_test.go | 9 +------- .../simple/issue_27_multi.feature_test.go | 4 ---- .../simple/issue_27_single.feature_test.go | 4 ---- .../examples/simple/readme.feature_test.go | 9 +------- .../examples/simple/rules.feature_test.go | 15 ++---------- .../examples/simple/scenario.feature_test.go | 4 ---- .../examples/simple/simple.feature_test.go | 4 ---- scripts/examples.sh | 2 +- 17 files changed, 55 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index a036297..ff2ade9 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,8 @@ See [internal/app/app.feature](internal/app/app.feature) and [internal/app/app_t ## Version 4 changes 1. Removed template "std.struct.v1.go.tmpl". +2. The flag "go-parallel" has been removed. +3. Loop variable "testCase" is not copied. Disable using `disable-go-22-scope` flag. # Install @@ -196,12 +198,12 @@ gherkingen -list gherkingen --help Usage of gherkingen [FEATURE_FILE]: + -enable-go-22-scope + do not copy loop variables -disable-go-parallel disable execution of tests in parallel -format string output format: autodetect, json, go, raw (default "autodetect") - -go-parallel - add parallel mark (deprecated, enabled by default) (default true) -help print usage -list diff --git a/internal/app/app.go b/internal/app/app.go index 1a7666d..e8f1501 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -16,6 +16,7 @@ const ( internalPathPrefix = "@/" defaultTemplate = "std.simple.v1.go.tmpl" defaultDisableGoParallel = false + defaultEnableGo22Scope = false defaultOutputFormat = model.FormatAutoDetect ) @@ -44,16 +45,16 @@ func Run(arguments []string, out io.Writer, version string) (err error) { false, "print usage", ) - _ = flagSet.Bool( - "go-parallel", - !defaultDisableGoParallel, - "add parallel mark (deprecated, enabled by default)", - ) disableGoParallel := flagSet.Bool( "disable-go-parallel", defaultDisableGoParallel, "disable execution of tests in parallel", ) + enableGo22Scope := flagSet.Bool( + "enable-go-22-scope", + defaultEnableGo22Scope, + "do not copy loop variables", + ) listCmd := flagSet.Bool( "list", false, @@ -101,6 +102,7 @@ func Run(arguments []string, out io.Writer, version string) (err error) { InputFile: inputFile, PackageName: *packageName, GoParallel: !(*disableGoParallel), + Go22Scope: *enableGo22Scope, GenerateUUID: newUUIDRandomGenerator(seed), }) } diff --git a/internal/app/generator.go b/internal/app/generator.go index d38eb30..33bab99 100644 --- a/internal/app/generator.go +++ b/internal/app/generator.go @@ -20,6 +20,7 @@ type appArgs struct { InputFile string PackageName string GoParallel bool + Go22Scope bool GenerateUUID func() string } @@ -41,7 +42,8 @@ func runGenerator( } goPlugin := goplugin.New(goplugin.Args{ - Parallel: args.GoParallel, + Parallel: args.GoParallel, + Go22Scope: args.Go22Scope, }) data, err := generator.Generate(generator.Args{ diff --git a/internal/assets/std.simple.v1.go.tmpl b/internal/assets/std.simple.v1.go.tmpl index 12dccf3..06df2bd 100644 --- a/internal/assets/std.simple.v1.go.tmpl +++ b/internal/assets/std.simple.v1.go.tmpl @@ -53,7 +53,7 @@ import ( } for name, testCase := range testCases { - {{- if $Scenario.PluginData.GoParallel }} + {{- if and $Scenario.PluginData.GoParallel (not $Scenario.PluginData.Go22Scope) }} testCase := testCase {{ end -}} diff --git a/internal/docplugin/goplugin/goplugin.go b/internal/docplugin/goplugin/goplugin.go index ff1e81e..f15b6f4 100644 --- a/internal/docplugin/goplugin/goplugin.go +++ b/internal/docplugin/goplugin/goplugin.go @@ -21,7 +21,8 @@ const ( dataFieldGoName = "GoName" dataFieldGoComment = "GoComment" dataFieldGoBackground = "GoHasBackground" - dataFieldGoparallel = "GoParallel" + dataFieldGoParallel = "GoParallel" + dataFieldGo22Scope = "Go22Scope" ) // GoPlugin injects golang specific information: go types, aliases. @@ -35,7 +36,8 @@ type GoPlugin struct { // Args contains optional arguments for GoPlugin. type Args struct { - Parallel bool + Parallel bool + Go22Scope bool } // New initializes a new go plugin. @@ -160,19 +162,22 @@ 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 + val.PluginData[dataFieldGoParallel] = p.args.Parallel + val.PluginData[dataFieldGo22Scope] = p.args.Go22Scope 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 + val.PluginData[dataFieldGoParallel] = p.args.Parallel + val.PluginData[dataFieldGo22Scope] = p.args.Go22Scope p.processRuleBackground(val) case model.Scenario: val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(formatScenarioKeyword(val.Keyword)) val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name) val.PluginData[dataFieldGoType] = string(goTypeString) - val.PluginData[dataFieldGoparallel] = p.args.Parallel + val.PluginData[dataFieldGoParallel] = p.args.Parallel + val.PluginData[dataFieldGo22Scope] = p.args.Go22Scope case model.Step: val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(val.Keyword) val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Text) diff --git a/internal/docplugin/goplugin/goplugin_test.go b/internal/docplugin/goplugin/goplugin_test.go index 1623cca..d136bc8 100644 --- a/internal/docplugin/goplugin/goplugin_test.go +++ b/internal/docplugin/goplugin/goplugin_test.go @@ -467,6 +467,29 @@ func TestParallel(t *testing.T) { } } +func TestGo22Scope(t *testing.T) { + t.Parallel() + + ctx := context.Background() + + for _, testCase := range [2]bool{true, false} { + t.Run(strconv.FormatBool(testCase), func(t *testing.T) { + t.Parallel() + + p := goplugin.New(goplugin.Args{ + Go22Scope: testCase, + }) + doc := getExampleDocument() + + if assert.NoError(t, p.Process(ctx, doc)) { + assert.Equal(t, testCase, doc.Feature.PluginData["Go22Scope"]) + assert.Equal(t, testCase, doc.Feature.Children[0].Scenario.PluginData["Go22Scope"]) + assert.Equal(t, testCase, doc.Feature.Children[0].Rule.PluginData["Go22Scope"]) + } + }) + } +} + func getExampleDocument() *model.GherkinDocument { return &model.GherkinDocument{ Feature: &model.Feature{ diff --git a/internal/generator/examples/simple/background.feature_test.go b/internal/generator/examples/simple/background.feature_test.go index 7f9346d..b6e9877 100644 --- a/internal/generator/examples/simple/background.feature_test.go +++ b/internal/generator/examples/simple/background.feature_test.go @@ -5,8 +5,6 @@ import ( ) func TestMultipleSiteSupport(t *testing.T) { - t.Parallel() - /* Only blog owners can post to a blog, except administrators, who can post to all blogs. @@ -29,8 +27,6 @@ func TestMultipleSiteSupport(t *testing.T) { } t.Run("Dr. Bill posts to his own blog", func(t *testing.T) { - t.Parallel() - _ = background(t) // Given I am logged in as Dr. Bill. @@ -42,8 +38,6 @@ func TestMultipleSiteSupport(t *testing.T) { }) t.Run("Dr. Bill tries to post to somebody else's blog, and fails", func(t *testing.T) { - t.Parallel() - _ = background(t) // Given I am logged in as Dr. Bill. @@ -55,8 +49,6 @@ func TestMultipleSiteSupport(t *testing.T) { }) t.Run("Greg posts to a client's blog", func(t *testing.T) { - t.Parallel() - _ = background(t) // Given I am logged in as Greg. diff --git a/internal/generator/examples/simple/bool.feature_test.go b/internal/generator/examples/simple/bool.feature_test.go index 70871ee..dd80a15 100644 --- a/internal/generator/examples/simple/bool.feature_test.go +++ b/internal/generator/examples/simple/bool.feature_test.go @@ -5,11 +5,8 @@ import ( ) func TestTypeDeterminatiopn(t *testing.T) { - t.Parallel() - - t.Run("All type are determinated", func(t *testing.T) { - t.Parallel() + t.Run("All type are determinated", func(_ *testing.T) { type testCase struct { Bool bool `field:""` Int int `field:""` @@ -24,11 +21,7 @@ func TestTypeDeterminatiopn(t *testing.T) { } for name, testCase := range testCases { - testCase := testCase - t.Run(name, func(t *testing.T) { - t.Parallel() - _ = testCase // TODO: Use and remove. // When generator completed. diff --git a/internal/generator/examples/simple/complex.feature_test.go b/internal/generator/examples/simple/complex.feature_test.go index 59fbc15..dbe013a 100644 --- a/internal/generator/examples/simple/complex.feature_test.go +++ b/internal/generator/examples/simple/complex.feature_test.go @@ -5,7 +5,6 @@ import ( ) func TestNestedBackground(t *testing.T) { - t.Parallel() type backgroundData struct{} @@ -24,8 +23,6 @@ func TestNestedBackground(t *testing.T) { } t.Run("Dr. Bill posts to his own blog", func(t *testing.T) { - t.Parallel() - _ = background(t) // Given I am logged in as Dr. Bill. @@ -36,9 +33,7 @@ func TestNestedBackground(t *testing.T) { }) - t.Run("There can be only One", func(t *testing.T) { - t.Parallel() - + t.Run("There can be only One", func(_ *testing.T) { type backgroundData struct{} background := func(t *testing.T) backgroundData { @@ -50,8 +45,6 @@ func TestNestedBackground(t *testing.T) { } t.Run("Only One -- One alive", func(t *testing.T) { - t.Parallel() - _ = background(t) // Given there is only 1 ninja alive. diff --git a/internal/generator/examples/simple/issue_26.feature_test.go b/internal/generator/examples/simple/issue_26.feature_test.go index 2411caf..53bb416 100644 --- a/internal/generator/examples/simple/issue_26.feature_test.go +++ b/internal/generator/examples/simple/issue_26.feature_test.go @@ -5,11 +5,8 @@ import ( ) func TestIssueExample(t *testing.T) { - t.Parallel() - - t.Run("Just a hello world", func(t *testing.T) { - t.Parallel() + t.Run("Just a hello world", func(_ *testing.T) { type testCase struct { Name string `field:""` } @@ -19,11 +16,7 @@ func TestIssueExample(t *testing.T) { } for name, testCase := range testCases { - testCase := testCase - t.Run(name, func(t *testing.T) { - t.Parallel() - _ = testCase // TODO: Use and remove. }) } diff --git a/internal/generator/examples/simple/issue_27_multi.feature_test.go b/internal/generator/examples/simple/issue_27_multi.feature_test.go index 19d59b2..3f34134 100644 --- a/internal/generator/examples/simple/issue_27_multi.feature_test.go +++ b/internal/generator/examples/simple/issue_27_multi.feature_test.go @@ -5,8 +5,6 @@ import ( ) func TestExampleIssue27Multi(t *testing.T) { - t.Parallel() - /* Details: - example 1 @@ -18,7 +16,5 @@ func TestExampleIssue27Multi(t *testing.T) { */ t.Run("Multi-line comment with indents", func(t *testing.T) { - t.Parallel() - }) } diff --git a/internal/generator/examples/simple/issue_27_single.feature_test.go b/internal/generator/examples/simple/issue_27_single.feature_test.go index f284e99..b21b559 100644 --- a/internal/generator/examples/simple/issue_27_single.feature_test.go +++ b/internal/generator/examples/simple/issue_27_single.feature_test.go @@ -5,12 +5,8 @@ import ( ) func TestExampleIssue27Single(t *testing.T) { - t.Parallel() - /* Hello world. */ t.Run("Single comment", func(t *testing.T) { - t.Parallel() - }) } diff --git a/internal/generator/examples/simple/readme.feature_test.go b/internal/generator/examples/simple/readme.feature_test.go index 8ba06b5..36a4960 100644 --- a/internal/generator/examples/simple/readme.feature_test.go +++ b/internal/generator/examples/simple/readme.feature_test.go @@ -5,11 +5,8 @@ import ( ) func TestApplicationCommandLineTool(t *testing.T) { - t.Parallel() - - t.Run("User wants to see usage information", func(t *testing.T) { - t.Parallel() + t.Run("User wants to see usage information", func(_ *testing.T) { type testCase struct { Flag string `field:""` ExitStatus int `field:""` @@ -23,11 +20,7 @@ func TestApplicationCommandLineTool(t *testing.T) { } for name, testCase := range testCases { - testCase := testCase - t.Run(name, func(t *testing.T) { - t.Parallel() - _ = testCase // TODO: Use and remove. // When the application is started with . diff --git a/internal/generator/examples/simple/rules.feature_test.go b/internal/generator/examples/simple/rules.feature_test.go index 4b61e1e..3b4a5fb 100644 --- a/internal/generator/examples/simple/rules.feature_test.go +++ b/internal/generator/examples/simple/rules.feature_test.go @@ -5,11 +5,8 @@ import ( ) func TestHighlander(t *testing.T) { - t.Parallel() - - t.Run("There can be only One", func(t *testing.T) { - t.Parallel() + t.Run("There can be only One", func(_ *testing.T) { type backgroundData struct{} background := func(t *testing.T) backgroundData { @@ -21,8 +18,6 @@ func TestHighlander(t *testing.T) { } t.Run("Only One -- More than one alive", func(t *testing.T) { - t.Parallel() - _ = background(t) // Given there are 3 ninjas. @@ -37,8 +32,6 @@ func TestHighlander(t *testing.T) { }) t.Run("Only One -- One alive", func(t *testing.T) { - t.Parallel() - _ = background(t) // Given there is only 1 ninja alive. @@ -48,12 +41,8 @@ func TestHighlander(t *testing.T) { }) }) - t.Run("There can be Two (in some cases)", func(t *testing.T) { - t.Parallel() - + t.Run("There can be Two (in some cases)", func(_ *testing.T) { t.Run("Two -- Dead and Reborn as Phoenix", func(t *testing.T) { - t.Parallel() - }) }) } diff --git a/internal/generator/examples/simple/scenario.feature_test.go b/internal/generator/examples/simple/scenario.feature_test.go index fada2a9..ed9dd52 100644 --- a/internal/generator/examples/simple/scenario.feature_test.go +++ b/internal/generator/examples/simple/scenario.feature_test.go @@ -5,8 +5,6 @@ import ( ) func TestSomeTerseYetDescriptiveTextOfWhatIsDesired(t *testing.T) { - t.Parallel() - /* In order to realize a named business value As an explicit system actor @@ -14,8 +12,6 @@ func TestSomeTerseYetDescriptiveTextOfWhatIsDesired(t *testing.T) { */ t.Run("Some determinable business situation", func(t *testing.T) { - t.Parallel() - // Given some precondition. // And some other precondition. diff --git a/internal/generator/examples/simple/simple.feature_test.go b/internal/generator/examples/simple/simple.feature_test.go index a56c573..ef5b031 100644 --- a/internal/generator/examples/simple/simple.feature_test.go +++ b/internal/generator/examples/simple/simple.feature_test.go @@ -5,8 +5,6 @@ import ( ) func TestGuessTheWord(t *testing.T) { - t.Parallel() - /* The word guess game is a turn-based game for two players. The Maker makes a word for the Breaker to guess. The game @@ -14,7 +12,5 @@ func TestGuessTheWord(t *testing.T) { */ t.Run("Maker starts a game", func(t *testing.T) { - t.Parallel() - }) } diff --git a/scripts/examples.sh b/scripts/examples.sh index cf4b47e..d5eb1f3 100755 --- a/scripts/examples.sh +++ b/scripts/examples.sh @@ -7,7 +7,6 @@ for f in internal/generator/examples/*.feature; do -package examples_test \ -permanent-ids \ -format go \ - -go-parallel \ -template "@/std.simple.v1.go.tmpl" \ $f \ > ${dir}/simpleparallel/${base}_test.go @@ -15,6 +14,7 @@ for f in internal/generator/examples/*.feature; do -package examples_test \ -permanent-ids \ -format go \ + -disable-go-parallel \ -template "@/std.simple.v1.go.tmpl" \ $f \ > ${dir}/simple/${base}_test.go