Skip to content

Commit

Permalink
Normalise newlines on Windows
Browse files Browse the repository at this point in the history
Otherwise assorted things go wrong when running tests using this
infrastructure on Windows where \r\n line endings confuse things.
  • Loading branch information
illicitonion committed May 8, 2024
1 parent 7c84c33 commit 454e1cc
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions testtools/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ func CheckFiles(t *testing.T, dir string, files []FileSpec) {
t.Errorf("not a directory: %s", f.Path)
}
} else {
want := strings.TrimSpace(f.Content)
want := normalizeSpace(f.Content)
gotBytes, err := os.ReadFile(filepath.Join(dir, f.Path))
if err != nil {
t.Errorf("could not read %s: %v", f.Path, err)
continue
}
got := strings.TrimSpace(string(gotBytes))
got := normalizeSpace(string(gotBytes))
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("%s diff (-want,+got):\n%s", f.Path, diff)
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestGazelleGenerationOnPath(t *testing.T, args *TestGazelleGenerationArgs)

// Read in expected stdout, stderr, and exit code files.
if d.Name() == argumentsFilename {
config.Args = strings.Split(string(content), "\n")
config.Args = strings.Split(normalizeSpace(string(content)), "\n")
return nil
}
if d.Name() == expectedStdoutFilename {
Expand Down Expand Up @@ -353,13 +353,13 @@ Run %s to update BUILD.out and expected{Stdout,Stderr,ExitCode}.txt files.
}
}
actualStdout := redactWorkspacePath(stdout.String(), workspaceRoot)
if strings.TrimSpace(config.Stdout) != strings.TrimSpace(actualStdout) {
if normalizeSpace(config.Stdout) != normalizeSpace(actualStdout) {
errs = append(errs, fmt.Errorf("expected gazelle stdout: %s\ngot: %s",
config.Stdout, actualStdout,
))
}
actualStderr := redactWorkspacePath(stderr.String(), workspaceRoot)
if strings.TrimSpace(config.Stderr) != strings.TrimSpace(actualStderr) {
if normalizeSpace(config.Stderr) != normalizeSpace(actualStderr) {
errs = append(errs, fmt.Errorf("expected gazelle stderr: %s\ngot: %s",
config.Stderr, actualStderr,
))
Expand Down Expand Up @@ -424,3 +424,7 @@ func updateExpectedConfig(t *testing.T, expected string, actual string, srcTestD
func redactWorkspacePath(s, wsPath string) string {
return strings.ReplaceAll(s, wsPath, "%WORKSPACEPATH%")
}

func normalizeSpace(s string) string {
return strings.TrimSpace(strings.ReplaceAll(s, "\r\n", "\n"))
}

0 comments on commit 454e1cc

Please sign in to comment.