Skip to content

Commit

Permalink
Add test for skipping over malformed files
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Gehrsitz <norman.gehrsitz@gmx.de>
  • Loading branch information
ngehrsitz committed Jan 18, 2022
1 parent 13f0e85 commit 30fa9a9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/gci/gci_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gci

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -69,3 +70,49 @@ func TestInitGciConfigFromYAML(t *testing.T) {
assert.True(t, gciCfg.NoInlineComments)
assert.True(t, gciCfg.NoPrefixComments)
}

func TestSkippingOverIncorrectlyFormattedFiles(t *testing.T) {
cfg, err := GciStringConfiguration{}.Parse()
assert.NoError(t, err)
validFileProcessedChan := make(chan bool, 1)

var importUnclosedCtr, noImportCtr, validCtr int
var files []io.FileObj
files = append(files, TestFile{io.File{"internal/skipTest/import-unclosed.testgo"}, &importUnclosedCtr})
files = append(files, TestFile{io.File{"internal/skipTest/no-import.testgo"}, &noImportCtr})
files = append(files, TestFile{io.File{"internal/skipTest/valid.testgo"}, &validCtr})

generatorFunc := func() ([]io.FileObj, error) {
return files, nil
}
fileAccessTestFunc := func(filePath string, unmodifiedFile, formattedFile []byte) error {
if filePath != "internal/skipTest/valid.testgo" {
return fmt.Errorf("malformed file %q should not have been processed", filePath)
}
validFileProcessedChan <- true
return nil
}
err = processFiles(generatorFunc, *cfg, fileAccessTestFunc)

assert.NoError(t, err)
// check all files have been accessed
assert.Equal(t, importUnclosedCtr, 1)
assert.Equal(t, noImportCtr, 1)
assert.Equal(t, validCtr, 1)
// check that processing for the valid file was called
assert.True(t, <-validFileProcessedChan)
}

type TestFile struct {
wrappedFile io.File
accessCounter *int
}

func (t TestFile) Load() ([]byte, error) {
*t.accessCounter++
return t.wrappedFile.Load()
}

func (t TestFile) Path() string {
return t.wrappedFile.Path()
}
8 changes: 8 additions & 0 deletions pkg/gci/internal/skipTest/import-unclosed.testgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import (
"golang.org/x/tools"

"fmt"

"github.com/daixiang0/gci"

4 changes: 4 additions & 0 deletions pkg/gci/internal/skipTest/no-import.testgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

func X() {
}
8 changes: 8 additions & 0 deletions pkg/gci/internal/skipTest/valid.testgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import (
"golang.org/x/tools"

"fmt"

"github.com/daixiang0/gci"
)

0 comments on commit 30fa9a9

Please sign in to comment.