From 6c22889d261c52112f87dfc34cce0fb02c48d5e6 Mon Sep 17 00:00:00 2001 From: Dominik Kapusta Date: Mon, 16 Sep 2024 23:08:01 +0200 Subject: [PATCH] Rewrite tag release action spec --- spec/tag_release_action_spec.rb | 166 +++++++++++++++++--------------- 1 file changed, 89 insertions(+), 77 deletions(-) diff --git a/spec/tag_release_action_spec.rb b/spec/tag_release_action_spec.rb index 12bdb26..00887cd 100644 --- a/spec/tag_release_action_spec.rb +++ b/spec/tag_release_action_spec.rb @@ -1,8 +1,7 @@ -describe Fastlane::Actions::TagReleaseAction do +shared_context "common setup" do before do @params = { asana_access_token: "asana-token", - platform: "macos", asana_task_url: "https://app.asana.com/0/0/1/f", is_prerelease: true, github_token: "github-token" @@ -12,8 +11,49 @@ allow(Fastlane::Action).to receive(:other_action).and_return(@other_action) allow(Fastlane::Helper).to receive(:setup_git_user) end +end + +shared_context "on ios" do + before do + @params[:platform] = "ios" + Fastlane::Actions::TagReleaseAction.setup_constants(@params[:platform]) + end +end + +shared_context "on macos" do + before do + @params[:platform] = "macos" + Fastlane::Actions::TagReleaseAction.setup_constants(@params[:platform]) + end +end + +shared_context "for prerelease" do + before do + @params[:is_prerelease] = true + @tag = "1.1.0-123" + @promoted_tag = nil + allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:compute_tag).and_return([@tag, @promoted_tag]) + end +end +shared_context "for public release" do + before do + @params[:is_prerelease] = false + @tag = "1.1.0" + @promoted_tag = "1.1.0-123" + allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:compute_tag).and_return([@tag, @promoted_tag]) + end +end + +describe Fastlane::Actions::TagReleaseAction do describe "#run" do + subject do + configuration = Fastlane::ConfigurationHelper.parse(Fastlane::Actions::TagReleaseAction, @params) + Fastlane::Actions::TagReleaseAction.run(configuration) + end + + include_context "common setup" + before do allow(Fastlane::Actions::TagReleaseAction).to receive(:create_tag_and_github_release).and_return(@tag_and_release_output) allow(Fastlane::Actions::TagReleaseAction).to receive(:merge_or_delete_branch) @@ -21,7 +61,7 @@ end it "creates tag and release, merges branch and reports status" do - test_action(@params) + subject expect(@tag_and_release_output[:merge_or_delete_successful]).to be_truthy # expect(Fastlane::Actions::TagReleaseAction).to have_received(:report_status).with("macos") @@ -29,57 +69,48 @@ it "reports status when merge or delete failed" do allow(Fastlane::Actions::TagReleaseAction).to receive(:merge_or_delete_branch).and_raise(StandardError) - test_action(@params) + subject expect(@tag_and_release_output[:merge_or_delete_successful]).to be_falsy # expect(Fastlane::Actions::TagReleaseAction).to have_received(:report_status).with("macos") end - - def test_action(params) - configuration = Fastlane::ConfigurationHelper.parse(Fastlane::Actions::TagReleaseAction, params) - Fastlane::Actions::TagReleaseAction.run(configuration) - end end describe "#create_tag_and_github_release" do + subject { Fastlane::Actions::TagReleaseAction.create_tag_and_github_release(@params[:is_prerelease], @params[:github_token]) } + let (:latest_public_release) { double(tag_name: "1.0.0") } let (:generated_release_notes) { { body: { "name" => "1.1.0", "body" => "Release notes" } } } + let (:other_action) { double(add_git_tag: nil, push_git_tags: nil, github_api: generated_release_notes, set_github_release: nil) } - before do - Fastlane::Actions::TagReleaseAction.setup_constants("macos") - allow(Octokit::Client).to receive(:new).and_return(double(latest_release: latest_public_release)) - allow(JSON).to receive(:parse).and_return(generated_release_notes[:body]) - @other_action = double(add_git_tag: nil, push_git_tags: nil, github_api: generated_release_notes, set_github_release: nil) - allow(Fastlane::Action).to receive(:other_action).and_return(@other_action) - allow(Fastlane::UI).to receive(:message) - allow(Fastlane::UI).to receive(:important) - end + shared_examples "expected" do |repo_name| + let (:repo_name) { repo_name } - describe "for prerelease" do - before do - @params[:is_prerelease] = true - @tag = "1.1.0-123" - allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:compute_tag).and_return([@tag, nil]) + before(:each) do + allow(Octokit::Client).to receive(:new).and_return(double(latest_release: latest_public_release)) + allow(JSON).to receive(:parse).and_return(generated_release_notes[:body]) + allow(Fastlane::Action).to receive(:other_action).and_return(other_action) + allow(Fastlane::UI).to receive(:message) + allow(Fastlane::UI).to receive(:important) end it "creates tag and github release" do - expect(create_tag_and_github_release(@params)).to eq({ - tag: @tag, - promoted_tag: nil, - tag_created: true, - latest_public_release_tag: latest_public_release.tag_name - }) + expect(subject).to eq({ + tag: @tag, + promoted_tag: @promoted_tag, + tag_created: true, + latest_public_release_tag: latest_public_release.tag_name + }) expect(Fastlane::UI).to have_received(:message).with("Latest public release: #{latest_public_release.tag_name}").ordered - expect(Fastlane::UI).to have_received(:message).with("Generating duckduckgo/macos-browser release notes for GitHub release for tag: #{@tag}").ordered + expect(Fastlane::UI).to have_received(:message).with("Generating #{repo_name} release notes for GitHub release for tag: #{@tag}").ordered + expect(other_action).to have_received(:add_git_tag).with(tag: @tag) + expect(other_action).to have_received(:push_git_tags).with(tag: @tag) - expect(@other_action).to have_received(:add_git_tag).with(tag: @tag) - expect(@other_action).to have_received(:push_git_tags).with(tag: @tag) - - expect(@other_action).to have_received(:github_api).with( + expect(other_action).to have_received(:github_api).with( api_bearer: @params[:github_token], http_method: "POST", - path: "/repos/duckduckgo/macos-browser/releases/generate-notes", + path: "/repos/#{repo_name}/releases/generate-notes", body: { tag_name: @tag, previous_tag_name: latest_public_release.tag_name @@ -88,8 +119,8 @@ def test_action(params) expect(JSON).to have_received(:parse).with(generated_release_notes[:body]) - expect(@other_action).to have_received(:set_github_release).with( - repository_name: "duckduckgo/macos-browser", + expect(other_action).to have_received(:set_github_release).with( + repository_name: repo_name, api_bearer: @params[:github_token], tag_name: @tag, name: generated_release_notes[:body]["name"], @@ -100,53 +131,34 @@ def test_action(params) end end - describe "for public release" do - before do - @params[:is_prerelease] = false - @tag = "1.1.0" - @promoted_tag = "1.1.0-123" - allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:compute_tag).and_return([@tag, @promoted_tag]) - end + include_context "common setup" - it "creates tag and github release" do - expect(create_tag_and_github_release(@params)).to eq({ - tag: @tag, - promoted_tag: @promoted_tag, - tag_created: true, - latest_public_release_tag: latest_public_release.tag_name - }) + context "on ios" do + include_context "on ios" - expect(Fastlane::UI).to have_received(:message).with("Latest public release: #{latest_public_release.tag_name}").ordered - expect(Fastlane::UI).to have_received(:message).with("Generating duckduckgo/macos-browser release notes for GitHub release for tag: #{@tag}").ordered - expect(@other_action).to have_received(:add_git_tag).with(tag: @tag) - expect(@other_action).to have_received(:push_git_tags).with(tag: @tag) + context "for prerelease" do + include_context "for prerelease" + it_behaves_like "expected", "duckduckgo/ios" + end - expect(@other_action).to have_received(:github_api).with( - api_bearer: @params[:github_token], - http_method: "POST", - path: "/repos/duckduckgo/macos-browser/releases/generate-notes", - body: { - tag_name: @tag, - previous_tag_name: latest_public_release.tag_name - } - ) + context "for public release" do + include_context "for public release" + it_behaves_like "expected", "duckduckgo/ios" + end + end - expect(JSON).to have_received(:parse).with(generated_release_notes[:body]) + context "on macos" do + include_context "on macos" - expect(@other_action).to have_received(:set_github_release).with( - repository_name: "duckduckgo/macos-browser", - api_bearer: @params[:github_token], - tag_name: @tag, - name: generated_release_notes[:body]["name"], - description: generated_release_notes[:body]["body"], - is_prerelease: @params[:is_prerelease] - ) - expect(Fastlane::UI).not_to have_received(:important) + context "for prerelease" do + include_context "for prerelease" + it_behaves_like "expected", "duckduckgo/macos-browser" end - end - def create_tag_and_github_release(params) - Fastlane::Actions::TagReleaseAction.create_tag_and_github_release(params[:is_prerelease], params[:github_token]) + context "for public release" do + include_context "for public release" + it_behaves_like "expected", "duckduckgo/macos-browser" + end end end end