Use Homebrew::Style for brew cask style.

This commit is contained in:
Markus Reiter 2020-09-01 18:31:09 +02:00
parent 2b4f3646b7
commit b4d14c193f
3 changed files with 15 additions and 63 deletions

View File

@ -1,6 +1,7 @@
# frozen_string_literal: true
require "json"
require "style"
module Cask
class Cmd
@ -19,48 +20,14 @@ module Cask
end
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"], "-S"]
args = [
"--force-exclusion",
"--config", HOMEBREW_LIBRARY/".rubocop.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
def run
result = self.class.rubocop(*cask_paths, auto_correct: args.fix?, debug: args.debug?)
raise CaskError, "Style check failed." unless result.status.success?
success = Homebrew::Style.check_style_and_print(
cask_paths,
fix: args.fix?,
debug: args.debug?,
verbose: args.verbose?,
)
raise CaskError, "Style check failed." unless success
end
def cask_paths

View File

@ -119,6 +119,7 @@ module Homebrew
when :print
args << "--debug" if debug
args << "--format" << "simple" if files.present?
args << "--color" if Tty.color?
system cache_env, "rubocop", *args
$CHILD_STATUS.success?

View File

@ -10,34 +10,18 @@ describe Cask::Cmd::Style, :cask do
it_behaves_like "a command that handles invalid options"
describe ".rubocop" do
describe "::run" do
subject { described_class.rubocop(cask_path) }
around do |example|
FileUtils.ln_s HOMEBREW_LIBRARY_PATH, HOMEBREW_LIBRARY/"Homebrew"
FileUtils.ln_s HOMEBREW_LIBRARY_PATH.parent/".rubocop.yml", HOMEBREW_LIBRARY/".rubocop.yml"
FileUtils.ln_s HOMEBREW_LIBRARY_PATH.parent/".rubocop_cask.yml", HOMEBREW_LIBRARY/".rubocop_cask.yml"
FileUtils.ln_s HOMEBREW_LIBRARY_PATH.parent/".rubocop_rspec.yml", HOMEBREW_LIBRARY/".rubocop_rspec.yml"
example.run
ensure
FileUtils.rm_f HOMEBREW_LIBRARY/"Homebrew"
FileUtils.rm_f HOMEBREW_LIBRARY/".rubocop.yml"
FileUtils.rm_f HOMEBREW_LIBRARY/".rubocop_cask.yml"
FileUtils.rm_f HOMEBREW_LIBRARY/".rubocop_rspec.yml"
end
before do
allow(Homebrew).to receive(:install_bundler_gems!)
end
context "with a valid Cask" do
let(:cask_path) do
Pathname.new("#{HOMEBREW_LIBRARY}/Homebrew/test/support/fixtures/cask/Casks/version-latest.rb")
Pathname.new("#{HOMEBREW_LIBRARY_PATH}/test/support/fixtures/cask/Casks/version-latest.rb")
end
it "returns true" do
expect(subject.success?).to be true
it "is successful" do
expect {
described_class.run(cask_path)
}.not_to raise_error
end
end
end