Refactor CLI::Style.
This commit is contained in:
parent
957c5fb4f0
commit
98f91fb883
@ -8,19 +8,24 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.run(*args)
|
def self.run(*args)
|
||||||
retval = new(args).run
|
new(*args).run
|
||||||
raise CaskError, "style check failed" unless retval
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :args
|
attr_reader :args
|
||||||
def initialize(args)
|
def initialize(*args)
|
||||||
@args = args
|
@cask_tokens = self.class.cask_tokens_from(args)
|
||||||
|
@fix = args.any? { |arg| arg =~ /^--(fix|(auto-?)?correct)$/ }
|
||||||
|
end
|
||||||
|
|
||||||
|
def fix?
|
||||||
|
@fix
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
install_rubocop
|
install_rubocop
|
||||||
system "rubocop", *rubocop_args, "--", *cask_paths
|
system "rubocop", *rubocop_args, "--", *cask_paths
|
||||||
$CHILD_STATUS.success?
|
raise CaskError, "style check failed" unless $CHILD_STATUS.success?
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def install_rubocop
|
def install_rubocop
|
||||||
@ -34,19 +39,15 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cask_paths
|
def cask_paths
|
||||||
@cask_paths ||= if cask_tokens.empty?
|
@cask_paths ||= if @cask_tokens.empty?
|
||||||
Hbc.all_tapped_cask_dirs
|
Hbc.all_tapped_cask_dirs
|
||||||
elsif cask_tokens.any? { |file| File.exist?(file) }
|
elsif @cask_tokens.any? { |file| File.exist?(file) }
|
||||||
cask_tokens
|
@cask_tokens
|
||||||
else
|
else
|
||||||
cask_tokens.map { |token| CaskLoader.path(token) }
|
@cask_tokens.map { |token| CaskLoader.path(token) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cask_tokens
|
|
||||||
@cask_tokens ||= self.class.cask_tokens_from(args)
|
|
||||||
end
|
|
||||||
|
|
||||||
def rubocop_args
|
def rubocop_args
|
||||||
fix? ? autocorrect_args : default_args
|
fix? ? autocorrect_args : default_args
|
||||||
end
|
end
|
||||||
@ -63,10 +64,6 @@ module Hbc
|
|||||||
def autocorrect_args
|
def autocorrect_args
|
||||||
default_args + ["--auto-correct"]
|
default_args + ["--auto-correct"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix?
|
|
||||||
args.any? { |arg| arg =~ /--(fix|(auto-?)?correct)/ }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,37 +4,12 @@ require "rubygems"
|
|||||||
|
|
||||||
describe Hbc::CLI::Style, :cask do
|
describe Hbc::CLI::Style, :cask do
|
||||||
let(:args) { [] }
|
let(:args) { [] }
|
||||||
let(:cli) { described_class.new(args) }
|
let(:cli) { described_class.new(*args) }
|
||||||
|
|
||||||
around do |example|
|
around do |example|
|
||||||
shutup { example.run }
|
shutup { example.run }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".run" do
|
|
||||||
subject { described_class.run(args) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
allow(described_class).to receive(:new).and_return(cli)
|
|
||||||
allow(cli).to receive(:run).and_return(retval)
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when rubocop succeeds" do
|
|
||||||
let(:retval) { true }
|
|
||||||
|
|
||||||
it "exits successfully" do
|
|
||||||
subject
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when rubocop fails" do
|
|
||||||
let(:retval) { false }
|
|
||||||
|
|
||||||
it "raises an exception" do
|
|
||||||
expect { subject }.to raise_error(Hbc::CaskError)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#run" do
|
describe "#run" do
|
||||||
subject { cli.run }
|
subject { cli.run }
|
||||||
|
|
||||||
@ -53,7 +28,10 @@ describe Hbc::CLI::Style, :cask do
|
|||||||
|
|
||||||
context "when rubocop fails" do
|
context "when rubocop fails" do
|
||||||
let(:success) { false }
|
let(:success) { false }
|
||||||
it { is_expected.to be_falsey }
|
|
||||||
|
it "raises an error" do
|
||||||
|
expect { subject }.to raise_error(Hbc::CaskError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -99,7 +77,7 @@ describe Hbc::CLI::Style, :cask do
|
|||||||
subject { cli.cask_paths }
|
subject { cli.cask_paths }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(cli).to receive(:cask_tokens).and_return(tokens)
|
allow(described_class).to receive(:cask_tokens_from).and_return(tokens)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when no cask tokens are given" do
|
context "when no cask tokens are given" do
|
||||||
@ -136,40 +114,6 @@ describe Hbc::CLI::Style, :cask do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#cask_tokens" do
|
|
||||||
subject { cli.cask_tokens }
|
|
||||||
|
|
||||||
context "when no args are given" do
|
|
||||||
let(:args) { [] }
|
|
||||||
it { is_expected.to be_empty }
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when only flags are given" do
|
|
||||||
let(:args) { ["--fix"] }
|
|
||||||
it { is_expected.to be_empty }
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when only empty args are given" do
|
|
||||||
let(:args) { ["", ""] }
|
|
||||||
it { is_expected.to be_empty }
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when a cask token is given" do
|
|
||||||
let(:args) { ["adium"] }
|
|
||||||
it { is_expected.to eq(["adium"]) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when multiple cask tokens are given" do
|
|
||||||
let(:args) { %w[adium dropbox] }
|
|
||||||
it { is_expected.to eq(%w[adium dropbox]) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when cask tokens are given with flags" do
|
|
||||||
let(:args) { ["adium", "dropbox", "--fix"] }
|
|
||||||
it { is_expected.to eq(%w[adium dropbox]) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#rubocop_args" do
|
describe "#rubocop_args" do
|
||||||
subject { cli.rubocop_args }
|
subject { cli.rubocop_args }
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user