Skip to content

Commit

Permalink
test(fix): test various path prefix cases separately
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Feb 22, 2023
1 parent f0211f6 commit 3aa455c
Showing 1 changed file with 50 additions and 39 deletions.
89 changes: 50 additions & 39 deletions test/fix_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"fmt"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,58 +17,68 @@ const envKeepTempFiles = "GL_KEEP_TEMP_FILES"

func TestFix(t *testing.T) {
testshared.SkipOnWindows(t)
testshared.InstallGolangciLint(t)
sourcesDir := filepath.Join(testdataDir, "fix")

tmpDir := filepath.Join(testdataDir, "fix.tmp")
_ = os.RemoveAll(tmpDir) // cleanup previous runs

if os.Getenv(envKeepTempFiles) == "1" {
t.Logf("Temp dir for fix test: %s", tmpDir)
} else {
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
tests := []struct {
Args []string
DirSuffix string
}{
{[]string{}, ""},
{[]string{"--path-prefix=simple-prefix"}, "-simple-prefix"},
{[]string{"--path-prefix=slash-prefix/"}, "-slash-prefix"},
}

sourcesDir := filepath.Join(testdataDir, "fix")
for _, test := range tests {
tmpDir := filepath.Join(testdataDir, fmt.Sprintf("fix%s.tmp", test.DirSuffix))
_ = os.RemoveAll(tmpDir) // cleanup previous runs

err := exec.Command("cp", "-R", sourcesDir, tmpDir).Run()
require.NoError(t, err)
if os.Getenv(envKeepTempFiles) == "1" {
t.Logf("Temp dir for fix with args %v test: %s", test.Args, tmpDir)
} else {
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
}

testshared.InstallGolangciLint(t)
err := exec.Command("cp", "-R", sourcesDir, tmpDir).Run()
require.NoError(t, err)

sources := findSources(t, tmpDir, "in", "*.go")
sources := findSources(t, tmpDir, "in", "*.go")

for _, input := range sources {
input := input
t.Run(filepath.Base(input), func(t *testing.T) {
t.Parallel()
for _, input := range sources {
input := input
t.Run(filepath.Base(input)+test.DirSuffix, func(t *testing.T) {
t.Parallel()

rc := testshared.ParseTestDirectives(t, input)
if rc == nil {
t.Logf("Skipped: %s", input)
return
}
rc := testshared.ParseTestDirectives(t, input)
if rc == nil {
t.Logf("Skipped: %s", input)
return
}

testshared.NewRunnerBuilder(t).
WithArgs(
args := []string{
"--disable-all",
"--print-issued-lines=false",
"--print-linter-name=false",
"--out-format=line-number",
"--fix",
"--path-prefix=mock",
).
WithRunContext(rc).
WithTargetPath(input).
Runner().
Run().
ExpectExitCode(rc.ExitCode)

output, err := os.ReadFile(input)
require.NoError(t, err)

expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
require.NoError(t, err)

require.Equal(t, string(expectedOutput), string(output))
})
}
args = append(args, test.Args...)
testshared.NewRunnerBuilder(t).
WithArgs(args...).
WithRunContext(rc).
WithTargetPath(input).
Runner().
Run().
ExpectExitCode(rc.ExitCode)

output, err := os.ReadFile(input)
require.NoError(t, err)

expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
require.NoError(t, err)

require.Equal(t, string(expectedOutput), string(output))
})
}
}
}

0 comments on commit 3aa455c

Please sign in to comment.