@@ -222,21 +222,20 @@ func GetNonEmptyLines(output string) []string {
222
222
func GetProjectDir() (string, error) {
223
223
wd, err := os.Getwd()
224
224
if err != nil {
225
- return wd, err
225
+ return wd, fmt.Errorf("failed to get current working directory: %w", err)
226
226
}
227
227
wd = strings.Replace(wd, "/test/e2e", "", -1)
228
228
return wd, nil
229
229
}
230
230
231
-
232
231
// UncommentCode searches for target in the file and remove the comment prefix
233
232
// of the target content. The target content may span multiple lines.
234
233
func UncommentCode(filename, target, prefix string) error {
235
234
// false positive
236
235
// nolint:gosec
237
236
content, err := os.ReadFile(filename)
238
237
if err != nil {
239
- return err
238
+ return fmt.Errorf("failed to read file %q: %w", filename, err)
240
239
}
241
240
strContent := string(content)
242
241
@@ -248,33 +247,36 @@ func UncommentCode(filename, target, prefix string) error {
248
247
out := new(bytes.Buffer)
249
248
_, err = out.Write(content[:idx])
250
249
if err != nil {
251
- return err
250
+ return fmt.Errorf("failed to write to output: %w", err)
252
251
}
253
252
254
253
scanner := bufio.NewScanner(bytes.NewBufferString(target))
255
254
if !scanner.Scan() {
256
255
return nil
257
256
}
258
257
for {
259
- _, err := out.WriteString(strings.TrimPrefix(scanner.Text(), prefix))
260
- if err != nil {
261
- return err
258
+ if _, err = out.WriteString(strings.TrimPrefix(scanner.Text(), prefix)); err != nil {
259
+ return fmt.Errorf("failed to write to output: %w", err)
262
260
}
263
261
// Avoid writing a newline in case the previous line was the last in target.
264
262
if !scanner.Scan() {
265
263
break
266
264
}
267
- if _, err : = out.WriteString("\n"); err != nil {
268
- return err
265
+ if _, err = out.WriteString("\n"); err != nil {
266
+ return fmt.Errorf("failed to write to output: %w", err)
269
267
}
270
268
}
271
269
272
- _, err = out.Write(content[idx+len(target):])
273
- if err != nil {
274
- return err
270
+ if _, err = out.Write(content[idx+len(target):]); err != nil {
271
+ return fmt.Errorf("failed to write to output: %w", err)
275
272
}
273
+
276
274
// false positive
277
275
// nolint:gosec
278
- return os.WriteFile(filename, out.Bytes(), 0644)
276
+ if err = os.WriteFile(filename, out.Bytes(), 0644); err != nil {
277
+ return fmt.Errorf("failed to write file %q: %w", filename, err)
278
+ }
279
+
280
+ return nil
279
281
}
280
282
`
0 commit comments