Skip to content

Commit

Permalink
Fix error checking for empty files, and for files with no comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Jan 20, 2021
1 parent f7646b5 commit 109cd93
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 29 deletions.
11 changes: 7 additions & 4 deletions getters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package godot

import (
"errors"
"fmt"
"go/ast"
"go/token"
Expand All @@ -9,6 +10,8 @@ import (
"strings"
)

var errEmptyInput = errors.New("empty input")

// specialReplacer is a replacer for some types of special lines in comments,
// which shouldn't be checked. For example, if comment ends with a block of
// code it should not necessarily have a period at the end.
Expand All @@ -21,6 +24,10 @@ type parsedFile struct {
}

func newParsedFile(file *ast.File, fset *token.FileSet) (*parsedFile, error) {
if file == nil || fset == nil || len(file.Comments) == 0 {
return nil, errEmptyInput
}

pf := parsedFile{
fset: fset,
file: file,
Expand Down Expand Up @@ -48,10 +55,6 @@ func newParsedFile(file *ast.File, fset *token.FileSet) (*parsedFile, error) {

// getComments extracts comments from a file.
func (pf *parsedFile) getComments(scope Scope, exclude []*regexp.Regexp) []comment {
if len(pf.file.Comments) == 0 {
return nil
}

var comments []comment
decl := pf.getDeclarationComments(exclude)
switch scope {
Expand Down
3 changes: 3 additions & 0 deletions godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type comment struct {
// Run runs this linter on the provided code.
func Run(file *ast.File, fset *token.FileSet, settings Settings) ([]Issue, error) {
pf, err := newParsedFile(file, fset)
if err == errEmptyInput {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("parse input file: %v", err)
}
Expand Down
151 changes: 126 additions & 25 deletions godot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,42 @@ import (
var testExclude = []string{"^ ?@"}

func TestRun(t *testing.T) {
t.Run("empty input", func(t *testing.T) {
issues, err := Run(nil, nil, Settings{})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(issues) > 0 {
t.Fatal("Unexpected issues")
}
})

t.Run("no comments", func(t *testing.T) {
testFile := filepath.Join("testdata", "nocomments", "main.go")
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse input file: %v", err)
}

issues, err := Run(f, fset, Settings{})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(issues) > 0 {
t.Fatal("Unexpected issues")
}
})

testFile := filepath.Join("testdata", "check", "main.go")
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
file, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse input file: %v", err)
}

// Test invalid regexp
_, err = Run(f, fset, Settings{
_, err = Run(file, fset, Settings{
Scope: DeclScope,
Exclude: []string{"["},
Period: true,
Expand Down Expand Up @@ -67,7 +94,7 @@ func TestRun(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var expected int
for _, c := range f.Comments {
for _, c := range file.Comments {
if strings.Contains(c.Text(), "[PASS]") {
continue
}
Expand All @@ -77,7 +104,7 @@ func TestRun(t *testing.T) {
}
}
}
issues, err := Run(f, fset, Settings{
issues, err := Run(file, fset, Settings{
Scope: tt.scope,
Exclude: testExclude,
Period: true,
Expand All @@ -95,28 +122,18 @@ func TestRun(t *testing.T) {
}

func TestFix(t *testing.T) {
testFile := filepath.Join("testdata", "check", "main.go")
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse file %s: %v", testFile, err)
}
content, err := ioutil.ReadFile(testFile) // nolint: gosec
if err != nil {
t.Fatalf("Failed to read test file %s: %v", testFile, err)
}

t.Run("file not found", func(t *testing.T) {
path := filepath.Join("testdata", "not-exists.go")
_, err := Fix(path, nil, nil, Settings{})
testFile := filepath.Join("testdata", "not-exists.go")
_, err := Fix(testFile, nil, nil, Settings{})
if err == nil {
t.Fatal("Expected error, got nil")
}
})

t.Run("empty file", func(t *testing.T) {
path := filepath.Join("testdata", "empty.go")
fixed, err := Fix(path, nil, nil, Settings{})
testFile := filepath.Join("testdata", "empty", "main.go")

fixed, err := Fix(testFile, nil, nil, Settings{})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
Expand All @@ -125,6 +142,47 @@ func TestFix(t *testing.T) {
}
})

t.Run("no comments", func(t *testing.T) {
testFile := filepath.Join("testdata", "nocomments", "main.go")
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse input file: %v", err)
}
content, err := ioutil.ReadFile(testFile)
if err != nil {
t.Fatalf("Failed to read input file: %v", err)
}

fixed, err := Fix(testFile, f, fset, Settings{})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
assertEqualContent(t, string(content), string(fixed))
})

testFile := filepath.Join("testdata", "check", "main.go")
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse file %s: %v", testFile, err)
}
content, err := ioutil.ReadFile(testFile) // nolint: gosec
if err != nil {
t.Fatalf("Failed to read test file %s: %v", testFile, err)
}

// Test invalid regexp
_, err = Fix(testFile, file, fset, Settings{
Scope: DeclScope,
Exclude: []string{"["},
Period: true,
Capital: true,
})
if err == nil {
t.Fatalf("Expected error, got nil on regexp parsing")
}

t.Run("scope: decl", func(t *testing.T) {
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
Expand Down Expand Up @@ -184,6 +242,46 @@ func TestFix(t *testing.T) {
}

func TestReplace(t *testing.T) {
t.Run("file not found", func(t *testing.T) {
path := filepath.Join("testdata", "not-exists.go")
err := Replace(path, nil, nil, Settings{})
if err == nil {
t.Fatal("Expected error, got nil")
}
})

t.Run("empty file", func(t *testing.T) {
testFile := filepath.Join("testdata", "empty", "main.go")

err := Replace(testFile, nil, nil, Settings{})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})

t.Run("no comments", func(t *testing.T) {
testFile := filepath.Join("testdata", "nocomments", "main.go")
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse input file: %v", err)
}
content, err := ioutil.ReadFile(testFile)
if err != nil {
t.Fatalf("Failed to read input file: %v", err)
}

err = Replace(testFile, f, fset, Settings{})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
fixed, err := ioutil.ReadFile(testFile)
if err != nil {
t.Fatalf("Failed to read fixed file: %v", err)
}
assertEqualContent(t, string(content), string(fixed))
})

testFile := filepath.Join("testdata", "check", "main.go")
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
Expand All @@ -200,13 +298,16 @@ func TestReplace(t *testing.T) {
t.Fatalf("Failed to read test file %s: %v", testFile, err)
}

t.Run("file not found", func(t *testing.T) {
path := filepath.Join("testdata", "not-exists.go")
err := Replace(path, nil, nil, Settings{})
if err == nil {
t.Fatal("Expected error, got nil")
}
// Test invalid regexp
err = Replace(testFile, file, fset, Settings{
Scope: DeclScope,
Exclude: []string{"["},
Period: true,
Capital: true,
})
if err == nil {
t.Fatalf("Expected error, got nil on regexp parsing")
}

t.Run("scope: decl", func(t *testing.T) {
defer func() {
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions testdata/nocomments/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {
println("Hello, world!")
}

0 comments on commit 109cd93

Please sign in to comment.