MCPcopy Create free account
hub / github.com/conforma/cli / MatchSnapshot

Function MatchSnapshot

acceptance/snaps/snaps.go:98–173  ·  view source on GitHub ↗
(ctx context.Context, qualifier, text string, vars map[string]string)

Source from the content-addressed store, hash-verified

96}
97
98func MatchSnapshot(ctx context.Context, qualifier, text string, vars map[string]string) error {
99 errs := capture(ctx, qualifier)
100
101 // snaps normalizes, but again reports this as a diff
102 text = strings.ReplaceAll(text, "\r", "\\r")
103
104 for k, v := range vars {
105 text = strings.ReplaceAll(text, v, "${"+k+"}")
106 // in case the value was quoted, so doubly-escaped
107 text = strings.ReplaceAll(text, strconv.Quote(v), `"${`+k+`}"`)
108 }
109
110 // replace any remaining timestamps
111 text = timestampRegex.ReplaceAllString(text, "$${TIMESTAMP}")
112
113 // replace any log timestamps
114 text = logTimestampRegex.ReplaceAllString(text, "$${TIMESTAMP}")
115
116 // replace any effective time timestamps
117 text = effectiveTimeRegex.ReplaceAllString(text, "$${TIMESTAMP}")
118
119 // more timestamps, Unix here
120 text = unixTimestamp.ReplaceAllString(text, "$1$${TIMESTAMP}$2")
121
122 // Tekton timestamps
123 text = tektonTimestamp.ReplaceAllString(text, "$${TIMESTAMP}")
124
125 // handle temp directories, replace local temp path with "${TEMP}"
126 text = strings.ReplaceAll(text, os.TempDir(), "${TEMP}")
127
128 // find all ${TEMP}/dir1/dir2/.../file.ext paths
129 submatches := tempPathRegex.FindAllStringSubmatch(text, -1)
130 for _, submatch := range submatches {
131 if len(submatch) == 1 {
132 continue
133 }
134
135 path := submatch[1]
136
137 parts := strings.Split(path, string(os.PathSeparator))
138
139 for i, part := range parts {
140 parts[i] = randomBitsRegex.ReplaceAllString(part, "$${RANDOM}")
141 }
142
143 // here we want `/` so that snapshots don't differentiate between
144 // systems
145 text = strings.ReplaceAll(text, path, strings.Join(parts, "/"))
146 }
147
148 wd, err := os.Getwd()
149 if err != nil {
150 return err
151 }
152
153 scenario := ctx.Value(testenv.Scenario).(*godog.Scenario)
154 snapshot := strings.TrimSuffix(filepath.Base(scenario.Uri), filepath.Ext(scenario.Uri))
155

Callers 4

matchSnapshotFunction · 0.92
matchFileSnapshotFunction · 0.92

Calls 1

captureFunction · 0.85

Tested by

no test coverage detected