From 5bdad8ae8eeba0fb1821c500e6344def6f6a04d8 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sat, 18 Nov 2023 17:17:59 +0100 Subject: [PATCH 1/2] ghokin: fix missing checks --- ghokin/token_generator.go | 7 ++++++- ghokin/transformer.go | 14 ++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ghokin/token_generator.go b/ghokin/token_generator.go index 7906018..f6c9265 100644 --- a/ghokin/token_generator.go +++ b/ghokin/token_generator.go @@ -1,7 +1,9 @@ package ghokin import ( - "github.com/cucumber/gherkin/go/v26" + "errors" + + gherkin "github.com/cucumber/gherkin/go/v26" ) type tokenGenerator struct { @@ -9,6 +11,9 @@ type tokenGenerator struct { } func (t *tokenGenerator) Build(tok *gherkin.Token) (bool, error) { + if tok == nil { + return false, errors.New("token is not defined") + } if tok.IsEOF() { return true, nil } diff --git a/ghokin/transformer.go b/ghokin/transformer.go index e9cf23f..732398c 100644 --- a/ghokin/transformer.go +++ b/ghokin/transformer.go @@ -107,10 +107,14 @@ func getTagOrCommentPadding(paddings map[gherkin.TokenType]int, indent int, sec var kind gherkin.TokenType excluded := []gherkin.TokenType{gherkin.TokenTypeTagLine, gherkin.TokenTypeComment} if sec.next(excluded) != nil { - kind = sec.next(excluded).kind + if s := sec.next(excluded); s != nil { + kind = s.kind + } } if kind == 0 && sec.previous(excluded) != nil { - kind = sec.previous(excluded).kind + if s := sec.previous(excluded); s != nil { + kind = s.kind + } } // indent the last comment line at the same level than scenario and background if sec.next([]gherkin.TokenType{gherkin.TokenTypeEmpty}) == nil { @@ -132,8 +136,10 @@ func computeCommand(cmd *exec.Cmd, lines []string, sec *section) (bool, []string func isDescriptionFeature(sec *section) bool { excluded := []gherkin.TokenType{gherkin.TokenTypeEmpty} - if sec.previous(excluded) != nil && sec.previous(excluded).kind == gherkin.TokenTypeFeatureLine { - return true + if sec.previous(excluded) != nil { + if s := sec.previous(excluded); s != nil && s.kind == gherkin.TokenTypeFeatureLine { + return true + } } return false } From d5bee678df9280fd0e8b1d3021cd14ba955532f6 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sat, 18 Nov 2023 17:39:36 +0100 Subject: [PATCH 2/2] ghokin: remove io/ioutil package --- cmd/check_test.go | 5 ++- cmd/fmt_replace_test.go | 9 +++-- cmd/fmt_stdout_test.go | 5 ++- cmd/root_test.go | 7 ++-- ghokin/file_manager.go | 3 +- ghokin/file_manager_test.go | 67 ++++++++++++++++++------------------ ghokin/stdin_manager_test.go | 3 +- ghokin/transformer_test.go | 5 ++- 8 files changed, 48 insertions(+), 56 deletions(-) diff --git a/cmd/check_test.go b/cmd/check_test.go index 83c48f7..b95c998 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -2,7 +2,6 @@ package cmd import ( "bytes" - "io/ioutil" "os" "sync" "testing" @@ -31,8 +30,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, os.WriteFile("/tmp/ghokin/file1.feature", []byte("Feature: Test\n Test\n Scenario: Scenario1\n Given a test\n"), 0755)) + assert.NoError(t, os.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/fmt_replace_test.go b/cmd/fmt_replace_test.go index d3b01ef..d56efaf 100644 --- a/cmd/fmt_replace_test.go +++ b/cmd/fmt_replace_test.go @@ -2,7 +2,6 @@ package cmd import ( "bytes" - "io/ioutil" "os" "sync" "testing" @@ -31,8 +30,8 @@ func TestFormatAndReplace(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\nTest\nScenario: Scenario1\nGiven a test\n"), 0755)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.feature", []byte("Feature: Test\nTest\nScenario: Scenario2\nGiven a test\n"), 0755)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.feature", []byte("Feature: Test\nTest\nScenario: Scenario1\nGiven a test\n"), 0755)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file2.feature", []byte("Feature: Test\nTest\nScenario: Scenario2\nGiven a test\n"), 0755)) w.Add(1) @@ -55,7 +54,7 @@ func TestFormatAndReplace(t *testing.T) { assert.EqualValues(t, 0, code, "Must exit with errors (exit 0)") assert.EqualValues(t, `"/tmp/ghokin" formatted`+"\n", stdout.String()) - b1, err := ioutil.ReadFile("/tmp/ghokin/file1.feature") + b1, err := os.ReadFile("/tmp/ghokin/file1.feature") assert.NoError(t, err) @@ -67,7 +66,7 @@ func TestFormatAndReplace(t *testing.T) { assert.EqualValues(t, b1Expected, string(b1)) - b2, err := ioutil.ReadFile("/tmp/ghokin/file2.feature") + b2, err := os.ReadFile("/tmp/ghokin/file2.feature") assert.NoError(t, err) diff --git a/cmd/fmt_stdout_test.go b/cmd/fmt_stdout_test.go index 71775a9..b73f3f4 100644 --- a/cmd/fmt_stdout_test.go +++ b/cmd/fmt_stdout_test.go @@ -2,7 +2,6 @@ package cmd import ( "bytes" - "io/ioutil" "os" "sync" "testing" @@ -48,7 +47,7 @@ func TestFormatOnStdoutFromFile(t *testing.T) { w.Wait() - b, err := ioutil.ReadFile("fixtures/feature.feature") + b, err := os.ReadFile("fixtures/feature.feature") assert.NoError(t, err) @@ -93,7 +92,7 @@ func TestFormatOnStdoutFromStdin(t *testing.T) { w.Wait() - b, err := ioutil.ReadFile("fixtures/feature.feature") + b, err := os.ReadFile("fixtures/feature.feature") assert.NoError(t, err) diff --git a/cmd/root_test.go b/cmd/root_test.go index b3b1097..ddc1508 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -2,7 +2,6 @@ package cmd import ( "bytes" - "io/ioutil" "os" "sync" "testing" @@ -73,7 +72,7 @@ func TestInitConfig(t *testing.T) { aliases: cat: cat ` - assert.NoError(t, ioutil.WriteFile(".ghokin.yml", []byte(data), 0777)) + assert.NoError(t, os.WriteFile(".ghokin.yml", []byte(data), 0777)) }, func(exitCode int, stdin string, stderr string) { assert.EqualValues(t, 12, viper.GetInt("indent")) @@ -90,7 +89,7 @@ aliases: seq: seq ` cfgFile = ".test.yml" - assert.NoError(t, ioutil.WriteFile(".test.yml", []byte(data), 0777)) + assert.NoError(t, os.WriteFile(".test.yml", []byte(data), 0777)) }, func(exitCode int, stdin string, stderr string) { assert.EqualValues(t, 14, viper.GetInt("indent")) @@ -104,7 +103,7 @@ aliases: { func() { data := `indent` - assert.NoError(t, ioutil.WriteFile(".ghokin.yml", []byte(data), 0777)) + assert.NoError(t, os.WriteFile(".ghokin.yml", []byte(data), 0777)) }, func(exitCode int, stdin string, stderr string) { assert.EqualValues(t, 1, exitCode) diff --git a/ghokin/file_manager.go b/ghokin/file_manager.go index 895daf9..af30f8e 100644 --- a/ghokin/file_manager.go +++ b/ghokin/file_manager.go @@ -3,7 +3,6 @@ package ghokin import ( "bytes" "fmt" - "io/ioutil" "os" mpath "path" "path/filepath" @@ -152,7 +151,7 @@ func replaceFileWithContent(file string, content []byte) error { } func check(file string, content []byte) error { - currentContent, err := ioutil.ReadFile(file) // #nosec + currentContent, err := os.ReadFile(file) // #nosec if err != nil { return ProcessFileError{Message: err.Error(), File: file} diff --git a/ghokin/file_manager_test.go b/ghokin/file_manager_test.go index a8b658b..038011a 100644 --- a/ghokin/file_manager_test.go +++ b/ghokin/file_manager_test.go @@ -2,7 +2,6 @@ package ghokin import ( "fmt" - "io/ioutil" "os" "testing" @@ -19,7 +18,7 @@ func TestFileManagerTransform(t *testing.T) { { "fixtures/file1.feature", func(buf []byte, err error) { - b, e := ioutil.ReadFile("fixtures/file1.feature") + b, e := os.ReadFile("fixtures/file1.feature") assert.NoError(t, e) assert.EqualValues(t, string(b), string(buf)) }, @@ -27,7 +26,7 @@ func TestFileManagerTransform(t *testing.T) { { "fixtures/utf8-with-bom.feature", func(buf []byte, err error) { - b, e := ioutil.ReadFile("fixtures/utf8-with-bom.feature") + b, e := os.ReadFile("fixtures/utf8-with-bom.feature") assert.NoError(t, e) assert.EqualValues(t, string(b), string(buf)) }, @@ -35,7 +34,7 @@ func TestFileManagerTransform(t *testing.T) { { "fixtures/file1-with-cr.feature", func(buf []byte, err error) { - b, e := ioutil.ReadFile("fixtures/file1-with-cr.feature") + b, e := os.ReadFile("fixtures/file1-with-cr.feature") assert.NoError(t, e) assert.EqualValues(t, string(b), string(buf)) }, @@ -43,7 +42,7 @@ func TestFileManagerTransform(t *testing.T) { { "fixtures/file1-with-crlf.feature", func(buf []byte, err error) { - b, e := ioutil.ReadFile("fixtures/file1-with-crlf.feature") + b, e := os.ReadFile("fixtures/file1-with-crlf.feature") assert.NoError(t, e) assert.EqualValues(t, string(b), string(buf)) }, @@ -104,7 +103,7 @@ hello world assert.NoError(t, os.RemoveAll("/tmp/ghokin")) assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) }, func(errs []error) { assert.Len(t, errs, 0) @@ -120,7 +119,7 @@ hello world """ ` - b, e := ioutil.ReadFile("/tmp/ghokin/file1.feature") + b, e := os.ReadFile("/tmp/ghokin/file1.feature") assert.NoError(t, e) assert.EqualValues(t, content, string(b)) }, @@ -154,7 +153,7 @@ hello world "/tmp/ghokin/test2/test3/file5.feature", "/tmp/ghokin/test2/test3/file6.feature", } { - assert.NoError(t, ioutil.WriteFile(f, []byte(fmt.Sprintf(string(content), i)), 0777)) + assert.NoError(t, os.WriteFile(f, []byte(fmt.Sprintf(string(content), i)), 0777)) } }, func(errs []error) { @@ -179,7 +178,7 @@ hello world "/tmp/ghokin/test2/test3/file5.feature", "/tmp/ghokin/test2/test3/file6.feature", } { - b, e := ioutil.ReadFile(f) + b, e := os.ReadFile(f) assert.NoError(t, e) assert.EqualValues(t, fmt.Sprintf(content, i), string(b)) } @@ -205,11 +204,11 @@ hello world assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) assert.NoError(t, os.MkdirAll("/tmp/ghokin/test1", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.feature", append([]byte("whatever"), content...), 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/test1/file3.feature", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/test1/file4.feature", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/test1/file5.feature", append([]byte("whatever"), content...), 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file2.feature", append([]byte("whatever"), content...), 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/test1/file3.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/test1/file4.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/test1/file5.feature", append([]byte("whatever"), content...), 0777)) }, func(errs []error) { assert.Len(t, errs, 2) @@ -254,9 +253,9 @@ hello world assert.NoError(t, os.RemoveAll("/tmp/ghokin")) assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.txt", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file3.feat", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file2.txt", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file3.feat", content, 0777)) }, func(errs []error) { assert.Len(t, errs, 0) @@ -300,7 +299,7 @@ hello world contentFormatted, }, } { - b, e := ioutil.ReadFile(s.filename) + b, e := os.ReadFile(s.filename) assert.NoError(t, e) assert.EqualValues(t, s.expected, string(b)) } @@ -313,8 +312,8 @@ hello world func() { assert.NoError(t, os.RemoveAll("/tmp/ghokin")) assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.txt", []byte("file1"), 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.txt", []byte("file2"), 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.txt", []byte("file1"), 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file2.txt", []byte("file2"), 0777)) }, func(errs []error) { assert.Len(t, errs, 0) @@ -392,7 +391,7 @@ hello world assert.NoError(t, os.RemoveAll("/tmp/ghokin")) assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) }, func(errs []error) { assert.Len(t, errs, 1) @@ -416,7 +415,7 @@ hello world assert.NoError(t, os.RemoveAll("/tmp/ghokin")) assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) }, func(errs []error) { assert.Len(t, errs, 0) @@ -451,7 +450,7 @@ hello world "/tmp/ghokin/test2/test3/file5.feature", "/tmp/ghokin/test2/test3/file6.feature", } { - assert.NoError(t, ioutil.WriteFile(f, []byte(fmt.Sprintf(string(content), i)), 0777)) + assert.NoError(t, os.WriteFile(f, []byte(fmt.Sprintf(string(content), i)), 0777)) } }, func(errs []error) { @@ -501,7 +500,7 @@ hello world "/tmp/ghokin/test2/test3/file5.feature", "/tmp/ghokin/test2/test3/file6.feature", } { - assert.NoError(t, ioutil.WriteFile(f, []byte(fmt.Sprintf(string(content), i)), 0777)) + assert.NoError(t, os.WriteFile(f, []byte(fmt.Sprintf(string(content), i)), 0777)) } }, func(errs []error) { @@ -527,11 +526,11 @@ hello world assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) assert.NoError(t, os.MkdirAll("/tmp/ghokin/test1", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.feature", append([]byte("whatever"), content...), 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/test1/file3.feature", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/test1/file4.feature", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/test1/file5.feature", append([]byte("whatever"), content...), 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file2.feature", append([]byte("whatever"), content...), 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/test1/file3.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/test1/file4.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/test1/file5.feature", append([]byte("whatever"), content...), 0777)) }, func(errs []error) { assert.Len(t, errs, 2) @@ -576,9 +575,9 @@ hello world assert.NoError(t, os.RemoveAll("/tmp/ghokin")) assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.txt", content, 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file3.feat", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.feature", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file2.txt", content, 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file3.feat", content, 0777)) }, func(errs []error) { assert.Len(t, errs, 2) @@ -602,8 +601,8 @@ hello world func() { assert.NoError(t, os.RemoveAll("/tmp/ghokin")) assert.NoError(t, os.MkdirAll("/tmp/ghokin", 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file1.txt", []byte("file1"), 0777)) - assert.NoError(t, ioutil.WriteFile("/tmp/ghokin/file2.txt", []byte("file2"), 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file1.txt", []byte("file1"), 0777)) + assert.NoError(t, os.WriteFile("/tmp/ghokin/file2.txt", []byte("file2"), 0777)) }, func(errs []error) { assert.Len(t, errs, 0) diff --git a/ghokin/stdin_manager_test.go b/ghokin/stdin_manager_test.go index 481a277..15baa02 100644 --- a/ghokin/stdin_manager_test.go +++ b/ghokin/stdin_manager_test.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "io" - "io/ioutil" "os" "testing" @@ -38,7 +37,7 @@ func TestStdinManagerTransform(t *testing.T) { return stdinManager, bytes.NewBuffer(content) }, func(buf []byte, err error) { - b, e := ioutil.ReadFile("fixtures/file1.feature") + b, e := os.ReadFile("fixtures/file1.feature") assert.NoError(t, e) assert.EqualValues(t, string(b), string(buf)) }, diff --git a/ghokin/transformer_test.go b/ghokin/transformer_test.go index 45e4677..d58aef5 100644 --- a/ghokin/transformer_test.go +++ b/ghokin/transformer_test.go @@ -1,12 +1,11 @@ package ghokin import ( - "io/ioutil" "os" "os/exec" "testing" - "github.com/cucumber/gherkin/go/v26" + gherkin "github.com/cucumber/gherkin/go/v26" "github.com/stretchr/testify/assert" ) @@ -470,7 +469,7 @@ func TestTransform(t *testing.T) { buf, err := transform(s, 2, aliases) assert.NoError(t, err) - b, e := ioutil.ReadFile(scenario.expected) + b, e := os.ReadFile(scenario.expected) assert.NoError(t, e) assert.EqualValues(t, string(b), string(buf)) })