Refactor brew cask style.

This commit is contained in:
Markus Reiter 2019-10-04 09:47:54 +02:00
parent a49282c318
commit b6b9cd248c
2 changed files with 43 additions and 126 deletions

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
require "json"
module Cask
class Cmd
class Style < AbstractCommand
@ -7,26 +9,50 @@ module Cask
"checks Cask style using RuboCop"
end
def self.rubocop(*paths, auto_correct: false, debug: false, json: false)
Homebrew.install_bundler_gems!
cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" }
hide_warnings = debug ? [] : [ENV["HOMEBREW_RUBY_PATH"], "-W0", "-S"]
args = [
"--force-exclusion",
"--config", "#{HOMEBREW_LIBRARY}/.rubocop_cask.yml"
]
if json
args << "--format" << "json"
else
if auto_correct
args << "--auto-correct"
else
args << "--debug" if debug
args << "--parallel"
end
args << "--format" << "simple"
args << "--color" if Tty.color?
end
executable, *args = [*hide_warnings, "rubocop", *args, "--", *paths]
result = Dir.mktmpdir do |tmpdir|
system_command executable, args: args, chdir: tmpdir, env: cache_env,
print_stdout: !json, print_stderr: !json
end
result.assert_success! unless (0..1).cover?(result.exit_status)
return JSON.parse(result.stdout) if json
result
end
option "--fix", :fix, false
def run
install_rubocop
cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" }
hide_warnings = debug? ? [] : [ENV["HOMEBREW_RUBY_PATH"], "-W0", "-S"]
Dir.mktmpdir do |tmpdir|
system(cache_env, *hide_warnings, "rubocop", *rubocop_args, "--", *cask_paths, chdir: tmpdir)
end
raise CaskError, "style check failed" unless $CHILD_STATUS.success?
end
def install_rubocop
capture_stderr do
begin
Homebrew.install_bundler_gems!
rescue SystemExit
raise CaskError, Tty.strip_ansi($stderr.string).chomp.sub(/\AError: /, "")
end
end
result = self.class.rubocop(*cask_paths, auto_correct: fix?, debug: debug?)
raise CaskError, "Style check failed." unless result.status.success?
end
def cask_paths
@ -39,32 +65,12 @@ module Cask
end
end
def rubocop_args
fix? ? autocorrect_args : normal_args
end
def default_args
[
"--force-exclusion",
"--config", "#{HOMEBREW_LIBRARY}/.rubocop_cask.yml",
"--format", "simple"
]
end
def test_cask_paths
[
Pathname.new("#{HOMEBREW_LIBRARY}/Homebrew/test/support/fixtures/cask/Casks"),
Pathname.new("#{HOMEBREW_LIBRARY}/Homebrew/test/support/fixtures/third-party/Casks"),
]
end
def normal_args
default_args + ["--parallel"]
end
def autocorrect_args
default_args + ["--auto-correct"]
end
end
end
end

View File

@ -11,58 +11,6 @@ describe Cask::Cmd::Style, :cask do
it_behaves_like "a command that handles invalid options"
describe "#run" do
subject { cli.run }
before do
allow(cli).to receive_messages(install_rubocop: nil,
system: nil,
rubocop_args: nil,
cask_paths: nil)
allow($CHILD_STATUS).to receive(:success?).and_return(success)
end
context "when rubocop succeeds" do
let(:success) { true }
it "does not raise an error" do
expect { subject }.not_to raise_error
end
end
context "when rubocop fails" do
let(:success) { false }
it "raises an error" do
expect { subject }.to raise_error(Cask::CaskError)
end
end
end
describe "#install_rubocop" do
subject { cli.install_rubocop }
context "when installation succeeds" do
before do
allow(Homebrew).to receive(:install_bundler_gems!)
end
it "exits successfully" do
expect { subject }.not_to raise_error
end
end
context "when installation fails" do
before do
allow(Homebrew).to receive(:install_bundler_gems!).and_raise(SystemExit)
end
it "raises an error" do
expect { subject }.to raise_error(Cask::CaskError)
end
end
end
describe "#cask_paths" do
subject { cli.cask_paths }
@ -117,41 +65,4 @@ describe Cask::Cmd::Style, :cask do
end
end
end
describe "#rubocop_args" do
subject { cli.rubocop_args }
before do
allow(cli).to receive(:fix?).and_return(fix)
end
context "when fix? is true" do
let(:fix) { true }
it { is_expected.to include("--auto-correct") }
end
context "when fix? is false" do
let(:fix) { false }
it { is_expected.not_to include("--auto-correct") }
end
end
describe "#default_args" do
subject { cli.default_args }
it { is_expected.to include("--format", "simple") }
end
describe "#autocorrect_args" do
subject { cli.autocorrect_args }
let(:default_args) { ["--format", "simple"] }
it "adds --auto-correct to default args" do
allow(cli).to receive(:default_args).and_return(default_args)
expect(subject).to include("--auto-correct", *default_args)
end
end
end