github.com/daixiang0/gci@v0.13.0/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  	// if runtime.GOOS == "windows" {
    25  	// 	t.Skip("Skipping test on Windows")
    26  	// }
    27  
    28  	for i := range testCases {
    29  		t.Run(fmt.Sprintf("run case: %s", testCases[i].name), func(t *testing.T) {
    30  			config, err := config.ParseConfig(testCases[i].config)
    31  			if err != nil {
    32  				t.Fatal(err)
    33  			}
    34  
    35  			old, new, err := LoadFormat([]byte(testCases[i].in), "", *config)
    36  			if err != nil {
    37  				t.Fatal(err)
    38  			}
    39  
    40  			assert.NoError(t, err)
    41  			assert.Equal(t, testCases[i].in, string(old))
    42  			assert.Equal(t, testCases[i].out, string(new))
    43  		})
    44  	}
    45  }
    46  
    47  func chdir(t *testing.T, dir string) {
    48  	oldWd, err := os.Getwd()
    49  	require.NoError(t, err)
    50  	require.NoError(t, os.Chdir(dir))
    51  
    52  	// change back at the end of the test
    53  	t.Cleanup(func() { os.Chdir(oldWd) })
    54  }
    55  
    56  func readConfig(t *testing.T, configPath string) *config.Config {
    57  	rawConfig, err := os.ReadFile(configPath)
    58  	require.NoError(t, err)
    59  	config, err := config.ParseConfig(string(rawConfig))
    60  	require.NoError(t, err)
    61  
    62  	return config
    63  }
    64  
    65  func TestRunWithLocalModule(t *testing.T) {
    66  	moduleDir := filepath.Join("testdata", "module")
    67  	// files with a corresponding '*.out.go' file containing the expected
    68  	// result of formatting
    69  	testedFiles := []string{
    70  		"main.go",
    71  		filepath.Join("internal", "foo", "lib.go"),
    72  	}
    73  
    74  	// run subtests for expected module loading behaviour
    75  	chdir(t, moduleDir)
    76  	cfg := readConfig(t, "config.yaml")
    77  
    78  	for _, path := range testedFiles {
    79  		t.Run(path, func(t *testing.T) {
    80  			// *.go -> *.out.go
    81  			expected, err := os.ReadFile(strings.TrimSuffix(path, ".go") + ".out.go")
    82  			require.NoError(t, err)
    83  
    84  			_, got, err := LoadFormatGoFile(io.File{path}, *cfg)
    85  
    86  			require.NoError(t, err)
    87  			require.Equal(t, string(expected), string(got))
    88  		})
    89  	}
    90  }
    91  
    92  func TestRunWithLocalModuleWithPackageLoadFailure(t *testing.T) {
    93  	// just a directory with no Go modules
    94  	dir := t.TempDir()
    95  	configContent := "sections:\n  - LocalModule\n"
    96  
    97  	chdir(t, dir)
    98  	_, err := config.ParseConfig(configContent)
    99  	require.ErrorContains(t, err, "failed to load local modules: ")
   100  }
   101  
   102  func TestRunWithLocalModuleWithModuleLookupError(t *testing.T) {
   103  	dir := t.TempDir()
   104  	// error from trying to list packages under module with no go files
   105  	configContent := "sections:\n  - LocalModule\n"
   106  	goModContent := "module example.com/foo\n"
   107  	require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(goModContent), 0o644))
   108  
   109  	chdir(t, dir)
   110  	_, err := config.ParseConfig(configContent)
   111  	require.ErrorContains(t, err, "error reading local packages: ")
   112  	require.ErrorContains(t, err, dir)
   113  }