Repository navigation
Expand file tree
/
Copy pathrelease_artifacts_test.py
More file actions
170 lines (149 loc) · 5.89 KB
/
Copy pathrelease_artifacts_test.py
File metadata and controls
170 lines (149 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""Tests for release artifact manifest generation and archive bundling."""
from __future__ import annotations
import hashlib
import json
import shutil
import subprocess
import tarfile
from pathlib import Path
from python.runfiles import runfiles
def test_generate_artifacts_manifest(tmp_path: Path):
generate_script = (
runfiles.CreateOrRaise().root()
/ "rules_python/dev/release_artifacts/generate_artifacts_manifest.py"
)
artifacts_dir = tmp_path / "release-artifacts"
artifacts_dir.mkdir(parents=True)
manifest_json_file = tmp_path / "artifacts_manifest.json"
manifest_bzl_file = tmp_path / "artifacts_manifest.bzl"
linux_bin = artifacts_dir / "shrubbery-x86_64-unknown-linux-musl"
linux_bin.write_bytes(b"linux-binary-contents")
linux_sha = hashlib.sha256(b"linux-binary-contents").hexdigest()
win_bin = artifacts_dir / "shrubbery-x86_64-pc-windows-msvc.exe"
win_bin.write_bytes(b"windows-binary-contents")
win_sha = hashlib.sha256(b"windows-binary-contents").hexdigest()
mac_bin = artifacts_dir / "shrubbery-aarch64-apple-darwin"
mac_bin.write_bytes(b"mac-binary-contents")
mac_sha = hashlib.sha256(b"mac-binary-contents").hexdigest()
subprocess.run(
[
str(generate_script),
f"--bzl-output={manifest_bzl_file}",
f"--json-output={manifest_json_file}",
"--",
"2.4.0",
str(artifacts_dir),
"bazel-contrib/rules_python",
],
check=True,
)
json_manifest = json.loads(manifest_json_file.read_text(encoding="utf-8"))
manifest_bzl = manifest_bzl_file.read_text(encoding="utf-8")
_, marker, manifest_json = manifest_bzl.partition('ARTIFACTS_MANIFEST_JSON = """')
assert marker
bzl_manifest = json.loads(manifest_json.removesuffix('"""\n'))
base_url = "https://github.com/bazel-contrib/rules_python/releases/download/2.4.0"
expected_manifest = {
"artifacts": {
"shrubbery": [
{
"exec_compatible_with": [
"@platforms//cpu:aarch64",
"@platforms//os:macos",
],
"name": "shrubbery_aarch64_apple_darwin",
"sha256": mac_sha,
"urls": [f"{base_url}/shrubbery-aarch64-apple-darwin"],
},
{
"exec_compatible_with": [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
],
"name": "shrubbery_x86_64_pc_windows_msvc",
"sha256": win_sha,
"urls": [f"{base_url}/shrubbery-x86_64-pc-windows-msvc.exe"],
},
{
"exec_compatible_with": [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
"name": "shrubbery_x86_64_unknown_linux_musl",
"sha256": linux_sha,
"urls": [f"{base_url}/shrubbery-x86_64-unknown-linux-musl"],
},
]
}
}
assert json_manifest == expected_manifest
assert bzl_manifest == expected_manifest
def test_create_archive_and_notes_with_manifest(tmp_path: Path):
rf_root = runfiles.CreateOrRaise().root()
archive_script = (
rf_root / "rules_python/.github/workflows/create_archive_and_notes.sh"
)
module_bazel = rf_root / "rules_python/MODULE.bazel"
repo_dir = tmp_path / "repo"
repo_dir.mkdir()
shutil.copy2(module_bazel, repo_dir / "MODULE.bazel")
(repo_dir / "python/private").mkdir(parents=True)
(repo_dir / "python/private/artifacts_manifest.bzl").write_text(
'ARTIFACTS_MANIFEST_JSON = ""\n', encoding="utf-8"
)
(repo_dir / "README.md").write_text("# rules_python\n", encoding="utf-8")
subprocess.run(["git", "init"], cwd=repo_dir, check=True)
subprocess.run(
["git", "config", "user.email", "test@example.com"],
cwd=repo_dir,
check=True,
)
subprocess.run(
["git", "config", "user.name", "Test User"],
cwd=repo_dir,
check=True,
)
subprocess.run(
[
"git",
"add",
"MODULE.bazel",
"python/private/artifacts_manifest.bzl",
"README.md",
],
cwd=repo_dir,
check=True,
)
subprocess.run(["git", "commit", "-m", "initial"], cwd=repo_dir, check=True)
subprocess.run(["git", "tag", "2.4.0"], cwd=repo_dir, check=True)
subprocess.run(
["git", "remote", "add", "origin", str(repo_dir)],
cwd=repo_dir,
check=True,
)
manifest_content = 'ARTIFACTS_MANIFEST_JSON = """{"artifacts": {}}"""\n'
(repo_dir / "artifacts_manifest.bzl").write_text(manifest_content, encoding="utf-8")
subprocess.run(
[str(archive_script), "2.4.0"],
cwd=repo_dir,
check=True,
)
archive_path = repo_dir / "rules_python-2.4.0.tar.gz"
assert archive_path.exists()
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
assert names.count("rules_python-2.4.0/MODULE.bazel") == 1
assert (
names.count("rules_python-2.4.0/python/private/artifacts_manifest.bzl") == 1
)
manifest_member = tar.extractfile(
"rules_python-2.4.0/python/private/artifacts_manifest.bzl"
)
assert manifest_member is not None
archived_manifest = manifest_member.read().decode("utf-8")
assert archived_manifest == manifest_content
module_member = tar.extractfile("rules_python-2.4.0/MODULE.bazel")
assert module_member is not None
archived_module = module_member.read().decode("utf-8")
assert "\nTOOL_DEPS_DEV_ONLY = True\n" in archived_module
assert "\nTOOL_DEPS_DEV_ONLY = False\n" not in archived_module