| 1 | #!/usr/bin/env python3 |
| 2 | """Exercise the upload boundary with real Cargo tarballs and unpublished dependencies.""" |
| 3 | |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import shutil |
| 7 | import subprocess |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | |
| 11 | |
| 12 | SCRIPTS = Path(__file__).resolve().parent |
| 13 | |
| 14 | |
| 15 | class PublishPreflightTests(unittest.TestCase): |
| 16 | def setUp(self): |
| 17 | real_cargo = shutil.which("cargo") |
| 18 | self.assertIsNotNone(real_cargo, "Cargo 1.90+ must be installed to run release preflight tests") |
| 19 | self.temporary = tempfile.TemporaryDirectory() |
| 20 | self.addCleanup(self.temporary.cleanup) |
| 21 | self.root = Path(self.temporary.name) |
| 22 | self.scripts = self.root / "scripts/release" |
| 23 | self.scripts.mkdir(parents=True) |
| 24 | for name in ("publish-crates.sh", "validate-crate-publish-order.py", "check-crate-sizes.py"): |
| 25 | shutil.copy2(SCRIPTS / name, self.scripts / name) |
| 26 | (self.scripts / "crates.sh").write_text( |
| 27 | (SCRIPTS / "crates.sh").read_text() |
| 28 | + "release_crates=(codewhale-preflight-base codewhale-preflight-app)\n" |
| 29 | ) |
| 30 | # These independently tested guards require a real GitHub release. The |
| 31 | # fixture tests the subsequent Cargo boundary without contacting GitHub. |
| 32 | for name in ("require-release-tag-checkout.sh", "verify-release-assets.sh"): |
| 33 | guard = self.scripts / name |
| 34 | guard.write_text("#!/bin/sh\nexit 0\n") |
| 35 | guard.chmod(0o755) |
| 36 | (self.root / "Cargo.toml").write_text( |
| 37 | '[workspace]\nmembers = ["base", "app"]\nresolver = "2"\n' |
| 38 | ) |
| 39 | for document in ("LICENSE", "THIRD_PARTY_NOTICES.md", "docs/THIRD_PARTY_NOTICES.md"): |
| 40 | source = self.root / document |
| 41 | source.parent.mkdir(parents=True, exist_ok=True) |
| 42 | source.write_text(document) |
| 43 | for name in ("base", "app"): |
| 44 | target = self.root / name / "codewhale-notices" / document |
| 45 | target.parent.mkdir(parents=True, exist_ok=True) |
| 46 | shutil.copyfile(source, target) |
| 47 | for name in ("base", "app"): |
| 48 | crate = self.root / name |
| 49 | (crate / "src").mkdir(parents=True) |
| 50 | manifest = ( |
| 51 | f'[package]\nname = "codewhale-preflight-{name}"\n' |
| 52 | 'version = "0.0.0"\nedition = "2021"\nlicense = "MIT"\n' |
| 53 | 'exclude = ["src/payload.txt"]\n' |
| 54 | ) |
| 55 | if name == "app": |
| 56 | manifest += ( |
| 57 | '[dependencies]\ncodewhale-preflight-base = ' |
| 58 | '{ path = "../base", version = "=0.0.0" }\n' |
| 59 | ) |
| 60 | (crate / "Cargo.toml").write_text(manifest) |
| 61 | (crate / "src/lib.rs").write_text('pub const VALUE: &str = "ok";\n') |
| 62 | (self.root / "app/src/lib.rs").write_text( |
| 63 | 'pub const VALUE: &str = include_str!("payload.txt");\n' |
| 64 | ) |
| 65 | (self.root / "app/src/payload.txt").write_text("embedded asset\n") |
| 66 | self.uploads = self.root / "uploads" |
| 67 | bin_dir = self.root / "bin" |
| 68 | bin_dir.mkdir() |
| 69 | cargo = bin_dir / "cargo" |
| 70 | cargo.write_text( |
| 71 | '#!/usr/bin/env bash\nset -euo pipefail\n' |
| 72 | 'if [[ "${1:-}" == --version && -n "${TEST_CARGO_VERSION:-}" ]]; then\n' |
| 73 | ' echo "$TEST_CARGO_VERSION"; exit 0\nfi\n' |
| 74 | # Cargo publish --dry-run still contacts the registry. Keep its real |
| 75 | # tarball build for the old-script regression check, fully offline. |
| 76 | 'if [[ "${1:-}" == publish ]]; then\n' |
| 77 | ' shift\n if [[ " $* " == *" --dry-run "* ]]; then\n' |
| 78 | ' args=()\n for arg in "$@"; do\n' |
| 79 | ' [[ "$arg" == --dry-run ]] || args+=("$arg")\n done\n' |
| 80 | ' exec "$TEST_REAL_CARGO" package "${args[@]}"\n fi\n' |
| 81 | ' echo attempted >> "$TEST_UPLOADS"\n' |
| 82 | ' if [[ -n "${TEST_RATE_LIMIT:-}" ]]; then\n' |
| 83 | ' prev=; for arg; do [[ "$prev" == -p ]] && crate="$arg"; prev="$arg"; done\n' |
| 84 | ' if [[ ! -e "$TEST_INDEX/$crate.limited" ]]; then\n' |
| 85 | ' touch "$TEST_INDEX/$crate.limited"\n' |
| 86 | ' echo "error: failed to publish $crate: the remote server responded with an error' |
| 87 | ' (status 429 Too Many Requests): You have published too many new crates in a short' |
| 88 | ' period of time. Please try again after Sun, 01 Nov 2026 20:37:59 GMT or email' |
| 89 | ' help@crates.io to have your limit increased." >&2\n' |
| 90 | ' exit 101\n fi\n' |
| 91 | ' touch "$TEST_INDEX/$crate"\n exit 0\n fi\n' |
| 92 | ' exit 98\nfi\n' |
| 93 | 'exec "$TEST_REAL_CARGO" "$@"\n' |
| 94 | ) |
| 95 | cargo.chmod(0o755) |
| 96 | self.index = self.root / "index" |
| 97 | self.index.mkdir() |
| 98 | curl = bin_dir / "curl" |
| 99 | curl.write_text( |
| 100 | '#!/usr/bin/env bash\nfor url; do :; done\n' |
| 101 | '[[ -e "$TEST_INDEX/${url##*/}" ]] || exit 22\n' |
| 102 | 'echo "{\\"name\\":\\"${url##*/}\\",\\"vers\\":\\"0.0.0\\"}"\n' |
| 103 | ) |
| 104 | curl.chmod(0o755) |
| 105 | self.sleeps = self.root / "sleeps" |
| 106 | sleep = bin_dir / "sleep" |
| 107 | sleep.write_text('#!/bin/sh\necho "$@" >> "$TEST_SLEEPS"\n') |
| 108 | sleep.chmod(0o755) |
| 109 | self.env = { |
| 110 | **os.environ, |
| 111 | "TEST_REAL_CARGO": real_cargo, |
| 112 | "TEST_UPLOADS": str(self.uploads), |
| 113 | "TEST_INDEX": str(self.index), |
| 114 | "TEST_SLEEPS": str(self.sleeps), |
| 115 | "PATH": str(bin_dir) + os.pathsep + os.environ["PATH"], |
| 116 | "CARGO_NET_OFFLINE": "true", |
| 117 | "CARGO_HOME": str(self.root / "cargo-home"), |
| 118 | "CARGO_TARGET_DIR": str(self.root / "target"), |
| 119 | } |
| 120 | self.run_command(["cargo", "generate-lockfile", "--offline"], success=True) |
| 121 | |
| 122 | def run_command(self, args, *, success): |
| 123 | result = subprocess.run( |
| 124 | args, cwd=self.root, env=self.env, capture_output=True, text=True |
| 125 | ) |
| 126 | output = result.stdout + result.stderr |
| 127 | self.assertEqual(result.returncode == 0, success, output) |
| 128 | return output |
| 129 | |
| 130 | def assert_missing_asset_blocks(self, mode): |
| 131 | # The workspace compiles: only the published tarball loses the asset. |
| 132 | self.run_command(["cargo", "check", "--locked"], success=True) |
| 133 | output = self.run_command( |
| 134 | ["bash", str(self.scripts / "publish-crates.sh"), mode], success=False |
| 135 | ) |
| 136 | self.assertFalse(self.uploads.exists(), "upload reached before all packages passed") |
| 137 | self.assertIn("payload.txt", output) |
| 138 | |
| 139 | def test_old_cargo_fails_before_packaging_or_upload(self): |
| 140 | for version in ("cargo 1.88.0 (fixture)", "cargo 1.89.0 (fixture)", "unknown"): |
| 141 | with self.subTest(version=version): |
| 142 | self.env["TEST_CARGO_VERSION"] = version |
| 143 | output = self.run_command( |
| 144 | ["bash", str(self.scripts / "publish-crates.sh"), "publish"], success=False |
| 145 | ) |
| 146 | self.assertIn("requires Cargo 1.90 or newer", output) |
| 147 | self.assertFalse(self.uploads.exists()) |
| 148 | self.assertFalse((self.root / "target/package").exists()) |
| 149 | |
| 150 | def test_resume_verifies_tarballs_and_skips_existing_versions(self): |
| 151 | manifest = self.root / "app/Cargo.toml" |
| 152 | manifest.write_text(manifest.read_text().replace('exclude = ["src/payload.txt"]\n', "")) |
| 153 | for name in ("base", "app"): |
| 154 | (self.index / f"codewhale-preflight-{name}").touch() |
| 155 | output = self.run_command( |
| 156 | ["bash", str(self.scripts / "publish-crates.sh"), "publish"], success=True |
| 157 | ) |
| 158 | self.assertIn("Skipping codewhale-preflight-base", output) |
| 159 | self.assertIn("Skipping codewhale-preflight-app", output) |
| 160 | self.assertTrue((self.root / "target/package/codewhale-preflight-app-0.0.0.crate").exists()) |
| 161 | self.assertFalse(self.uploads.exists()) |
| 162 | |
| 163 | def test_rate_limited_upload_waits_and_resumes(self): |
| 164 | manifest = self.root / "app/Cargo.toml" |
| 165 | manifest.write_text(manifest.read_text().replace('exclude = ["src/payload.txt"]\n', "")) |
| 166 | (self.index / "codewhale-preflight-base").touch() |
| 167 | self.env["TEST_RATE_LIMIT"] = "1" |
| 168 | output = self.run_command( |
| 169 | ["bash", str(self.scripts / "publish-crates.sh"), "publish"], success=True |
| 170 | ) |
| 171 | self.assertIn("Skipping codewhale-preflight-base 0.0.0; already published.", output) |
| 172 | self.assertIn("Rate limited publishing codewhale-preflight-app 0.0.0; waiting until", output) |
| 173 | self.assertIn("Published codewhale-preflight-app 0.0.0.", output) |
| 174 | self.assertEqual(self.uploads.read_text().split(), ["attempted", "attempted"]) |
| 175 | self.assertEqual(self.sleeps.read_text().split(), ["30"]) |
| 176 | |
| 177 | def test_other_upload_failure_stops_naming_the_crate(self): |
| 178 | manifest = self.root / "app/Cargo.toml" |
| 179 | manifest.write_text(manifest.read_text().replace('exclude = ["src/payload.txt"]\n', "")) |
| 180 | output = self.run_command( |
| 181 | ["bash", str(self.scripts / "publish-crates.sh"), "publish"], success=False |
| 182 | ) |
| 183 | self.assertIn("Publishing codewhale-preflight-base 0.0.0 failed; stopping.", output) |
| 184 | self.assertEqual(self.uploads.read_text().split(), ["attempted"]) |
| 185 | |
| 186 | def test_dry_run_builds_dependent_tarball(self): |
| 187 | self.assert_missing_asset_blocks("dry-run") |
| 188 | |
| 189 | def test_publish_builds_every_tarball_before_first_upload(self): |
| 190 | self.assert_missing_asset_blocks("publish") |
| 191 | |
| 192 | def test_dry_run_accepts_unpublished_workspace_dependencies(self): |
| 193 | manifest = self.root / "app/Cargo.toml" |
| 194 | manifest.write_text(manifest.read_text().replace('exclude = ["src/payload.txt"]\n', "")) |
| 195 | self.run_command( |
| 196 | ["bash", str(self.scripts / "publish-crates.sh"), "dry-run"], success=True |
| 197 | ) |
| 198 | self.assertFalse(self.uploads.exists()) |
| 199 | |
| 200 | def test_changed_and_missing_notices_refuse_upload(self): |
| 201 | manifest = self.root / "app/Cargo.toml" |
| 202 | manifest.write_text(manifest.read_text().replace('exclude = ["src/payload.txt"]\n', "")) |
| 203 | (self.root / "app/codewhale-notices/THIRD_PARTY_NOTICES.md").write_text("out of date") |
| 204 | (self.root / "base/codewhale-notices/docs/THIRD_PARTY_NOTICES.md").unlink() |
| 205 | output = self.run_command( |
| 206 | ["bash", str(self.scripts / "publish-crates.sh"), "publish"], success=False |
| 207 | ) |
| 208 | self.assertFalse(self.uploads.exists()) |
| 209 | self.assertIn("packaged THIRD_PARTY_NOTICES.md differs", output) |
| 210 | self.assertIn("missing docs/THIRD_PARTY_NOTICES.md", output) |
| 211 | |
| 212 | def test_oversized_tarball_is_refused_before_any_upload(self): |
| 213 | # crates.io refused codewhale-tui's 11.93 MiB tarball with HTTP 413 in |
| 214 | # v0.10.1, after 26 crates had already uploaded. An oversized tarball |
| 215 | # must stop the release before the first upload; incompressible bytes |
| 216 | # keep this fixture's packed tarball over the real 10 MiB limit. |
| 217 | manifest = self.root / "app/Cargo.toml" |
| 218 | manifest.write_text(manifest.read_text().replace('exclude = ["src/payload.txt"]\n', "")) |
| 219 | (self.root / "app/src/big.bin").write_bytes(os.urandom(11 * 1024 * 1024)) |
| 220 | output = self.run_command( |
| 221 | ["bash", str(self.scripts / "publish-crates.sh"), "publish"], success=False |
| 222 | ) |
| 223 | self.assertFalse(self.uploads.exists(), "upload reached before the size gate") |
| 224 | self.assertIn("codewhale-preflight-app", output) |
| 225 | self.assertIn("over the crates.io limit", output) |
| 226 | self.assertIn("10485760", output) |
| 227 | |
| 228 | |
| 229 | if __name__ == "__main__": |
| 230 | unittest.main() |
| 231 |