-
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathcli_spec.rb
More file actions
244 lines (208 loc) · 6.77 KB
/
cli_spec.rb
File metadata and controls
244 lines (208 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require 'spec_helper'
require 'bundler/audit/cli'
describe Bundler::Audit::CLI do
describe ".start" do
context "with wrong arguments" do
it "exits with error status code" do
expect {
described_class.start ["check", "foo/bar/baz"]
}.to raise_error(SystemExit) do |error|
expect(error.success?).to eq(false)
expect(error.status).to eq(1)
end
end
end
end
describe "#stats" do
let(:size) { 1234 }
let(:last_updated_at) { Time.now }
let(:commit_id) { 'f0f97c4c493b853319e029d226e96f2c2f0dc539' }
let(:database) { double(Bundler::Audit::Database) }
before do
expect(Bundler::Audit::Database).to receive(:new).and_return(database)
expect(database).to receive(:size).and_return(size)
expect(database).to receive(:last_updated_at).and_return(last_updated_at)
expect(database).to receive(:commit_id).and_return(commit_id)
end
it "prints total advisory count" do
expect { subject.stats }.to output(
include(
"advisories:\t#{size} advisories",
"last updated:\t#{last_updated_at}",
"commit:\t#{commit_id}"
)
).to_stdout
end
end
describe "#update" do
let(:database) { double(Bundler::Audit::Database) }
before do
allow(Bundler::Audit::Database).to receive(:new).and_return(database)
end
context "not --quiet (the default)" do
context "when update succeeds" do
let(:size) { 1234 }
let(:last_updated_at) { Time.now }
let(:commit_id) { 'f0f97c4c493b853319e029d226e96f2c2f0dc539' }
before do
expect(database).to receive(:update!).and_return(true)
expect(database).to receive(:size).and_return(size)
expect(database).to receive(:last_updated_at).and_return(last_updated_at)
expect(database).to receive(:commit_id).and_return(commit_id)
end
it "prints updated message and then the stats" do
expect { subject.update }.to output(
include(
"Updated ruby-advisory-db",
"ruby-advisory-db:",
" advisories:\t#{size} advisories",
" last updated:\t#{last_updated_at}",
" commit:\t#{commit_id}"
)
).to_stdout
end
end
context "when update fails" do
before do
expect(database).to receive(:update!).and_return(false)
end
it "prints failure message" do
expect {
begin
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 raise_error(SystemExit) do |error|
expect(error.success?).to eq(false)
expect(error.status).to eq(1)
end
end
end
context "when git is not installed" do
before do
expect(database).to receive(:update!).and_return(nil)
expect(Bundler).to receive(:git_present?).and_return(false)
end
it "prints failure message" do
expect {
begin
subject.update
rescue SystemExit
end
}.to output(/Git is not installed!/).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 raise_error(SystemExit) do |error|
expect(error.success?).to eq(false)
expect(error.status).to eq(1)
end
end
end
end
context "--quiet" do
subject do
described_class.new([], {quiet: true})
end
context "when update succeeds" do
before do
expect(database).to(
receive(:update!).with(quiet: true).and_return(true)
)
end
it "does not print any output" do
expect { subject.update }.to_not output.to_stdout
end
end
context "when update fails" do
before do
expect(database).to(
receive(:update!).with(quiet: true).and_return(false)
)
end
it "prints failure message" do
expect {
begin
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 raise_error(SystemExit) do |error|
expect(error.success?).to eq(false)
expect(error.status).to eq(1)
end
end
end
end
end
describe "#check with gems.locked support" do
let(:fixtures_dir) { File.join(__dir__, 'fixtures') }
let(:bundle_dir) { File.join(fixtures_dir, 'gems_locked_cli_test') }
before do
FileUtils.mkdir_p(bundle_dir)
File.write(File.join(bundle_dir, 'gems.locked'), <<~LOCKFILE)
GEM
remote: https://rubygems.org/
specs:
thor (1.1.0)
PLATFORMS
ruby
DEPENDENCIES
thor
BUNDLED WITH
2.2.33
LOCKFILE
end
after do
FileUtils.rm_rf(bundle_dir)
end
it "should auto-detect gems.locked file" do
cli = described_class.new
expect {
cli.check(bundle_dir)
}.to output(/thor/).to_stdout
end
it "should work with explicit --gemfile-lock gems.locked" do
cli = described_class.new([], { gemfile_lock: 'gems.locked' })
expect {
cli.check(bundle_dir)
}.to output(/thor/).to_stdout
end
end
describe "#check with gems.rb support" do
let(:fixtures_dir) { File.join(__dir__, 'fixtures') }
let(:gems_rb_dir) { File.join(fixtures_dir, 'gems_rb_test') }
before do
FileUtils.mkdir_p(gems_rb_dir)
File.write(File.join(gems_rb_dir, 'gems.rb'), <<~GEMFILE)
source 'https://rubygems.org'
gem 'thor'
GEMFILE
end
after do
FileUtils.rm_rf(gems_rb_dir)
end
it "should provide helpful error when gems.locked is missing" do
cli = described_class.new
expect {
cli.check(gems_rb_dir)
}.to output(/gems.rb found but gems.locked is missing/).to_stderr
end
end
end