Skip to content

Commit

Permalink
Add a block option to checkout_git_repository
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Oct 17, 2024
1 parent 60324ac commit f243d3a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,22 @@ def sync
raise NotImplementedError, N_("sync must be implemented in a subclass")
end

def checkout_git_repository(target_directory)
def checkout_git_repository(target_directory = nil)
return if git_repository.nil?

target_directory_given = !!target_directory
target_directory ||= Pathname.new(Dir.mktmpdir)

git_repository.update_repo
git_repository.checkout(scm_branch, target_directory)

return target_directory unless block_given?

begin
yield target_directory
ensure
FileUtils.rm_rf(target_directory.to_s) unless target_directory_given
end
end

private
Expand Down
8 changes: 6 additions & 2 deletions spec/factories/configuration_script_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
sequence(:name) { |n| "configuration_script_source#{seq_padded_for_sorting(n)}" }
end

factory :embedded_automation_configuration_script_source,
:parent => :configuration_script_source,
:class => "ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource"

factory :ansible_configuration_script_source,
:parent => :configuration_script_source,
:class => "ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScriptSource"
Expand All @@ -12,13 +16,13 @@
:class => "ManageIQ::Providers::Awx::AutomationManager::ConfigurationScriptSource"

factory :embedded_ansible_configuration_script_source,
:parent => :configuration_script_source,
:parent => :embedded_automation_configuration_script_source,
:class => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource" do
scm_url { "https://example.com/foo.git" }
end

factory :embedded_workflow_configuration_script_source,
:parent => :configuration_script_source,
:parent => :embedded_automation_configuration_script_source,
:class => "ManageIQ::Providers::Workflows::AutomationManager::ConfigurationScriptSource" do
scm_url { "https://example.com/foo.git" }
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
RSpec.describe ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource do
let(:subject) { FactoryBot.create(:embedded_automation_configuration_script_source, :scm_url => "https://example.com/foo.git") }
let(:git_repository) { FactoryBot.create(:git_repository) }

describe "#checkout_git_repository" do
before do
expect(subject.git_repository).to receive(:update_repo)
expect(subject.git_repository).to receive(:checkout)
end

context "without a block" do
it "creates a temporary directory" do
expect(Dir).to receive(:mktmpdir).and_return("/tmp/mydir")

subject.checkout_git_repository
end

it "doesn't delete the temporary directory" do
expect(Dir).to receive(:mktmpdir).and_return("/tmp/mydir")
expect(FileUtils).not_to receive(:rm_rf)

subject.checkout_git_repository
end

it "doesn't create a new temp dir when passing in a target directory" do
expect(Dir).not_to receive(:mktmpdir)

subject.checkout_git_repository("/tmp/mydir")
end
end

context "with a block" do
it "creates a temporary directory and deletes it" do
expect(Dir).to receive(:mktmpdir).and_return("/tmp/mydir")
expect(FileUtils).to receive(:rm_rf).with("/tmp/mydir")

subject.checkout_git_repository { |target_directory| target_directory }
end

it "deletes the temporary directory if the block raises an exception" do
expect(Dir).to receive(:mktmpdir).and_return("/tmp/mydir")
expect(FileUtils).to receive(:rm_rf).with("/tmp/mydir")

expect { subject.checkout_git_repository { |_| raise "Exception" } }.to raise_error(RuntimeError, "Exception")
end

it "doesn't create a new temp dir when passing in a target directory" do
expect(Dir).not_to receive(:mktmpdir)
expect(FileUtils).not_to receive(:rm_rf).with("/tmp/mydir")

subject.checkout_git_repository("/tmp/mydir") { |target_directory| target_directory }
end
end
end
end

0 comments on commit f243d3a

Please sign in to comment.