package main import ( "bytes" "context" "os" "os/exec" "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/moond4rk/hackbrowserdata/internal/browser/browsertest" ) func TestStoppedRunPrintsNoErrorOrUsage(t *testing.T) { home := t.TempDir() setHome(t, home) browsertest.WriteChromiumUserDataDir(t, userDataDirOf(t, home, "chrome"), "chrome") for _, testCase := range []struct { name, browser string stop bool }{ {name: "stopped", browser: "chrome", stop: true}, {name: "failed", browser: "nosuch"}, } { testCase := testCase t.Run(testCase.name, func(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() if testCase.stop { cancel() } var screen bytes.Buffer root := newRootCommand() root.SetOut(&screen) root.SetErr(&screen) root.SetArgs([]string{"-b", testCase.browser, "-d", filepath.Join(t.TempDir(), "results")}) err := root.ExecuteContext(ctx) require.Error(t, err) if testCase.stop { require.ErrorIs(t, err, context.Canceled) assert.Empty(t, screen.String()) } else { assert.Contains(t, screen.String(), "Error: ") } }) } } func TestExitByEndsTheProcessAsTheSignalWould(t *testing.T) { if os.Getenv("HBD_TEST_EXIT_BY") == "1" { exitBy(os.Interrupt) return } command := exec.Command(os.Args[0], "-test.run=^TestExitByEndsTheProcessAsTheSignalWould$") command.Env = append(os.Environ(), "HBD_TEST_EXIT_BY=1") var exitErr *exec.ExitError require.ErrorAs(t, command.Run(), &exitErr) assertEndedByInterrupt(t, exitErr.ProcessState) }