logExecution logs the details of the execution and offers hits as how to troubleshoot test failures by using persistent environment
(ctx context.Context)
| 719 | // logExecution logs the details of the execution and offers hits as how to |
| 720 | // troubleshoot test failures by using persistent environment |
| 721 | func logExecution(ctx context.Context) { |
| 722 | s, err := ecStatusFrom(ctx) |
| 723 | if err != nil { |
| 724 | return // the ec wasn't invoked no status was stored |
| 725 | } |
| 726 | |
| 727 | noColors := testenv.NoColorOutput(ctx) |
| 728 | if c.SUPPORT_COLOR != !noColors { |
| 729 | c.SUPPORT_COLOR = !noColors |
| 730 | } |
| 731 | |
| 732 | verbose, _ := ctx.Value(testenv.VerboseOutput).(bool) |
| 733 | if verbose { |
| 734 | output := &strings.Builder{} |
| 735 | outputSegment := func(name string, v any) { |
| 736 | output.WriteString("\n\n") |
| 737 | output.WriteString(c.Underline(c.Bold(name))) |
| 738 | output.WriteString(fmt.Sprintf("\n%v", v)) |
| 739 | } |
| 740 | |
| 741 | outputSegment("Command", s.Cmd) |
| 742 | outputSegment("State", fmt.Sprintf("Exit code: %d\nPid: %d", s.ProcessState.ExitCode(), s.ProcessState.Pid())) |
| 743 | outputSegment("Environment", strings.Join(s.Env, "\n")) |
| 744 | var varsStr []string |
| 745 | for k, v := range s.vars { |
| 746 | varsStr = append(varsStr, fmt.Sprintf("%s=%s", k, v)) |
| 747 | } |
| 748 | outputSegment("Variables", strings.Join(varsStr, "\n")) |
| 749 | if s.stdout.Len() == 0 { |
| 750 | outputSegment("Stdout", c.Italic("* No standard output")) |
| 751 | } else { |
| 752 | outputSegment("Stdout", c.Green(s.stdout.String())) |
| 753 | } |
| 754 | if s.stderr.Len() == 0 { |
| 755 | outputSegment("Stderr", c.Italic("* No standard error")) |
| 756 | } else { |
| 757 | outputSegment("Stderr", c.Red(s.stderr.String())) |
| 758 | } |
| 759 | fmt.Print(output.String()) |
| 760 | } |
| 761 | |
| 762 | if testenv.Persisted(ctx) { |
| 763 | var environment []string |
| 764 | for _, e := range s.Env { |
| 765 | if strings.HasPrefix(e, "KUBECONFIG=") || strings.HasPrefix(e, "SIGSTORE_") { |
| 766 | environment = append(environment, e) |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | fmt.Printf("\n%s: The test environment is persisted, to recreate the failure run:\n%s %s\n\n", |
| 771 | c.Bold("NOTE"), strings.Join(environment, " "), strings.Join(s.Cmd.Args, " ")) |
| 772 | } else { |
| 773 | fmt.Printf("\n%s: To recreate the failure re-run the test with `-args -persist` to persist the stubbed environment, or `-args -verbose` for detailed execution output\n\n", c.Bold("HINT")) |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | func matchSnapshot(ctx context.Context) error { |
| 778 | status, err := ecStatusFrom(ctx) |
no test coverage detected