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 end
def self.run(*args) def self.run(*args)
failed_casks = new(args, Auditor).run new(*args).run
return if failed_casks.empty?
raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}"
end end
def initialize(args, auditor) def initialize(*args, auditor: Auditor)
@args = args @args = args
@auditor = auditor @auditor = auditor
end end
def run def run
casks_to_audit.each_with_object([]) do |cask, failed| failed_casks = []
failed << cask unless audit(cask)
casks_to_audit.each do |cask|
next if audit(cask)
failed_casks << cask
end end
return if failed_casks.empty?
raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}"
end end
def audit(cask) 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 it "audits all Casks if no tokens are given" do
allow(Hbc).to receive(:all).and_return([cask, cask]) 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 end
it "audits specified Casks if tokens are given" do it "audits specified Casks if tokens are given" do
cask_token = "nice-app" cask_token = "nice-app"
expect(Hbc::CaskLoader).to receive(:load).with(cask_token).and_return(cask) 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
end end
describe "rules for downloading a Cask" do describe "rules for downloading a Cask" do
it "does not download the Cask per default" do it "does not download the Cask per default" do
allow(Hbc::CaskLoader).to receive(:load).and_return(cask) 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 end
it "download a Cask if --download flag is set" do it "download a Cask if --download flag is set" do
allow(Hbc::CaskLoader).to receive(:load).and_return(cask) 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
end end
describe "rules for checking token conflicts" do describe "rules for checking token conflicts" do
it "does not check for token conflicts per default" do it "does not check for token conflicts per default" do
allow(Hbc::CaskLoader).to receive(:load).and_return(cask) 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 end
it "checks for token conflicts if --token-conflicts flag is set" do it "checks for token conflicts if --token-conflicts flag is set" do
allow(Hbc::CaskLoader).to receive(:load).and_return(cask) 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
end end
def run_audit(args, auditor) def run_audit(*args)
Hbc::CLI::Audit.new(args, auditor).run Hbc::CLI::Audit.new(*args, auditor: auditor).run
end end
end end