github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/test/fix_test.go (about)

     1  package test
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/vanstinator/golangci-lint/test/testshared"
    12  )
    13  
    14  // value: "1"
    15  const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
    16  
    17  func setupTestFix(t *testing.T) []string {
    18  	t.Helper()
    19  
    20  	testshared.SkipOnWindows(t)
    21  
    22  	tmpDir := filepath.Join(testdataDir, "fix.tmp")
    23  	_ = os.RemoveAll(tmpDir) // cleanup previous runs
    24  
    25  	if os.Getenv(envKeepTempFiles) == "1" {
    26  		t.Logf("Temp dir for fix test: %s", tmpDir)
    27  	} else {
    28  		t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
    29  	}
    30  
    31  	sourcesDir := filepath.Join(testdataDir, "fix")
    32  
    33  	err := exec.Command("cp", "-R", sourcesDir, tmpDir).Run()
    34  	require.NoError(t, err)
    35  
    36  	testshared.InstallGolangciLint(t)
    37  
    38  	return findSources(t, tmpDir, "in", "*.go")
    39  }
    40  
    41  func TestFix(t *testing.T) {
    42  	sources := setupTestFix(t)
    43  
    44  	for _, input := range sources {
    45  		input := input
    46  
    47  		t.Run(filepath.Base(input), func(t *testing.T) {
    48  			t.Parallel()
    49  
    50  			rc := testshared.ParseTestDirectives(t, input)
    51  			if rc == nil {
    52  				t.Logf("Skipped: %s", input)
    53  				return
    54  			}
    55  
    56  			testshared.NewRunnerBuilder(t).
    57  				WithArgs("--disable-all",
    58  					"--print-issued-lines=false",
    59  					"--print-linter-name=false",
    60  					"--out-format=line-number",
    61  					"--fix").
    62  				WithRunContext(rc).
    63  				WithTargetPath(input).
    64  				Runner().
    65  				Run().
    66  				ExpectExitCode(rc.ExitCode)
    67  
    68  			output, err := os.ReadFile(input)
    69  			require.NoError(t, err)
    70  
    71  			expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
    72  			require.NoError(t, err)
    73  
    74  			require.Equal(t, string(expectedOutput), string(output))
    75  		})
    76  	}
    77  }
    78  
    79  func TestFix_pathPrefix(t *testing.T) {
    80  	sources := setupTestFix(t)
    81  
    82  	for _, input := range sources {
    83  		input := input
    84  
    85  		t.Run(filepath.Base(input), func(t *testing.T) {
    86  			t.Parallel()
    87  
    88  			rc := testshared.ParseTestDirectives(t, input)
    89  			if rc == nil {
    90  				t.Logf("Skipped: %s", input)
    91  				return
    92  			}
    93  
    94  			testshared.NewRunnerBuilder(t).
    95  				WithArgs("--disable-all",
    96  					"--print-issued-lines=false",
    97  					"--print-linter-name=false",
    98  					"--out-format=line-number",
    99  					"--fix",
   100  					"--path-prefix=foobar/").
   101  				WithRunContext(rc).
   102  				WithTargetPath(input).
   103  				Runner().
   104  				Run().
   105  				ExpectExitCode(rc.ExitCode)
   106  
   107  			output, err := os.ReadFile(input)
   108  			require.NoError(t, err)
   109  
   110  			expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
   111  			require.NoError(t, err)
   112  
   113  			require.Equal(t, string(expectedOutput), string(output))
   114  		})
   115  	}
   116  }