|
| 1 | +require "minitest/autorun" |
| 2 | +require "tmpdir" |
| 3 | +require "fileutils" |
| 4 | +require "open3" |
| 5 | + |
| 6 | +class ReleaseTest < Minitest::Test |
| 7 | + ROOT = File.expand_path("..", __dir__) |
| 8 | + CURRENT = Gem::Specification.load(Dir[File.join(ROOT, "*.gemspec")].fetch(0)).version.to_s |
| 9 | + |
| 10 | + def setup |
| 11 | + @directory = Dir.mktmpdir("quack-release") |
| 12 | + @checkout = File.join(@directory, "checkout") |
| 13 | + @remote = File.join(@directory, "remote.git") |
| 14 | + FileUtils.mkdir_p(@checkout) |
| 15 | + paths = Dir.chdir(ROOT) { Dir["*.gemspec", "**/Gemfile*.lock"] } |
| 16 | + paths.concat(%w[lib/quack.rb ext/quack/Cargo.toml ext/quack/Cargo.lock]) if File.file?(File.join(ROOT, "quack-ruby.gemspec")) |
| 17 | + paths.each do |path| |
| 18 | + target = File.join(@checkout, path) |
| 19 | + FileUtils.mkdir_p(File.dirname(target)) |
| 20 | + FileUtils.cp(File.join(ROOT, path), target) |
| 21 | + end |
| 22 | + command("git", "init", "--bare", @remote) |
| 23 | + command("git", "init", "-b", "main") |
| 24 | + command("git", "config", "user.name", "Release test") |
| 25 | + command("git", "config", "user.email", "release@example.invalid") |
| 26 | + command("git", "add", ".") |
| 27 | + command("git", "commit", "-m", "Initial") |
| 28 | + command("git", "remote", "add", "origin", @remote) |
| 29 | + command("git", "push", "origin", "main") |
| 30 | + end |
| 31 | + |
| 32 | + def teardown |
| 33 | + FileUtils.remove_entry(@directory) |
| 34 | + end |
| 35 | + |
| 36 | + def command(*args) |
| 37 | + output, status = Open3.capture2e(*args, chdir: @checkout) |
| 38 | + assert status.success?, output |
| 39 | + output.strip |
| 40 | + end |
| 41 | + |
| 42 | + def release(bump: "patch", tag: "") |
| 43 | + Open3.capture2e({"BUMP" => bump, "RELEASE_TAG" => tag, "GITHUB_REF" => "refs/heads/main", |
| 44 | + "GITHUB_OUTPUT" => File.join(@directory, "outputs")}, |
| 45 | + RbConfig.ruby, File.join(ROOT, "script/prepare-release"), chdir: @checkout) |
| 46 | + end |
| 47 | + |
| 48 | + %w[major minor patch].each_with_index do |bump, index| |
| 49 | + define_method("test_#{bump}_bumps_and_pushes_consistent_versions") do |
| 50 | + parts = CURRENT.split(".").map(&:to_i) |
| 51 | + parts[index] += 1 |
| 52 | + ((index + 1)...3).each { |position| parts[position] = 0 } |
| 53 | + version = parts.join(".") |
| 54 | + output, status = release(bump: bump) |
| 55 | + assert status.success?, output |
| 56 | + spec = Gem::Specification.load(Dir[File.join(@checkout, "*.gemspec")].fetch(0)) |
| 57 | + assert_equal version, spec.version.to_s |
| 58 | + Dir[File.join(@checkout, "**/Gemfile*.lock")].each do |lock| |
| 59 | + assert_includes File.read(lock), " #{spec.name} (#{version})" |
| 60 | + end |
| 61 | + if spec.name == "quack-ruby" |
| 62 | + assert_includes File.read(File.join(@checkout, "lib/quack.rb")), "VERSION = \"#{version}\"" |
| 63 | + assert_includes File.read(File.join(@checkout, "ext/quack/Cargo.toml")), "version = \"#{version}\"" |
| 64 | + assert_includes File.read(File.join(@checkout, "ext/quack/Cargo.lock")), "name = \"quack-ruby\"\nversion = \"#{version}\"" |
| 65 | + end |
| 66 | + assert_equal command("git", "rev-parse", "HEAD"), command("git", "--git-dir", @remote, "rev-parse", "refs/tags/v#{version}^{commit}") |
| 67 | + assert_empty command("git", "status", "--porcelain") |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def test_existing_tag_keeps_version_and_commit |
| 72 | + command("git", "tag", "v#{CURRENT}") |
| 73 | + before = command("git", "rev-parse", "HEAD") |
| 74 | + output, status = release(tag: "v#{CURRENT}") |
| 75 | + assert status.success?, output |
| 76 | + assert_equal before, command("git", "rev-parse", "HEAD") |
| 77 | + assert_includes File.read(File.join(@directory, "outputs")), "tag=v#{CURRENT}" |
| 78 | + end |
| 79 | + |
| 80 | + def test_mismatched_tag_is_rejected |
| 81 | + command("git", "tag", "v9999.0.0") |
| 82 | + output, status = release(tag: "v9999.0.0") |
| 83 | + refute status.success? |
| 84 | + assert_includes output, "release tag does not match source version" |
| 85 | + assert_empty command("git", "status", "--porcelain") |
| 86 | + end |
| 87 | +end |
0 commit comments