Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/test_loopguard_js_package_managers.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 31 additions & 2 deletions tokeneconomizer/loopguard/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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|"
Expand All @@ -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`
Expand Down