require "bundler/audit/database"
require "bundler/audit/scanner"
require "bundler/lockfile_parser"

module GovukSecurityAudit
  class Scanner < Bundler::Audit::Scanner
    def initialize(path=Dir.pwd)
      path = File.expand_path(path)
      if File.directory?(path)
        path = File.join(path, "Gemfile.lock")
      end

      @root = File.dirname(path)
      @database = Bundler::Audit::Database.new
      @lockfile = Bundler::LockfileParser.new(File.read(path))
    end
  end
end

require "bundler/audit/database"
require "bundler/audit/scanner"
require "bundler/lockfile_parser"

module GovukSecurityAudit
  class Scanner < Bundler::Audit::Scanner
    def initialize(path=Dir.pwd)
      path = File.expand_path(path)
      if File.directory?(path)
        path = File.join(path, "Gemfile.lock")
      end

      @root = File.dirname(path)
      @database = Bundler::Audit::Database.new

      # Stop Bundler trying to find a Gemfile to accompany our Lockfiles
      ENV["BUNDLE_GEMFILE"] = "Dummy"

      @lockfile = Bundler::LockfileParser.new(File.read(path))
    end
  end
end

Allow Scanner to work outside a Bundler environment
Allow Scanner to work outside a Bundler environment

When a LockfileParser is initialised, Bundler will try to find the
related Gemfile in the current directory’s hierarchy. If the tool is
run from a directory that doesn't have a Gemfile in its hierarchy,
Bundler will raise an exception and make the tool fail.

As we don’t need to do anything with the Gemfile in this tool, we tell
Bundler the location of a (dummy) Gemfile explicitly so it doesn’t go
looking.
