| 506 | } |
| 507 | |
| 508 | func TestListFlag(t *testing.T) { |
| 509 | uninstrumentedSource := strings.Join([]string{ |
| 510 | "package foo", |
| 511 | `import "errors"`, |
| 512 | "func foo() error {", |
| 513 | ` return errors.New("foo")`, |
| 514 | "}", |
| 515 | }, "\n") |
| 516 | |
| 517 | instrumentedSource := strings.Join([]string{ |
| 518 | "package foo", |
| 519 | `import "errors"; import "braces.dev/errtrace"`, |
| 520 | "func foo() error {", |
| 521 | ` return errtrace.Wrap(errors.New("foo"))`, |
| 522 | "}", |
| 523 | }, "\n") |
| 524 | |
| 525 | dir := t.TempDir() |
| 526 | |
| 527 | instrumented := filepath.Join(dir, "instrumented.go") |
| 528 | if err := os.WriteFile(instrumented, []byte(instrumentedSource), 0o600); err != nil { |
| 529 | t.Fatal(err) |
| 530 | } |
| 531 | |
| 532 | uninstrumented := filepath.Join(dir, "uninstrumented.go") |
| 533 | if err := os.WriteFile(uninstrumented, []byte(uninstrumentedSource), 0o600); err != nil { |
| 534 | t.Fatal(err) |
| 535 | } |
| 536 | |
| 537 | var out bytes.Buffer |
| 538 | exitCode := (&mainCmd{ |
| 539 | Stdout: &out, |
| 540 | Stderr: testWriter{t}, |
| 541 | }).Run([]string{"-l", uninstrumented, instrumented}) |
| 542 | if want := 0; exitCode != want { |
| 543 | t.Errorf("exit code = %d, want %d", exitCode, want) |
| 544 | } |
| 545 | |
| 546 | // Only the uninstrumented file should be listed. |
| 547 | if want, got := uninstrumented+"\n", out.String(); got != want { |
| 548 | t.Errorf("got:\n%s\nwant:\n%s\ndiff:\n%s", indent(got), indent(want), indent(diff.Lines(want, got))) |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | func TestOptoutLines(t *testing.T) { |
| 553 | fset := token.NewFileSet() |