From 1714c73b497b44358fa9fc17d05c767996ac6b7f Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 21:07:25 +0200 Subject: [PATCH] Refactor `CLI::Audit`. --- Library/Homebrew/cask/lib/hbc/cli/audit.rb | 16 +++++---- Library/Homebrew/test/cask/cli/audit_spec.rb | 38 ++++++++++++-------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/audit.rb b/Library/Homebrew/cask/lib/hbc/cli/audit.rb index ec1c33754e..5da2be4cfb 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/audit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/audit.rb @@ -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) diff --git a/Library/Homebrew/test/cask/cli/audit_spec.rb b/Library/Homebrew/test/cask/cli/audit_spec.rb index 007ba1eb3e..312a267f8e 100644 --- a/Library/Homebrew/test/cask/cli/audit_spec.rb +++ b/Library/Homebrew/test/cask/cli/audit_spec.rb @@ -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