Skip to content

Commit

Permalink
Correctly handle when Database#update! raises `Database::UpdateFail…
Browse files Browse the repository at this point in the history
…ed`.

* Updated the CLI specs for when `Database::UpdateFailed` is raised.
  • Loading branch information
postmodern committed Sep 13, 2023
1 parent 75e113e commit d8af649
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
28 changes: 16 additions & 12 deletions lib/bundler/audit/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,23 @@ def update(path=Database.path)

database = Database.new(path)

case database.update!(quiet: options.quiet?)
when true
say("Updated ruby-advisory-db", :green) unless options.quiet?
when false
say_error "Failed updating ruby-advisory-db!", :red
exit 1
when nil
unless Bundler.git_present?
say_error "Git is not installed!", :red
exit 1
begin
case database.update!(quiet: options.quiet?)
when true
say("Updated ruby-advisory-db", :green) unless options.quiet?
when nil
if Bundler.git_present?
unless options.quiet?
say "Skipping update, ruby-advisory-db is not a git repository", :yellow
end
else
say_error "Git is not installed!", :red
exit 1
end
end

say "Skipping update", :yellow
rescue Database::UpdateFailed => error
say error.message, :red
exit 1
end

stats(path) unless options.quiet?
Expand Down
46 changes: 16 additions & 30 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'bundler/audit/cli'

describe Bundler::Audit::CLI do
let(:database_path) { "/path/to/ruby-advisory-db" }

describe ".start" do
context "with wrong arguments" do
it "exits with error status code" do
Expand Down Expand Up @@ -76,23 +78,17 @@

context "when update fails" do
before do
expect(database).to receive(:update!).and_return(false)
expect(database).to receive(:update!).with(quiet: false).and_raise(
Bundler::Audit::Database::UpdateFailed,
"failed to update #{database_path.inspect}"
)
end

it "prints failure message" do
it "must print an error message and exit with 1" do
expect {
begin
expect {
subject.update
rescue SystemExit
end
}.to output(/Failed updating ruby-advisory-db!/).to_stderr
end

it "exits with error status code" do
expect {
# Capture output of `update` only to keep spec output clean.
# The test regarding specific output is above.
expect { subject.update }.to output.to_stdout
}.to output("failed to update #{database_path.inspect}").to_stderr
}.to raise_error(SystemExit) do |error|
expect(error.success?).to eq(false)
expect(error.status).to eq(1)
Expand Down Expand Up @@ -136,9 +132,7 @@

context "when update succeeds" do
before do
expect(database).to(
receive(:update!).with(quiet: true).and_return(true)
)
expect(database).to receive(:update!).with(quiet: true).and_return(true)
end

it "does not print any output" do
Expand All @@ -148,25 +142,17 @@

context "when update fails" do
before do
expect(database).to(
receive(:update!).with(quiet: true).and_return(false)
expect(database).to receive(:update!).with(quiet: true).and_raise(
Bundler::Audit::Database::UpdateFailed,
"failed to update #{database_path.inspect}"
)
end

it "prints failure message" do
it "must print the error message and exit with an error code" do
expect {
begin
expect {
subject.update
rescue SystemExit
end
}.to_not output.to_stderr
end

it "exits with error status code" do
expect {
# Capture output of `update` only to keep spec output clean.
# The test regarding specific output is above.
expect { subject.update }.to output.to_stdout
}.to output("failed to update: #{database_path.inspect}").to_stderr
}.to raise_error(SystemExit) do |error|
expect(error.success?).to eq(false)
expect(error.status).to eq(1)
Expand Down

0 comments on commit d8af649

Please sign in to comment.