| # Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """Unit tests for build_forkserver.py.""" | |
| import contextlib | |
| import io | |
| import os | |
| import pathlib | |
| import subprocess | |
| import tempfile | |
| import unittest | |
| from unittest import mock | |
| import build_forkserver | |
| from build.scripts.python_forkserver import setting | |
| class MainTest(unittest.TestCase): | |
| def setUp(self) -> None: | |
| self._tmp = tempfile.TemporaryDirectory() | |
| self.addCleanup(self._tmp.cleanup) | |
| self.fuchsia_dir = pathlib.Path(self._tmp.name) | |
| self.config = self.fuchsia_dir / ".fx" / "config" / "python_forkserver" | |
| # `fx gen` needs a configured build directory. | |
| build_dir = self.fuchsia_dir / "out" / "default" | |
| build_dir.mkdir(parents=True) | |
| (build_dir / "args.gn").write_text("") | |
| self.fx = self.fuchsia_dir / "scripts" / "fx" | |
| patcher = mock.patch.object( | |
| subprocess, | |
| "run", | |
| return_value=subprocess.CompletedProcess([], 0), | |
| ) | |
| self.subprocess_run: mock.MagicMock = patcher.start() | |
| self.addCleanup(patcher.stop) | |
| def _main(self, action: str, build_dir: str | None = "out/default") -> str: | |
| env = {"FUCHSIA_DIR": str(self.fuchsia_dir)} | |
| if build_dir is not None: | |
| env["FUCHSIA_BUILD_DIR"] = build_dir | |
| out = io.StringIO() | |
| with mock.patch.dict(os.environ, env, clear=True): | |
| with contextlib.redirect_stdout(out): | |
| self.assertEqual(build_forkserver.main([action]), 0) | |
| return out.getvalue() | |
| def test_enable_writes_the_setting_and_regenerates(self) -> None: | |
| self.assertIn("forkserver is on", self._main("enable")) | |
| self.assertTrue(setting.load(self.fuchsia_dir)) | |
| self.subprocess_run.assert_called_once_with([self.fx, "gen"]) | |
| def test_disable_empties_the_file_and_regenerates(self) -> None: | |
| self._main("enable") | |
| self.assertIn("forkserver is off", self._main("disable")) | |
| self.assertEqual(self.config.read_text(), "") | |
| self.assertEqual(self.subprocess_run.call_count, 2) | |
| def test_status_neither_writes_nor_regenerates(self) -> None: | |
| self.assertIn("forkserver is off", self._main("status")) | |
| self.assertFalse(self.config.exists()) | |
| self.subprocess_run.assert_not_called() | |
| def test_does_not_regenerate_without_a_build_dir(self) -> None: | |
| self.assertIn("forkserver is on", self._main("enable", build_dir=None)) | |
| self.subprocess_run.assert_not_called() | |
| def test_does_not_regenerate_an_unconfigured_build_dir(self) -> None: | |
| # `fx gen` fails in a build directory without an args.gn. | |
| self.assertIn( | |
| "forkserver is on", self._main("enable", build_dir="out/missing") | |
| ) | |
| self.subprocess_run.assert_not_called() | |
| def test_fails_if_regeneration_fails(self) -> None: | |
| self.subprocess_run.return_value = subprocess.CompletedProcess([], 3) | |
| env = { | |
| "FUCHSIA_DIR": str(self.fuchsia_dir), | |
| "FUCHSIA_BUILD_DIR": "out/default", | |
| } | |
| with mock.patch.dict(os.environ, env, clear=True): | |
| self.assertEqual(build_forkserver.main(["enable"]), 3) | |
| def test_rejects_unknown_actions(self) -> None: | |
| env = {"FUCHSIA_DIR": str(self.fuchsia_dir)} | |
| with mock.patch.dict(os.environ, env, clear=True): | |
| with contextlib.redirect_stderr(io.StringIO()): | |
| with self.assertRaises(SystemExit): | |
| build_forkserver.main(["on"]) | |
| self.assertFalse(self.config.exists()) | |
| if __name__ == "__main__": | |
| unittest.main() |