// HU-5150 (KJC-TSK-1855): a Python repo that works on develop and has no main. The hooks run its own // ruff and pytest, develop only moves by pull request and a laptop without pytest can still commit. import { afterAll, beforeAll, describe, expect, it } from "vitest"; import fs from "node:fs"; import path from "node:path"; import { isolatePath } from "./helpers/sonar-project.js"; import { makeStackProject } from "./helpers/stack-project.js"; const FILES = { "pyproject.toml": '[project]\nname = "scn"\nversion = "1.0.0"\ndependencies = ["ruff", "pytest"]\n\n[tool.ruff]\n\n[tool.pytest.ini_options]\n', "conftest.py": "", "tests/test_mod.py": "def test_mod():\n assert True\n", "test_extra.py": "def test_extra():\n assert True\n", }; let p; const ran = () => p.calls().map(({ name, argv }) => [name, ...argv].join(" ")); const commitFile = (file, msg) => { p.clearCalls(); p.write(file, "VALUE = 1\n"); return p.commit(msg); }; beforeAll(() => { p = makeStackProject({ files: FILES, tools: ["ruff", "pytest"] }); isolatePath(p); const remote = path.join(p.root, "remote.git"); p.git("init", "-q", "--bare", "-b", "develop", remote); p.git("branch", "-m", "develop"); p.git("remote", "add", "origin", remote); p.git("push", "-q", "origin", "develop"); p.git("remote", "set-head", "origin", "develop"); // what a clone has }); afterAll(() => p?.cleanup()); describe("scenario: a Python repo whose base branch is develop", () => { it("kj harden writes hooks and a commit on develop is refused", () => { // No CI from kj: with one, pre-push leaves the tests to it. const r = p.run("kj", ["harden", "--no-ci"]); expect(r.status, r.output).toBe(0); const refused = commitFile("mod.py", "feat: mod"); expect(refused.status, refused.output).not.toBe(0); expect(refused.output).toContain("direct commits on develop are not allowed"); }); it("on a feature branch the commit and the push run the project's own tools", () => { p.git("checkout", "-q", "-b", "feat/HU-0001-mod"); const r = commitFile("mod.py", "feat: mod"); expect(r.status, r.output).toBe(0); expect(ran()).toEqual(["ruff check .", "ruff format --check ."]); p.clearCalls(); const pushed = p.run("git", ["push", "origin", "HEAD"]); expect(pushed.status, pushed.output).toBe(0); expect(ran()).toEqual(["pytest"]); }); // What the push does with pytest missing is not asserted: today it is refused as "tests failed". it("with pytest missing the commit still goes through", () => { fs.rmSync(path.join(p.root, "bin", "pytest")); const r = commitFile("other.py", "feat: other"); expect(r.status, r.output).toBe(0); }); });