MCPcopy Index your code
hub / github.com/bracesdev/errtrace / TestFormatAuto

Function TestFormatAuto

cmd/errtrace/main_test.go:398–506  ·  view source on GitHub ↗

-format=auto should format the file if used with -w, and not format the file if used without -w.

(t *testing.T)

Source from the content-addressed store, hash-verified

396// -format=auto should format the file if used with -w,
397// and not format the file if used without -w.
398func TestFormatAuto(t *testing.T) {
399 give := strings.Join([]string{
400 "package foo",
401 `import "errors"`,
402 "func foo() error {",
403 ` return errors.New("foo")`,
404 "}",
405 }, "\n")
406
407 wantUnformatted := strings.Join([]string{
408 "package foo",
409 `import "errors"; import "braces.dev/errtrace"`,
410 "func foo() error {",
411 ` return errtrace.Wrap(errors.New("foo"))`,
412 "}",
413 }, "\n")
414
415 wantFormatted := strings.Join([]string{
416 "package foo",
417 "",
418 `import "errors"`,
419 `import "braces.dev/errtrace"`,
420 "",
421 "func foo() error {",
422 ` return errtrace.Wrap(errors.New("foo"))`,
423 "}",
424 "",
425 }, "\n")
426
427 t.Run("write", func(t *testing.T) {
428 srcPath := filepath.Join(t.TempDir(), "src.go")
429 if err := os.WriteFile(srcPath, []byte(give), 0o600); err != nil {
430 t.Fatal(err)
431 }
432
433 exitCode := (&mainCmd{
434 Stdout: testWriter{t},
435 Stderr: testWriter{t},
436 }).Run([]string{"-w", srcPath})
437 if want := 0; exitCode != want {
438 t.Errorf("exit code = %d, want %d", exitCode, want)
439 }
440
441 bs, err := os.ReadFile(srcPath)
442 if err != nil {
443 t.Fatal(err)
444 }
445
446 if want, got := wantFormatted, string(bs); got != want {
447 t.Errorf("got:\n%s\nwant:\n%s\ndiff:\n%s", indent(got), indent(want), indent(diff.Lines(want, got)))
448 }
449 })
450
451 t.Run("stdout", func(t *testing.T) {
452 srcPath := filepath.Join(t.TempDir(), "src.go")
453 if err := os.WriteFile(srcPath, []byte(give), 0o600); err != nil {
454 t.Fatal(err)
455 }

Callers

nothing calls this directly

Calls 4

LinesFunction · 0.92
indentFunction · 0.85
RunMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected