MCPcopy Create free account
hub / github.com/bracesdev/errtrace / MustClean

Function MustClean

internal/tracetest/clean.go:34–50  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

32// with earlier positions in the file getting lower numbers.
33// The relative ordering of lines within a file is maintained.
34func 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
52func getErrtraceDir() string {
53 _, file, _, _ := runtime.Caller(0)

Callers 8

Example_getCallerFunction · 0.92
Example_treeFunction · 0.92
Example_traceFunction · 0.92
ExampleUnwrapFrameFunction · 0.92
newExampleLoggerFunction · 0.92
Example_httpFunction · 0.92
Example_httpFunction · 0.92
TestClean_RealTraceFunction · 0.85

Calls 3

getErrtraceDirFunction · 0.85
AddMethod · 0.80
ReplacementsMethod · 0.80

Tested by 8

Example_getCallerFunction · 0.74
Example_treeFunction · 0.74
Example_traceFunction · 0.74
ExampleUnwrapFrameFunction · 0.74
newExampleLoggerFunction · 0.74
Example_httpFunction · 0.74
Example_httpFunction · 0.74
TestClean_RealTraceFunction · 0.68