|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Run with `brew ruby` inside the disposable Homebrew e2e prefix. |
| 4 | +require "formulary" |
| 5 | + |
| 6 | +def check(condition, message) |
| 7 | + raise message unless condition |
| 8 | +end |
| 9 | + |
| 10 | +def rejects(message) |
| 11 | + yield |
| 12 | +rescue StandardError => e |
| 13 | + raise unless e.message.include?(message) |
| 14 | +else |
| 15 | + raise "Expected failure: #{message}" |
| 16 | +end |
| 17 | + |
| 18 | +source = Pathname.new(__dir__).join("../../../HomebrewFormula/vp.rb").realpath |
| 19 | +load_formula = lambda do |
| 20 | + Formulary.clear_cache |
| 21 | + Formulary.from_contents("vp", source, source.read) |
| 22 | +end |
| 23 | + |
| 24 | +# Metadata requests are fixtures. Archive downloads use Homebrew's real cache |
| 25 | +# and resource implementation, with bytes placed at the expected cache key. |
| 26 | +metadata = nil |
| 27 | +requests = [] |
| 28 | +commit = "a" * 40 |
| 29 | +commit_key = "voidzero-dev:vite-plus:#{commit}" |
| 30 | +Utils::Curl.define_singleton_method(:curl_output) do |*args, **_options| |
| 31 | + requests << args.last |
| 32 | + raise "Unexpected metadata request" unless metadata |
| 33 | + |
| 34 | + Struct.new(:stdout) do |
| 35 | + def assert_success!; end |
| 36 | + end.new(JSON.generate(metadata)) |
| 37 | +end |
| 38 | +Utils::Curl.define_singleton_method(:curl_headers) do |*args, **_options| |
| 39 | + requests << args.last |
| 40 | + { responses: [{ headers: { "x-commit-key" => commit_key } }] } |
| 41 | +end |
| 42 | + |
| 43 | +ENV.delete("HOMEBREW_VP_PR_VERSION") |
| 44 | +stable = load_formula.call |
| 45 | +check(stable.stable.url.start_with?("https://github.com/voidzero-dev/vite-plus/releases/"), "Stable URL changed") |
| 46 | +check(requests.empty?, "Stable formula requested preview metadata") |
| 47 | +preview = stable.class.const_get(:Preview) |
| 48 | + |
| 49 | +seed = lambda do |platform, sha, bytes = "preview archive"| |
| 50 | + version = "0.0.0-commit.#{sha}" |
| 51 | + package = "@voidzero-dev/vite-plus-cli-#{platform}" |
| 52 | + shasum = Digest::SHA1.hexdigest(bytes) |
| 53 | + url = "https://registry-bridge.viteplus.dev/tarballs/#{package}/#{version}/#{shasum}.tgz" |
| 54 | + archive = HOMEBREW_CACHE/"downloads/#{Digest::SHA256.hexdigest(url)}--#{shasum}.tgz" |
| 55 | + archive.dirname.mkpath |
| 56 | + archive.write(bytes) |
| 57 | + cached_metadata = HOMEBREW_CACHE/"vp-preview/#{sha}-#{platform}.json" |
| 58 | + cached_metadata.unlink if cached_metadata.exist? |
| 59 | + metadata = { |
| 60 | + "name" => package, "version" => version, |
| 61 | + "dist" => { "tarball" => url, "shasum" => shasum, |
| 62 | + "integrity" => "sha512-#{Digest::SHA512.base64digest(bytes)}" }, |
| 63 | + } |
| 64 | + archive |
| 65 | +end |
| 66 | + |
| 67 | +%w[../main main 0 12x abcdef0].each do |ref| |
| 68 | + rejects("PR number or a full commit SHA") { preview.resolve(ref, "darwin-arm64") } |
| 69 | +end |
| 70 | +check(requests.empty?, "Invalid selectors made network requests") |
| 71 | + |
| 72 | +commit_key = "" |
| 73 | +rejects("No published Vite+ preview") { preview.resolve("2740", "darwin-arm64") } |
| 74 | +commit_key = "voidzero-dev:vite-plus:#{commit}" |
| 75 | + |
| 76 | +%w[darwin-arm64 darwin-x64 linux-arm64-gnu linux-x64-gnu].each do |platform| |
| 77 | + archive = seed.call(platform, commit) |
| 78 | + result = preview.resolve("2740", platform) |
| 79 | + check(result[:version] == "0.0.0-commit.#{commit}", "PR did not resolve to a commit") |
| 80 | + check(result[:sha256] == Digest::SHA256.file(archive).hexdigest, "Wrong SHA-256") |
| 81 | + check(ENV["HOMEBREW_VP_PR_VERSION"] == commit, "Build subprocess was not pinned") |
| 82 | + count = requests.length |
| 83 | + check(preview.resolve(commit, platform) == result, "Cached preview changed") |
| 84 | + check(requests.length == count, "Pinned build requested metadata again") |
| 85 | +end |
| 86 | +puts "PR resolution, all four platforms, integrity verification, and offline build subprocesses pass." |
| 87 | + |
| 88 | +platform = "#{OS.mac? ? "darwin" : "linux"}-#{Hardware::CPU.arm? ? "arm64" : "x64"}#{"-gnu" if OS.linux?}" |
| 89 | +expected = preview.resolve(commit, platform) |
| 90 | +ENV["HOMEBREW_VP_PR_VERSION"] = commit.upcase |
| 91 | +selected = load_formula.call |
| 92 | +check(selected.version.to_s == "0.0.0-commit.#{commit}", "Formula did not select the preview") |
| 93 | +check(!selected.disabled?, "Preview kept the stable launch gate") |
| 94 | +check(selected.stable.url == expected[:url], "Wrong formula URL") |
| 95 | +check(selected.stable.checksum.to_s == Digest::SHA256.hexdigest("preview archive"), "Formula lost its checksum") |
| 96 | + |
| 97 | +seed.call(platform, "b" * 40) |
| 98 | +metadata["version"] = "0.3.3" |
| 99 | +rejects("does not match") { preview.resolve("b" * 40, platform) } |
| 100 | +seed.call(platform, "c" * 40) |
| 101 | +metadata["dist"]["tarball"] = "https://example.test/untrusted.tgz" |
| 102 | +rejects("invalid download URL or checksum") { preview.resolve("c" * 40, platform) } |
| 103 | +seed.call(platform, "d" * 40) |
| 104 | +metadata["dist"]["integrity"] = "sha512-invalid" |
| 105 | +rejects("invalid download URL or checksum") { preview.resolve("d" * 40, platform) } |
| 106 | +archive = seed.call(platform, "e" * 40) |
| 107 | +archive.write("corrupted archive") |
| 108 | +rejects("checksum mismatch") { preview.resolve("e" * 40, platform) } |
| 109 | +check(!archive.exist?, "Corrupted download stayed in the cache") |
| 110 | +puts "Wrong versions, foreign URLs, invalid integrity, and corrupt archives are rejected." |
| 111 | + |
| 112 | +ENV.delete("HOMEBREW_VP_PR_VERSION") |
| 113 | +restored = load_formula.call |
| 114 | +check(restored.version == stable.version && restored.stable.url == stable.stable.url, "Stable selection did not return") |
| 115 | +puts "Removing the selector restores the stable formula." |
0 commit comments