2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2017-12-17 13:20:03 +08:00
|
|
|
|
2020-09-03 13:48:39 +08:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-06-24 03:22:48 +08:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
. "src.elv.sh/pkg/eval"
|
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
2022-06-24 03:22:48 +08:00
|
|
|
"src.elv.sh/pkg/testutil"
|
2020-09-03 13:48:39 +08:00
|
|
|
)
|
2017-12-22 04:49:14 +08:00
|
|
|
|
2022-06-24 03:22:48 +08:00
|
|
|
func TestExit(t *testing.T) {
|
|
|
|
var exitCodes []int
|
|
|
|
testutil.Set(t, OSExit, func(i int) { exitCodes = append(exitCodes, i) })
|
|
|
|
|
|
|
|
Test(t,
|
|
|
|
That("exit").DoesNothing(),
|
|
|
|
That("exit 1").DoesNothing(),
|
|
|
|
That("exit 1 2").Throws(
|
|
|
|
errs.ArityMismatch{What: "arguments",
|
|
|
|
ValidLow: 0, ValidHigh: 1, Actual: 2},
|
|
|
|
"exit 1 2"),
|
|
|
|
)
|
|
|
|
|
|
|
|
if diff := cmp.Diff([]int{0, 1}, exitCodes); diff != "" {
|
|
|
|
t.Errorf("got unexpected exit codes (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExit_RunsPreExit(t *testing.T) {
|
|
|
|
testutil.Set(t, OSExit, func(int) {})
|
|
|
|
|
|
|
|
calls := 0
|
|
|
|
setup := func(ev *Evaler) {
|
|
|
|
ev.PreExitHooks = append(ev.PreExitHooks, func() { calls++ })
|
|
|
|
}
|
|
|
|
|
2023-07-23 06:04:18 +08:00
|
|
|
TestWithEvalerSetup(t, setup,
|
2022-06-24 03:22:48 +08:00
|
|
|
That("exit").DoesNothing())
|
|
|
|
|
|
|
|
if calls != 1 {
|
|
|
|
t.Errorf("pre-exit hook called %v times, want 1", calls)
|
|
|
|
}
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|