Skip to content

Commit 2a3f9d9

Browse files
committed
🌱 (chore): consistently wrap and enrich error messages across test utils
It standardizes error handling and improves traceability.
1 parent fa1d379 commit 2a3f9d9

File tree

7 files changed

+105
-85
lines changed
  • docs/book/src
    • cronjob-tutorial/testdata/project/test/utils
    • getting-started/testdata/project/test/utils
    • multiversion-tutorial/testdata/project/test/utils
  • pkg/plugins/golang/v4/scaffolds/internal/templates/test/utils
  • testdata

7 files changed

+105
-85
lines changed

docs/book/src/cronjob-tutorial/testdata/project/test/utils/utils.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func GetNonEmptyLines(output string) []string {
195195
func GetProjectDir() (string, error) {
196196
wd, err := os.Getwd()
197197
if err != nil {
198-
return wd, err
198+
return wd, fmt.Errorf("failed to get current working directory: %w", err)
199199
}
200200
wd = strings.Replace(wd, "/test/e2e", "", -1)
201201
return wd, nil
@@ -208,7 +208,7 @@ func UncommentCode(filename, target, prefix string) error {
208208
// nolint:gosec
209209
content, err := os.ReadFile(filename)
210210
if err != nil {
211-
return err
211+
return fmt.Errorf("failed to read file %q: %w", filename, err)
212212
}
213213
strContent := string(content)
214214

@@ -220,32 +220,35 @@ func UncommentCode(filename, target, prefix string) error {
220220
out := new(bytes.Buffer)
221221
_, err = out.Write(content[:idx])
222222
if err != nil {
223-
return err
223+
return fmt.Errorf("failed to write to output: %w", err)
224224
}
225225

226226
scanner := bufio.NewScanner(bytes.NewBufferString(target))
227227
if !scanner.Scan() {
228228
return nil
229229
}
230230
for {
231-
_, err := out.WriteString(strings.TrimPrefix(scanner.Text(), prefix))
232-
if err != nil {
233-
return err
231+
if _, err = out.WriteString(strings.TrimPrefix(scanner.Text(), prefix)); err != nil {
232+
return fmt.Errorf("failed to write to output: %w", err)
234233
}
235234
// Avoid writing a newline in case the previous line was the last in target.
236235
if !scanner.Scan() {
237236
break
238237
}
239-
if _, err := out.WriteString("\n"); err != nil {
240-
return err
238+
if _, err = out.WriteString("\n"); err != nil {
239+
return fmt.Errorf("failed to write to output: %w", err)
241240
}
242241
}
243242

244-
_, err = out.Write(content[idx+len(target):])
245-
if err != nil {
246-
return err
243+
if _, err = out.Write(content[idx+len(target):]); err != nil {
244+
return fmt.Errorf("failed to write to output: %w", err)
247245
}
246+
248247
// false positive
249248
// nolint:gosec
250-
return os.WriteFile(filename, out.Bytes(), 0644)
249+
if err = os.WriteFile(filename, out.Bytes(), 0644); err != nil {
250+
return fmt.Errorf("failed to write file %q: %w", filename, err)
251+
}
252+
253+
return nil
251254
}

docs/book/src/getting-started/testdata/project/test/utils/utils.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func GetNonEmptyLines(output string) []string {
195195
func GetProjectDir() (string, error) {
196196
wd, err := os.Getwd()
197197
if err != nil {
198-
return wd, err
198+
return wd, fmt.Errorf("failed to get current working directory: %w", err)
199199
}
200200
wd = strings.Replace(wd, "/test/e2e", "", -1)
201201
return wd, nil
@@ -208,7 +208,7 @@ func UncommentCode(filename, target, prefix string) error {
208208
// nolint:gosec
209209
content, err := os.ReadFile(filename)
210210
if err != nil {
211-
return err
211+
return fmt.Errorf("failed to read file %q: %w", filename, err)
212212
}
213213
strContent := string(content)
214214

@@ -220,32 +220,35 @@ func UncommentCode(filename, target, prefix string) error {
220220
out := new(bytes.Buffer)
221221
_, err = out.Write(content[:idx])
222222
if err != nil {
223-
return err
223+
return fmt.Errorf("failed to write to output: %w", err)
224224
}
225225

226226
scanner := bufio.NewScanner(bytes.NewBufferString(target))
227227
if !scanner.Scan() {
228228
return nil
229229
}
230230
for {
231-
_, err := out.WriteString(strings.TrimPrefix(scanner.Text(), prefix))
232-
if err != nil {
233-
return err
231+
if _, err = out.WriteString(strings.TrimPrefix(scanner.Text(), prefix)); err != nil {
232+
return fmt.Errorf("failed to write to output: %w", err)
234233
}
235234
// Avoid writing a newline in case the previous line was the last in target.
236235
if !scanner.Scan() {
237236
break
238237
}
239-
if _, err := out.WriteString("\n"); err != nil {
240-
return err
238+
if _, err = out.WriteString("\n"); err != nil {
239+
return fmt.Errorf("failed to write to output: %w", err)
241240
}
242241
}
243242

244-
_, err = out.Write(content[idx+len(target):])
245-
if err != nil {
246-
return err
243+
if _, err = out.Write(content[idx+len(target):]); err != nil {
244+
return fmt.Errorf("failed to write to output: %w", err)
247245
}
246+
248247
// false positive
249248
// nolint:gosec
250-
return os.WriteFile(filename, out.Bytes(), 0644)
249+
if err = os.WriteFile(filename, out.Bytes(), 0644); err != nil {
250+
return fmt.Errorf("failed to write file %q: %w", filename, err)
251+
}
252+
253+
return nil
251254
}

docs/book/src/multiversion-tutorial/testdata/project/test/utils/utils.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func GetNonEmptyLines(output string) []string {
195195
func GetProjectDir() (string, error) {
196196
wd, err := os.Getwd()
197197
if err != nil {
198-
return wd, err
198+
return wd, fmt.Errorf("failed to get current working directory: %w", err)
199199
}
200200
wd = strings.Replace(wd, "/test/e2e", "", -1)
201201
return wd, nil
@@ -208,7 +208,7 @@ func UncommentCode(filename, target, prefix string) error {
208208
// nolint:gosec
209209
content, err := os.ReadFile(filename)
210210
if err != nil {
211-
return err
211+
return fmt.Errorf("failed to read file %q: %w", filename, err)
212212
}
213213
strContent := string(content)
214214

@@ -220,32 +220,35 @@ func UncommentCode(filename, target, prefix string) error {
220220
out := new(bytes.Buffer)
221221
_, err = out.Write(content[:idx])
222222
if err != nil {
223-
return err
223+
return fmt.Errorf("failed to write to output: %w", err)
224224
}
225225

226226
scanner := bufio.NewScanner(bytes.NewBufferString(target))
227227
if !scanner.Scan() {
228228
return nil
229229
}
230230
for {
231-
_, err := out.WriteString(strings.TrimPrefix(scanner.Text(), prefix))
232-
if err != nil {
233-
return err
231+
if _, err = out.WriteString(strings.TrimPrefix(scanner.Text(), prefix)); err != nil {
232+
return fmt.Errorf("failed to write to output: %w", err)
234233
}
235234
// Avoid writing a newline in case the previous line was the last in target.
236235
if !scanner.Scan() {
237236
break
238237
}
239-
if _, err := out.WriteString("\n"); err != nil {
240-
return err
238+
if _, err = out.WriteString("\n"); err != nil {
239+
return fmt.Errorf("failed to write to output: %w", err)
241240
}
242241
}
243242

244-
_, err = out.Write(content[idx+len(target):])
245-
if err != nil {
246-
return err
243+
if _, err = out.Write(content[idx+len(target):]); err != nil {
244+
return fmt.Errorf("failed to write to output: %w", err)
247245
}
246+
248247
// false positive
249248
// nolint:gosec
250-
return os.WriteFile(filename, out.Bytes(), 0644)
249+
if err = os.WriteFile(filename, out.Bytes(), 0644); err != nil {
250+
return fmt.Errorf("failed to write file %q: %w", filename, err)
251+
}
252+
253+
return nil
251254
}

pkg/plugins/golang/v4/scaffolds/internal/templates/test/utils/utils.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,20 @@ func GetNonEmptyLines(output string) []string {
222222
func GetProjectDir() (string, error) {
223223
wd, err := os.Getwd()
224224
if err != nil {
225-
return wd, err
225+
return wd, fmt.Errorf("failed to get current working directory: %w", err)
226226
}
227227
wd = strings.Replace(wd, "/test/e2e", "", -1)
228228
return wd, nil
229229
}
230230
231-
232231
// UncommentCode searches for target in the file and remove the comment prefix
233232
// of the target content. The target content may span multiple lines.
234233
func UncommentCode(filename, target, prefix string) error {
235234
// false positive
236235
// nolint:gosec
237236
content, err := os.ReadFile(filename)
238237
if err != nil {
239-
return err
238+
return fmt.Errorf("failed to read file %q: %w", filename, err)
240239
}
241240
strContent := string(content)
242241
@@ -248,33 +247,36 @@ func UncommentCode(filename, target, prefix string) error {
248247
out := new(bytes.Buffer)
249248
_, err = out.Write(content[:idx])
250249
if err != nil {
251-
return err
250+
return fmt.Errorf("failed to write to output: %w", err)
252251
}
253252
254253
scanner := bufio.NewScanner(bytes.NewBufferString(target))
255254
if !scanner.Scan() {
256255
return nil
257256
}
258257
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)
262260
}
263261
// Avoid writing a newline in case the previous line was the last in target.
264262
if !scanner.Scan() {
265263
break
266264
}
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)
269267
}
270268
}
271269
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)
275272
}
273+
276274
// false positive
277275
// 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
279281
}
280282
`

testdata/project-v4-multigroup/test/utils/utils.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func GetNonEmptyLines(output string) []string {
195195
func GetProjectDir() (string, error) {
196196
wd, err := os.Getwd()
197197
if err != nil {
198-
return wd, err
198+
return wd, fmt.Errorf("failed to get current working directory: %w", err)
199199
}
200200
wd = strings.Replace(wd, "/test/e2e", "", -1)
201201
return wd, nil
@@ -208,7 +208,7 @@ func UncommentCode(filename, target, prefix string) error {
208208
// nolint:gosec
209209
content, err := os.ReadFile(filename)
210210
if err != nil {
211-
return err
211+
return fmt.Errorf("failed to read file %q: %w", filename, err)
212212
}
213213
strContent := string(content)
214214

@@ -220,32 +220,35 @@ func UncommentCode(filename, target, prefix string) error {
220220
out := new(bytes.Buffer)
221221
_, err = out.Write(content[:idx])
222222
if err != nil {
223-
return err
223+
return fmt.Errorf("failed to write to output: %w", err)
224224
}
225225

226226
scanner := bufio.NewScanner(bytes.NewBufferString(target))
227227
if !scanner.Scan() {
228228
return nil
229229
}
230230
for {
231-
_, err := out.WriteString(strings.TrimPrefix(scanner.Text(), prefix))
232-
if err != nil {
233-
return err
231+
if _, err = out.WriteString(strings.TrimPrefix(scanner.Text(), prefix)); err != nil {
232+
return fmt.Errorf("failed to write to output: %w", err)
234233
}
235234
// Avoid writing a newline in case the previous line was the last in target.
236235
if !scanner.Scan() {
237236
break
238237
}
239-
if _, err := out.WriteString("\n"); err != nil {
240-
return err
238+
if _, err = out.WriteString("\n"); err != nil {
239+
return fmt.Errorf("failed to write to output: %w", err)
241240
}
242241
}
243242

244-
_, err = out.Write(content[idx+len(target):])
245-
if err != nil {
246-
return err
243+
if _, err = out.Write(content[idx+len(target):]); err != nil {
244+
return fmt.Errorf("failed to write to output: %w", err)
247245
}
246+
248247
// false positive
249248
// nolint:gosec
250-
return os.WriteFile(filename, out.Bytes(), 0644)
249+
if err = os.WriteFile(filename, out.Bytes(), 0644); err != nil {
250+
return fmt.Errorf("failed to write file %q: %w", filename, err)
251+
}
252+
253+
return nil
251254
}

testdata/project-v4-with-plugins/test/utils/utils.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func GetNonEmptyLines(output string) []string {
195195
func GetProjectDir() (string, error) {
196196
wd, err := os.Getwd()
197197
if err != nil {
198-
return wd, err
198+
return wd, fmt.Errorf("failed to get current working directory: %w", err)
199199
}
200200
wd = strings.Replace(wd, "/test/e2e", "", -1)
201201
return wd, nil
@@ -208,7 +208,7 @@ func UncommentCode(filename, target, prefix string) error {
208208
// nolint:gosec
209209
content, err := os.ReadFile(filename)
210210
if err != nil {
211-
return err
211+
return fmt.Errorf("failed to read file %q: %w", filename, err)
212212
}
213213
strContent := string(content)
214214

@@ -220,32 +220,35 @@ func UncommentCode(filename, target, prefix string) error {
220220
out := new(bytes.Buffer)
221221
_, err = out.Write(content[:idx])
222222
if err != nil {
223-
return err
223+
return fmt.Errorf("failed to write to output: %w", err)
224224
}
225225

226226
scanner := bufio.NewScanner(bytes.NewBufferString(target))
227227
if !scanner.Scan() {
228228
return nil
229229
}
230230
for {
231-
_, err := out.WriteString(strings.TrimPrefix(scanner.Text(), prefix))
232-
if err != nil {
233-
return err
231+
if _, err = out.WriteString(strings.TrimPrefix(scanner.Text(), prefix)); err != nil {
232+
return fmt.Errorf("failed to write to output: %w", err)
234233
}
235234
// Avoid writing a newline in case the previous line was the last in target.
236235
if !scanner.Scan() {
237236
break
238237
}
239-
if _, err := out.WriteString("\n"); err != nil {
240-
return err
238+
if _, err = out.WriteString("\n"); err != nil {
239+
return fmt.Errorf("failed to write to output: %w", err)
241240
}
242241
}
243242

244-
_, err = out.Write(content[idx+len(target):])
245-
if err != nil {
246-
return err
243+
if _, err = out.Write(content[idx+len(target):]); err != nil {
244+
return fmt.Errorf("failed to write to output: %w", err)
247245
}
246+
248247
// false positive
249248
// nolint:gosec
250-
return os.WriteFile(filename, out.Bytes(), 0644)
249+
if err = os.WriteFile(filename, out.Bytes(), 0644); err != nil {
250+
return fmt.Errorf("failed to write file %q: %w", filename, err)
251+
}
252+
253+
return nil
251254
}

0 commit comments

Comments
 (0)