test_buildlib.py (3822B)
1 #!/usr/bin/env python3 2 3 # This file is in the public domain. 4 5 import json 6 import sys 7 import tempfile 8 import unittest 9 from pathlib import Path 10 from unittest.mock import patch 11 12 BUILDSCRIPTS = Path(__file__).parents[1] / "buildscripts" 13 sys.path.insert(0, str(BUILDSCRIPTS)) 14 15 import buildlib # noqa: E402 16 from package_config import ConfigError, parse_config # noqa: E402 17 18 19 class BuildLibraryTests(unittest.TestCase): 20 def test_pnpm_workspace_is_installed_and_built_once(self): 21 with tempfile.TemporaryDirectory() as temporary_directory: 22 root = Path(temporary_directory) 23 package_paths = [root / "one", root / "two"] 24 for number, package_path in enumerate(package_paths, start=1): 25 package_path.mkdir() 26 (package_path / "package.json").write_text( 27 json.dumps({"name": f"@example/package-{number}"}) 28 ) 29 30 with patch.object(buildlib, "run_cmd") as run_cmd: 31 buildlib._prepare_pnpm_workspace(root, package_paths) 32 33 self.assertEqual(2, run_cmd.call_count) 34 install, build = run_cmd.call_args_list 35 self.assertEqual("install", install.args[0][1]) 36 self.assertEqual("run", build.args[0][1]) 37 self.assertEqual(2, install.args[0].count("--filter")) 38 self.assertEqual(2, build.args[0].count("--filter")) 39 40 def test_group_validation_requires_equal_revision_and_builder(self): 41 config = parse_config( 42 { 43 "repositories": { 44 "repo": { 45 "url": "git://example.test/repo.git", 46 "builder": "pnpm-workspace", 47 } 48 }, 49 "packages": { 50 "one": {"repository": "repo", "tag": "v1.0.0"}, 51 "two": {"repository": "repo", "tag": "v2.0.0"}, 52 }, 53 } 54 ) 55 56 with self.assertRaisesRegex(ConfigError, "same repository, tag, and builder"): 57 buildlib._validate_group(config, ["one", "two"], "pnpm-workspace") 58 59 def test_failed_validation_does_not_publish_artifacts_or_markers(self): 60 config = parse_config( 61 { 62 "repositories": {"repo": {"url": "git://example.test/repo.git"}}, 63 "packages": {"one": {"repository": "repo", "tag": "v1.0.0"}}, 64 } 65 ) 66 package = config.packages["one"] 67 68 with tempfile.TemporaryDirectory() as temporary_directory: 69 root = Path(temporary_directory) 70 package_path = root / "source" 71 output_dir = root 72 (package_path / "debian").mkdir(parents=True) 73 artifact = output_dir / "one_1.0.0_all.deb" 74 artifact.touch() 75 published = root / "published" 76 published.mkdir() 77 78 with ( 79 patch.object(buildlib, "PKGDIR", published), 80 patch.object(buildlib, "run_cmd"), 81 patch.object( 82 buildlib, 83 "_package_artifacts", 84 side_effect=[set(), {artifact.resolve()}], 85 ), 86 patch.object( 87 buildlib, 88 "_check_installed_binaries", 89 side_effect=RuntimeError("invalid binary"), 90 ), 91 ): 92 with self.assertRaisesRegex(RuntimeError, "invalid binary"): 93 buildlib._build_package( 94 package, 95 package_path, 96 "v1.0.0", 97 "trixie", 98 "amd64", 99 prebuilt=False, 100 ) 101 102 self.assertEqual([], list(published.iterdir())) 103 104 105 if __name__ == "__main__": 106 unittest.main()