From 2f7d2dab80d1465561db8e67b7f287abd145a18b Mon Sep 17 00:00:00 2001 From: Rithindatta Gundu Date: Wed, 25 Nov 2026 16:22:03 -0800 Subject: [PATCH] loopguard: count npm, pnpm and yarn test runs past their options and aliases pnpm --filter web test, npm -w web test, yarn workspace web test, npm t and pnpm vitest run were not counted as test runs, so a failing one after the last edit left the turn green and fed the envelope. --- tests/test_loopguard_js_package_managers.py | 62 +++++++++++++++++++++ tokeneconomizer/loopguard/turn.py | 33 ++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 tests/test_loopguard_js_package_managers.py diff --git a/tests/test_loopguard_js_package_managers.py b/tests/test_loopguard_js_package_managers.py new file mode 100644 index 0000000..52540eb --- /dev/null +++ b/tests/test_loopguard_js_package_managers.py @@ -0,0 +1,62 @@ +"""A JavaScript package manager running the tests past its own options is a test run. + +The loop guard read `npm test`, `pnpm test` and `yarn test` only right after the package manager. +A monorepo names the workspace first (`pnpm --filter web test`, `npm -w web test`, `pnpm -r test`, +`yarn --cwd web test`, `yarn workspace web test`), npm and pnpm also take `t` and `tst` for +`test`, and pnpm and yarn run an installed runner by its name (`pnpm vitest run`, `yarn jest`). +None of these was a verification, so a failing one after the last edit left the turn counted as +ended green. Each manager's options that take a value are read with it: npm's `-w` names a +workspace while pnpm's is `--workspace-root`, a flag.""" +import time + +import pytest + +from tokeneconomizer import loopguard as guard +from tokeneconomizer.loopguard import turn + + +@pytest.mark.parametrize("cmd", [ + "npm t", "npm tst", "pnpm t", "pnpm tst", + "pnpm --filter web test", "pnpm -F web test", "pnpm --filter=web test", "pnpm -r test", + "pnpm --recursive --if-present test", "pnpm -C web test", "pnpm --dir web run test", + "pnpm -w test", "pnpm --filter $PKG test", "pnpm --filter=${PKG} test", + "npm --prefix web test", "npm -C web test", "npm -w web test", "npm --workspace=web test", + "npm -ws test", "npm --workspaces --if-present run test", "npm -s test", + "npm --loglevel silent test", "npm -w web run-script test:unit", + "yarn --cwd web test", "yarn --cwd=web run test", "yarn workspace web test", + "yarn workspace @app/web run test", + "pnpm vitest", "pnpm vitest run", "pnpm jest --ci", "pnpm --filter web exec vitest run", + "pnpm playwright test", "yarn jest", "yarn vitest run", "yarn run mocha", + "yarn workspace web jest"]) +def test_a_package_manager_running_the_tests_counts_as_a_verification(cmd): + assert turn.status_of_tests(cmd) == "own" + g = guard.TurnGuard(turn_key="t") + g.fold("Edit", {"file_path": "/r/src/a.ts", "old_string": "x", "new_string": "y"}, "ok") + g.fold("Bash", {"command": cmd}, "FAIL src/a.test.ts > adds\nTests 1 failed (1)", 1) + assert g.verifications == 1 and not g.resolved_proxy() + + +@pytest.mark.parametrize("cmd", [ + "npm token list", "npm team ls org", "npm run t", "npm run tst", "pnpm tsc", + "npm test-build", "pnpm jest-cli", "yarn avatar", + "npm -w test", "pnpm -F test", "pnpm -C test build", "yarn --cwd test build", + "yarn workspace test", "npm -w test run build", "npm --workspace=test run build", + "npm --prefix test run build", "pnpm --filter test add vitest", + "yarn workspace web add jest", "pnpm --filter web build", "yarn add -D vitest", + "pnpm vitest --help", "yarn jest --listTests"]) +def test_other_package_manager_commands_are_not_test_runs(cmd): + """`npm -w test run build` builds the workspace named `test`: `-w` always takes a value.""" + assert turn.status_of_tests(cmd) is None + + +@pytest.mark.parametrize("cmd", [ + "npm" + " -w a -s --workspace b -ws -C c" * 12 + " run build", + "pnpm" + " -F a -r --filter=b --filter $(x) -C c" * 12 + " build", + "yarn" + " --cwd a -s --cwd=b --cwd ${y}" * 12 + " build"], ids=["npm", "pnpm", "yarn"]) +def test_a_long_line_of_options_that_runs_no_tests_is_read_quickly(cmd): + """An option is read one way only. Read two ways, a line of n options that ends in no test + run would backtrack through 2^n readings before the pattern gave up.""" + start = time.perf_counter() + assert turn.status_of_tests(cmd) is None + assert turn.status_of_tests(cmd.rsplit(" ", 1)[0] + " test") == "own" + assert time.perf_counter() - start < 1 diff --git a/tokeneconomizer/loopguard/turn.py b/tokeneconomizer/loopguard/turn.py index 670c8fa..c59d123 100644 --- a/tokeneconomizer/loopguard/turn.py +++ b/tokeneconomizer/loopguard/turn.py @@ -53,6 +53,34 @@ # A value of one of make's options or variables: a word, or a `$(...)`/`${...}` whose brackets # `_pieces` has turned into spaces (`-j$(nproc)` reads `-j$ nproc`). _MAKE_VALUE = r"(?:\$\s*\w+|[^\s\-]\S*)" + + +def _options(value): + """A command's own options before what it runs: one of `value` with the value it takes (a + word, or a `$(...)` read as `_MAKE_VALUE` reads one), any other as a flag, which may carry + its value after `=` (`--filter=web`). Case-sensitive. An option of `value` before a space is + read only that way, so a long line of options that runs no tests does not backtrack + exponentially.""" + return (r"(?-i:(?:\s+(?:" + value + r"\s+" + _MAKE_VALUE + r"|(?!" + value + r"\s)" + r"-[-\w]+(?:=(?:\$\s*\w+|\S*))?))*)") + + +# A JavaScript test runner, run by its name (`jest`, `npx vitest run`) or by a package manager +_JS_RUNNERS = r"(?:jest|vitest|mocha|ava|playwright\s+test)" +# A package manager running the test script, or a runner, past its own options (`npm -w web +# test`, `pnpm --filter web test`, `pnpm -r test`, `yarn --cwd web test`, `yarn workspace web +# test`), of which these take a value. Each has its own: npm's `-w` names a workspace, pnpm's is +# `--workspace-root`, a flag. `t` and `tst` are npm's and pnpm's aliases of `test`, and pnpm and +# yarn run an installed runner by its name (`pnpm vitest run`, `yarn jest`). +_NPM_TEST = (r"npm" + _options(r"(?:-[wC]|--(?:workspace|prefix|loglevel|script-shell|" + r"userconfig))") + + r"\s+(?:(?:run(?:-script)?\s+)?test|t|tst)") +_PNPM_TEST = (r"pnpm" + _options(r"(?:-[FC]|--(?:filter|filter-prod|dir|workspace-packages|" + r"env-file|reporter|loglevel|test-pattern|" + r"changed-files-ignore-pattern))") + + r"\s+(?:(?:run\s+)?test|t|tst|(?:exec\s+)?" + _JS_RUNNERS + r")") +_YARN_TEST = (r"yarn" + _options(r"--cwd") + r"(?:\s+workspace\s+" + _MAKE_VALUE + r")?" + r"\s+(?:run\s+)?(?:test|" + _JS_RUNNERS + r")") VERIFY_RE = re.compile( r"(?i)(?:^|[\s;&|(])(?:(?:~?/)?(?:[\w.\-]+/)*" + _PYTHON + r"\s+)?" r"(?:~?/)?(?:[\w.\-]+/)*(?:pytest|py\.test|tox|nox|" @@ -62,8 +90,9 @@ r"(?:-m\s+)?coverage\s+run(?:\s+(?:--(?:rcfile|source|omit|include|data-file|context|" r"concurrency|debug|save-signal)(?:=|\s+)\S+|-[-\w]+))*\s+" r"(?:-m\s+(?:pytest|unittest)|(?:[\w.\-]+/)*(?:manage\.py\s+test|runtests(?:\.py)?))|" - r"manage\.py\s+test|runtests(?:\.py)?|jest|vitest|mocha|ava|playwright\s+test|" - r"(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?test|go\s+test|cargo\s+(?:test|nextest)|" + r"manage\.py\s+test|runtests(?:\.py)?|" + _JS_RUNNERS + r"|" + + _NPM_TEST + r"|" + _PNPM_TEST + r"|" + _YARN_TEST + r"|" + r"bun\s+(?:run\s+)?test|go\s+test|cargo\s+(?:test|nextest)|" r"(?:mvn|mvnw|\./mvnw)" + _UNLESS_MVN_SKIPS_TESTS + r"\s+(?:\S+\s+)*test|" # also a Gradle subproject's test task by its path, `./gradlew :app:test`, but not an # option's value (`--exclude-task=:app:test`) or a task of a subproject named `test`