Skip to content

Commit

Permalink
Merge pull request #115 from antham/fix-nil-missing-check
Browse files Browse the repository at this point in the history
Fix missing nil checks
  • Loading branch information
antham committed Nov 18, 2023
2 parents f8cf2a4 + d5bee67 commit c3e3046
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 61 deletions.
5 changes: 2 additions & 3 deletions cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bytes"
"io/ioutil"
"os"
"sync"
"testing"
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 4 additions & 5 deletions cmd/fmt_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bytes"
"io/ioutil"
"os"
"sync"
"testing"
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions cmd/fmt_stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bytes"
"io/ioutil"
"os"
"sync"
"testing"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
7 changes: 3 additions & 4 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bytes"
"io/ioutil"
"os"
"sync"
"testing"
Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions ghokin/file_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ghokin
import (
"bytes"
"fmt"
"io/ioutil"
"os"
mpath "path"
"path/filepath"
Expand Down Expand Up @@ -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}
Expand Down
67 changes: 33 additions & 34 deletions ghokin/file_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ghokin

import (
"fmt"
"io/ioutil"
"os"
"testing"

Expand All @@ -19,31 +18,31 @@ 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))
},
},
{
"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))
},
},
{
"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))
},
},
{
"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))
},
Expand Down Expand Up @@ -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)
Expand All @@ -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))
},
Expand Down Expand Up @@ -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) {
Expand All @@ -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))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions ghokin/stdin_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -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))
},
Expand Down
7 changes: 6 additions & 1 deletion ghokin/token_generator.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package ghokin

import (
"github.com/cucumber/gherkin/go/v26"
"errors"

gherkin "github.com/cucumber/gherkin/go/v26"
)

type tokenGenerator struct {
section *section
}

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
}
Expand Down
Loading

0 comments on commit c3e3046

Please sign in to comment.