Files
HackBrowserData/cmd/hack-browser-data/main_test.go
T
Roger 3e9150249c refactor: rename core types and spell out every name (#677)
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent.
varnamelen and staticcheck ST1020-22 now keep both from coming back.
2026-11-02 11:06:05 +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.ChromiumUserData(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 := rootCmd()
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)
}