Skip to content

Commit

Permalink
Add support for detecting gems.locked (closes #325, #110).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Feb 8, 2022
1 parent 88a6725 commit bc4b540
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions lib/bundler/audit/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Scanner
# @param [String] root
# The path to the project root.
#
# @param [String] gemfile_lock
# @param [String] lock_file
# Alternative name for the `Gemfile.lock` file.
#
# @param [Database] database
Expand All @@ -70,20 +70,27 @@ class Scanner
# The file name of the bundler-audit config file.
#
# @raise [Bundler::GemfileLockNotFound]
# The `gemfile_lock` file could not be found within the `root`
# directory.
# The `lock_file` file could not be found within the `root`
# directory, or `Gemfiles.lock` and `gems.locked` both do not exist
# within `root`.
#
def initialize(root=Dir.pwd,gemfile_lock='Gemfile.lock',database=Database.new,config_dot_file='.bundler-audit.yml')
def initialize(root=Dir.pwd,lock_file=nil,database=Database.new,config_dot_file='.bundler-audit.yml')
@root = File.expand_path(root)
@database = database

gemfile_lock_path = File.join(@root,gemfile_lock)
if lock_file
lock_file_path = File.join(@root,lock_file)

unless File.file?(gemfile_lock_path)
raise(Bundler::GemfileLockNotFound,"Could not find #{gemfile_lock.inspect} in #{@root.inspect}")
unless File.file?(lock_file_path)
raise(Bundler::GemfileLockNotFound,"Could not find #{lock_file.inspect} in #{@root.inspect}")
end
else
unless (lock_file_path = detect_lock_file)
raise(Bundler::GemfileLockNotFound,"neither Gemfile.lock nor gems.locked found in #{@root.inspect}")
end
end

@lockfile = LockfileParser.new(File.read(gemfile_lock_path))
@lockfile = LockfileParser.new(File.read(lock_file_path))

config_dot_file_full_path = File.absolute_path(config_dot_file, @root)

Expand Down Expand Up @@ -233,6 +240,28 @@ def scan_specs(options={})

private

# Supported lock files.
LOCK_FILES = %w[Gemfile.lock gems.locked]

#
# Detects `Gemfile.lock` or `gems.locked` files within {#root}.
#
# @return [String, nil]
# The path to `Gemfile.lock` or `gems.locked`. `nil` is returned
# if niether `Gemfile.lock` or `gems.locked` were found.
#
def detect_lock_file
LOCK_FILES.each do |name|
path = File.join(@root,name)

if File.file?(path)
return path
end
end

return nil
end

#
# Determines whether a source is internal.
#
Expand Down

0 comments on commit bc4b540

Please sign in to comment.