Skip to content

Commit

Permalink
Raise a GitnotInstalled exception if git is not installed (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Sep 13, 2023
1 parent 35c9f2f commit 449625e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/bundler/audit/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def download(path=Database.path)

begin
Database.download(path: path, quiet: options.quiet?)
rescue Database::DownloadFailed => error
rescue Database::GitNotInstalled, Database::DownloadFailed => error
say error.message, :red
exit 1
end
Expand Down
48 changes: 40 additions & 8 deletions lib/bundler/audit/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ module Audit
#
class Database

#
# @since 0.10.0
#
class GitNotInstalled < RuntimeError
end

class DownloadFailed < RuntimeError
end

Expand Down Expand Up @@ -104,8 +110,9 @@ def self.exists?(path=DEFAULT_PATH)
# @return [Dataase]
# The newly downloaded database.
#
# @raise [DownloadFailed]
# Indicates that the download failed.
# @raise [DownloadFailed, GitNotInstalled]
# * {DownloadFailed} - the `git clone` command failed.
# * {GitNotInstalled} - the `git` command is not installed.
#
# @note
# Requires network access.
Expand All @@ -123,8 +130,11 @@ def self.download(options={})
command << '--quiet' if options[:quiet]
command << URL << path

unless system(*command)
case system(*command)
when false
raise(DownloadFailed,"failed to download #{URL} to #{path.inspect}")
when nil
raise(GitNotInstalled,"the git command is not installed")
end

return new(path)
Expand All @@ -143,8 +153,9 @@ def self.download(options={})
# Specifies whether the update was successful.
# A `nil` indicates no update was performed.
#
# @raise [ArgumentError]
# Invalid options were given.
# @raise [UpdateFailed, GitNotInstalled]
# * {UpdateFailed} - the `git pull` command failed.
# * {GitNotInstalled} - the `git` command is not installed.
#
# @note
# Requires network access.
Expand Down Expand Up @@ -194,6 +205,10 @@ def git?
# `nil` indicates the database is not a git repository, thus not
# capable of being updated.
#
# @raise [UpdateFailed, GitNotInstalled]
# * {UpdateFailed} - the `git pull` command failed.
# * {GitNotInstalled} - the `git` command is not installed.
#
# @since 0.8.0
#
def update!(options={})
Expand All @@ -203,8 +218,11 @@ def update!(options={})
command << '--quiet' if options[:quiet]
command << 'origin' << 'master'

unless system(*command)
case system(*command)
when false
raise(UpdateFailed,"failed to update #{@path.inspect}")
when nil
raise(GitNotInstalled,"the git command is not installed")
end

return true
Expand All @@ -218,12 +236,19 @@ def update!(options={})
# @return [String, nil]
# The commit hash or `nil` if the database is not a git repository.
#
# @raise [GitNotInstalled]
# The `git` command is not installed.
#
# @since 0.9.0
#
def commit_id
if git?
Dir.chdir(@path) do
`git rev-parse HEAD`.chomp
begin
`git rev-parse HEAD`.chomp
rescue Errno::ENOENT
raise(GitNotInstalled,"the git command is not installed")
end
end
end
end
Expand All @@ -233,12 +258,19 @@ def commit_id
#
# @return [Time]
#
# @raise [GitNotInstalled]
# The `git` command is not installed.
#
# @since 0.8.0
#
def last_updated_at
if git?
Dir.chdir(@path) do
Time.parse(`git log --date=iso8601 --pretty="%cd" -1`)
begin
Time.parse(`git log --date=iso8601 --pretty="%cd" -1`)
rescue Errno::ENOENT
raise(GitNotInstalled,"the git command is not installed")
end
end
else
File.mtime(@path)
Expand Down
52 changes: 52 additions & 0 deletions spec/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@
end
end

context "when git is not installed" do
it do
expect(subject).to receive(:system).with('git', 'clone', url, path).and_return(nil)

expect {
subject.download
}.to raise_error(described_class::GitNotInstalled,"the git command is not installed")
end
end

context "with an unknown option" do
it do
expect {
Expand Down Expand Up @@ -131,6 +141,18 @@
end
end

context "when git is not installed" do
before { stub_const("#{described_class}::URL",'https://example.com/') }

it do
expect(subject).to receive(:system).with('git', 'clone', url, path).and_return(nil)

expect {
subject.update!(quiet: false)
}.to raise_error(described_class::GitNotInstalled,"the git command is not installed")
end
end

after { FileUtils.rm_rf(dest_dir) }
end

Expand All @@ -155,6 +177,16 @@
expect(subject.update!(quiet: false)).to eq(false)
end
end

context "when git is not installed" do
it do
expect_any_instance_of(subject).to receive(:system).with('git', 'pull', 'origin', 'master').and_return(nil)

expect {
subject.update!(quiet: false)
}.to raise_error(described_class::GitNotInstalled,"the git command is not installed")
end
end
end

context "when given an invalid option" do
Expand Down Expand Up @@ -270,6 +302,16 @@
it "should return the last commit ID" do
expect(subject.commit_id).to be == last_commit
end

context "when git is not installed" do
it do
expect(subject).to receive(:`).with('git rev-parse HEAD').and_raise(Errno::ENOENT)

expect {
subject.commit_id
}.to raise_error(described_class::GitNotInstalled,"the git command is not installed")
end
end
end

context "when the database is a bare directory" do
Expand Down Expand Up @@ -299,6 +341,16 @@
it "should return the timestamp of the last commit" do
expect(subject.last_updated_at).to be == last_commit_timestamp
end

context "when git is not installed" do
it do
expect(subject).to receive(:`).with('git log --date=iso8601 --pretty="%cd" -1').and_raise(Errno::ENOENT)

expect {
subject.last_updated_at
}.to raise_error(described_class::GitNotInstalled,"the git command is not installed")
end
end
end

context "when the database is a bare directory" do
Expand Down

0 comments on commit 449625e

Please sign in to comment.