github.com/daixiang0/gci@v0.13.4/pkg/gci/gci_test.go (about) 1 package gci 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/daixiang0/gci/pkg/config" 14 "github.com/daixiang0/gci/pkg/io" 15 "github.com/daixiang0/gci/pkg/log" 16 ) 17 18 func init() { 19 log.InitLogger() 20 defer log.L().Sync() 21 } 22 23 func TestRun(t *testing.T) { 24 for i := range testCases { 25 t.Run(fmt.Sprintf("run case: %s", testCases[i].name), func(t *testing.T) { 26 config, err := config.ParseConfig(testCases[i].config) 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 old, new, err := LoadFormat([]byte(testCases[i].in), "", *config) 32 if err != nil { 33 t.Fatal(err) 34 } 35 36 assert.NoError(t, err) 37 assert.Equal(t, testCases[i].in, string(old)) 38 assert.Equal(t, testCases[i].out, string(new)) 39 }) 40 } 41 } 42 43 func chdir(t *testing.T, dir string) { 44 oldWd, err := os.Getwd() 45 require.NoError(t, err) 46 require.NoError(t, os.Chdir(dir)) 47 48 // change back at the end of the test 49 t.Cleanup(func() { os.Chdir(oldWd) }) 50 } 51 52 func readConfig(t *testing.T, configPath string) *config.Config { 53 rawConfig, err := os.ReadFile(configPath) 54 require.NoError(t, err) 55 cfg, err := config.ParseConfig(string(rawConfig)) 56 require.NoError(t, err) 57 58 return cfg 59 } 60 61 func TestRunWithLocalModule(t *testing.T) { 62 tests := []struct { 63 name string 64 moduleDir string 65 // files with a corresponding '*.out.go' file containing the expected 66 // result of formatting 67 testedFiles []string 68 }{ 69 { 70 name: `default module test case`, 71 moduleDir: filepath.Join("testdata", "module"), 72 testedFiles: []string{ 73 "main.go", 74 filepath.Join("internal", "foo", "lib.go"), 75 }, 76 }, 77 { 78 name: `canonical module without go sources in root dir`, 79 moduleDir: filepath.Join("testdata", "module_canonical"), 80 testedFiles: []string{ 81 filepath.Join("cmd", "client", "main.go"), 82 filepath.Join("cmd", "server", "main.go"), 83 filepath.Join("internal", "foo", "lib.go"), 84 }, 85 }, 86 { 87 name: `non-canonical module without go sources in root dir`, 88 moduleDir: filepath.Join("testdata", "module_noncanonical"), 89 testedFiles: []string{ 90 filepath.Join("cmd", "client", "main.go"), 91 filepath.Join("cmd", "server", "main.go"), 92 filepath.Join("internal", "foo", "lib.go"), 93 }, 94 }, 95 } 96 for _, tt := range tests { 97 t.Run(tt.name, func(t *testing.T) { 98 // run subtests for expected module loading behaviour 99 chdir(t, tt.moduleDir) 100 cfg := readConfig(t, "config.yaml") 101 102 for _, path := range tt.testedFiles { 103 t.Run(path, func(t *testing.T) { 104 // *.go -> *.out.go 105 expected, err := os.ReadFile(strings.TrimSuffix(path, ".go") + ".out.go") 106 require.NoError(t, err) 107 108 _, got, err := LoadFormatGoFile(io.File{path}, *cfg) 109 110 require.NoError(t, err) 111 require.Equal(t, string(expected), string(got)) 112 }) 113 } 114 }) 115 } 116 } 117 118 func TestRunWithLocalModuleWithPackageLoadFailure(t *testing.T) { 119 // just a directory with no Go modules 120 dir := t.TempDir() 121 configContent := "sections:\n - LocalModule\n" 122 123 chdir(t, dir) 124 _, err := config.ParseConfig(configContent) 125 require.ErrorContains(t, err, "go.mod: open go.mod:") 126 }