Skip to content

Commit

Permalink
Switch to using kwargs in Database.download, update!, and `#updat…
Browse files Browse the repository at this point in the history
…e!`.
  • Loading branch information
postmodern committed Sep 13, 2023
1 parent 35c9f2f commit 44267a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
43 changes: 13 additions & 30 deletions lib/bundler/audit/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,10 @@ def self.exists?(path=DEFAULT_PATH)
#
# Downloads the ruby-advisory-db.
#
# @param [Hash] options
# Additional options.
#
# @option options [String] :path (DEFAULT_PATH)
# @param [String] path
# The destination path for the new ruby-advisory-db.
#
# @option options [Boolean] :quiet
# @param [Boolean] quiet
# Specify whether `git` should be `--quiet`.
#
# @return [Dataase]
Expand All @@ -112,15 +109,9 @@ def self.exists?(path=DEFAULT_PATH)
#
# @since 0.8.0
#
def self.download(options={})
unless (options.keys - [:path, :quiet]).empty?
raise(ArgumentError,"Invalid option(s)")
end

path = options.fetch(:path,DEFAULT_PATH)

def self.download(path: DEFAULT_PATH, quiet: false)
command = %w[git clone]
command << '--quiet' if options[:quiet]
command << '--quiet' if quiet
command << URL << path

unless system(*command)
Expand All @@ -133,37 +124,32 @@ def self.download(options={})
#
# Updates the ruby-advisory-db.
#
# @param [Hash] options
# Additional options.
# @param [Hash{Symbol => Object}] kwargs
# Additional optional keyword arguments for {download} or {#update!}.
#
# @option options [Boolean] :quiet
# @option kwargs [Boolean] :quiet
# Specify whether `git` should be `--quiet`.
#
# @return [Boolean, nil]
# Specifies whether the update was successful.
# A `nil` indicates no update was performed.
#
# @raise [ArgumentError]
# Invalid options were given.
#
# @note
# Requires network access.
#
# @since 0.3.0
#
# @deprecated Use {#update!} instead.
#
def self.update!(options={})
raise "Invalid option(s)" unless (options.keys - [:quiet]).empty?

def self.update!(**kwargs)
if File.directory?(DEFAULT_PATH)
begin
new(DEFAULT_PATH).update!(options)
new(DEFAULT_PATH).update!(**kwargs)
rescue UpdateFailed then false
end
else
begin
download(options.merge(path: DEFAULT_PATH))
download(**kwargs, path: DEFAULT_PATH)
rescue DownloadFailed then false
end
end
Expand All @@ -183,10 +169,7 @@ def git?
#
# Updates the ruby-advisory-db.
#
# @param [Hash] options
# Additional options.
#
# @option options [Boolean] :quiet
# @param [Boolean] quiet
# Specify whether `git` should be `--quiet`.
#
# @return [true, nil]
Expand All @@ -196,11 +179,11 @@ def git?
#
# @since 0.8.0
#
def update!(options={})
def update!(quiet: false)
if git?
Dir.chdir(@path) do
command = %w[git pull]
command << '--quiet' if options[:quiet]
command << '--quiet' if quiet
command << 'origin' << 'master'

unless system(*command)
Expand Down
6 changes: 0 additions & 6 deletions spec/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@
end
end
end

context "when given an invalid option" do
it do
expect { subject.update!(foo: 1) }.to raise_error(RuntimeError)
end
end
end

describe "#initialize" do
Expand Down

0 comments on commit 44267a3

Please sign in to comment.