Files
HackBrowserData/cmd/hack-browser-data/main_test.go
T
Roger 9e9d1c531d refactor: rename identifiers and expand the browser table (#685)
Names say what each thing is: no package stutter, one word per concept, spelled out, none that differ only in case.
The browser table puts one field per line. Wire names (flags, output fields, keys.json) are unchanged.
2026-11-09 17:25:35 +08:00

65 lines
1.6 KiB
Go

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)
}