Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing #12

Open
wants to merge 6 commits into
base: dominik/start-new-release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions lib/fastlane/plugin/ddg_apple_automation/helper/asana_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def self.extract_asana_task_assignee(task_id, asana_access_token)
client = make_asana_client(asana_access_token)

begin
task = client.tasks.get_task(task_gid: task_id, options: { fields: ["assignee"] })
task = client.tasks.get_task(task_gid: task_id, options: { opt_fields: ["assignee"] })
rescue StandardError => e
UI.user_error!("Failed to fetch task assignee: #{e}")
return
Expand All @@ -90,7 +90,7 @@ def self.get_release_automation_subtask_id(task_url, asana_access_token)
asana_client = make_asana_client(asana_access_token)

begin
subtasks = asana_client.tasks.get_subtasks_for_task(task_gid: task_id, options: { fields: ["name", "created_at"] })
subtasks = asana_client.tasks.get_subtasks_for_task(task_gid: task_id, options: { opt_fields: ["name", "created_at"] })
rescue StandardError => e
UI.user_error!("Failed to fetch 'Automation' subtasks for task #{task_id}: #{e}")
return
Expand Down Expand Up @@ -298,10 +298,9 @@ def self.update_asana_tasks_for_public_release(params)

def self.fetch_tasks_for_tag(tag_id, asana_access_token)
asana_client = make_asana_client(asana_access_token)

task_ids = []
begin
response = asana_client.tasks.get_tasks_for_tag(tag_gid: tag_id, options: { fields: ["gid"] })
response = asana_client.tasks.get_tasks_for_tag(tag_gid: tag_id, options: { opt_fields: ["gid"] })
loop do
task_ids += response.map(&:gid)
response = response.next_page
Expand All @@ -315,10 +314,9 @@ def self.fetch_tasks_for_tag(tag_id, asana_access_token)

def self.fetch_subtasks(task_id, asana_access_token)
asana_client = make_asana_client(asana_access_token)

task_ids = []
begin
response = asana_client.tasks.get_subtasks_for_task(task_gid: task_id, options: { fields: ["gid"] })
response = asana_client.tasks.get_subtasks_for_task(task_gid: task_id, options: { opt_fields: ["gid"] })
loop do
task_ids += response.map(&:gid)
response = response.next_page
Expand Down Expand Up @@ -350,19 +348,18 @@ def self.move_tasks_to_section(task_ids, section_id, asana_access_token)

def self.complete_tasks(task_ids, asana_access_token)
asana_client = make_asana_client(asana_access_token)

incident_task_ids = fetch_subtasks(INCIDENTS_PARENT_TASK_ID, asana_access_token)

task_ids.each do |task_id|
if incident_task_ids.include?(task_id)
UI.important("Not completing task #{task_id} because it's an incident task")
break
next
end

projects_ids = asana_client.projects.get_projects_for_task(task_gid: task_id, options: { fields: ["gid"] }).map(&:gid)
projects_ids = asana_client.projects.get_projects_for_task(task_gid: task_id, options: { opt_fields: ["gid"] }).map(&:gid)
if projects_ids.include?(CURRENT_OBJECTIVES_PROJECT_ID)
UI.important("Not completing task #{task_id} because it's a Current Objective")
break
next
end

UI.message("Completing task #{task_id}")
Expand All @@ -373,7 +370,7 @@ def self.complete_tasks(task_ids, asana_access_token)

def self.find_asana_release_tag(tag_name, release_task_id, asana_access_token)
asana_client = make_asana_client(asana_access_token)
release_task_tags = asana_client.tasks.get_task(task_gid: release_task_id, options: { fields: ["tags"] }).tags
release_task_tags = asana_client.tasks.get_task(task_gid: release_task_id, options: { opt_fields: ["tags"] }).tags

if (tag_id = release_task_tags.find { |t| t.name == tag_name }&.gid) && !tag_id.to_s.empty?
return tag_id
Expand Down Expand Up @@ -426,7 +423,7 @@ def self.get_task_ids_from_git_log(from_ref, to_ref = "HEAD")

def self.fetch_release_notes(release_task_id, asana_access_token, output_type: "asana")
asana_client = make_asana_client(asana_access_token)
release_task_body = asana_client.tasks.get_task(task_gid: release_task_id, options: { fields: ["notes"] }).notes
release_task_body = asana_client.tasks.get_task(task_gid: release_task_id, options: { opt_fields: ["notes"] }).notes
ReleaseTaskHelper.extract_release_notes(release_task_body, output_type: output_type)
end
end
Expand Down
418 changes: 418 additions & 0 deletions spec/asana_helper_spec.rb

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions spec/bump_build_number_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@
end
end

describe "class methods" do
it "returns the correct description" do
expect(Fastlane::Actions::BumpBuildNumberAction.description).to eq("Prepares a subsequent internal release")
end

it "returns the correct authors" do
expect(Fastlane::Actions::BumpBuildNumberAction.authors).to eq(["DuckDuckGo"])
end

it "returns the correct return value description" do
expect(Fastlane::Actions::BumpBuildNumberAction.return_value).to eq("The newly created release task ID")
end

it "returns the correct details" do
expect(Fastlane::Actions::BumpBuildNumberAction.details).to eq("This action increments the project build number and pushes the changes to the remote repository.")
end
end

def test_action(platform)
params = { platform: platform }
allow(params).to receive(:values).and_return(params)
Expand Down
201 changes: 201 additions & 0 deletions spec/ddg_apple_automation_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
describe Fastlane::Helper::DdgAppleAutomationHelper do
let(:other_action) { double("other_action") }
let(:platform) { "ios" }
let(:version) { "1.0.0" }
let(:asana_access_token) { "secret-token" }
let(:options) { { username: "user" } }

describe "#process_erb_template" do
it "processes ERB template" do
template = "<h1>Hello, <%= x %>!</h1>"
Expand Down Expand Up @@ -57,4 +63,199 @@ def load_file(file)
Fastlane::Helper::DdgAppleAutomationHelper.load_file(file)
end
end

describe ".code_freeze_prechecks" do
it "performs git and submodule checks" do
expect(other_action).to receive(:ensure_git_status_clean).twice
expect(other_action).to receive(:ensure_git_branch).with(branch: Fastlane::Helper::DdgAppleAutomationHelper::DEFAULT_BRANCH)
expect(other_action).to receive(:git_pull)
expect(other_action).to receive(:git_submodule_update).with(recursive: true, init: true)
Fastlane::Helper::DdgAppleAutomationHelper.code_freeze_prechecks(other_action)
end
end

describe ".validate_new_version" do
it "validates and returns the new version" do
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:current_version).and_return(version)
expect(Fastlane::UI).to receive(:important).with("Current version in project settings is #{version}.")
expect(Fastlane::UI).to receive(:important).with("New version is #{version}.")
allow(Fastlane::UI).to receive(:interactive?).and_return(true)
allow(Fastlane::UI).to receive(:confirm).and_return(true)
expect(Fastlane::Helper::DdgAppleAutomationHelper.validate_new_version(version)).to eq(version)
end
end

describe ".format_version" do
it "formats a version string" do
expect(Fastlane::Helper::DdgAppleAutomationHelper.format_version("1.2.3.4")).to eq("1.2.3")
end
end

describe ".bump_minor_version" do
it "increments the minor version" do
expect(Fastlane::Helper::DdgAppleAutomationHelper.bump_minor_version("1.2.0")).to eq("1.3.0")
end
end

describe ".bump_patch_version" do
it "increments the patch version" do
expect(Fastlane::Helper::DdgAppleAutomationHelper.bump_patch_version("1.2.3")).to eq("1.2.4")
end
end

describe ".current_build_number" do
it "reads the current build number from config" do
allow(File).to receive(:read).and_return("CURRENT_PROJECT_VERSION = 123")
expect(Fastlane::Helper::DdgAppleAutomationHelper.current_build_number).to eq(123)
end
end

describe ".current_version" do
it "reads the current version from config" do
allow(File).to receive(:read).and_return("MARKETING_VERSION = 1.2.3")
expect(Fastlane::Helper::DdgAppleAutomationHelper.current_version).to eq("1.2.3")
end
end

describe ".prepare_release_branch" do
it "prepares the release branch with version updates" do
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:code_freeze_prechecks)
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:validate_new_version).and_return(version)
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:create_release_branch)
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:update_embedded_files)
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:update_version_config)
expect(other_action).to receive(:push_to_git_remote)
release_branch, new_version = Fastlane::Helper::DdgAppleAutomationHelper.prepare_release_branch(platform, version, other_action)
expect(release_branch).to eq("#{Fastlane::Helper::DdgAppleAutomationHelper::RELEASE_BRANCH}/#{version}")
expect(new_version).to eq(version)
end
end

describe ".create_release_branch" do
it "creates a new release branch" do
allow(Fastlane::Actions).to receive(:sh).and_return("")
Fastlane::Helper::DdgAppleAutomationHelper.create_release_branch(version)
expect(Fastlane::Actions).to have_received(:sh).with("git", "branch", "--list", "release/#{version}")
expect(Fastlane::Actions).to have_received(:sh).with("git", "checkout", "-b", "release/#{version}")
expect(Fastlane::Actions).to have_received(:sh).with("git", "push", "-u", "origin", "release/#{version}")
end
end

describe ".update_embedded_files" do
it "updates embedded files and commits them" do
allow(Fastlane::Actions).to receive(:sh).with("./scripts/update_embedded.sh").and_return("")
git_status_output = "On branch main\nmodified: Core/trackerData.json\n"
allow(Fastlane::Actions).to receive(:sh).with("git", "status").and_return(git_status_output)
allow(Fastlane::Actions).to receive(:sh).with("git", "add", "Core/trackerData.json").and_return("")
allow(Fastlane::Actions).to receive(:sh).with("git", "commit", "-m", "Update embedded files").and_return("")
expect(other_action).to receive(:ensure_git_status_clean)
described_class.update_embedded_files(platform, other_action)
expect(Fastlane::Actions).to have_received(:sh).with("git", "status")
expect(Fastlane::Actions).to have_received(:sh).with("git", "add", "Core/trackerData.json")
expect(Fastlane::Actions).to have_received(:sh).with("git", "commit", "-m", "Update embedded files")
end
end

describe ".increment_build_number" do
it "increments the build number" do
allow(File).to receive(:read).with("Configuration/Version.xcconfig").and_return("MARKETING_VERSION = 1.0.0\n")
allow(File).to receive(:read).with("Configuration/BuildNumber.xcconfig").and_return("CURRENT_PROJECT_VERSION = 123\n")
allow(File).to receive(:write)
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:calculate_next_build_number).and_return(124)
allow(Fastlane::UI).to receive(:interactive?).and_return(false)
allow(Fastlane::UI).to receive(:confirm).and_return(true)
allow(Fastlane::UI).to receive(:select).and_return("Current release (123)")
expect(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:update_version_and_build_number_config)
expect(other_action).to receive(:push_to_git_remote)
Fastlane::Helper::DdgAppleAutomationHelper.increment_build_number(platform, options, other_action)
end
end

describe ".calculate_next_build_number" do
it "calculates the next build number" do
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:fetch_testflight_build_number).and_return(123)
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:current_build_number).and_return(124)
allow(Fastlane::UI).to receive(:interactive?).and_return(false)
expect(Fastlane::Helper::DdgAppleAutomationHelper.calculate_next_build_number(platform, options, other_action)).to eq(125)
end
end

describe ".fetch_appcast_build_number" do
it "fetches the highest appcast build number for macOS" do
allow(Fastlane::Helper::DdgAppleAutomationHelper).to receive(:`).with("plutil -extract SUFeedURL raw #{Fastlane::Helper::DdgAppleAutomationHelper::INFO_PLIST}").and_return("https://dummy-url.com/feed.xml\n")
allow(HTTParty).to receive(:get).with("https://dummy-url.com/feed.xml").and_return(
double(body: <<-XML
<rss xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle">
<channel>
<item>
<sparkle:version>100</sparkle:version>
</item>
</channel>
</rss>
XML
)
)

expect(Fastlane::Helper::DdgAppleAutomationHelper.fetch_appcast_build_number("macos")).to eq(100)
end
end

describe ".fetch_testflight_build_number" do
it "fetches the latest testflight build number" do
expect(other_action).to receive(:latest_testflight_build_number).and_return(125)
expect(Fastlane::Helper::DdgAppleAutomationHelper.fetch_testflight_build_number(platform, options, other_action)).to eq(125)
end
end

describe ".get_api_key" do
it "returns the API key if available in environment" do
ENV["APPLE_API_KEY_ID"] = "key_id"
ENV["APPLE_API_KEY_ISSUER"] = "issuer_id"
ENV["APPLE_API_KEY_BASE64"] = "key_base64"
expect(other_action).to receive(:app_store_connect_api_key).with(
key_id: "key_id", issuer_id: "issuer_id", key_content: "key_base64", is_key_content_base64: true
)
Fastlane::Helper::DdgAppleAutomationHelper.get_api_key(other_action)
end
end

describe ".get_username" do
before do
@original_ci_value = Fastlane::Helper.is_ci?
allow(Fastlane::Helper).to receive(:is_ci?).and_return(false)
end

after do
allow(Fastlane::Helper).to receive(:is_ci?).and_return(@original_ci_value)
end

it "fetches the username from options or git config" do
expect(Fastlane::Helper::DdgAppleAutomationHelper.get_username(username: "username")).to eq("username")
end
end

describe ".update_version_config" do
it "updates the version in the config file" do
expect(File).to receive(:write).with(
Fastlane::Helper::DdgAppleAutomationHelper::VERSION_CONFIG_PATH,
"#{Fastlane::Helper::DdgAppleAutomationHelper::VERSION_CONFIG_DEFINITION} = #{version}\n"
)

expect(other_action).to receive(:git_commit).with(
path: Fastlane::Helper::DdgAppleAutomationHelper::VERSION_CONFIG_PATH,
message: "Set marketing version to #{version}"
)

Fastlane::Helper::DdgAppleAutomationHelper.update_version_config(version, other_action)
end
end

describe ".update_version_and_build_number_config" do
it "updates both version and build number in config files" do
expect(File).to receive(:write).with(Fastlane::Helper::DdgAppleAutomationHelper::VERSION_CONFIG_PATH, "#{Fastlane::Helper::DdgAppleAutomationHelper::VERSION_CONFIG_DEFINITION} = #{version}\n")
expect(File).to receive(:write).with(Fastlane::Helper::DdgAppleAutomationHelper::BUILD_NUMBER_CONFIG_PATH, "#{Fastlane::Helper::DdgAppleAutomationHelper::BUILD_NUMBER_CONFIG_DEFINITION} = 123\n")
expect(other_action).to receive(:git_commit)
Fastlane::Helper::DdgAppleAutomationHelper.update_version_and_build_number_config(version, 123, other_action)
end
end
end
38 changes: 38 additions & 0 deletions spec/github_actions_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,44 @@
end
end

describe ".assert_branch_has_changes" do
before do
allow(Fastlane::UI).to receive(:important)
end

context "when the release branch has no changes since the latest tag" do
it "returns false and shows a message" do
allow(Fastlane::Helper::GitHelper).to receive(:`).with("git describe --tags --abbrev=0").and_return("v1.0.0\n")
allow(Fastlane::Helper::GitHelper).to receive(:`).with('git rev-parse "v1.0.0"^{}').and_return("abc123\n")
allow(Fastlane::Helper::GitHelper).to receive(:`).with('git rev-parse "origin/release_branch"').and_return("abc123\n")

expect(Fastlane::Helper::GitHelper.assert_branch_has_changes("release_branch")).to eq(false)
expect(Fastlane::UI).to have_received(:important).with("Release branch's HEAD is already tagged. Skipping automatic release.")
end
end

context "when the release branch has changes since the latest tag" do
it "returns true" do
allow(Fastlane::Helper::GitHelper).to receive(:`).with("git describe --tags --abbrev=0").and_return("v1.0.0\n")
allow(Fastlane::Helper::GitHelper).to receive(:`).with('git rev-parse "v1.0.0"^{}').and_return("abc123\n")
allow(Fastlane::Helper::GitHelper).to receive(:`).with('git rev-parse "origin/release_branch"').and_return("def456\n")
allow(Fastlane::Helper::GitHelper).to receive(:`).with('git diff --name-only "v1.0.0".."origin/release_branch"').and_return("app/file1.rb\napp/file2.rb\n")
expect(Fastlane::Helper::GitHelper.assert_branch_has_changes("release_branch")).to eq(true)
end
end

context "when changes are only in scripts or workflows" do
it "returns false" do
allow(Fastlane::Helper::GitHelper).to receive(:`).with("git describe --tags --abbrev=0").and_return("v1.0.0\n")
allow(Fastlane::Helper::GitHelper).to receive(:`).with('git rev-parse "v1.0.0"^{}').and_return("abc123\n")
allow(Fastlane::Helper::GitHelper).to receive(:`).with('git rev-parse "origin/release_branch"').and_return("def456\n")
allow(Fastlane::Helper::GitHelper).to receive(:`).with('git diff --name-only "v1.0.0".."origin/release_branch"').and_return(".github/workflows/workflow.yml\nscripts/deploy.sh\nfastlane/Fastfile\n")

expect(Fastlane::Helper::GitHelper.assert_branch_has_changes("release_branch")).to eq(false)
end
end
end

def set_output(key, value)
Fastlane::Helper::GitHubActionsHelper.set_output(key, value)
end
Expand Down
Loading