Skip to content

Commit 021f85f

Browse files
committed
Have Scanner#initialize raise Bundler::GemfileLockNotFound if no Gemfile.lock exists.
* Catch Bundler::GemfileLockNotFound in Bundler::Audit::CLI#check, print the error message, and exit 1.
1 parent 71ea5bd commit 021f85f

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

lib/bundler/audit/cli.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ def check(dir=Dir.pwd)
5555
end
5656

5757
database = Database.new(options[:database])
58-
scanner = Scanner.new(dir,options[:gemfile_lock],database)
58+
scanner = begin
59+
Scanner.new(dir,options[:gemfile_lock],database)
60+
rescue Bundler::GemfileLockNotFound => exception
61+
say exception.message, :red
62+
exit 1
63+
end
5964
report = scanner.report(:ignore => options.ignore)
6065

6166
output = if options[:output] then File.new(options[:output],'w')

lib/bundler/audit/scanner.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,21 @@ class Scanner
6363
# @param [Database] database
6464
# The database to scan against.
6565
#
66+
# @raise [Bundler::GemfileLockNotFound]
67+
# The `gemfile_lock` file could not be found within the `root`
68+
# directory.
69+
#
6670
def initialize(root=Dir.pwd,gemfile_lock='Gemfile.lock',database=Database.new,config_dot_file='.bundler-audit.yml')
6771
@root = File.expand_path(root)
6872
@database = database
69-
@lockfile = LockfileParser.new(
70-
File.read(File.join(@root,gemfile_lock))
71-
)
73+
74+
gemfile_lock_path = File.join(@root,gemfile_lock)
75+
76+
unless File.file?(gemfile_lock_path)
77+
raise(Bundler::GemfileLockNotFound,"Could not find #{gemfile_lock.inspect} in #{@root.inspect}")
78+
end
79+
80+
@lockfile = LockfileParser.new(File.read(gemfile_lock_path))
7281

7382
config_dot_file_full_path = File.join(@root,config_dot_file)
7483

spec/integration_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@
7979
end
8080
end
8181

82+
context "when auditing a non-existent Gemfile.lock file" do
83+
let(:bundle) { 'secure' }
84+
let(:directory) { File.join('spec','bundle',bundle) }
85+
let(:root) { File.expand_path(directory) }
86+
87+
let(:gemfile_lock) { 'Gemfile.foo.lock' }
88+
let(:command) { "#{super()} --gemfile-lock #{gemfile_lock}" }
89+
90+
subject do
91+
Dir.chdir(directory) { sh(command, :fail => true) }
92+
end
93+
94+
it "should print an error message" do
95+
expect(subject.strip).to eq("Could not find #{gemfile_lock.inspect} in #{root.inspect}")
96+
end
97+
end
98+
8299
describe "update" do
83100

84101
let(:update_command) { "#{command} update" }

0 commit comments

Comments
 (0)