MustClean makes traces more deterministic for tests by: - replacing the environment-specific path to errtrace with the fixed path /path/to/errtrace - replacing line numbers with the lowest values that maintain relative ordering within the file Note that lines numbers are replaced with increasing v
(trace string)
| 32 | // with earlier positions in the file getting lower numbers. |
| 33 | // The relative ordering of lines within a file is maintained. |
| 34 | func MustClean(trace string) string { |
| 35 | // Get deterministic file paths first. |
| 36 | trace = strings.ReplaceAll(trace, getErrtraceDir(), _fixedDir) |
| 37 | |
| 38 | replacer := make(fileLineReplacer) |
| 39 | for _, m := range _fileLineMatcher.FindAllStringSubmatch(trace, -1) { |
| 40 | file := m[1] |
| 41 | lineStr := m[2] |
| 42 | line, err := strconv.Atoi(lineStr) |
| 43 | if err != nil { |
| 44 | panic(fmt.Sprintf("matched bad line number in %q: %v", m[0], err)) |
| 45 | } |
| 46 | replacer.Add(file, line) |
| 47 | } |
| 48 | |
| 49 | return strings.NewReplacer(replacer.Replacements()...).Replace(trace) |
| 50 | } |
| 51 | |
| 52 | func getErrtraceDir() string { |
| 53 | _, file, _, _ := runtime.Caller(0) |