Skip to content

Commit d8af649

Browse files
committed
Correctly handle when Database#update! raises Database::UpdateFailed.
* Updated the CLI specs for when `Database::UpdateFailed` is raised.
1 parent 75e113e commit d8af649

File tree

2 files changed

+32
-42
lines changed

2 files changed

+32
-42
lines changed

lib/bundler/audit/cli.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,23 @@ def update(path=Database.path)
137137

138138
database = Database.new(path)
139139

140-
case database.update!(quiet: options.quiet?)
141-
when true
142-
say("Updated ruby-advisory-db", :green) unless options.quiet?
143-
when false
144-
say_error "Failed updating ruby-advisory-db!", :red
145-
exit 1
146-
when nil
147-
unless Bundler.git_present?
148-
say_error "Git is not installed!", :red
149-
exit 1
140+
begin
141+
case database.update!(quiet: options.quiet?)
142+
when true
143+
say("Updated ruby-advisory-db", :green) unless options.quiet?
144+
when nil
145+
if Bundler.git_present?
146+
unless options.quiet?
147+
say "Skipping update, ruby-advisory-db is not a git repository", :yellow
148+
end
149+
else
150+
say_error "Git is not installed!", :red
151+
exit 1
152+
end
150153
end
151-
152-
say "Skipping update", :yellow
154+
rescue Database::UpdateFailed => error
155+
say error.message, :red
156+
exit 1
153157
end
154158

155159
stats(path) unless options.quiet?

spec/cli_spec.rb

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require 'bundler/audit/cli'
33

44
describe Bundler::Audit::CLI do
5+
let(:database_path) { "/path/to/ruby-advisory-db" }
6+
57
describe ".start" do
68
context "with wrong arguments" do
79
it "exits with error status code" do
@@ -76,23 +78,17 @@
7678

7779
context "when update fails" do
7880
before do
79-
expect(database).to receive(:update!).and_return(false)
81+
expect(database).to receive(:update!).with(quiet: false).and_raise(
82+
Bundler::Audit::Database::UpdateFailed,
83+
"failed to update #{database_path.inspect}"
84+
)
8085
end
8186

82-
it "prints failure message" do
87+
it "must print an error message and exit with 1" do
8388
expect {
84-
begin
89+
expect {
8590
subject.update
86-
rescue SystemExit
87-
end
88-
}.to output(/Failed updating ruby-advisory-db!/).to_stderr
89-
end
90-
91-
it "exits with error status code" do
92-
expect {
93-
# Capture output of `update` only to keep spec output clean.
94-
# The test regarding specific output is above.
95-
expect { subject.update }.to output.to_stdout
91+
}.to output("failed to update #{database_path.inspect}").to_stderr
9692
}.to raise_error(SystemExit) do |error|
9793
expect(error.success?).to eq(false)
9894
expect(error.status).to eq(1)
@@ -136,9 +132,7 @@
136132

137133
context "when update succeeds" do
138134
before do
139-
expect(database).to(
140-
receive(:update!).with(quiet: true).and_return(true)
141-
)
135+
expect(database).to receive(:update!).with(quiet: true).and_return(true)
142136
end
143137

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

149143
context "when update fails" do
150144
before do
151-
expect(database).to(
152-
receive(:update!).with(quiet: true).and_return(false)
145+
expect(database).to receive(:update!).with(quiet: true).and_raise(
146+
Bundler::Audit::Database::UpdateFailed,
147+
"failed to update #{database_path.inspect}"
153148
)
154149
end
155150

156-
it "prints failure message" do
151+
it "must print the error message and exit with an error code" do
157152
expect {
158-
begin
153+
expect {
159154
subject.update
160-
rescue SystemExit
161-
end
162-
}.to_not output.to_stderr
163-
end
164-
165-
it "exits with error status code" do
166-
expect {
167-
# Capture output of `update` only to keep spec output clean.
168-
# The test regarding specific output is above.
169-
expect { subject.update }.to output.to_stdout
155+
}.to output("failed to update: #{database_path.inspect}").to_stderr
170156
}.to raise_error(SystemExit) do |error|
171157
expect(error.success?).to eq(false)
172158
expect(error.status).to eq(1)

0 commit comments

Comments
 (0)