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

Remove Dir.chdir for thread-safety #397

Open
wants to merge 1 commit into
base: master
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
25 changes: 10 additions & 15 deletions lib/bundler/audit/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

require 'bundler/audit/advisory'

require 'open3'
require 'time'
require 'yaml'

Expand Down Expand Up @@ -203,17 +204,15 @@ def git?
#
def update!(options={})
if git?
Dir.chdir(@path) do
command = %w[git pull]
command << '--quiet' if options[:quiet]
command << 'origin' << 'master'
command = %w[git pull]
command << '--quiet' if options[:quiet]
command << 'origin' << 'master'

unless system(*command)
raise(UpdateFailed,"failed to update #{@path.inspect}")
end
_, status = Open3.capture2(*command, chdir: @path)

return true
end
raise(UpdateFailed,"failed to update #{@path.inspect}") unless status.success?

true
end
end

Expand All @@ -227,9 +226,7 @@ def update!(options={})
#
def commit_id
if git?
Dir.chdir(@path) do
`git rev-parse HEAD`.chomp
end
Open3.capture2('git rev-parse HEAD', chdir: @path).first.chomp
end
end

Expand All @@ -242,9 +239,7 @@ def commit_id
#
def last_updated_at
if git?
Dir.chdir(@path) do
Time.parse(`git log --date=iso8601 --pretty="%cd" -1`)
end
Time.parse(Open3.capture2('git log --date=iso8601 --pretty="%cd" -1', chdir: @path).first)
else
File.mtime(@path)
end
Expand Down
20 changes: 15 additions & 5 deletions spec/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@
before { stub_const("#{described_class}::DEFAULT_PATH",dest_dir) }

it "should execute `git pull`" do
expect_any_instance_of(subject).to receive(:system).with('git', 'pull', 'origin', 'master').and_return(true)
status = instance_double('Process::Status', success?: true)
expect(Open3).to receive(:capture2).with('git', 'pull', 'origin', 'master', chdir: dest_dir)
.and_return([anything, status])

subject.update!(quiet: false)
end
Expand All @@ -150,7 +152,9 @@

context "when the `git pull` fails" do
it do
expect_any_instance_of(subject).to receive(:system).with('git', 'pull', 'origin', 'master').and_return(false)
status = instance_double('Process::Status', success?: false)
expect(Open3).to receive(:capture2).with('git', 'pull', 'origin', 'master', chdir: dest_dir)
.and_return([anything, status])

expect(subject.update!(quiet: false)).to eq(false)
end
Expand Down Expand Up @@ -224,22 +228,28 @@
describe "#update!" do
context "when the database is a git repository" do
it do
expect(subject).to receive(:system).with('git', 'pull', 'origin', 'master').and_return(true)
status = instance_double('Process::Status', success?: true)
expect(Open3).to receive(:capture2).with('git', 'pull', 'origin', 'master', chdir: Fixtures::Database::PATH)
.and_return([anything, status])

subject.update!
end

context "when the :quiet option is given" do
it do
expect(subject).to receive(:system).with('git', 'pull', '--quiet', 'origin', 'master').and_return(true)
status = instance_double('Process::Status', success?: true)
expect(Open3).to receive(:capture2).with('git', 'pull', '--quiet', 'origin', 'master', chdir: Fixtures::Database::PATH)
.and_return([anything, status])

subject.update!(quiet: true)
end
end

context "when the `git pull` command fails" do
it do
expect(subject).to receive(:system).with('git', 'pull', 'origin', 'master').and_return(false)
status = instance_double('Process::Status', success?: false)
expect(Open3).to receive(:capture2).with('git', 'pull', 'origin', 'master', chdir: Fixtures::Database::PATH)
.and_return([anything, status])

expect {
subject.update!
Expand Down