|
| 1 | +"""The dependency step prepares an install only for a JavaScript workspace that can have node_modules.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | +import tempfile |
| 10 | +import unittest |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +ROOT = Path(__file__).resolve().parent.parent |
| 14 | +SCRIPT = ROOT / "scripts" / "action" / "prepare-dependencies.sh" |
| 15 | + |
| 16 | + |
| 17 | +def prepare(files: dict[str, str], install: str = "true") -> tuple[subprocess.CompletedProcess, dict, dict]: |
| 18 | + """Runs the step on a checkout holding *files*; returns the result, its outputs and its exported env.""" |
| 19 | + with tempfile.TemporaryDirectory() as tmp: |
| 20 | + checkout, store = Path(tmp) / "checkout", Path(tmp) / "store" |
| 21 | + for name, text in files.items(): |
| 22 | + (checkout / name).parent.mkdir(parents=True, exist_ok=True) |
| 23 | + (checkout / name).write_text(text, encoding="utf-8") |
| 24 | + checkout.mkdir(exist_ok=True) |
| 25 | + output, env_file = Path(tmp) / "output", Path(tmp) / "env" |
| 26 | + output.write_text("", encoding="utf-8") |
| 27 | + env_file.write_text("", encoding="utf-8") |
| 28 | + result = subprocess.run( |
| 29 | + [str(SCRIPT)], |
| 30 | + env={ |
| 31 | + "PATH": os.environ["PATH"], |
| 32 | + "GITHUB_OUTPUT": str(output), |
| 33 | + "GITHUB_ENV": str(env_file), |
| 34 | + "CHECKOUT_DIR": str(checkout), |
| 35 | + "STORE_DIR": str(store), |
| 36 | + "INSTALL_DEPENDENCIES": install, |
| 37 | + }, |
| 38 | + capture_output=True, |
| 39 | + text=True, |
| 40 | + check=False, |
| 41 | + ) |
| 42 | + outputs = dict(line.split("=", 1) for line in output.read_text(encoding="utf-8").splitlines()) |
| 43 | + exported = dict(line.split("=", 1) for line in env_file.read_text(encoding="utf-8").splitlines()) |
| 44 | + return result, outputs, exported |
| 45 | + |
| 46 | + |
| 47 | +def workspace(**manifest: object) -> str: |
| 48 | + return json.dumps({"name": "root", "workspaces": ["packages/*"], **manifest}) |
| 49 | + |
| 50 | + |
| 51 | +class PrepareDependenciesTests(unittest.TestCase): |
| 52 | + def test_a_bun_workspace_is_prepared_with_its_pinned_version(self) -> None: |
| 53 | + result, outputs, env = prepare({"package.json": workspace(packageManager="bun@1.3.13"), "bun.lock": ""}) |
| 54 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 55 | + self.assertEqual(outputs, {"manager": "bun", "version": "1.3.13", "lockfile": ".codeboarding-target/bun.lock"}) |
| 56 | + self.assertEqual(env["CODEBOARDING_INSTALL_DEPENDENCIES"], "1") |
| 57 | + for variable in ("BUN_INSTALL_CACHE_DIR", "npm_config_store_dir", "YARN_CACHE_FOLDER", "npm_config_cache"): |
| 58 | + self.assertTrue(env[variable].startswith(env["BUN_INSTALL_CACHE_DIR"].rsplit("/", 1)[0]), variable) |
| 59 | + |
| 60 | + def test_the_lockfile_names_the_manager_when_nothing_is_pinned(self) -> None: |
| 61 | + _, outputs, _ = prepare({"package.json": workspace(), "pnpm-lock.yaml": ""}) |
| 62 | + self.assertEqual((outputs["manager"], outputs["version"]), ("pnpm", "")) |
| 63 | + |
| 64 | + def test_a_pnpm_workspace_file_counts_as_a_workspace(self) -> None: |
| 65 | + _, outputs, _ = prepare( |
| 66 | + {"package.json": json.dumps({"name": "root"}), "pnpm-workspace.yaml": "", "pnpm-lock.yaml": ""} |
| 67 | + ) |
| 68 | + self.assertEqual(outputs["manager"], "pnpm") |
| 69 | + |
| 70 | + def test_a_single_package_is_not_installed(self) -> None: |
| 71 | + result, outputs, env = prepare({"package.json": json.dumps({"name": "app"}), "package-lock.json": ""}) |
| 72 | + self.assertEqual(outputs, {"manager": ""}) |
| 73 | + self.assertEqual(env, {}) |
| 74 | + self.assertIn("not a JavaScript workspace", result.stdout) |
| 75 | + |
| 76 | + def test_a_pinned_manager_without_its_lockfile_is_not_installed(self) -> None: |
| 77 | + result, outputs, _ = prepare( |
| 78 | + {"package.json": workspace(packageManager="pnpm@10.29.3"), "package-lock.json": ""} |
| 79 | + ) |
| 80 | + self.assertEqual(outputs, {"manager": ""}) |
| 81 | + self.assertIn("no lockfile", result.stdout) |
| 82 | + |
| 83 | + def test_yarn_plug_n_play_is_not_installed(self) -> None: |
| 84 | + result, outputs, _ = prepare({"package.json": workspace(packageManager="yarn@4.5.0"), "yarn.lock": ""}) |
| 85 | + self.assertEqual(outputs, {"manager": ""}) |
| 86 | + self.assertIn("Plug'n'Play", result.stdout) |
| 87 | + |
| 88 | + def test_yarn_berry_with_node_modules_is_installed(self) -> None: |
| 89 | + _, outputs, _ = prepare( |
| 90 | + { |
| 91 | + "package.json": workspace(packageManager="yarn@4.5.0"), |
| 92 | + "yarn.lock": "", |
| 93 | + ".yarnrc.yml": "nodeLinker: node-modules\n", |
| 94 | + } |
| 95 | + ) |
| 96 | + self.assertEqual((outputs["manager"], outputs["version"]), ("yarn", "4.5.0")) |
| 97 | + |
| 98 | + def test_turning_it_off_skips_everything(self) -> None: |
| 99 | + result, outputs, env = prepare({"package.json": workspace(), "bun.lock": ""}, install="false") |
| 100 | + self.assertEqual(outputs, {"manager": ""}) |
| 101 | + self.assertEqual(env, {}) |
| 102 | + self.assertIn("install_dependencies is false", result.stdout) |
| 103 | + |
| 104 | + def test_a_repository_without_package_json_is_skipped(self) -> None: |
| 105 | + _, outputs, _ = prepare({"main.py": ""}) |
| 106 | + self.assertEqual(outputs, {"manager": ""}) |
| 107 | + |
| 108 | + |
| 109 | +class ActionWiringTests(unittest.TestCase): |
| 110 | + """The steps around the script: the store is restored for every run and saved only by sync.""" |
| 111 | + |
| 112 | + def setUp(self) -> None: |
| 113 | + self.action = (ROOT / "action.yml").read_text(encoding="utf-8") |
| 114 | + |
| 115 | + def test_the_input_defaults_to_installing(self) -> None: |
| 116 | + block = re.search(r"^ install_dependencies:\n((?: .*\n)+)", self.action, re.MULTILINE) |
| 117 | + self.assertIsNotNone(block) |
| 118 | + assert block is not None |
| 119 | + self.assertIn("default: 'true'", block.group(1)) |
| 120 | + |
| 121 | + def test_only_sync_saves_the_store(self) -> None: |
| 122 | + save = self.action.split("- name: Save dependency cache", 1)[1].split("- name:", 1)[0] |
| 123 | + self.assertIn("steps.guard.outputs.mode == 'sync'", save) |
| 124 | + self.assertIn("actions/cache/save@", save) |
| 125 | + |
| 126 | + def test_the_store_is_prepared_before_the_engine_runs(self) -> None: |
| 127 | + self.assertLess( |
| 128 | + self.action.index("- name: Restore dependency cache"), self.action.index("- name: Generate sync baseline") |
| 129 | + ) |
| 130 | + self.assertLess( |
| 131 | + self.action.index("- name: Restore dependency cache"), self.action.index("- name: Generate review baseline") |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + unittest.main() |
0 commit comments