|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "regexp" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/g-rath/osv-detector/internal/cachedregexp" |
| 12 | +) |
| 13 | + |
| 14 | +// normalizeFilePaths attempts to normalize any file paths in the given `output` |
| 15 | +// so that they can be compared reliably regardless of the file path separator |
| 16 | +// being used. |
| 17 | +// |
| 18 | +// Namely, escaped forward slashes are replaced with backslashes. |
| 19 | +func normalizeFilePaths(t *testing.T, output string) string { |
| 20 | + t.Helper() |
| 21 | + |
| 22 | + return strings.ReplaceAll(strings.ReplaceAll(output, "\\\\", "/"), "\\", "/") |
| 23 | +} |
| 24 | + |
| 25 | +// normalizeRootDirectory attempts to replace references to the current working |
| 26 | +// directory with "<rootdir>", in order to reduce the noise of the cmp diff |
| 27 | +func normalizeRootDirectory(t *testing.T, str string) string { |
| 28 | + t.Helper() |
| 29 | + |
| 30 | + cwd, err := os.Getwd() |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("failed to get current directory: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + cwd = normalizeFilePaths(t, cwd) |
| 36 | + |
| 37 | + // file uris with Windows end up with three slashes, so we normalize that too |
| 38 | + str = strings.ReplaceAll(str, "file:///"+cwd, "file://<rootdir>") |
| 39 | + str = strings.ReplaceAll(str, cwd, "<rootdir>") |
| 40 | + |
| 41 | + // Replace versions without the root as well |
| 42 | + str = strings.ReplaceAll(str, pathWithoutRoot(t, cwd), "<rootdir>") |
| 43 | + |
| 44 | + return str |
| 45 | +} |
| 46 | + |
| 47 | +// normalizeUserCacheDirectory attempts to replace references to the current working |
| 48 | +// directory with "<tempdir>", in order to reduce the noise of the cmp diff |
| 49 | +func normalizeUserCacheDirectory(t *testing.T, str string) string { |
| 50 | + t.Helper() |
| 51 | + |
| 52 | + cacheDir, err := os.UserCacheDir() |
| 53 | + if err != nil { |
| 54 | + t.Errorf("could not get user cache (%v) - results and diff might be inaccurate!", err) |
| 55 | + } |
| 56 | + |
| 57 | + cacheDir = normalizeFilePaths(t, cacheDir) |
| 58 | + |
| 59 | + // file uris with Windows end up with three slashes, so we normalize that too |
| 60 | + str = strings.ReplaceAll(str, "file:///"+cacheDir, "file://<tempdir>") |
| 61 | + |
| 62 | + return strings.ReplaceAll(str, cacheDir, "<tempdir>") |
| 63 | +} |
| 64 | + |
| 65 | +// normalizeTempDirectory attempts to replace references to the temp directory |
| 66 | +// with "<tempdir>", to ensure tests pass across different OSs |
| 67 | +func normalizeTempDirectory(t *testing.T, str string) string { |
| 68 | + t.Helper() |
| 69 | + |
| 70 | + //nolint:gocritic // ensure that the directory doesn't end with a trailing slash |
| 71 | + tempDir := normalizeFilePaths(t, filepath.Join(os.TempDir())) |
| 72 | + re := cachedregexp.MustCompile(regexp.QuoteMeta(tempDir+`/osv-detector-test-`) + `\d+`) |
| 73 | + str = re.ReplaceAllString(str, "<tempdir>") |
| 74 | + |
| 75 | + // Replace versions without the root as well |
| 76 | + re = cachedregexp.MustCompile(regexp.QuoteMeta(pathWithoutRoot(t, tempDir)+`/osv-detector-test-`) + `\d+`) |
| 77 | + |
| 78 | + return re.ReplaceAllString(str, "<tempdir>") |
| 79 | +} |
| 80 | + |
| 81 | +// normalizeDatabaseStats attempts to replace references to database stats (such as |
| 82 | +// the number of vulnerabilities and the time that the database was last updated) |
| 83 | +// in the output with %% wildcards, in order to reduce the noise of the cmp diff |
| 84 | +func normalizeDatabaseStats(t *testing.T, str string) string { |
| 85 | + t.Helper() |
| 86 | + |
| 87 | + re := cachedregexp.MustCompile(`(\w+) \(\d+ vulnerabilities, including withdrawn - last updated \w{3}, \d\d \w{3} \d{4} [012]\d:\d\d:\d\d GMT\)`) |
| 88 | + |
| 89 | + return re.ReplaceAllString(str, "$1 (%% vulnerabilities, including withdrawn - last updated %%)") |
| 90 | +} |
| 91 | + |
| 92 | +// normalizeErrors attempts to replace error messages on alternative OSs with their |
| 93 | +// known linux equivalents, to ensure tests pass across different OSs |
| 94 | +func normalizeErrors(t *testing.T, str string) string { |
| 95 | + t.Helper() |
| 96 | + |
| 97 | + str = strings.ReplaceAll(str, "The system cannot find the path specified.", "no such file or directory") |
| 98 | + str = strings.ReplaceAll(str, "The system cannot find the file specified.", "no such file or directory") |
| 99 | + |
| 100 | + return str |
| 101 | +} |
| 102 | + |
| 103 | +// normalizeSnapshot applies a series of normalizes to the buffer from a std stream like stdout and stderr |
| 104 | +func normalizeSnapshot(t *testing.T, str string) string { |
| 105 | + t.Helper() |
| 106 | + |
| 107 | + for _, normalizer := range []func(t *testing.T, str string) string{ |
| 108 | + normalizeFilePaths, |
| 109 | + normalizeRootDirectory, |
| 110 | + normalizeTempDirectory, |
| 111 | + normalizeUserCacheDirectory, |
| 112 | + normalizeDatabaseStats, |
| 113 | + normalizeErrors, |
| 114 | + } { |
| 115 | + str = normalizer(t, str) |
| 116 | + } |
| 117 | + |
| 118 | + return str |
| 119 | +} |
| 120 | + |
| 121 | +func pathWithoutRoot(t *testing.T, str string) string { |
| 122 | + t.Helper() |
| 123 | + |
| 124 | + // Replace versions without the root as well |
| 125 | + var root string |
| 126 | + if runtime.GOOS == "windows" { |
| 127 | + root = filepath.VolumeName(str) + "\\" |
| 128 | + } |
| 129 | + |
| 130 | + if strings.HasPrefix(str, "/") { |
| 131 | + root = "/" |
| 132 | + } |
| 133 | + |
| 134 | + return str[len(root):] |
| 135 | +} |
0 commit comments