Refactor CLI::Audit.

This commit is contained in:
Markus Reiter 2017-05-19 21:07:25 +02:00
parent a44d4ce88b
commit 1714c73b49
2 changed files with 34 additions and 20 deletions

View File

@ -6,20 +6,24 @@ module Hbc
end
def self.run(*args)
failed_casks = new(args, Auditor).run
return if failed_casks.empty?
raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}"
new(*args).run
end
def initialize(args, auditor)
def initialize(*args, auditor: Auditor)
@args = args
@auditor = auditor
end
def run
casks_to_audit.each_with_object([]) do |cask, failed|
failed << cask unless audit(cask)
failed_casks = []
casks_to_audit.each do |cask|
next if audit(cask)
failed_casks << cask
end
return if failed_casks.empty?
raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}"
end
def audit(cask)

View File

@ -6,54 +6,64 @@ describe Hbc::CLI::Audit, :cask do
it "audits all Casks if no tokens are given" do
allow(Hbc).to receive(:all).and_return([cask, cask])
expect(auditor).to receive(:audit).twice
expect(auditor).to receive(:audit).twice.and_return(true)
run_audit([], auditor)
run_audit
end
it "audits specified Casks if tokens are given" do
cask_token = "nice-app"
expect(Hbc::CaskLoader).to receive(:load).with(cask_token).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false)
expect(auditor).to receive(:audit)
.with(cask, audit_download: false, check_token_conflicts: false)
.and_return(true)
run_audit([cask_token], auditor)
run_audit(cask_token)
end
end
describe "rules for downloading a Cask" do
it "does not download the Cask per default" do
allow(Hbc::CaskLoader).to receive(:load).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false)
expect(auditor).to receive(:audit)
.with(cask, audit_download: false, check_token_conflicts: false)
.and_return(true)
run_audit(["casktoken"], auditor)
run_audit("casktoken")
end
it "download a Cask if --download flag is set" do
allow(Hbc::CaskLoader).to receive(:load).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: true, check_token_conflicts: false)
expect(auditor).to receive(:audit)
.with(cask, audit_download: true, check_token_conflicts: false)
.and_return(true)
run_audit(["casktoken", "--download"], auditor)
run_audit("casktoken", "--download")
end
end
describe "rules for checking token conflicts" do
it "does not check for token conflicts per default" do
allow(Hbc::CaskLoader).to receive(:load).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false)
expect(auditor).to receive(:audit)
.with(cask, audit_download: false, check_token_conflicts: false)
.and_return(true)
run_audit(["casktoken"], auditor)
run_audit("casktoken")
end
it "checks for token conflicts if --token-conflicts flag is set" do
allow(Hbc::CaskLoader).to receive(:load).and_return(cask)
expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: true)
expect(auditor).to receive(:audit)
.with(cask, audit_download: false, check_token_conflicts: true)
.and_return(true)
run_audit(["casktoken", "--token-conflicts"], auditor)
run_audit("casktoken", "--token-conflicts")
end
end
def run_audit(args, auditor)
Hbc::CLI::Audit.new(args, auditor).run
def run_audit(*args)
Hbc::CLI::Audit.new(*args, auditor: auditor).run
end
end