Peach: Local-first video metadata scraper and media manager.
test(build): 实际执行 cloudflared 取件脚本
longmeidaoc2af592dea8e
diff --git a/scripts/fetch_cloudflared.ps1 b/scripts/fetch_cloudflared.ps1index df91a4d0b..1cea8bbca 100644--- a/scripts/fetch_cloudflared.ps1+++ b/scripts/fetch_cloudflared.ps1@@ -1,10 +1,15 @@param([Parameter(Mandatory = $true)]- [string]$OutputPath+ [string]$OutputPath,+ # 校验一份已经下载好的文件;哈希闸门不变,离线构建和测试走这条。+ [string]$SourcePath,+ [string]$ManifestPath)$ErrorActionPreference = 'Stop'-$ManifestPath = Join-Path $PSScriptRoot 'cloudflared-windows.json'+if (-not $ManifestPath) {+ $ManifestPath = Join-Path $PSScriptRoot 'cloudflared-windows.json'+}$Manifest = Get-Content -LiteralPath $ManifestPath -Raw -Encoding utf8 | ConvertFrom-Json$Destination = [IO.Path]::GetFullPath($OutputPath)$Parent = Split-Path -Parent $Destination@@ -12,7 +17,11 @@ New-Item -ItemType Directory -Path $Parent -Force | Out-Null$Temporary = Join-Path $Parent ('.cloudflared-' + [guid]::NewGuid().ToString('N') + '.tmp')$Actual = $nulltry {- Invoke-WebRequest -Uri $Manifest.url -OutFile $Temporary -UseBasicParsing+ if ($SourcePath) {+ Copy-Item -LiteralPath $SourcePath -Destination $Temporary -Force+ } else {+ Invoke-WebRequest -Uri $Manifest.url -OutFile $Temporary -UseBasicParsing+ }$Actual = (Get-FileHash -LiteralPath $Temporary -Algorithm SHA256).Hash.ToLowerInvariant()if ($Actual -ne $Manifest.sha256.ToLowerInvariant()) {throw "cloudflared SHA-256 mismatch: expected $($Manifest.sha256), got $Actual"diff --git a/tests/test_cloudflared_packaging.py b/tests/test_cloudflared_packaging.pyindex a20eb8840..f3aa0c01b 100644--- a/tests/test_cloudflared_packaging.py+++ b/tests/test_cloudflared_packaging.py@@ -1,9 +1,16 @@+import hashlibimport json+import re+import shutil+import subprocess+import tempfileimport unittestfrom pathlib import PathROOT = Path(__file__).resolve().parents[1]+FETCH = ROOT / "scripts" / "fetch_cloudflared.ps1"+PWSH = shutil.which("pwsh")class CloudflaredPackagingTests(unittest.TestCase):@@ -14,14 +21,62 @@ class CloudflaredPackagingTests(unittest.TestCase):self.assertTrue(manifest["url"].startswith("https://github.com/cloudflare/cloudflared/"))self.assertEqual(manifest["license"], "Apache-2.0")- def test_fetch_and_build_verify_the_pinned_hash_and_only_standalone_carries_the_sidecar(self):- fetch = (ROOT / "scripts" / "fetch_cloudflared.ps1").read_text(encoding="utf-8-sig")+ def test_only_the_standalone_build_copies_the_verified_sidecar(self):build = (ROOT / "scripts" / "build_windows.ps1").read_text(encoding="utf-8-sig")- self.assertIn("Get-FileHash", fetch)- self.assertIn("Get-FileHash", build)- self.assertIn("Copy-Item -LiteralPath $CloudflaredSource", build)- self.assertIn("if ($Standalone)", build)- self.assertLess(build.index("if ($Standalone) {\n $BuildMode"), build.index("Copy-Item -LiteralPath $CloudflaredSource"))+ guarded = re.search(+ r"if \(\$Standalone\) \{(?:(?!\n\}).)*Copy-Item -LiteralPath \$CloudflaredSource",+ build, re.DOTALL,+ )+ self.assertIsNotNone(guarded, "sidecar 的复制必须留在 -Standalone 分支里")+ self.assertIn("Get-FileHash -LiteralPath $CloudflaredSource", build)++[email protected](PWSH is None, "本机没有 pwsh,跳过实际执行 fetch_cloudflared.ps1")+class CloudflaredFetchTests(unittest.TestCase):+ def setUp(self):+ self.temp = tempfile.TemporaryDirectory()+ self.root = Path(self.temp.name).resolve()+ self.source = self.root / "downloaded.exe"+ self.source.write_bytes(b"pretend cloudflared")+ self.digest = hashlib.sha256(self.source.read_bytes()).hexdigest()+ self.destination = self.root / "vendor" / "cloudflared.exe"++ def tearDown(self):+ self.temp.cleanup()++ def manifest(self, sha256: str) -> Path:+ path = self.root / "manifest.json"+ path.write_text(json.dumps({+ "asset": "cloudflared-windows-amd64.exe",+ "url": "https://github.com/cloudflare/cloudflared/releases/download/test/x.exe",+ "sha256": sha256,+ "version": "test",+ "license": "Apache-2.0",+ }), encoding="utf-8")+ return path++ def fetch(self, sha256: str) -> subprocess.CompletedProcess:+ return subprocess.run(+ [PWSH, "-NoProfile", "-File", str(FETCH),+ "-OutputPath", str(self.destination),+ "-SourcePath", str(self.source),+ "-ManifestPath", str(self.manifest(sha256))],+ capture_output=True, text=True, timeout=120, check=False,+ # pwsh 的报错正文按系统区域编码输出,这里只判断脚本自己抛的那句 ASCII。+ encoding="utf-8", errors="replace",+ )++ def test_a_matching_hash_lands_the_file(self):+ result = self.fetch(self.digest)+ self.assertEqual(result.returncode, 0, result.stderr)+ self.assertEqual(self.destination.read_bytes(), b"pretend cloudflared")++ def test_a_mismatched_hash_fails_and_leaves_nothing_behind(self):+ result = self.fetch("0" * 64)+ self.assertNotEqual(result.returncode, 0)+ self.assertIn("SHA-256 mismatch", result.stderr)+ self.assertFalse(self.destination.exists())+ self.assertEqual(list(self.destination.parent.glob("*")), [])if __name__ == "__main__":