forked from daixiang0/gci
/
gci_test.go
109 lines (89 loc) · 2.86 KB
/
gci_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package gci
import (
"os"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/daixiang0/gci/pkg/config"
"github.com/daixiang0/gci/pkg/io"
"github.com/daixiang0/gci/pkg/log"
)
func init() {
log.InitLogger()
defer log.L().Sync()
}
var testFilesPath = "internal/testdata"
func isTestInputFile(file os.FileInfo) bool {
return !file.IsDir() && strings.HasSuffix(file.Name(), ".in.go")
}
func TestRun(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping test on Windows")
}
testFiles, err := io.FindFilesForPath(testFilesPath, isTestInputFile)
if err != nil {
t.Fatal(err)
}
for _, testFile := range testFiles {
fileBaseName := strings.TrimSuffix(testFile, ".in.go")
t.Run("pkg/gci/"+testFile, func(t *testing.T) {
t.Parallel()
gciCfg, err := config.InitializeGciConfigFromYAML(fileBaseName + ".cfg.yaml")
if err != nil {
t.Fatal(err)
}
inputSrcFile := io.File{FilePath: fileBaseName + ".in.go"}
inputSrc, err := inputSrcFile.Load()
require.NoError(t, err)
unmodifiedFile, formattedFile, err := LoadFormatGoFile(inputSrcFile, *gciCfg)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, inputSrc, unmodifiedFile)
expectedOutput, err := os.ReadFile(fileBaseName + ".out.go")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(expectedOutput), string(formattedFile), "output")
assert.NoError(t, err)
})
}
}
// func TestSkippingOverIncorrectlyFormattedFiles(t *testing.T) {
// cfg, err := config.YamlConfig{}.Parse()
// assert.NoError(t, err)
// var importUnclosedCtr, noImportCtr, validCtr int
// var files []io.FileObj
// files = append(files, TestFile{io.File{FilePath: "internal/skipTest/import-unclosed.testgo"}, &importUnclosedCtr})
// files = append(files, TestFile{io.File{FilePath: "internal/skipTest/no-import.testgo"}, &noImportCtr})
// files = append(files, TestFile{io.File{FilePath: "internal/skipTest/valid.testgo"}, &validCtr})
// validFileProcessedChan := make(chan bool, len(files))
// generatorFunc := func() ([]io.FileObj, error) {
// return files, nil
// }
// fileAccessTestFunc := func(filePath string, unmodifiedFile, formattedFile []byte) error {
// 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()
// }