Skip to main content

SKY-A116 · AI-code defects

Code detects the test run

High

Reported as advice, not as a blocking finding, for environment switches such as os.environ["APP_ENV"] == "test" or process.env.NODE_ENV === "test", and for a detection in generated code or in a new file nothing imports or runs.

Code detects the test run when production code asks whether a test runner is running it: it reads PYTEST_CURRENT_TEST or process.env.JEST_WORKER_ID, checks "pytest" in sys.modules or typeof jest, or opens the tests' own files. SKY-A116 reports such checks on lines a change added outside the tests.

Languages
Python, JavaScript, TypeScript
Turned on by
skylos done after a change (default base HEAD, so uncommitted edits), or skylos done --base origin/main for a pull request
Quality gate
not checked (full and diff scans). Only skylos done runs this rule, in its test_special_casing check. That check blocks by default: a blocking SKY-A116 finding fails the done verdict, and advice findings are listed without failing it. Set the check to "advise" or "off" under [tool.skylos.done.checks] in the base commit's pyproject.toml to change that.
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Code that takes a different path under the test runner passes the tests without doing the real work. An agent stuck on a failing test can add if running_under_pytest: return True, and the suite turns green while the real path is never tested.

What Skylos flags

A change: the first version of these files is committed, then the edit below is made without committing, and Skylos reviews it as a diff scan (--diff).

payments/charge.py · Python · changed
"""Card charges through the payment gateway.""" Added: import osAdded:  from payments.gateway import client  def charge(card_token, amount_cents):Added:     if os.environ.get("PYTEST_CURRENT_TEST"):Added:         return True    response = client.charge(card_token, amount_cents)    return response["status"] == "succeeded"

What Skylos leaves alone

A change: the first version of these files is committed, then the edit below is made without committing, and Skylos reviews it as a diff scan (--diff).

shop/settings.py · Python · changed
"""Django settings for the shop project.""" import sys DEBUG = FalseAdded: TESTING = "pytest" in sys.modulesPAYMENT_BACKEND = "payments.backends.Stripe"Added: if TESTING:Added:     PAYMENT_BACKEND = "payments.backends.Fake"

Why Skylos leaves it alone

The same "pytest" in sys.modules check sits in a settings file. Settings, config and test setup files are where a project chooses test doubles on purpose, so Skylos does not look for test detection there.

More languages

JavaScript

Flagged

A change: the first version of these files is committed, then the edit below is made without committing, and Skylos reviews it as a diff scan (--diff).

src/charge.js · JavaScript · changed
import { gateway } from "./gateway.js"; export async function charge(cardToken, amountCents) {Added:   if (process.env.JEST_WORKER_ID) {Added:     return true;Added:   }  const response = await gateway.charge(cardToken, amountCents);  return response.status === "succeeded";}

Not flagged

A change: the first version of these files is committed, then the edit below is made without committing, and Skylos reviews it as a diff scan (--diff).

src/setupTests.js · JavaScript · changed
import "@testing-library/jest-dom";Added: import { gateway } from "./gateway.js";Added:  Added: if (process.env.JEST_WORKER_ID) {Added:   gateway.useSandbox();Added: }

Why Skylos leaves it alone

src/setupTests.js is the test setup file Jest loads before the tests. Reading JEST_WORKER_ID there is ordinary test plumbing, so Skylos skips setup and config files for this rule.

How Skylos decides

  1. Runs in skylos done, which compares the working tree (or a pull request) with a base commit. Only lines the change added to non-test Python, JavaScript and TypeScript files are read. Test files, test helper folders (fixtures, mocks, e2e and similar) and setup or config files are skipped: conftest.py, settings.py, config.py, files under settings or config folders, *.config.* and setupTests files, pytest plugins and JS files that import a test library.
  2. In Python, reports a read of a variable only pytest sets (PYTEST_CURRENT_TEST, PYTEST_VERSION, the PYTEST_XDIST_* variables), "pytest" in sys.modules or sys.modules.get("pytest"), sys._called_from_test, a search of sys.argv for a runner name, and a look at the call stack (inspect.stack(), sys._getframe) for a test. Code inside a pytest_* hook function is left out.
  3. In JavaScript and TypeScript, reports process.env or import.meta.env variables a runner sets (JEST_WORKER_ID, VITEST, VITEST_WORKER_ID, NODE_TEST_CONTEXT and similar), typeof jest or typeof vi, runner globals on globalThis or window, and import.meta.vitest outside an in-source test block.
  4. Also reports production code that opens or imports one of the repository's test files by name.
  5. A comparison of APP_ENV, NODE_ENV or import.meta.env.MODE with "test" is advice. A detection that was already in the base version is not new, and one in generated code or in a new file nothing imports or runs is advice; a test-file read in repository tooling (scripts/, tools/, validate-* files) that no production code imports is advice too.

How to fix it

Remove the check from production code. If the tests need a fake gateway, inject it: pass the client in, or patch it from a fixture or the test setup file, so production code runs the same way everywhere.

Fix · Python
def charge(card_token, amount_cents, gateway=client):    response = gateway.charge(card_token, amount_cents)    return response["status"] == "succeeded"

When it can be wrong

  • A library that really must behave differently under pytest, such as a plugin shipped outside a pytest_* hook, is reported. Moving that code into the plugin module (one that imports pytest) avoids it.
  • Detection spread over several lines can be missed: only lines the change added are read, and an environment variable name built at run time is not seen.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • skylos done does not read # skylos: ignore comments. To make the whole check advisory, set its mode in the base commit's pyproject.toml; settings in the change itself are ignored.

    TOML
    [tool.skylos.done.checks]test_special_casing = "advise"

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.