mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
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.
65 lines
1.6 KiB
Go
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)
|
|
}
|