|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "go/build" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +var regexReplacements = []struct { |
| 19 | + regexp *regexp.Regexp |
| 20 | + replaceStr string |
| 21 | +}{ |
| 22 | + { |
| 23 | + // Test duration |
| 24 | + regexp: regexp.MustCompile(`\\t[0-9]+\.[0-9]+s`), |
| 25 | + replaceStr: "", |
| 26 | + }, |
| 27 | + { |
| 28 | + // Test duration in brackets |
| 29 | + regexp: regexp.MustCompile(`\([0-9]+\.[0-9]+s\)`), |
| 30 | + replaceStr: "", |
| 31 | + }, |
| 32 | + { |
| 33 | + // Pointer |
| 34 | + regexp: regexp.MustCompile(`\+?\(?0x[0-9a-f]+\)?`), |
| 35 | + replaceStr: "", |
| 36 | + }, |
| 37 | + { |
| 38 | + // Goroutine |
| 39 | + regexp: regexp.MustCompile(`goroutine [0-9]+`), |
| 40 | + replaceStr: "goroutine x", |
| 41 | + }, |
| 42 | + { |
| 43 | + // Line number |
| 44 | + regexp: regexp.MustCompile(`\.go:[0-9]+(:[0-9]+)?`), |
| 45 | + replaceStr: ".go", |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +func TestIntegration(t *testing.T) { |
| 50 | + tests := []struct { |
| 51 | + inputDir string |
| 52 | + expected string |
| 53 | + }{ |
| 54 | + { |
| 55 | + // This test case covers the case the code under test does not compile, |
| 56 | + // i.e. "go build ." would fail. |
| 57 | + inputDir: "./testrunner/testdata/practice/broken", |
| 58 | + expected: "./testrunner/testdata/expected/broken.json", |
| 59 | + }, |
| 60 | + { |
| 61 | + // This test case covers the case that the test code does not compile, |
| 62 | + // i.e. "go build ." would succeed but "go test" returns compilation errors. |
| 63 | + inputDir: "./testrunner/testdata/practice/missing_func", |
| 64 | + expected: "./testrunner/testdata/expected/missing_func.json", |
| 65 | + }, |
| 66 | + { |
| 67 | + inputDir: "./testrunner/testdata/practice/broken_import", |
| 68 | + expected: "./testrunner/testdata/expected/broken_import.json", |
| 69 | + }, |
| 70 | + { |
| 71 | + inputDir: "./testrunner/testdata/practice/passing", |
| 72 | + expected: "./testrunner/testdata/expected/passing.json", |
| 73 | + }, |
| 74 | + { |
| 75 | + inputDir: "./testrunner/testdata/practice/pkg_level_error", |
| 76 | + expected: "./testrunner/testdata/expected/pkg_level_error.json", |
| 77 | + }, |
| 78 | + { |
| 79 | + inputDir: "./testrunner/testdata/practice/failing", |
| 80 | + expected: "./testrunner/testdata/expected/failing.json", |
| 81 | + }, |
| 82 | + { |
| 83 | + inputDir: "./testrunner/testdata/concept/auto_assigned_task_ids", |
| 84 | + expected: "./testrunner/testdata/expected/auto_assigned_task_ids.json", |
| 85 | + }, |
| 86 | + { |
| 87 | + inputDir: "./testrunner/testdata/concept/explicit_task_ids", |
| 88 | + expected: "./testrunner/testdata/expected/explicit_task_ids.json", |
| 89 | + }, |
| 90 | + { |
| 91 | + inputDir: "./testrunner/testdata/concept/missing_task_ids", |
| 92 | + expected: "./testrunner/testdata/expected/missing_task_ids.json", |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + goExe, err := exec.LookPath("go") |
| 97 | + require.NoError(t, err, "failed to find go executable") |
| 98 | + |
| 99 | + goRoot := os.Getenv("GOROOT") |
| 100 | + if goRoot == "" { |
| 101 | + goRoot = build.Default.GOROOT |
| 102 | + } |
| 103 | + |
| 104 | + currentDir, err := os.Getwd() |
| 105 | + require.NoError(t, err, "failed to determine current directory") |
| 106 | + |
| 107 | + for _, tt := range tests { |
| 108 | + t.Run(tt.inputDir, func(t *testing.T) { |
| 109 | + err := os.RemoveAll("./outdir") |
| 110 | + require.NoError(t, err, "failed to clean up output directory") |
| 111 | + |
| 112 | + var stdout, stderr bytes.Buffer |
| 113 | + cmd := &exec.Cmd{ |
| 114 | + Path: goExe, |
| 115 | + Args: []string{goExe, "run", ".", tt.inputDir, "outdir"}, |
| 116 | + Stdout: &stdout, |
| 117 | + Stderr: &stderr, |
| 118 | + } |
| 119 | + err = cmd.Run() |
| 120 | + require.NoErrorf(t, err, "failed to execute test runner: %s %s", stdout.String(), stderr.String()) |
| 121 | + |
| 122 | + resultBytes, err := os.ReadFile("./outdir/results.json") |
| 123 | + require.NoError(t, err, "failed to read results") |
| 124 | + |
| 125 | + result := sanitizeResult(string(resultBytes), []string{goExe, currentDir, goRoot}) |
| 126 | + |
| 127 | + expected, err := os.ReadFile(tt.expected) |
| 128 | + require.NoError(t, err, "failed to read expected result file") |
| 129 | + |
| 130 | + assert.JSONEq(t, string(expected), result) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func sanitizeResult(s string, paths []string) string { |
| 136 | + result := s |
| 137 | + |
| 138 | + for _, p := range pathVariations(paths) { |
| 139 | + result = strings.ReplaceAll(result, p, "PATH_PLACEHOLDER") |
| 140 | + } |
| 141 | + |
| 142 | + if runtime.GOOS == "windows" { |
| 143 | + result = strings.ReplaceAll(result, `\n.//`, `\n./`) |
| 144 | + result = strings.ReplaceAll(result, `\n.\\`, `\n./`) |
| 145 | + result = strings.ReplaceAll(result, `\n.\`, `\n./`) |
| 146 | + } |
| 147 | + |
| 148 | + for _, replacement := range regexReplacements { |
| 149 | + result = replacement.regexp.ReplaceAllString(result, replacement.replaceStr) |
| 150 | + } |
| 151 | + |
| 152 | + return result |
| 153 | +} |
| 154 | + |
| 155 | +func pathVariations(paths []string) []string { |
| 156 | + result := []string{} |
| 157 | + for _, p := range paths { |
| 158 | + normalizedPath := filepath.ToSlash(p) |
| 159 | + result = append(result, normalizedPath) |
| 160 | + |
| 161 | + if runtime.GOOS == "windows" { |
| 162 | + // On windows, the paths that are included in the test results can have |
| 163 | + // various formats. We try to include all variants here so we catch |
| 164 | + // everything when we do the replace later. |
| 165 | + result = append(result, strings.ReplaceAll(normalizedPath, "/", "//")) |
| 166 | + result = append(result, strings.ReplaceAll(normalizedPath, "/", `\`)) |
| 167 | + result = append(result, strings.ReplaceAll(normalizedPath, "/", `\\`)) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return result |
| 172 | +} |
0 commit comments