From 25fe6a06b04c355b5594b505d8dfe59ce55579a8 Mon Sep 17 00:00:00 2001 From: Yohe-Am <56622350+Yohe-Am@users.noreply.github.com> Date: Sat, 16 Nov 2024 13:00:19 +0300 Subject: [PATCH] fix: tricky tests --- install/hook.fish | 10 ++-- tests/reloadHooks.ts | 115 +++++++++++++++++++++++++------------------ 2 files changed, 71 insertions(+), 54 deletions(-) diff --git a/install/hook.fish b/install/hook.fish index 285920d..916890c 100644 --- a/install/hook.fish +++ b/install/hook.fish @@ -114,11 +114,11 @@ function __ghjk_preexec --on-event fish_preexec end end -if status is-interactive; or begin; - set --query GHJK_AUTO_HOOK; - and test $GHJK_AUTO_HOOK != "0"; + +if set --query GHJK_AUTO_HOOK; and begin; + test $GHJK_AUTO_HOOK != "0"; and test $GHJK_AUTO_HOOK != "false"; - and test $GHJK_AUTO_HOOK != "0" -end; + and test $GHJK_AUTO_HOOK != "" +end; or status is-interactive; ghjk_hook end diff --git a/tests/reloadHooks.ts b/tests/reloadHooks.ts index f4ae9c5..fda6298 100644 --- a/tests/reloadHooks.ts +++ b/tests/reloadHooks.ts @@ -1,7 +1,7 @@ import "../setup_logger.ts"; import { E2eTestCase, genTsGhjkFile, harness } from "./utils.ts"; import dummy from "../ports/dummy.ts"; -import type { InstallConfigFat } from "../port.ts"; +import { type InstallConfigFat } from "../port.ts"; // TODO: test for hook reload when ghjk.ts is touched // TODO: test for hook reload when nextfile is touched @@ -19,7 +19,7 @@ pushd ../ # it shouldn't be avail here set +ex [ $(dummy) ] && exit 102 -[ "\${DUMMY_ENV:-}" = "dummy" ] && exit 103 +[ "\${DUMMY_ENV:-}" = "old_dummy" ] || exit 103 set -ex # cd back in @@ -38,15 +38,8 @@ echo "test" > $GHJK_NEXTFILE const posixNonInteractiveScript = ` set -eux -# test that ghjk_hook doesn't run by default on non-interactive shells -# [ "\${DUMMY_ENV:-}" = "dummy" ] && exit 1011 - -# prepare DUMMY_ENV for restore check - -DUMMY_ENV=old_dummy - -# test that ghjk_hook is avail because BASH_ENV exposed by the suite -ghjk_hook +# items should be avail on new shell +# either due to auto hook and BASH_ENV/ZDOTDIR [ "\${DUMMY_ENV:-}" = "dummy" ] || exit 101 dummy @@ -87,15 +80,21 @@ GHJK_ENV=test ghjk_hook [ "\${GHJK_ENV:-}" = "test" ] || exit 112 `; -const fishNonInteractiveScript = ` -set fish_trace 1 +// assumes BASH_ENV/ZDOTDIR +const posixNonInteractiveScriptNoHook = `set -eux # test that ghjk_hook doesn't run by default on non-interactive shells -test $DUMMY_ENV = "dummy"; and exit 1011 - -set DUMMY_ENV old_dummy +[ $(set +e; dummy) ] && exit 1021 +# [ "\${DUMMY_ENV:-}" = "dummy" ] && exit 1011 -# test that ghjk_hook is avail because config.fish exposed by the suite +# test that ghjk_hook is avail because BASH_ENV exposed by the suite ghjk_hook +` + posixNonInteractiveScript; + +const fishNonInteractiveScript = ` +set fish_trace 1 + +# items should be avail on new shell +# either due to auto hook or custom code down below which dummy; or exit 101 test $DUMMY_ENV = "dummy"; or exit 102 @@ -145,12 +144,25 @@ test "$GHJK_ENV" = "main"; or exit 111 GHJK_ENV=test ghjk_hook test "$GHJK_ENV" = "test"; or exit 112`; +const fishNonInteractiveScriptNoHook = ` +set fish_trace 1 + +# test that ghjk_hook doesn't run by default on non-interactive shells +which dummy; and exit 1030 +test $DUMMY_ENV = "dummy"; and exit 1011 + +# test that ghjk_hook is avail because config.fish exposed by the suite +# simulate auto hook so that we can re-use test +ghjk_hook +` + fishNonInteractiveScript; + // simulate interactive mode by emitting postexec after each line // after each line. postexec isn't emitted by default on interactive // fish shells const fishInteractiveScript = ` set fish_trace 1 which dummy; or exit 101 +env test $DUMMY_ENV = "dummy"; or exit 102 # it should be avail in subshells @@ -181,9 +193,9 @@ GHJK_ENV=test ghjk_hook test "$GHJK_ENV" = "test"; or exit 112 ` .split("\n") - .map((line) => [ + .flatMap((line) => [ line, - `emit fish_preexec`, + `emit fish_preexec;`, ]) .join("\n"); @@ -202,78 +214,83 @@ const bashInteractiveEpoint = Deno.env.get("GHJK_TEST_E2E_TYPE") == "local" const cases: CustomE2eTestCase[] = [ { - name: "bash_interactive_with_auto_hook", + name: "bash_interactive", ePoint: bashInteractiveEpoint, stdin: posixInteractiveScript, + envVars: { + DUMMY_ENV: "old_dummy", + }, }, { - name: "bash_interactive", - ePoint: bashInteractiveEpoint, - stdin: ` -[ $(dummy) ] && exit 1020 -[ "\${DUMMY_ENV:-}" = "dummy" ] && exit 1030 -ghjk_hook` + posixInteractiveScript, + name: "bash_scripting", + ePoint: `bash -s`, + stdin: posixNonInteractiveScriptNoHook, envVars: { GHJK_AUTO_HOOK: "0", + DUMMY_ENV: "old_dummy", }, }, { - name: "bash_scripting", + // assumes BASH_ENV or ZDOTDIR are set + // for ghjk with GHJK_AUTO_HOOK set to 1 + name: "bash_scripting_with_auto_hook", ePoint: `bash -s`, stdin: posixNonInteractiveScript, envVars: { - GHJK_AUTO_HOOK: "0", + DUMMY_ENV: "old_dummy", }, }, { - name: "zsh_interactive_with_auto_hook", + name: "zsh_interactive", ePoint: `zsh -sli`, stdin: posixInteractiveScript .split("\n").filter((line) => !/^#/.test(line)).join("\n"), + envVars: { + DUMMY_ENV: "old_dummy", + }, }, { - name: "zsh_interactive", - ePoint: `zsh -sli`, - stdin: ` -[ $(dummy) ] && exit 1020 -[ "\${DUMMY_ENV:-}" = "dummy" ] && exit 1030 -ghjk_hook` + posixInteractiveScript, + name: "zsh_scripting", + ePoint: `zsh -s`, + stdin: posixNonInteractiveScriptNoHook, envVars: { GHJK_AUTO_HOOK: "0", + DUMMY_ENV: "old_dummy", }, }, { - name: "zsh_scripting", + // assumes BASH_ENV or ZDOTDIR are set + // for ghjk with GHJK_AUTO_HOOK set to 1 + name: "zsh_scripting_with_auto_hook", ePoint: `zsh -s`, stdin: posixNonInteractiveScript, envVars: { - GHJK_AUTO_HOOK: "0", + DUMMY_ENV: "old_dummy", }, }, { - name: "fish_interactive_with_auto_hook", + name: "fish_interactive", ePoint: `fish -il`, - stdin: `set DUMMY_ENV old_dummy\n` + - fishInteractiveScript, + stdin: fishInteractiveScript, + envVars: { + DUMMY_ENV: "old_dummy", + }, }, { - name: "fish_interactive", - ePoint: `fish -il`, - stdin: `set DUMMY_ENV old_dummy -which dummy; and exit 1030 -test $DUMMY_ENV = "old_dummy"; or exit 1040 -ghjk_hook -` + fishInteractiveScript, + name: "fish_scripting", + ePoint: `fish`, + stdin: fishNonInteractiveScriptNoHook, envVars: { GHJK_AUTO_HOOK: "0", + DUMMY_ENV: "old_dummy", }, }, { - name: "fish_scripting", + name: "fish_scripting_with_auto_hook", ePoint: `fish`, stdin: fishNonInteractiveScript, envVars: { - GHJK_AUTO_HOOK: "0", + DUMMY_ENV: "old_dummy", }, }, ];