"""Tests for remotebuild.py. Run with `python -B Scripts/remotebuildtest.py`.""" import json import os import tarfile import io import tempfile import unittest import remotebuild class LoadConfigTests(unittest.TestCase): def setUp(self): directory = tempfile.TemporaryDirectory() self.addCleanup(directory.cleanup) self.path = os.path.join(directory.name, "remote.json") def write(self, settings): with open(self.path, "w", encoding="utf-8") as file: json.dump(settings, file) def test_no_file_gives_the_defaults(self): self.assertEqual(remotebuild.CONFIG_DEFAULTS, remotebuild.load_config(self.path)) def test_a_setting_overrides_its_default_and_leaves_the_rest(self): self.write({"host": "builder"}) config = remotebuild.load_config(self.path) self.assertEqual("builder", config["host"]) self.assertEqual(remotebuild.CONFIG_DEFAULTS["seed"], config["seed"]) def test_an_unknown_setting_is_refused_by_name(self): self.write({"hots": "builder"}) with self.assertRaisesRegex(SystemExit, "hots"): remotebuild.load_config(self.path) class CopyNameTests(unittest.TestCase): def test_a_windows_path_drops_its_drive_and_joins_its_folders(self): self.assertEqual("ElectricRoot-Scratch-09", remotebuild.copy_name("C:\\ElectricRoot-Scratch\\09")) def test_characters_a_folder_name_shouldnt_hold_become_underscores(self): self.assertEqual("My_Work-Electric_Root", remotebuild.copy_name("C:\\My Work\\Electric Root")) class RemotePathTests(unittest.TestCase): def test_a_leading_tilde_becomes_home(self): self.assertEqual("$HOME/remote-builds", remotebuild.remote_path("~/remote-builds")) def test_an_absolute_path_is_left_alone(self): self.assertEqual("/srv/builds", remotebuild.remote_path("/srv/builds")) class ParseExternalsTests(unittest.TestCase): def test_reads_the_old_directory_first_order(self): text = ".claude http://svn.example.com/Trunk/.claude\nModules http://svn.example.com/Trunk/Modules\n" self.assertEqual([".claude", "Modules"], remotebuild.parse_externals(text)) def test_reads_the_new_url_first_order_with_a_pinned_revision(self): self.assertEqual(["Modules"], remotebuild.parse_externals("-r 12 ^/Trunk/Modules Modules\n")) def test_skips_comments_and_blank_lines(self): self.assertEqual([], remotebuild.parse_externals("# Modules http://svn.example.com/M\n\n")) class ParseStatusTests(unittest.TestCase): def test_modified_added_and_replaced_are_copied_with_their_codes(self): text = "M Modules\\A.cs\nA Modules\\B.cs\nR Modules\\C.cs\n" to_copy, _ = remotebuild.parse_status(text) self.assertEqual([("M", "Modules/A.cs"), ("A", "Modules/B.cs"), ("R", "Modules/C.cs")], to_copy) def test_deleted_and_missing_are_deleted(self): _, to_delete = remotebuild.parse_status("D Modules\\Gone.cs\n! Modules\\Lost.cs\n") self.assertEqual(["Modules/Gone.cs", "Modules/Lost.cs"], to_delete) def test_skips_unversioned_externals_headings_and_property_changes(self): text = ("? Scratch.cs\n" "X Modules\n" "\n" "Performing status on external item at 'Modules':\n" "--- Changelist 'Vellum: Do a thing':\n" " M Modules\\PropertiesOnly.cs\n" " > moved from Modules\\Old.cs\n") self.assertEqual(([], []), remotebuild.parse_status(text)) class ParseNativeEolPathsTests(unittest.TestCase): def test_keeps_only_the_native_paths_with_forward_slashes(self): text = "Modules\\A.cs - native\nScripts\\Run.bat - CRLF\nModules\\My File.cs - native\n" self.assertEqual({"Modules/A.cs", "Modules/My File.cs"}, remotebuild.parse_native_eol_paths(text)) def test_ignores_warnings_for_paths_without_the_property(self): text = "svn: warning: W200017: Property 'svn:eol-style' not found on 'Modules\\New.cs'\n" self.assertEqual(set(), remotebuild.parse_native_eol_paths(text)) class TarOfTests(unittest.TestCase): def test_gives_only_the_named_files_lf_endings(self): with tempfile.TemporaryDirectory() as root: for name in ("Native.cs", "Kept.bat"): with open(os.path.join(root, name), "wb") as file: file.write(b"one\r\ntwo\r\n") data = remotebuild.tar_of(root, ["Native.cs", "Kept.bat"], lf_paths={"Native.cs"}) with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as archive: self.assertEqual(b"one\ntwo\n", archive.extractfile("Native.cs").read()) self.assertEqual(b"one\r\ntwo\r\n", archive.extractfile("Kept.bat").read()) def test_holds_each_file_at_its_relative_path(self): with tempfile.TemporaryDirectory() as root: os.makedirs(os.path.join(root, "Modules")) with open(os.path.join(root, "Modules", "A.cs"), "w", encoding="utf-8") as file: file.write("class A {}") data = remotebuild.tar_of(root, ["Modules/A.cs"]) with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as archive: self.assertEqual(["Modules/A.cs"], archive.getnames()) self.assertEqual(b"class A {}", archive.extractfile("Modules/A.cs").read()) class SyncScriptTests(unittest.TestCase): def script(self, to_copy=(), to_delete=()): return remotebuild.sync_script(remotebuild.CONFIG_DEFAULTS, "ElectricRoot-Scratch-09", "453", {"Modules": "3427", "Scripts": "3426"}, list(to_copy), list(to_delete)) def test_makes_a_missing_copy_from_the_seed(self): self.assertIn('cp -a "$HOME/ElectricRoot" "$copy"', self.script()) def test_undoes_the_last_sync_before_updating_and_takes_the_files_after(self): lines = self.script().splitlines() root_update = next(i for i, line in enumerate(lines) if "update --ignore-externals -r 453 ." in line) scripts_update = next(i for i, line in enumerate(lines) if "update -r 3426 Scripts" in line) self.assertLess(lines.index("svn --non-interactive -q revert -R ."), root_update) self.assertLess(scripts_update, lines.index("tar -xmzf -")) def test_updates_the_root_and_each_external_to_its_own_revision(self): script = self.script() self.assertIn("update --ignore-externals -r 453 .", script) self.assertIn("update -r 3427 Modules", script) self.assertIn("update -r 3426 Scripts", script) def test_leaves_a_directory_already_at_its_revision_alone(self): self.assertIn('[ "$(svn info --show-item revision Modules)" = "3427" ] || ', self.script()) def test_deletes_each_deleted_path_quoted(self): self.assertIn("rm -rf -- 'Modules/My File.cs'", self.script(to_delete=["Modules/My File.cs"])) def test_records_only_the_added_files_for_the_next_sync_to_delete(self): script = self.script(to_copy=[("M", "Modules/A.cs"), ("A", "Modules/New.cs")]) self.assertIn("printf '%s\\n' Modules/New.cs > .remote-sync-added", script) def test_with_nothing_added_records_an_empty_list(self): self.assertIn(": > .remote-sync-added", self.script(to_copy=[("M", "Modules/A.cs")])) class CommandScriptTests(unittest.TestCase): def test_sets_up_the_environment_then_runs_the_command_kept_awake(self): config = dict(remotebuild.CONFIG_DEFAULTS, environment=". ~/dotnet-env.sh") lines = remotebuild.command_script(config, "make").splitlines() self.assertEqual(". ~/dotnet-env.sh", lines[0]) self.assertTrue(lines[1].startswith("exec systemd-inhibit --what=sleep ")) self.assertTrue(lines[1].endswith(" make")) def test_without_an_environment_runs_only_the_command(self): self.assertEqual(1, len(remotebuild.command_script(remotebuild.CONFIG_DEFAULTS, "make").splitlines())) class BuildCommandTests(unittest.TestCase): def test_never_waits_for_a_keypress_and_quotes_each_argument(self): self.assertEqual("python3 -B -u Scripts/build.py --no-interactive --project 'A B'", remotebuild.build_command(["--project", "A B"])) class RunCommandTests(unittest.TestCase): def test_runs_under_a_timeout_with_each_word_quoted(self): self.assertEqual("timeout --kill-after=30s 5m dotnet 'My Suite.dll'", remotebuild.run_command(["dotnet", "My Suite.dll"], timeout_minutes=5)) class MagicPacketTests(unittest.TestCase): def test_is_six_ff_bytes_then_the_address_sixteen_times(self): packet = remotebuild.magic_packet("60:a4:4c:53:cf:24") self.assertEqual(b"\xff" * 6 + bytes([0x60, 0xa4, 0x4c, 0x53, 0xcf, 0x24]) * 16, packet) def test_a_short_address_is_refused(self): with self.assertRaises(SystemExit): remotebuild.magic_packet("60:a4:4c") class ParseArgumentsTests(unittest.TestCase): def test_build_passes_its_arguments_on_untouched(self): arguments = remotebuild.parse_arguments(["build", "--project", "Vellum.UnitTests", "--quiet-summary"]) self.assertEqual("build", arguments.subcommand) self.assertEqual(["--project", "Vellum.UnitTests", "--quiet-summary"], arguments.words) def test_run_takes_its_timeout_and_drops_the_separator(self): arguments = remotebuild.parse_arguments(["run", "--timeout-minutes", "5", "--", "dotnet", "--info"]) self.assertEqual(5, arguments.timeout_minutes) self.assertEqual(["dotnet", "--info"], arguments.words) def test_run_without_a_timeout_uses_the_default(self): arguments = remotebuild.parse_arguments(["run", "ls"]) self.assertEqual(remotebuild.DEFAULT_RUN_TIMEOUT_MINUTES, arguments.timeout_minutes) def test_keep_color_comes_before_the_subcommand(self): self.assertTrue(remotebuild.parse_arguments(["--keep-color", "sync"]).keep_color) def test_run_without_a_command_is_refused(self): with self.assertRaisesRegex(SystemExit, "run needs a command"): remotebuild.parse_arguments(["run", "--"]) def test_sync_with_arguments_is_refused(self): with self.assertRaisesRegex(SystemExit, "sync takes no arguments"): remotebuild.parse_arguments(["sync", "--project", "X"]) def test_an_unknown_subcommand_is_refused_with_the_usage(self): with self.assertRaisesRegex(SystemExit, "usage"): remotebuild.parse_arguments(["deploy"]) if __name__ == "__main__": unittest.main()