diff --git a/tests/test_generic_microskills.py b/tests/test_generic_microskills.py index 57b9fab1..5ded41cd 100644 --- a/tests/test_generic_microskills.py +++ b/tests/test_generic_microskills.py @@ -25,6 +25,16 @@ def test_the_generic_reading_knows_builds_and_other_ecosystems(): assert _form("go test ./...", GENERIC) == ("run_tests", "go test") assert _form("./gradlew test", GENERIC) == ("run_tests", "gradlew test") assert _form("bundle install", GENERIC) == ("package_tool", "bundle") + assert _form("zig test src/main.zig", GENERIC) == ("run_tests", "zig test") + assert _form("zig build test", GENERIC) == ("run_tests", "zig build test") + assert _form("zig build", GENERIC) == ("run_build", "zig build") + assert _form("meson test", GENERIC) == ("run_tests", "meson test") + assert _form("meson compile", GENERIC) == ("run_build", "meson compile") + assert _form("dart test", GENERIC) == ("run_tests", "dart test") + assert _form("flutter test", GENERIC) == ("run_tests", "flutter test") + assert _form("forge test", GENERIC) == ("run_tests", "forge test") + assert _form("ninja test", GENERIC) == ("run_tests", "ninja test") + assert _form("ninja check", GENERIC) == ("run_tests", "ninja check") # The experiments' vocabularies read as they did. assert _form("tsc -p .", PYTHON_GIT) is None and _form("npm run build", NODE_WEB) == ( "package_tool", "npm") diff --git a/tests/test_loopguard_polyglot_runners.py b/tests/test_loopguard_polyglot_runners.py new file mode 100644 index 00000000..11481061 --- /dev/null +++ b/tests/test_loopguard_polyglot_runners.py @@ -0,0 +1,47 @@ +"""Test runners for polyglot ecosystems (Zig, Meson, Dart, Flutter, Forge, Ninja) count as verifications. + +A test run in a Zig, Meson, Dart/Flutter, Foundry, or CMake/Ninja repository runs the project's +checks and exits with their status, so a failing one after the last edit must leave the turn +not green. +""" +import pytest + +from tokeneconomizer import loopguard as guard +from tokeneconomizer.loopguard import turn + + +@pytest.mark.parametrize("cmd, fail_out", [ + ("zig test src/main.zig", "1/5 test.a... FAIL (TestUnexpectedResult)"), + ("zig build test", "1/3 test.unit... FAIL"), + ("meson test", "Fail: 1"), + ("meson test -C build --verbose", "Fail: 2"), + ("dart test", "Some tests failed."), + ("flutter test", "Some tests failed."), + ("forge test", "[FAIL. Reason: assertion failed] test_transfer()"), + ("ninja test", "FAILED: test\nninja: build stopped: subcommand failed."), + ("ninja check", "FAILED: check\nninja: build stopped: subcommand failed."), + ("ninja -C build test", "FAILED: test\nninja: build stopped: subcommand failed."), + ("samu check", "FAILED: check\nsamu: subcommand failed."), +]) +def test_polyglot_test_runners_count_as_verifications(cmd, fail_out): + assert turn.status_of_tests(cmd) == "own" + g = guard.TurnGuard(turn_key="t") + g.fold("Edit", {"file_path": "/r/src/code.ext", "old_string": "x", "new_string": "y"}, "ok") + g.fold("Bash", {"command": cmd}, fail_out, 1) + assert g.verifications == 1 and not g.resolved_proxy() + + +@pytest.mark.parametrize("cmd", [ + ("zig build"), + ("zig fmt src/main.zig"), + ("meson setup build"), + ("meson compile -C build"), + ("dart format lib/"), + ("dart compile exe bin/main.dart"), + ("forge build"), + ("ninja"), + ("ninja all"), + ("ninja -C build"), +]) +def test_non_test_commands_are_not_verifications(cmd): + assert turn.status_of_tests(cmd) is None diff --git a/tests/test_observe_bun_deno.py b/tests/test_observe_bun_deno.py new file mode 100644 index 00000000..ebff31fc --- /dev/null +++ b/tests/test_observe_bun_deno.py @@ -0,0 +1,57 @@ +"""Commands executed with Bun, Deno, and Node --test are correctly observed. + +`bun test`, `bun run test:unit`, `deno test`, and `node --test` are test runs. +`bun run build`, `bun run lint`, `deno check`, and `deno lint` are builds/checks. +`bun add `, `bun install`, and `deno add ` are package/fetch operations. +""" +import pytest + +from tokeneconomizer.microskills import GENERIC, NODE_WEB, observe + + +def _seen(cmd, vocab=GENERIC): + return [(o.intent, o.form) for o in observe.classify_command(cmd, vocab=vocab)] + + +@pytest.mark.parametrize("cmd, form", [ + ("bun test", "bun test"), + ("bun test tests/auth.test.ts", "bun test"), + ("bun run test", "bun run test"), + ("bun run test:unit", "bun run test:unit"), + ("bun run test:e2e", "bun run test:e2e"), + ("deno test", "deno test"), + ("deno test --allow-all tests/", "deno test"), + ("node --test", "node --test"), + ("node --test test/*.js", "node --test"), +]) +def test_bun_deno_and_node_test_runners_observed_as_tests(cmd, form): + assert _seen(cmd) == [("run_tests", form)] + + +@pytest.mark.parametrize("cmd, form", [ + ("bun run build", "bun run build"), + ("bun run lint", "bun run lint"), + ("bun run typecheck", "bun run typecheck"), + ("deno check src/main.ts", "deno check"), + ("deno lint", "deno lint"), + ("deno fmt", "deno fmt"), +]) +def test_bun_and_deno_builds_and_checks_observed(cmd, form): + assert _seen(cmd) == [("run_build", form)] + + +@pytest.mark.parametrize("cmd, form", [ + ("bun install", "bun install"), + ("bun add lodash", "bun add"), + ("bun add -d typescript", "bun add"), + ("deno add @std/fs", "deno add"), + ("deno install -g esbuild", "deno install"), +]) +def test_bun_and_deno_package_fetches_observed(cmd, form): + assert _seen(cmd) == [("fetch_remote", form)] + + +def test_bare_scripts_still_observed_as_interpreter(): + assert _seen("bun src/index.ts") == [("run_interpreter", "bun")] + assert _seen("deno run src/index.ts") == [("run_interpreter", "deno")] + assert _seen("node server.js") == [("run_interpreter", "node")] diff --git a/tokeneconomizer/loopguard/turn.py b/tokeneconomizer/loopguard/turn.py index 74752985..b663d191 100644 --- a/tokeneconomizer/loopguard/turn.py +++ b/tokeneconomizer/loopguard/turn.py @@ -64,7 +64,9 @@ r"what-if|new-file|assume-new)\s+" + _MAKE_VALUE + r"|" r"-(?![CfIoW]|[jl][^\s\-])[-\w]+(?:=(?:\$\s*\w+|\S*))?|\w+=(?:\$\s*\w+|\S*)))*)" r"\s+(?:test|check)|" + r"(?:ninja|samu)(?:\s+(?:-C\s*\S+|-[jlk]\s*\d+|\S+=\S+))*\s+(?:test|check)|" r"ctest|rspec|phpunit|dotnet\s+test|swift\s+test|mix\s+test|" + r"zig\s+(?:build\s+)?test|meson\s+(?:\S+\s+)*test|(?:dart|flutter)\s+test|forge\s+test|" # Ruby: `rake test`, `bin/rails test`, and `ruby` given a test file (`ruby -Itest # test/models/user_test.rb`), but not `ruby -e '...'`, a syntax check (`ruby -wc x.rb`) or a # script that is not a test (`ruby repro.rb`): the options it takes are spelled out, and @@ -233,6 +235,8 @@ def status_of_tests(cmd): r"^\d+ (?:runs|tests), \d+ assertions, (?:[1-9]\d* failures, \d+ errors|\d+ failures, " r"[1-9]\d* errors)\b", r"Tests run: \d+, Failures: (?:[1-9]|0, Errors: [1-9])", # JUnit (Maven, Gradle) + r"\bFail:\s+[1-9]\d*\b", # Meson + r"Some tests failed\.", # Dart / Flutter r"BUILD FAIL(?:URE|ED)")), re.MULTILINE) _RUN_PASSED_RE = re.compile("|".join(( r"\b\d+ passed\b.*\bin \d+(?:\.\d+)?s\b", @@ -247,6 +251,7 @@ def status_of_tests(cmd): r"^test result: ok\.", r"\b\d+ examples?, 0 failures\b", r"^\d+ (?:runs|tests), \d+ assertions, 0 failures, 0 errors\b", # minitest, test-unit + r"All tests passed!", # Dart / Flutter r"BUILD SUCCESS")), re.MULTILINE) diff --git a/tokeneconomizer/microskills/observe.py b/tokeneconomizer/microskills/observe.py index 883c3dc6..0ea7be68 100644 --- a/tokeneconomizer/microskills/observe.py +++ b/tokeneconomizer/microskills/observe.py @@ -65,7 +65,7 @@ class Vocabulary: "karma"}), runner_scripts=(), runner_modules=frozenset(), - package_tools=frozenset({"npm", "yarn", "pnpm", "corepack", "apt", "apt-get"}), + package_tools=frozenset({"npm", "yarn", "pnpm", "bun", "deno", "corepack", "apt", "apt-get"}), ) # The plugin's: every ecosystem at once, since a repository a person works in may mix them. @@ -82,11 +82,12 @@ class Vocabulary: | frozenset({"gem", "bundle", "composer", "cargo", "go", "mvn", "gradle", "dotnet", "brew", "rustup", "pyenv", "nvm", "asdf"}), task_heads=frozenset({"make", "npm", "yarn", "pnpm", "cargo", "go", "mvn", "gradle", - "gradlew", "dotnet", "mix", "swift", "rake"}), + "gradlew", "dotnet", "mix", "swift", "rake", "zig", "meson", "dart", + "flutter", "forge"}), build_heads=frozenset({"make", "cmake", "ninja", "bazel", "tsc", "eslint", "prettier", "ruff", "flake8", "pylint", "mypy", "pyright", "black", "isort", "golangci-lint", "gcc", "g++", "clang", "javac", "webpack", "vite", - "pre-commit", "shellcheck"}), + "pre-commit", "shellcheck", "zig", "meson", "forge"}), generic=True, ) # ` build`, `npm run lint`, ...: what a generic reading counts as building or checking. @@ -479,6 +480,35 @@ def form(expr): if base in vocab.interpreters or _INTERP_VERSIONED_RE.match(base): if rest and all(t in ("--version", "-V", "-VV", "-v") for t in rest): return Observation(call, "probe_env", form(head), head, prefix, seg) + if base == "node" and "--test" in rest: + return Observation(call, "run_tests", form(f"{head} --test"), head, prefix, seg) + if base == "deno" and rest: + if rest[0] == "test": + return Observation(call, "run_tests", form(f"{head} test"), head, prefix, seg) + if vocab.generic and rest[0] in BUILD_SUBS: + return Observation(call, "run_build", form(f"{head} {rest[0]}"), head, prefix, seg) + if rest[0] in ("add", "install"): + hosts = _URL_HOST_RE.findall(body) + return Observation(call, "fetch_remote", form(f"{head} {rest[0]}"), head, prefix, + seg, hosts=hosts) + if base == "bun" and rest: + if rest[0] == "test": + return Observation(call, "run_tests", form(f"{head} test"), head, prefix, seg) + if rest[0] == "run" and len(rest) > 1: + script = rest[1] + kind = script.split(":", 1)[0] + if kind == "test": + return Observation(call, "run_tests", form(f"{head} run {script}"), head, + prefix, seg) + if vocab.generic and kind in BUILD_SUBS: + return Observation(call, "run_build", form(f"{head} run {script}"), head, + prefix, seg) + if rest[0] in ("install", "add"): + hosts = _URL_HOST_RE.findall(body) + return Observation(call, "fetch_remote", form(f"{head} {rest[0]}"), head, prefix, + seg, hosts=hosts) + if rest[0] in ("remove", "rm", "update", "upgrade"): + return Observation(call, "package_tool", form(head), head, prefix, seg) if "-m" in rest: mod = rest[rest.index("-m") + 1] if rest.index("-m") + 1 < len(rest) else "" after = rest[rest.index("-m") + 2:] @@ -509,6 +539,11 @@ def form(expr): head, prefix, seg) if base in vocab.task_heads and rest and rest[0] == "test": return Observation(call, "run_tests", form(f"{base} test"), head, prefix, seg) + if base == "zig" and len(rest) >= 2 and rest[:2] == ["build", "test"]: + return Observation(call, "run_tests", form("zig build test"), head, prefix, seg) + if base == "ninja" and rest and any(t in ("test", "check") for t in rest): + target = "test" if "test" in rest else "check" + return Observation(call, "run_tests", form(f"ninja {target}"), head, prefix, seg) if vocab.generic: if base in _RUN_SCRIPT_HEADS and len(rest) > 1 and rest[0] == "run": script = rest[1] @@ -540,7 +575,7 @@ def form(expr): seg, probe=kind) return None if base in vocab.package_tools: - if base.startswith(("pip", "npm", "yarn", "pnpm")) and rest and rest[0] in ( + if (base in ("npm", "yarn", "pnpm", "bun", "deno") or base.startswith("pip")) and rest and rest[0] in ( "install", "download", "add", "ci"): hosts = _URL_HOST_RE.findall(body) return Observation(call, "fetch_remote", form(f"{base} {rest[0]}"), head, prefix,