From c2e445d24c49ee248c9d14922f45e54598188f6b Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Thu, 23 Dec 2021 23:48:00 +0100 Subject: [PATCH 1/2] ghokin : add description feature ident * add the ability to update the indent description feature out of any other indentation * set the default value to the same than background and scenario --- README.md | 14 ++++++++------ cmd/check_test.go | 5 +++-- cmd/cmd.go | 1 + cmd/fixtures/feature.feature | 2 +- cmd/fmt_replace_test.go | 4 ++-- cmd/root.go | 2 ++ cmd/root_test.go | 8 ++++++++ ghokin/file_manager.go | 4 +++- ghokin/file_manager_test.go | 16 ++++++++-------- ghokin/fixtures/cmd.expected.feature | 2 +- ghokin/fixtures/comment-after-background.feature | 2 +- ghokin/fixtures/comment-after-scenario.feature | 2 +- ghokin/fixtures/docstring-empty.expected.feature | 2 +- ghokin/fixtures/docstring-empty.input.feature | 2 +- ghokin/fixtures/file1.feature | 4 ++-- ghokin/fixtures/multisize-table.expected.feature | 2 +- ghokin/fixtures/multisize-table.input.feature | 2 +- ghokin/fixtures/utf8-with-bom.feature | 2 +- ghokin/transformer.go | 1 + ghokin/transformer_test.go | 2 +- 20 files changed, 48 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index e19ac42..424394f 100644 --- a/README.md +++ b/README.md @@ -104,9 +104,10 @@ It's possible to override configuration by defining a ```.ghokin.yml``` file in ``` indent: - backgroundAndScenario: 4 - step: 6 - tableAndDocString: 8 + featureDescription : 2 + backgroundAndScenario: 2 + step: 4 + tableAndDocString: 6 aliases: json: "jq ." @@ -117,9 +118,10 @@ Aliases key defined [shell commands](#shell-commands) callable in comments as we It's possible to use environments variables instead of a static config file : ``` -export GHOKIN_INDENT_BACKGROUNDANDSCENARIO=4 -export GHOKIN_INDENT_STEP=6 -export GHOKIN_INDENT_TABLEANDDOCSTRING=8 +export GHOKIN_INDENT_FEATUREDESCRIPTION=2 +export GHOKIN_INDENT_BACKGROUNDANDSCENARIO=2 +export GHOKIN_INDENT_STEP=4 +export GHOKIN_INDENT_TABLEANDDOCSTRING=6 export GHOKIN_ALIASES='{"json":"jq ."}' ``` diff --git a/cmd/check_test.go b/cmd/check_test.go index 00f24c0..904a551 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -19,6 +19,7 @@ func TestCheck(t *testing.T) { var stdout bytes.Buffer var stderr bytes.Buffer + viper.Set("indent.featureDescription", 2) viper.Set("indent.backgroundAndScenario", 2) viper.Set("indent.step", 4) viper.Set("indent.tableAndDocString", 6) @@ -33,8 +34,8 @@ func TestCheck(t *testing.T) { assert.NoError(t, os.RemoveAll("/tmp/ghokin")) assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", []byte("Feature: Test\n Test\n Scenario: Scenario1\n Given a test\n"), 0755)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.feature", []byte("Feature: Test\n Test\n Scenario: Scenario2\n Given a test\n"), 0755)) + assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", []byte("Feature: Test\n Test\n Scenario: Scenario1\n Given a test\n"), 0755)) + assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.feature", []byte("Feature: Test\n Test\n Scenario: Scenario2\n Given a test\n"), 0755)) w.Add(1) diff --git a/cmd/cmd.go b/cmd/cmd.go index 6f7ac8b..b4b6f7b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -16,6 +16,7 @@ func setupCmdFunc(f func(messageHandler, *cobra.Command, []string)) func(*cobra. func getFileManager() ghokin.FileManager { return ghokin.NewFileManager( + viper.GetInt("indent.featureDescription"), viper.GetInt("indent.backgroundAndScenario"), viper.GetInt("indent.step"), viper.GetInt("indent.tableAndDocString"), diff --git a/cmd/fixtures/feature.feature b/cmd/fixtures/feature.feature index c0ace01..07dfab3 100644 --- a/cmd/fixtures/feature.feature +++ b/cmd/fixtures/feature.feature @@ -1,5 +1,5 @@ Feature: Test - This is a description + This is a description Background: Given something diff --git a/cmd/fmt_replace_test.go b/cmd/fmt_replace_test.go index ad5ff01..2078a4e 100644 --- a/cmd/fmt_replace_test.go +++ b/cmd/fmt_replace_test.go @@ -62,7 +62,7 @@ func TestFormatAndReplace(t *testing.T) { assert.NoError(t, err) b1Expected := `Feature: Test - Test + Test Scenario: Scenario1 Given a test ` @@ -74,7 +74,7 @@ func TestFormatAndReplace(t *testing.T) { assert.NoError(t, err) b2Expected := `Feature: Test - Test + Test Scenario: Scenario2 Given a test ` diff --git a/cmd/root.go b/cmd/root.go index 9eae0e1..2295319 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -46,6 +46,7 @@ func initConfig(msgHandler messageHandler) func() { viper.SetEnvPrefix("ghokin") for _, err := range []error{ + viper.BindEnv("indent.featureDescription"), viper.BindEnv("indent.backgroundAndScenario"), viper.BindEnv("indent.step"), viper.BindEnv("indent.tableAndDocString"), @@ -58,6 +59,7 @@ func initConfig(msgHandler messageHandler) func() { viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() + viper.SetDefault("indent.featureDescription", 2) viper.SetDefault("indent.backgroundAndScenario", 2) viper.SetDefault("indent.step", 4) viper.SetDefault("indent.tableAndDocString", 6) diff --git a/cmd/root_test.go b/cmd/root_test.go index d4a9cf4..8967700 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -36,6 +36,7 @@ func TestInitConfig(t *testing.T) { { func() {}, func(exitCode int, stdin string, stderr string) { + assert.EqualValues(t, 2, viper.GetInt("indent.featureDescription")) assert.EqualValues(t, 2, viper.GetInt("indent.backgroundAndScenario")) assert.EqualValues(t, 4, viper.GetInt("indent.step")) assert.EqualValues(t, 6, viper.GetInt("indent.tableAndDocString")) @@ -45,18 +46,21 @@ func TestInitConfig(t *testing.T) { }, { func() { + os.Setenv("GHOKIN_INDENT_FEATUREDESCRIPTION", "1") os.Setenv("GHOKIN_INDENT_BACKGROUNDANDSCENARIO", "4") os.Setenv("GHOKIN_INDENT_STEP", "6") os.Setenv("GHOKIN_INDENT_TABLEANDDOCSTRING", "8") os.Setenv("GHOKIN_ALIASES", `{"json":"jq"}`) }, func(exitCode int, stdin string, stderr string) { + assert.EqualValues(t, 1, viper.GetInt("indent.featureDescription")) assert.EqualValues(t, 4, viper.GetInt("indent.backgroundAndScenario")) assert.EqualValues(t, 6, viper.GetInt("indent.step")) assert.EqualValues(t, 8, viper.GetInt("indent.tableAndDocString")) assert.EqualValues(t, map[string]string{"json": "jq"}, viper.GetStringMapString("aliases")) }, func() { + os.Unsetenv("GHOKIN_INDENT_FEATUREDESCRIPTION") os.Unsetenv("GHOKIN_INDENT_BACKGROUNDANDSCENARIO") os.Unsetenv("GHOKIN_INDENT_STEP") os.Unsetenv("GHOKIN_INDENT_TABLEANDDOCSTRING") @@ -78,6 +82,7 @@ func TestInitConfig(t *testing.T) { { func() { data := `indent: + featureDescription: 6 backgroundAndScenario: 8 step: 10 tableAndDocString: 12 @@ -87,6 +92,7 @@ aliases: assert.NoError(t, ioutil.WriteFile(".ghokin.yml", []byte(data), 0777)) }, func(exitCode int, stdin string, stderr string) { + assert.EqualValues(t, 6, viper.GetInt("indent.featureDescription")) assert.EqualValues(t, 8, viper.GetInt("indent.backgroundAndScenario")) assert.EqualValues(t, 10, viper.GetInt("indent.step")) assert.EqualValues(t, 12, viper.GetInt("indent.tableAndDocString")) @@ -99,6 +105,7 @@ aliases: { func() { data := `indent: + featureDescription: 8 backgroundAndScenario: 10 step: 12 tableAndDocString: 14 @@ -109,6 +116,7 @@ aliases: assert.NoError(t, ioutil.WriteFile(".test.yml", []byte(data), 0777)) }, func(exitCode int, stdin string, stderr string) { + assert.EqualValues(t, 8, viper.GetInt("indent.featureDescription")) assert.EqualValues(t, 10, viper.GetInt("indent.backgroundAndScenario")) assert.EqualValues(t, 12, viper.GetInt("indent.step")) assert.EqualValues(t, 14, viper.GetInt("indent.tableAndDocString")) diff --git a/ghokin/file_manager.go b/ghokin/file_manager.go index 73af1bf..1bba6dd 100644 --- a/ghokin/file_manager.go +++ b/ghokin/file_manager.go @@ -25,6 +25,7 @@ func (p ProcessFileError) Error() string { type aliases map[string]string type indent struct { + featureDescription int backgroundAndScenario int step int tableAndDocString int @@ -38,9 +39,10 @@ type FileManager struct { // NewFileManager creates a brand new FileManager, it requires indentation values and aliases defined // as a shell commands in comments -func NewFileManager(backgroundAndScenarioIndent int, stepIndent int, tableAndDocStringIndent int, aliases map[string]string) FileManager { +func NewFileManager(featureDescription int, backgroundAndScenarioIndent int, stepIndent int, tableAndDocStringIndent int, aliases map[string]string) FileManager { return FileManager{ indent{ + featureDescription, backgroundAndScenarioIndent, stepIndent, tableAndDocStringIndent, diff --git a/ghokin/file_manager_test.go b/ghokin/file_manager_test.go index e71661c..9bca723 100644 --- a/ghokin/file_manager_test.go +++ b/ghokin/file_manager_test.go @@ -44,7 +44,7 @@ func TestFileManagerTransform(t *testing.T) { } for _, scenario := range scenarios { - f := NewFileManager(2, 4, 6, + f := NewFileManager(2, 2, 4, 6, map[string]string{ "seq": "seq 1 3", }, @@ -88,7 +88,7 @@ hello world assert.Len(t, errs, 0) content := `Feature: test - test + test Scenario: scenario1 Given whatever @@ -110,7 +110,7 @@ hello world []string{"feature"}, func() { content := []byte(`Feature: test - test + test Scenario: scenario%d Given whatever @@ -140,7 +140,7 @@ hello world assert.Len(t, errs, 0) content := `Feature: test - test + test Scenario: scenario%d Given whatever @@ -171,7 +171,7 @@ hello world []string{"feature"}, func() { content := []byte(`Feature: test - test + test Scenario: scenario Given whatever @@ -242,7 +242,7 @@ hello world assert.Len(t, errs, 0) contentFormatted := `Feature: test - test + test Scenario: scenario Given whatever @@ -327,7 +327,7 @@ hello world t.Run(scenario.testName, func(t *testing.T) { scenario.setup() - f := NewFileManager(2, 4, 6, + f := NewFileManager(1, 2, 4, 6, map[string]string{ "seq": "seq 1 3", }, @@ -609,7 +609,7 @@ hello world t.Run(scenario.testName, func(t *testing.T) { scenario.setup() - f := NewFileManager(2, 4, 6, + f := NewFileManager(1, 2, 4, 6, map[string]string{ "seq": "seq 1 3", }, diff --git a/ghokin/fixtures/cmd.expected.feature b/ghokin/fixtures/cmd.expected.feature index 07da5b3..d7a49b0 100644 --- a/ghokin/fixtures/cmd.expected.feature +++ b/ghokin/fixtures/cmd.expected.feature @@ -1,5 +1,5 @@ Feature: A Feature - Description + Description Scenario: A scenario to test Given a thing diff --git a/ghokin/fixtures/comment-after-background.feature b/ghokin/fixtures/comment-after-background.feature index ddfbe68..083d03e 100644 --- a/ghokin/fixtures/comment-after-background.feature +++ b/ghokin/fixtures/comment-after-background.feature @@ -1,5 +1,5 @@ Feature: A Feature - Description + Description #### Background: diff --git a/ghokin/fixtures/comment-after-scenario.feature b/ghokin/fixtures/comment-after-scenario.feature index 9193fad..368254b 100644 --- a/ghokin/fixtures/comment-after-scenario.feature +++ b/ghokin/fixtures/comment-after-scenario.feature @@ -1,5 +1,5 @@ Feature: A Feature - Description + Description #### Background: diff --git a/ghokin/fixtures/docstring-empty.expected.feature b/ghokin/fixtures/docstring-empty.expected.feature index e830f6b..48419e8 100644 --- a/ghokin/fixtures/docstring-empty.expected.feature +++ b/ghokin/fixtures/docstring-empty.expected.feature @@ -1,5 +1,5 @@ Feature: A Feature - Description + Description Scenario: A scenario to test Given a thing diff --git a/ghokin/fixtures/docstring-empty.input.feature b/ghokin/fixtures/docstring-empty.input.feature index e830f6b..48419e8 100644 --- a/ghokin/fixtures/docstring-empty.input.feature +++ b/ghokin/fixtures/docstring-empty.input.feature @@ -1,5 +1,5 @@ Feature: A Feature - Description + Description Scenario: A scenario to test Given a thing diff --git a/ghokin/fixtures/file1.feature b/ghokin/fixtures/file1.feature index 144d39e..e362a7b 100644 --- a/ghokin/fixtures/file1.feature +++ b/ghokin/fixtures/file1.feature @@ -1,8 +1,8 @@ @tag1 @tag2 # language: en Feature: A Feature - Description - Description + Description + Description # A comment # A second comment diff --git a/ghokin/fixtures/multisize-table.expected.feature b/ghokin/fixtures/multisize-table.expected.feature index f1547e6..6f8e8f6 100644 --- a/ghokin/fixtures/multisize-table.expected.feature +++ b/ghokin/fixtures/multisize-table.expected.feature @@ -1,5 +1,5 @@ Feature: A Feature - Description + Description Scenario: A scenario to test Given a thing diff --git a/ghokin/fixtures/multisize-table.input.feature b/ghokin/fixtures/multisize-table.input.feature index f1547e6..6f8e8f6 100644 --- a/ghokin/fixtures/multisize-table.input.feature +++ b/ghokin/fixtures/multisize-table.input.feature @@ -1,5 +1,5 @@ Feature: A Feature - Description + Description Scenario: A scenario to test Given a thing diff --git a/ghokin/fixtures/utf8-with-bom.feature b/ghokin/fixtures/utf8-with-bom.feature index 41b4d6c..7a7d7b2 100644 --- a/ghokin/fixtures/utf8-with-bom.feature +++ b/ghokin/fixtures/utf8-with-bom.feature @@ -1,5 +1,5 @@ Feature: Test - This is a description + This is a description Background: Given something diff --git a/ghokin/transformer.go b/ghokin/transformer.go index 83bc4be..1641db7 100644 --- a/ghokin/transformer.go +++ b/ghokin/transformer.go @@ -88,6 +88,7 @@ func transform(section *section, indentConf indent, aliases aliases) (bytes.Buff case gherkin.TokenTypeOther: if isDescriptionFeature(sec) { lines = trimLinesSpace(lines) + padding = indentConf.featureDescription } } diff --git a/ghokin/transformer_test.go b/ghokin/transformer_test.go index 0511485..b05376c 100644 --- a/ghokin/transformer_test.go +++ b/ghokin/transformer_test.go @@ -453,7 +453,7 @@ func TestTransform(t *testing.T) { "seq": "seq 1 3", } - buf, err := transform(s, indent{2, 4, 6}, aliases) + buf, err := transform(s, indent{2, 2, 4, 6}, aliases) assert.NoError(t, err) b, e := ioutil.ReadFile(scenario.expected) From d36d72c558b6522f8739d0629da1b8a58d909092 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Thu, 23 Dec 2021 23:50:29 +0100 Subject: [PATCH 2/2] ghokin : fix test locally --- ghokin/transformer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ghokin/transformer_test.go b/ghokin/transformer_test.go index b05376c..d6a24b4 100644 --- a/ghokin/transformer_test.go +++ b/ghokin/transformer_test.go @@ -223,7 +223,7 @@ func TestRunCommand(t *testing.T) { []string{"hello world !", "hello universe !"}, func(lines []string, err error) { assert.Equal(t, []string{}, lines) - assert.Regexp(t, ".*catttttt.*not found.*", err.Error()) + assert.Regexp(t, ".*catttttt.*(not found|introuvable).*", err.Error()) }, }, }