Skip to content

Commit

Permalink
Merge pull request #68 from antham/fix-indentation-feature-description
Browse files Browse the repository at this point in the history
Fix indentation feature description
  • Loading branch information
antham authored Dec 23, 2021
2 parents c6490b7 + d36d72c commit ba5d6ac
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 32 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
Expand All @@ -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 ."}'
```

Expand Down
5 changes: 3 additions & 2 deletions cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion cmd/fixtures/feature.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: Test
This is a description
This is a description

Background:
Given something
Expand Down
4 changes: 2 additions & 2 deletions cmd/fmt_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestFormatAndReplace(t *testing.T) {
assert.NoError(t, err)

b1Expected := `Feature: Test
Test
Test
Scenario: Scenario1
Given a test
`
Expand All @@ -74,7 +74,7 @@ func TestFormatAndReplace(t *testing.T) {
assert.NoError(t, err)

b2Expected := `Feature: Test
Test
Test
Scenario: Scenario2
Given a test
`
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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")
Expand All @@ -78,6 +82,7 @@ func TestInitConfig(t *testing.T) {
{
func() {
data := `indent:
featureDescription: 6
backgroundAndScenario: 8
step: 10
tableAndDocString: 12
Expand All @@ -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"))
Expand All @@ -99,6 +105,7 @@ aliases:
{
func() {
data := `indent:
featureDescription: 8
backgroundAndScenario: 10
step: 12
tableAndDocString: 14
Expand All @@ -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"))
Expand Down
4 changes: 3 additions & 1 deletion ghokin/file_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions ghokin/file_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down Expand Up @@ -88,7 +88,7 @@ hello world
assert.Len(t, errs, 0)

content := `Feature: test
test
test
Scenario: scenario1
Given whatever
Expand All @@ -110,7 +110,7 @@ hello world
[]string{"feature"},
func() {
content := []byte(`Feature: test
test
test
Scenario: scenario%d
Given whatever
Expand Down Expand Up @@ -140,7 +140,7 @@ hello world
assert.Len(t, errs, 0)

content := `Feature: test
test
test
Scenario: scenario%d
Given whatever
Expand Down Expand Up @@ -171,7 +171,7 @@ hello world
[]string{"feature"},
func() {
content := []byte(`Feature: test
test
test
Scenario: scenario
Given whatever
Expand Down Expand Up @@ -242,7 +242,7 @@ hello world
assert.Len(t, errs, 0)

contentFormatted := `Feature: test
test
test
Scenario: scenario
Given whatever
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down
2 changes: 1 addition & 1 deletion ghokin/fixtures/cmd.expected.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: A Feature
Description
Description

Scenario: A scenario to test
Given a thing
Expand Down
2 changes: 1 addition & 1 deletion ghokin/fixtures/comment-after-background.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: A Feature
Description
Description

####
Background:
Expand Down
2 changes: 1 addition & 1 deletion ghokin/fixtures/comment-after-scenario.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: A Feature
Description
Description

####
Background:
Expand Down
2 changes: 1 addition & 1 deletion ghokin/fixtures/docstring-empty.expected.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: A Feature
Description
Description

Scenario: A scenario to test
Given a thing
Expand Down
2 changes: 1 addition & 1 deletion ghokin/fixtures/docstring-empty.input.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: A Feature
Description
Description

Scenario: A scenario to test
Given a thing
Expand Down
4 changes: 2 additions & 2 deletions ghokin/fixtures/file1.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@tag1 @tag2
# language: en
Feature: A Feature
Description
Description
Description
Description

# A comment
# A second comment
Expand Down
2 changes: 1 addition & 1 deletion ghokin/fixtures/multisize-table.expected.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: A Feature
Description
Description

Scenario: A scenario to test
Given a thing
Expand Down
2 changes: 1 addition & 1 deletion ghokin/fixtures/multisize-table.input.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: A Feature
Description
Description

Scenario: A scenario to test
Given a thing
Expand Down
2 changes: 1 addition & 1 deletion ghokin/fixtures/utf8-with-bom.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: Test
This is a description
This is a description

Background:
Given something
Expand Down
1 change: 1 addition & 0 deletions ghokin/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions ghokin/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
},
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ba5d6ac

Please sign in to comment.