Skip to content

Commit

Permalink
Disable autofix by default
Browse files Browse the repository at this point in the history
Now autofix is off by default. Soon a CLI parameter will be introduced
with the option to activate autofix.
  • Loading branch information
fernandrone committed May 3, 2020
1 parent 4e2b044 commit 90e37fc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Or:
linelint README.md LICENSE linter/config.go
```

In case any rule fails, it will end with an error (exit code 1). If the `autofix` option is set (on by default), it will attempt to fix any file with error. If all files are fixed, the program will terminate successfully.
In case any rule fails, it will end with an error (exit code 1). If the `autofix` option is set to `true` (it is `false` by default), it will attempt to fix any file with error. If all files are fixed, the program will terminate successfully.

## Configuration

Expand All @@ -64,7 +64,7 @@ rules:
# set to true to enable this rule
enable: true

# set to true to disable autofix (if enabled globally)
# set to true to disable autofix (if it is enabled globally)
disable-autofix: false

# will merge with global configuration
Expand Down
2 changes: 1 addition & 1 deletion linter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewConfig() Config {

func newDefaultConfig() Config {
return Config{
AutoFix: true,
AutoFix: false,
Ignore: []string{".git/"},
Rules: RulesConfig{
EndOfFile: EndOfFileConfig{
Expand Down
23 changes: 16 additions & 7 deletions linter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"gopkg.in/yaml.v2"
)

var yamlTestConfig = `
autofix: true
var yamlDefaultTestConfig = `
autofix: false
ignore:
- .git/
Expand All @@ -20,17 +20,26 @@ rules:
single-new-line: true
`

var defaultTestConf = newDefaultConfig()
var autofixTestConf = Config{
AutoFix: true,
Ignore: []string{".git/"},
Rules: RulesConfig{
EndOfFile: EndOfFileConfig{
Enable: true,
SingleNewLine: true,
},
},
}

func TestConfig(t *testing.T) {
func TestDefaultConfig(t *testing.T) {
c := Config{}

err := yaml.Unmarshal([]byte(yamlTestConfig), &c)
err := yaml.Unmarshal([]byte(yamlDefaultTestConfig), &c)
if err != nil {
t.Fatalf("yaml.Unmarshal(Config): %v", err)
}

if !reflect.DeepEqual(c, defaultTestConf) {
t.Errorf("yaml.Unmarshal(Config):\n\tExpected %+v, got %+v", defaultTestConf, c)
if !reflect.DeepEqual(c, newDefaultConfig()) {
t.Errorf("yaml.Unmarshal(Config):\n\tExpected %+v, got %+v", autofixTestConf, c)
}
}
28 changes: 14 additions & 14 deletions linter/eof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,59 @@ import (
)

func TestEOFLint_TextWithSingleNewLine(t *testing.T) {
got, fix := NewEndOfFileRule(defaultTestConf).Lint([]byte(textWithSingleNewLine))
got, fix := NewEndOfFileRule(autofixTestConf).Lint([]byte(textWithSingleNewLine))

if fix != nil {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(textWithSingleNewLine):\n\tExpected nil, got:\n%v", string(fix))
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(textWithSingleNewLine):\n\tExpected nil, got:\n%v", string(fix))
}

if got != true {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(textWithSingleNewLine):\n\tExpected %v, got %v", true, got)
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(textWithSingleNewLine):\n\tExpected %v, got %v", true, got)
}
}

func TestEOFLint_ShortTextWithSingleNewLine(t *testing.T) {
got, fix := NewEndOfFileRule(defaultTestConf).Lint([]byte(shortTextWithSingleNewLine))
got, fix := NewEndOfFileRule(autofixTestConf).Lint([]byte(shortTextWithSingleNewLine))

if fix != nil {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(shortTextWithSingleNewLine):\n\tExpected nil, got:\n%v", string(fix))
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(shortTextWithSingleNewLine):\n\tExpected nil, got:\n%v", string(fix))
}

if got != true {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(shortTextWithSingleNewLine):\n\tExpected %v, got %v", true, got)
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(shortTextWithSingleNewLine):\n\tExpected %v, got %v", true, got)
}
}

func TestEOFLint_TextWithTwoNewLines(t *testing.T) {
got, fixed := NewEndOfFileRule(defaultTestConf).Lint([]byte(textWithTwoNewLines))
got, fixed := NewEndOfFileRule(autofixTestConf).Lint([]byte(textWithTwoNewLines))

if got != false {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(textWithTwoNewLines):\n\tExpected %v, got %v", false, got)
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(textWithTwoNewLines):\n\tExpected %v, got %v", false, got)
}

if string(fixed) != string(textWithSingleNewLine) {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(textWithTwoNewLines): autofix did not work\n\tExpected:\n%q\n\tGot:\n%q", textWithSingleNewLine, string(fixed))
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(textWithTwoNewLines): autofix did not work\n\tExpected:\n%q\n\tGot:\n%q", textWithSingleNewLine, string(fixed))
}
}

func TestEOFLint_TextWithoutNewLine(t *testing.T) {
got, fixed := NewEndOfFileRule(defaultTestConf).Lint([]byte(textWithoutNewLine))
got, fixed := NewEndOfFileRule(autofixTestConf).Lint([]byte(textWithoutNewLine))

if string(fixed) != string(textWithSingleNewLine) {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(textWithoutNewLine): autofix did not work\n\tExpected:\n%q\n\tGot:\n%q", textWithSingleNewLine, string(fixed))
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(textWithoutNewLine): autofix did not work\n\tExpected:\n%q\n\tGot:\n%q", textWithSingleNewLine, string(fixed))
}

if got != false {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(textWithoutNewLine):\n\tExpected %v, got %v", false, got)
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(textWithoutNewLine):\n\tExpected %v, got %v", false, got)
}
}

func TestEOFLint_NotTextFile(t *testing.T) {
// the 0xFFFD UTF-8 control character should be ignored, because the Lint method
// does not check if the input is a valid Text file or not 'IsText' check fail
got, _ := NewEndOfFileRule(defaultTestConf).Lint([]byte(string([]rune{0xFFFD, '👋'})))
got, _ := NewEndOfFileRule(autofixTestConf).Lint([]byte(string([]rune{0xFFFD, '👋'})))

if got != false {
t.Errorf("NewEndOfFileRule(defaultTestConf).Lint(textNotText):\n\tExpected %v, got %v", false, got)
t.Errorf("NewEndOfFileRule(autofixTestConf).Lint(textNotText):\n\tExpected %v, got %v", false, got)
}
}
17 changes: 15 additions & 2 deletions linter/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@ var ignoreTests = []struct {
{".git/objects/04/9f2973ffc85f71da1fd5a", true},
}

var yamlAutofixTestConfig = `
autofix: true
ignore:
- .git/
rules:
end-of-file:
enable: true
disable-autofix: false
single-new-line: true
`

func TestShouldIgnore_DefaultConf(t *testing.T) {
for _, tt := range ignoreTests {
t.Run(tt.file, func(t *testing.T) {
got := NewEndOfFileRule(defaultTestConf).ShouldIgnore(tt.file)
got := NewEndOfFileRule(autofixTestConf).ShouldIgnore(tt.file)
want := tt.ignore

if got != want {
Expand All @@ -30,7 +43,7 @@ func TestShouldIgnore_DefaultConf(t *testing.T) {
func TestShouldIgnore_YAMLParsedConf(t *testing.T) {
c := Config{}

err := yaml.Unmarshal([]byte(yamlTestConfig), &c)
err := yaml.Unmarshal([]byte(yamlAutofixTestConfig), &c)
if err != nil {
t.Fatalf("yaml.Unmarshal(Config): %v", err)
}
Expand Down

0 comments on commit 90e37fc

Please sign in to comment.