Refactor RuboCop tests.

This commit is contained in:
Markus Reiter 2023-05-08 11:02:44 +02:00
parent 82dc1d25ef
commit 02dbeb62fc
No known key found for this signature in database
GPG Key ID: 245293B51702655B
15 changed files with 1320 additions and 2252 deletions

View File

@ -23,7 +23,7 @@ module RuboCop
extend AutoCorrector extend AutoCorrector
include CaskHelp include CaskHelp
MESSAGE = "Use `%<preferred>s` instead of `%<current>s`" MESSAGE = "Use `%<preferred>s` instead of `%<current>s`."
def on_cask(cask_block) def on_cask(cask_block)
@cask_header = cask_block.header @cask_header = cask_block.header

View File

@ -13,10 +13,9 @@ module RuboCop
:appcast, :arch, :auto_updates, :conflicts_with, :container, :appcast, :arch, :auto_updates, :conflicts_with, :container,
:desc, :homepage, :sha256, :url, :version :desc, :homepage, :sha256, :url, :version
].freeze ].freeze
MESSAGE = <<~EOS MESSAGE = "Do not use a top-level `%<stanza>s` stanza as the default. " \
Do not use a top-level `%<stanza>s` stanza as the default. Add it to an `on_{system}` block instead. "Add it to an `on_{system}` block instead. " \
Use `:or_older` or `:or_newer` to specify a range of macOS versions. "Use `:or_older` or `:or_newer` to specify a range of macOS versions."
EOS
def on_cask(cask_block) def on_cask(cask_block)
cask_stanzas = cask_block.toplevel_stanzas cask_stanzas = cask_block.toplevel_stanzas

View File

@ -4,13 +4,13 @@
module RuboCop module RuboCop
module Cop module Cop
module Cask module Cask
# This cop checks for version.before_comma and version.after_comma # This cop checks for `version.before_comma` and `version.after_comma`.
class UrlLegacyCommaSeparators < Url class UrlLegacyCommaSeparators < Url
include OnUrlStanza include OnUrlStanza
extend AutoCorrector extend AutoCorrector
MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma' " \ MSG_CSV = "Use `version.csv.first` instead of `version.before_comma` " \
"and 'version.csv.second' instead of 'version.after_comma'" "and `version.csv.second` instead of `version.after_comma`."
def on_url_stanza(stanza) def on_url_stanza(stanza)
return if stanza.stanza_node.type == :block return if stanza.stanza_node.type == :block

View File

@ -50,7 +50,7 @@ module RuboCop
replacement_parameters << "intel: #{intel_node.source}" unless blank_node?(intel_node) replacement_parameters << "intel: #{intel_node.source}" unless blank_node?(intel_node)
replacement_string += replacement_parameters.join(", ") replacement_string += replacement_parameters.join(", ")
add_offense(node, message: "Use `#{replacement_string}` instead of `#{node.source}`") do |corrector| add_offense(node, message: "Use `#{replacement_string}` instead of `#{node.source}`.") do |corrector|
corrector.replace(node, replacement_string) corrector.replace(node, replacement_string)
end end
end end

View File

@ -1,11 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::Desc do
subject(:cop) { described_class.new }
describe RuboCop::Cop::Cask::Desc, :config do
it "does not start with an article" do it "does not start with an article" do
expect_no_offenses <<~RUBY expect_no_offenses <<~RUBY
cask "foo" do cask "foo" do
@ -16,14 +13,14 @@ describe RuboCop::Cop::Cask::Desc do
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo' do cask 'foo' do
desc 'A bar program' desc 'A bar program'
^ Cask/Desc: Description shouldn't start with an article. ^ Description shouldn't start with an article.
end end
RUBY RUBY
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo' do cask 'foo' do
desc 'The bar program' desc 'The bar program'
^^^ Cask/Desc: Description shouldn't start with an article. ^^^ Description shouldn't start with an article.
end end
RUBY RUBY
@ -38,35 +35,35 @@ describe RuboCop::Cop::Cask::Desc do
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foobar' do cask 'foobar' do
desc 'Foo bar program' desc 'Foo bar program'
^^^^^^^ Cask/Desc: Description shouldn't start with the cask name. ^^^^^^^ Description shouldn't start with the cask name.
end end
RUBY RUBY
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foobar' do cask 'foobar' do
desc 'Foo-Bar program' desc 'Foo-Bar program'
^^^^^^^ Cask/Desc: Description shouldn't start with the cask name. ^^^^^^^ Description shouldn't start with the cask name.
end end
RUBY RUBY
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo-bar' do cask 'foo-bar' do
desc 'Foo bar program' desc 'Foo bar program'
^^^^^^^ Cask/Desc: Description shouldn't start with the cask name. ^^^^^^^ Description shouldn't start with the cask name.
end end
RUBY RUBY
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo-bar' do cask 'foo-bar' do
desc 'Foo-Bar program' desc 'Foo-Bar program'
^^^^^^^ Cask/Desc: Description shouldn't start with the cask name. ^^^^^^^ Description shouldn't start with the cask name.
end end
RUBY RUBY
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo-bar' do cask 'foo-bar' do
desc 'Foo Bar' desc 'Foo Bar'
^^^^^^^ Cask/Desc: Description shouldn't start with the cask name. ^^^^^^^ Description shouldn't start with the cask name.
end end
RUBY RUBY
end end
@ -75,28 +72,28 @@ describe RuboCop::Cop::Cask::Desc do
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo-bar' do cask 'foo-bar' do
desc 'macOS status bar monitor' desc 'macOS status bar monitor'
^^^^^ Cask/Desc: Description shouldn't contain the platform. ^^^^^ Description shouldn't contain the platform.
end end
RUBY RUBY
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo-bar' do cask 'foo-bar' do
desc 'Toggles dark mode on Mac OS Mojave' desc 'Toggles dark mode on Mac OS Mojave'
^^^^^^ Cask/Desc: Description shouldn't contain the platform. ^^^^^^ Description shouldn't contain the platform.
end end
RUBY RUBY
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo-bar' do cask 'foo-bar' do
desc 'Better input source switcher for OS X' desc 'Better input source switcher for OS X'
^^^^ Cask/Desc: Description shouldn't contain the platform. ^^^^ Description shouldn't contain the platform.
end end
RUBY RUBY
expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb" expect_offense <<~RUBY, "/homebrew-cask/Casks/foo.rb"
cask 'foo-bar' do cask 'foo-bar' do
desc 'Media Manager for Mac OS X' desc 'Media Manager for Mac OS X'
^^^^^^^^ Cask/Desc: Description shouldn't contain the platform. ^^^^^^^^ Description shouldn't contain the platform.
end end
RUBY RUBY
@ -109,7 +106,7 @@ describe RuboCop::Cop::Cask::Desc do
expect_offense <<~RUBY expect_offense <<~RUBY
cask 'foo' do cask 'foo' do
desc 'Application for managing macOS virtual machines on macOS' desc 'Application for managing macOS virtual machines on macOS'
^^^^^ Cask/Desc: Description shouldn't contain the platform. ^^^^^ Description shouldn't contain the platform.
end end
RUBY RUBY

View File

@ -1,64 +1,36 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::HomepageUrlTrailingSlash do describe RuboCop::Cop::Cask::HomepageUrlTrailingSlash, :config do
include CaskCop it "accepts a homepage URL ending with a slash" do
expect_no_offenses <<~CASK
subject(:cop) { described_class.new }
context "when the homepage URL ends with a slash" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
homepage 'https://foo.brew.sh/' homepage 'https://foo.brew.sh/'
end end
CASK CASK
end end
include_examples "does not report any offenses" it "accepts a homepage URL with a path" do
end expect_no_offenses <<~CASK
context "when the homepage URL does not end with a slash but has a path" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
homepage 'https://foo.brew.sh/path' homepage 'https://foo.brew.sh/path'
end end
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense when the homepage URL does not end with a slash and has no path" do
end expect_offense <<~CASK
context "when the homepage URL does not end with a slash and has no path" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
homepage 'https://foo.brew.sh' homepage 'https://foo.brew.sh'
^^^^^^^^^^^^^^^^^^^^^ 'https://foo.brew.sh' must have a slash after the domain.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
homepage 'https://foo.brew.sh/' homepage 'https://foo.brew.sh/'
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "Cask/HomepageUrlTrailingSlash: 'https://foo.brew.sh' must have a slash after the domain.",
severity: :convention,
line: 2,
column: 11,
source: "'https://foo.brew.sh'",
}]
end
include_examples "reports offenses"
include_examples "autocorrects source"
end
end end

View File

@ -1,34 +1,20 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::NoDslVersion do describe RuboCop::Cop::Cask::NoDslVersion, :config do
include CaskCop it "accepts `cask` without a DSL version" do
expect_no_offenses "cask 'foo' do; end"
subject(:cop) { described_class.new }
context "with no dsl version" do
let(:source) { "cask 'foo' do; end" }
include_examples "does not report any offenses"
end end
context "with dsl version" do it "reports an offense when `cask` has a DSL version" do
let(:source) { "cask :v1 => 'foo' do; end" } expect_offense <<~CASK
let(:correct_source) { "cask 'foo' do; end" } cask :v1 => 'foo' do; end
let(:expected_offenses) do ^^^^^^^^^^^^^^^^^ Use `cask 'foo'` instead of `cask :v1 => 'foo'`.
[{ CASK
message: "Cask/NoDslVersion: Use `cask 'foo'` instead of `cask :v1 => 'foo'`",
severity: :convention,
line: 1,
column: 0,
source: "cask :v1 => 'foo'",
}]
end
include_examples "reports offenses" expect_correction <<~CASK
cask 'foo' do; end
include_examples "autocorrects source" CASK
end end
end end

View File

@ -1,16 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::NoOverrides do describe RuboCop::Cop::Cask::NoOverrides, :config do
include CaskCop it "accepts when there are no `on_*` blocks" do
expect_no_offenses <<~CASK
subject(:cop) { described_class.new }
context "when there are no on_system blocks" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version '1.2.3' version '1.2.3'
url 'https://brew.sh/foo.pkg' url 'https://brew.sh/foo.pkg'
@ -20,12 +14,8 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts when there are no top-level standalone stanzas" do
end expect_no_offenses <<~CASK
context "when there are no top-level standalone stanzas" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
on_mojave :or_later do on_mojave :or_later do
version :latest version :latest
@ -34,12 +24,8 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts non-overridable stanzas in `on_*` blocks" do
end expect_no_offenses <<~CASK
context "when there are top-level stanzas also in `on_*` blocks that should not override" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version '1.2.3' version '1.2.3'
@ -54,31 +40,25 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts `arch` and `version` interpolations in strings in `on_*` blocks" do
end expect_no_offenses <<~CASK
context "when there are `arch` variables in the `url` in the `on_*` blocks" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86" arch arm: "arm64", intel: "x86"
version '1.2.3' version '1.2.3'
on_mojave :or_later do on_mojave :or_later do
url "https://brew.sh/foo-\#{version}-\#{arch}.pkg"
sha256 "aaa" sha256 "aaa"
url "https://brew.sh/foo-\#{version}-\#{arch}.pkg"
end end
end end
CASK CASK
end end
include_examples "does not report any offenses" it "accepts `version` interpolations with method calls in strings in `on_*` blocks" do
end expect_no_offenses <<~CASK
context "when there are `version` interpolations in `on_*` blocks with methods called on them" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version 0.99,123.3 version '0.99,123.3'
on_mojave :or_later do on_mojave :or_later do
url "https://brew.sh/foo-\#{version.csv.first}-\#{version.csv.second}.pkg" url "https://brew.sh/foo-\#{version.csv.first}-\#{version.csv.second}.pkg"
@ -87,16 +67,12 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts `arch` interpolations in regexes in `on_*` blocks" do
end expect_no_offenses <<~CASK
context "when there are `arch` interpolations in regexps in `on_*` blocks" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86" arch arm: "arm64", intel: "x86"
version 0.99,123.3 version '0.99,123.3'
on_mojave :or_later do on_mojave :or_later do
url "https://brew.sh/foo-\#{arch}-\#{version.csv.first}-\#{version.csv.last}.pkg" url "https://brew.sh/foo-\#{arch}-\#{version.csv.first}-\#{version.csv.last}.pkg"
@ -110,12 +86,8 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
include_examples "does not report any offenses" it "ignores contents of single-line `livecheck` blocks in `on_*` blocks" do
end expect_no_offenses <<~CASK
context "when there are single-line livecheck blocks within `on_*` blocks, ignore their contents" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
on_intel do on_intel do
livecheck do livecheck do
@ -133,12 +105,8 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
include_examples "does not report any offenses" it "ignores contents of multi-line `livecheck` blocks in `on_*` blocks" do
end expect_no_offenses <<~CASK
context "when there are multi-line livecheck blocks within `on_*` blocks, ignore their contents" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
on_intel do on_intel do
livecheck do livecheck do
@ -157,12 +125,8 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts `on_*` blocks that don't override upper-level stanzas" do
end expect_no_offenses <<~CASK
context "when there's only one difference between the `on_*` blocks" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
version "1.2.3" version "1.2.3"
@ -178,55 +142,12 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense when `on_*` blocks override a single upper-level stanza" do
end expect_offense <<~CASK
context "when there are multiple differences between the `on_*` blocks" do
let(:source) do
<<~CASK
cask "foo" do
version "1.2.3"
sha256 "aaa"
url "https://brew.sh/foo-2.3.4.dmg"
on_big_sur :or_older do
sha256 "bbb"
url "https://brew.sh/legacy/foo-2.3.4.dmg"
end
end
CASK
end
let(:expected_offenses) do
[{
message: <<~EOS,
Cask/NoOverrides: Do not use a top-level `sha256` stanza as the default. Add it to an `on_{system}` block instead.
Use `:or_older` or `:or_newer` to specify a range of macOS versions.
EOS
severity: :convention,
line: 3,
column: 2,
source: "sha256 \"aaa\"",
}, {
message: <<~EOS,
Cask/NoOverrides: Do not use a top-level `url` stanza as the default. Add it to an `on_{system}` block instead.
Use `:or_older` or `:or_newer` to specify a range of macOS versions.
EOS
severity: :convention,
line: 4,
column: 2,
source: "url \"https://brew.sh/foo-2.3.4.dmg\"",
}]
end
include_examples "reports offenses"
end
context "when there are top-level standalone stanzas" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version '2.3.4' version '2.3.4'
^^^^^^^^^^^^^^^ Do not use a top-level `version` stanza as the default. Add it to an `on_{system}` block instead. Use `:or_older` or `:or_newer` to specify a range of macOS versions.
on_mojave :or_older do on_mojave :or_older do
version '1.2.3' version '1.2.3'
end end
@ -236,19 +157,20 @@ describe RuboCop::Cop::Cask::NoOverrides do
CASK CASK
end end
let(:expected_offenses) do it "reports an offense when `on_*` blocks override multiple upper-level stanzas" do
[{ expect_offense <<~CASK
message: <<~EOS, cask "foo" do
Cask/NoOverrides: Do not use a top-level `version` stanza as the default. Add it to an `on_{system}` block instead. version "1.2.3"
Use `:or_older` or `:or_newer` to specify a range of macOS versions. sha256 "aaa"
EOS ^^^^^^^^^^^^ Do not use a top-level `sha256` stanza as the default. Add it to an `on_{system}` block instead. Use `:or_older` or `:or_newer` to specify a range of macOS versions.
severity: :convention, url "https://brew.sh/foo-2.3.4.dmg"
line: 2, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use a top-level `url` stanza as the default. Add it to an `on_{system}` block instead. Use `:or_older` or `:or_newer` to specify a range of macOS versions.
column: 2,
source: "version '2.3.4'",
}]
end
include_examples "reports offenses" on_big_sur :or_older do
sha256 "bbb"
url "https://brew.sh/legacy/foo-2.3.4.dmg"
end
end
CASK
end end
end end

View File

@ -1,17 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::OnSystemConditionals do
include CaskCop
subject(:cop) { described_class.new }
describe RuboCop::Cop::Cask::OnSystemConditionals, :config do
context "when auditing `postflight` stanzas" do context "when auditing `postflight` stanzas" do
context "when there are no on_system blocks" do it "accepts when there are no `on_*` blocks" do
let(:source) do expect_no_offenses <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
postflight do postflight do
foobar foobar
@ -20,23 +14,20 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense it contains an `on_intel` block" do
end expect_offense <<~CASK
context "when there is an `on_intel` block" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
postflight do postflight do
on_intel do on_intel do
^^^^^^^^ Don't use `on_intel` in `postflight do`, use `if Hardware::CPU.intel?` instead.
foobar foobar
end end
end end
end end
CASK CASK
end
let(:correct_source) do # FIXME: Infinite loop alternating between `if Hardware::CPU.intel?` and `on_intel do`.
<<~CASK expect_correction <<~CASK, loop: false
cask 'foo' do cask 'foo' do
postflight do postflight do
if Hardware::CPU.intel? if Hardware::CPU.intel?
@ -46,37 +37,21 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Don't use `on_intel` in `postflight do`, use `if Hardware::CPU.intel?` instead.
EOS
severity: :convention,
line: 3,
column: 4,
source: "on_intel",
}]
end
include_examples "reports offenses" it "reports an offense when it contains an `on_monterey` block" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when there is an `on_monterey` block" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
postflight do postflight do
on_monterey do on_monterey do
^^^^^^^^^^^ Don't use `on_monterey` in `postflight do`, use `if MacOS.version == :monterey` instead.
foobar foobar
end end
end end
end end
CASK CASK
end
let(:correct_source) do # FIXME: Infinite loop alternating between `if MacOS.version == :monterey` and `on_monterey do`.
<<~CASK expect_correction <<~CASK, loop: false
cask 'foo' do cask 'foo' do
postflight do postflight do
if MacOS.version == :monterey if MacOS.version == :monterey
@ -86,36 +61,21 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "Cask/OnSystemConditionals: Don't use `on_monterey` in `postflight do`, use " \
"`if MacOS.version == :monterey` instead.",
severity: :convention,
line: 3,
column: 4,
source: "on_monterey",
}]
end
include_examples "reports offenses" it "reports an offense when it contains an `on_monterey :or_older` block" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when there is an `on_monterey :or_older` block" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
postflight do postflight do
on_monterey :or_older do on_monterey :or_older do
^^^^^^^^^^^^^^^^^^^^^ Don't use `on_monterey :or_older` in `postflight do`, use `if MacOS.version <= :monterey` instead.
foobar foobar
end end
end end
end end
CASK CASK
end
let(:correct_source) do # FIXME: Infinite loop alternating between `if MacOS.version <= :monterey` and `on_monterey :or_older do`.
<<~CASK expect_correction <<~CASK, loop: false
cask 'foo' do cask 'foo' do
postflight do postflight do
if MacOS.version <= :monterey if MacOS.version <= :monterey
@ -125,39 +85,19 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "Cask/OnSystemConditionals: Don't use `on_monterey :or_older` in `postflight do`, " \
"use `if MacOS.version <= :monterey` instead.",
severity: :convention,
line: 3,
column: 4,
source: "on_monterey :or_older",
}]
end end
include_examples "reports offenses" context "when auditing `sha256` stanzas inside `on_arch` blocks" do
it "accepts when there are no `on_arch` blocks" do
include_examples "autocorrects source" expect_no_offenses <<~CASK
end
end
context "when auditing `sha256` stanzas inside on_arch blocks" do
context "when there are no on_arch blocks" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
end end
CASK CASK
end end
include_examples "does not report any offenses" it "accepts when the `sha256` stanza is used with keyword arguments" do
end expect_no_offenses <<~CASK
context "when the proper `sha256` stanza is used" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
sha256 arm: "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94", sha256 arm: "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94",
intel: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" intel: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
@ -165,71 +105,39 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts when there is only one `on_arch` block" do
expect_no_offenses <<~CASK
cask 'foo' do
on_intel do
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
end
end
CASK
end end
context "when the `sha256` stanza needs to be removed from the on_arch blocks" do it "reports an offense when `sha256` is specified in all `on_arch` blocks" do
let(:source) do expect_offense <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
on_intel do on_intel do
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
end end
on_arm do on_arm do
^^^^^^^^^ Use `sha256 arm: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b", intel: "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"` instead of nesting the `sha256` stanzas in `on_intel` and `on_arm` blocks
sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
end end
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
#{" "} #{" "}
sha256 arm: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b", intel: "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" sha256 arm: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b", intel: "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
end end
CASK CASK
end end
let(:offense_source) do
<<~CASK
on_arm do
sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
end
CASK
end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Use `sha256 arm: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b", intel: "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"` instead of nesting the `sha256` stanzas in `on_intel` and `on_arm` blocks
EOS
severity: :convention,
line: 5,
column: 2,
source: offense_source.strip,
}]
end
include_examples "reports offenses" it "accepts when there is also a `version` stanza inside the `on_arch` blocks" do
expect_no_offenses <<~CASK
include_examples "autocorrects source"
end
context "when there is only one on_arch block" do
let(:source) do
<<~CASK
cask 'foo' do
on_intel do
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
end
end
CASK
end
include_examples "does not report any offenses"
end
context "when there is also a `version` stanza inside the on_arch blocks" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
on_intel do on_intel do
version "1.0.0" version "1.0.0"
@ -243,12 +151,8 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts when there is also a `version` stanza inside only a single `on_arch` block" do
end expect_no_offenses <<~CASK
context "when there is also a `version` stanza inside only a single on_arch block" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
on_intel do on_intel do
version "2.0.0" version "2.0.0"
@ -260,17 +164,14 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
include_examples "does not report any offenses"
end
end end
context "when auditing loose `Hardware::CPU` method calls" do context "when auditing loose `Hardware::CPU` method calls" do
context "when there is a `Hardware::CPU.arm?` reference" do it "reports an offense when `Hardware::CPU.arm?` is used" do
let(:source) do expect_offense <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
if Hardware::CPU.arm? && other_condition if Hardware::CPU.arm? && other_condition
^^^^^^^^^^^^^^^^^^ Don't use `Hardware::CPU.arm?`, use `on_arm` and `on_intel` blocks instead.
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
else else
sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
@ -278,26 +179,12 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Don't use `Hardware::CPU.arm?`, use `on_arm` and `on_intel` blocks instead.
EOS
severity: :convention,
line: 2,
column: 5,
source: "Hardware::CPU.arm?",
}]
end
include_examples "reports offenses" it "reports an offense when `Hardware::CPU.intel?` is used" do
end expect_offense <<~CASK
context "when there is a `Hardware::CPU.intel?` reference" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
if Hardware::CPU.intel? && other_condition if Hardware::CPU.intel? && other_condition
^^^^^^^^^^^^^^^^^^^^ Don't use `Hardware::CPU.intel?`, use `on_arm` and `on_intel` blocks instead.
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
else else
sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
@ -305,54 +192,26 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Don't use `Hardware::CPU.intel?`, use `on_arm` and `on_intel` blocks instead.
EOS
severity: :convention,
line: 2,
column: 5,
source: "Hardware::CPU.intel?",
}]
end
include_examples "reports offenses" it "reports an offense when `Hardware::CPU.arch` is used" do
end expect_offense <<~'CASK'
context "when there is a `Hardware::CPU.arch` reference" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version "1.2.3" version "1.2.3"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
url "https://example.com/foo-\#{version}-\#{Hardware::CPU.arch}.zip" url "https://example.com/foo-#{version}-#{Hardware::CPU.arch}.zip"
^^^^^^^^^^^^^^^^^^ Don't use `Hardware::CPU.arch`, use `on_arm` and `on_intel` blocks instead.
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Don't use `Hardware::CPU.arch`, use `on_arm` and `on_intel` blocks instead.
EOS
severity: :convention,
line: 5,
column: 44,
source: "Hardware::CPU.arch",
}]
end
include_examples "reports offenses"
end
end end
context "when auditing loose `MacOS.version` method calls" do context "when auditing loose `MacOS.version` method calls" do
context "when there is a `MacOS.version ==` reference" do it "reports an offense when `MacOS.version ==` is used" do
let(:source) do expect_offense <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
if MacOS.version == :catalina if MacOS.version == :catalina
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use `if MacOS.version == :catalina`, use `on_catalina do` instead.
version "1.0.0" version "1.0.0"
else else
version "2.0.0" version "2.0.0"
@ -360,26 +219,12 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Don't use `if MacOS.version == :catalina`, use `on_catalina do` instead.
EOS
severity: :convention,
line: 2,
column: 2,
source: "if MacOS.version == :catalina\n version \"1.0.0\"\n else\n version \"2.0.0\"\n end",
}]
end
include_examples "reports offenses" it "reports an offense when `MacOS.version <=` is used" do
end expect_offense <<~CASK
context "when there is a `MacOS.version <=` reference" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
if MacOS.version <= :catalina if MacOS.version <= :catalina
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use `if MacOS.version <= :catalina`, use `on_catalina :or_older do` instead.
version "1.0.0" version "1.0.0"
else else
version "2.0.0" version "2.0.0"
@ -387,26 +232,12 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Don't use `if MacOS.version <= :catalina`, use `on_catalina :or_older do` instead.
EOS
severity: :convention,
line: 2,
column: 2,
source: "if MacOS.version <= :catalina\n version \"1.0.0\"\n else\n version \"2.0.0\"\n end",
}]
end
include_examples "reports offenses" it "reports an offense when `MacOS.version >=` is used" do
end expect_offense <<~CASK
context "when there is a `MacOS.version >=` reference" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
if MacOS.version >= :catalina if MacOS.version >= :catalina
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use `if MacOS.version >= :catalina`, use `on_catalina :or_newer do` instead.
version "1.0.0" version "1.0.0"
else else
version "2.0.0" version "2.0.0"
@ -414,45 +245,5 @@ describe RuboCop::Cop::Cask::OnSystemConditionals do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Don't use `if MacOS.version >= :catalina`, use `on_catalina :or_newer do` instead.
EOS
severity: :convention,
line: 2,
column: 2,
source: "if MacOS.version >= :catalina\n version \"1.0.0\"\n else\n version \"2.0.0\"\n end",
}]
end
include_examples "reports offenses"
end
context "when there is a `MacOS.version` reference" do
let(:source) do
<<~CASK
cask 'foo' do
version "1.2.3"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
url "https://example.com/foo-\#{version}-\#{MacOS.version == :monterey}.zip"
end
CASK
end
let(:expected_offenses) do
[{
message: <<~EOS.chomp,
Cask/OnSystemConditionals: Don't use `MacOS.version == :monterey`, use `on_{macos_version}` blocks instead.
EOS
severity: :convention,
line: 5,
column: 44,
source: "MacOS.version == :monterey",
}]
end
include_examples "reports offenses"
end
end end
end end

View File

@ -1,62 +0,0 @@
# frozen_string_literal: true
module CaskCop
shared_examples "does not report any offenses" do
it "does not report any offenses" do
expect_no_offenses(source)
end
end
shared_examples "reports offenses" do
it "reports offenses" do
expect_reported_offenses(source, expected_offenses)
end
end
shared_examples "autocorrects source" do
it "autocorrects source" do
expect_autocorrected_source(source, correct_source)
end
end
def expect_no_offenses(source)
expect(inspect_source(source)).to be_empty
end
def expect_reported_offenses(source, expected_offenses)
offenses = inspect_source(source)
expect(offenses.size).to eq(expected_offenses.size)
expected_offenses.zip(offenses).each do |expected, actual|
expect_offense2(expected, actual)
end
end
def expect_offense2(expected, actual)
expect(actual.message).to eq(expected[:message])
expect(actual.severity).to eq(expected[:severity])
expect(actual.line).to eq(expected[:line])
expect(actual.column).to eq(expected[:column])
expect(actual.location.source).to eq(expected[:source])
end
# TODO: Replace with `expect_correction` from `rubocop-rspec`.
def expect_autocorrected_source(source, correct_source)
correct_source = Array(correct_source).join("\n")
current_source = source
# RuboCop runs auto-correction in a loop to handle nested offenses.
loop do
current_source = autocorrect_source(current_source)
if (ignored_nodes = cop.instance_variable_get(:@ignored_nodes)) && ignored_nodes.any?
ignored_nodes.clear
next
end
break
end
expect(current_source).to eq correct_source
end
end

View File

@ -1,35 +1,18 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::StanzaGrouping do describe RuboCop::Cop::Cask::StanzaGrouping, :config do
include CaskCop it "accepts a sole stanza" do
expect_no_offenses <<~CASK
subject(:cop) { described_class.new }
let(:missing_line_msg) do
"Cask/StanzaGrouping: stanza groups should be separated by a single empty line"
end
let(:extra_line_msg) do
"Cask/StanzaGrouping: stanzas within the same group should have no lines between them"
end
context "when there is only one stanza" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
end end
CASK CASK
end end
include_examples "does not report any offenses" it "accepts correctly grouped stanzas" do
end expect_no_offenses <<~CASK
context "when no stanzas are incorrectly grouped" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -37,12 +20,8 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts correctly grouped stanzas and variable assignments" do
end expect_no_offenses <<~CASK
context "when no stanzas or variable assignments are incorrectly grouped" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
@ -53,54 +32,35 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense when a stanza is grouped incorrectly" do
end expect_offense <<~CASK
context "when one stanza is incorrectly grouped" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
^{} stanzas within the same group should have no lines between them
sha256 :no_check sha256 :no_check
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: extra_line_msg,
severity: :convention,
line: 3,
column: 0,
source: "\n",
}]
end
include_examples "reports offenses" it "reports an offense for an incorrectly grouped `arch` stanza" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when the arch stanza is incorrectly grouped" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
version :latest version :latest
^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
sha256 :no_check sha256 :no_check
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
@ -109,34 +69,19 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: missing_line_msg,
severity: :convention,
line: 3,
column: 0,
source: " version :latest",
}]
end
include_examples "reports offenses" it "reports an offense for an incorrectly grouped variable assignment" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when one variable assignment is incorrectly grouped" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
version :latest version :latest
^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
sha256 :no_check sha256 :no_check
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
@ -146,41 +91,29 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: missing_line_msg,
severity: :convention,
line: 4,
column: 0,
source: " version :latest",
}]
end
include_examples "reports offenses" it "reports an offense for multiple incorrectly grouped stanzas" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when many stanzas are incorrectly grouped" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
^{} stanzas within the same group should have no lines between them
name 'Foo' name 'Foo'
^{} stanzas within the same group should have no lines between them
homepage 'https://foo.brew.sh' homepage 'https://foo.brew.sh'
app 'Foo.app' app 'Foo.app'
uninstall :quit => 'com.example.foo', uninstall :quit => 'com.example.foo',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
:kext => 'com.example.foo.kextextension' :kext => 'com.example.foo.kextextension'
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -196,63 +129,35 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: missing_line_msg,
severity: :convention,
line: 4,
column: 0,
source: " url 'https://foo.brew.sh/foo.zip'",
}, {
message: extra_line_msg,
severity: :convention,
line: 5,
column: 0,
source: "\n",
}, {
message: extra_line_msg,
severity: :convention,
line: 7,
column: 0,
source: "\n",
}, {
message: missing_line_msg,
severity: :convention,
line: 11,
column: 0,
source: " uninstall :quit => 'com.example.foo',",
}]
end
include_examples "reports offenses" it "reports an offense for multiple incorrectly grouped stanzas and variable assignments" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when many stanzas and variable assignments are incorrectly grouped" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
^{} stanzas within the same group should have no lines between them
platform = on_arch_conditional arm: "darwin-arm64", intel: "darwin" platform = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
version :latest version :latest
^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
sha256 :no_check sha256 :no_check
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^{} stanzas within the same group should have no lines between them
name 'Foo' name 'Foo'
^{} stanzas within the same group should have no lines between them
homepage 'https://foo.brew.sh' homepage 'https://foo.brew.sh'
app 'Foo.app' app 'Foo.app'
uninstall :quit => 'com.example.foo', uninstall :quit => 'com.example.foo',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
:kext => 'com.example.foo.kextextension' :kext => 'com.example.foo.kextextension'
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
@ -272,66 +177,29 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: extra_line_msg,
severity: :convention,
line: 4,
column: 0,
source: "\n",
}, {
message: missing_line_msg,
severity: :convention,
line: 6,
column: 0,
source: " version :latest",
}, {
message: missing_line_msg,
severity: :convention,
line: 8,
column: 0,
source: " url 'https://foo.brew.sh/foo.zip'",
}, {
message: extra_line_msg,
severity: :convention,
line: 9,
column: 0,
source: "\n",
}, {
message: extra_line_msg,
severity: :convention,
line: 11,
column: 0,
source: "\n",
}, {
message: missing_line_msg,
severity: :convention,
line: 15,
column: 0,
source: " uninstall :quit => 'com.example.foo',",
}]
end
include_examples "reports offenses" shared_examples "caveats" do
it "reports an offense for an incorrectly grouped `caveats` stanza" do
# Indent all except the first line.
interpolated_caveats = caveats.strip
include_examples "autocorrects source" expect_offense <<~CASK
end
context "when caveats stanza is incorrectly grouped" do
let(:source) do
format(<<~CASK, caveats: caveats.strip)
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
name 'Foo' name 'Foo'
app 'Foo.app' app 'Foo.app'
%<caveats>s ^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
#{interpolated_caveats}
end end
CASK CASK
end
let(:correct_source) do # Remove offense annotations.
format(<<~CASK, caveats: caveats.strip) corrected_caveats = interpolated_caveats.gsub(/\n\s*\^+\s+.*$/, "")
expect_correction <<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -341,21 +209,28 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
app 'Foo.app' app 'Foo.app'
%<caveats>s #{corrected_caveats}
end end
CASK CASK
end end
context "when caveats is a one-line string" do
let(:caveats) { "caveats 'This is a one-line caveat.'" }
include_examples "autocorrects source"
end end
context "when caveats is a heredoc" do context "when `caveats` is a one-line string" do
let(:caveats) do
<<~CAVEATS
caveats 'This is a one-line caveat.'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
CAVEATS
end
include_examples "caveats"
end
context "when `caveats` is a heredoc" do
let(:caveats) do let(:caveats) do
<<~CAVEATS <<~CAVEATS
caveats <<~EOS caveats <<~EOS
^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
This is a multiline caveat. This is a multiline caveat.
Let's hope it doesn't cause any problems! Let's hope it doesn't cause any problems!
@ -363,13 +238,14 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
CAVEATS CAVEATS
end end
include_examples "autocorrects source" include_examples "caveats"
end end
context "when caveats is a block" do context "when `caveats` is a block" do
let(:caveats) do let(:caveats) do
<<~CAVEATS <<~CAVEATS
caveats do caveats do
^^^^^^^^^^^^ stanza groups should be separated by a single empty line
puts 'This is a multiline caveat.' puts 'This is a multiline caveat.'
puts "Let's hope it doesn't cause any problems!" puts "Let's hope it doesn't cause any problems!"
@ -377,27 +253,27 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
CAVEATS CAVEATS
end end
include_examples "autocorrects source" include_examples "caveats"
end
end end
context "when the postflight stanza is incorrectly grouped" do it "reports an offense for an incorrectly grouped `postflight` stanza" do
let(:source) do expect_offense <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
name 'Foo' name 'Foo'
app 'Foo.app' app 'Foo.app'
^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
postflight do postflight do
^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
puts 'We have liftoff!' puts 'We have liftoff!'
end end
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -414,29 +290,27 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
CASK CASK
end end
include_examples "autocorrects source" it "reports an offense for incorrectly grouped comments" do
end expect_offense <<~CASK
context "when a stanza has a comment" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
# comment with an empty line between # comment with an empty line between
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
# comment directly above # comment directly above
postflight do postflight do
puts 'We have liftoff!' puts 'We have liftoff!'
end end
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
name 'Foo' name 'Foo'
app 'Foo.app' app 'Foo.app'
^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -456,16 +330,13 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
CASK CASK
end end
include_examples "autocorrects source" it "reports an offense for incorrectly grouped comments and variable assignments" do
end expect_offense <<~CASK
context "when a stanza has a comment and there is a variable assignment" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
# comment with an empty line between # comment with an empty line between
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -474,13 +345,14 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
puts 'We have liftoff!' puts 'We have liftoff!'
end end
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
name 'Foo' name 'Foo'
app 'Foo.app' app 'Foo.app'
^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64", intel: "x86_64" arch arm: "arm64", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
@ -502,30 +374,25 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
CASK CASK
end end
include_examples "autocorrects source" it "reports an offense for incorrectly grouped stanzas in `on_*` blocks" do
end expect_offense <<~CASK
context "when stanzas are nested one-level in `on_*` blocks" do
describe "basic nesting" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
on_arm do on_arm do
version "1.0.2" version "1.0.2"
^{} stanzas within the same group should have no lines between them
sha256 :no_check sha256 :no_check
end end
on_intel do on_intel do
version "0.9.8" version "0.9.8"
sha256 :no_check sha256 :no_check
url "https://foo.brew.sh/foo-intel.zip" url "https://foo.brew.sh/foo-intel.zip"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ stanza groups should be separated by a single empty line
end end
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
on_arm do on_arm do
version "1.0.2" version "1.0.2"
@ -541,16 +408,13 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
CASK CASK
end end
include_examples "autocorrects source" it "reports an offense for incorrectly grouped stanzas with comments in `on_*` blocks" do
end expect_offense <<~CASK
describe "nested `on_*` blocks with comments" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
on_arm do on_arm do
version "1.0.2" version "1.0.2"
^{} stanzas within the same group should have no lines between them
sha256 :no_check # comment on same line sha256 :no_check # comment on same line
end end
on_intel do on_intel do
@ -559,10 +423,8 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
end end
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
on_arm do on_arm do
version "1.0.2" version "1.0.2"
@ -575,8 +437,4 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
end end
CASK CASK
end end
include_examples "autocorrects source"
end
end
end end

View File

@ -1,26 +1,18 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::StanzaOrder, :config do describe RuboCop::Cop::Cask::StanzaOrder, :config do
include CaskCop it "accepts a sole stanza" do
expect_no_offenses <<~CASK
context "when there is only one stanza" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
end end
CASK CASK
end end
include_examples "does not report any offenses" it "accepts when all stanzas are in order" do
end expect_no_offenses <<~CASK
context "when no stanzas are out of order" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm", intel: "x86_64" arch arm: "arm", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
@ -31,59 +23,37 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense when stanzas are out of order" do
end expect_offense <<~CASK
context "when one pair of stanzas is out of order" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
version :latest version :latest
^^^^^^^^^^^^^^^ `version` stanza out of order
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "`sha256` stanza out of order",
severity: :convention,
line: 2,
column: 2,
source: "sha256 :no_check",
}, {
message: "`version` stanza out of order",
severity: :convention,
line: 3,
column: 2,
source: "version :latest",
}]
end
include_examples "reports offenses" it "reports an offense when an `arch` stanza is out of order" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when the arch stanza is out of order" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
^^^^^^^^^^^^^^^ `version` stanza out of order
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
arch arm: "arm", intel: "x86_64" arch arm: "arm", intel: "x86_64"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `arch` stanza out of order
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm", intel: "x86_64" arch arm: "arm", intel: "x86_64"
version :latest version :latest
@ -91,46 +61,20 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "`version` stanza out of order",
severity: :convention,
line: 2,
column: 2,
source: "version :latest",
}, {
message: "`sha256` stanza out of order",
severity: :convention,
line: 3,
column: 2,
source: "sha256 :no_check",
}, {
message: "`arch` stanza out of order",
severity: :convention,
line: 4,
column: 2,
source: 'arch arm: "arm", intel: "x86_64"',
}]
end
include_examples "reports offenses" it "reports an offense when an `on_arch_conditional` variable assignment is out of order" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when an arch variable assignment is out of order" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm", intel: "x86_64" arch arm: "arm", intel: "x86_64"
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
version :latest version :latest
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `on_arch_conditional` stanza out of order
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm", intel: "x86_64" arch arm: "arm", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
@ -139,40 +83,20 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "`sha256` stanza out of order",
severity: :convention,
line: 3,
column: 2,
source: "sha256 :no_check",
}, {
message: "`on_arch_conditional` stanza out of order",
severity: :convention,
line: 5,
column: 2,
source: 'folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"',
}]
end
include_examples "reports offenses" it "reports an offense when an `on_arch_conditional` variable assignment is above an `arch` stanza" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when an arch variable assignment is above the arch stanza" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `on_arch_conditional` stanza out of order
arch arm: "arm", intel: "x86_64" arch arm: "arm", intel: "x86_64"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `arch` stanza out of order
version :latest version :latest
sha256 :no_check sha256 :no_check
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm", intel: "x86_64" arch arm: "arm", intel: "x86_64"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
@ -181,42 +105,24 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "`on_arch_conditional` stanza out of order",
severity: :convention,
line: 2,
column: 2,
source: 'folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"',
}, {
message: "`arch` stanza out of order",
severity: :convention,
line: 3,
column: 2,
source: 'arch arm: "arm", intel: "x86_64"',
}]
end
include_examples "reports offenses" it "reports an offense when multiple stanzas are out of order" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when many stanzas are out of order" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `url` stanza out of order
uninstall :quit => 'com.example.foo', uninstall :quit => 'com.example.foo',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `uninstall` stanza out of order
:kext => 'com.example.foo.kext' :kext => 'com.example.foo.kext'
version :latest version :latest
^^^^^^^^^^^^^^^ `version` stanza out of order
app 'Foo.app' app 'Foo.app'
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -227,56 +133,28 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "`url` stanza out of order",
severity: :convention,
line: 2,
column: 2,
source: "url 'https://foo.brew.sh/foo.zip'",
}, {
message: "`uninstall` stanza out of order",
severity: :convention,
line: 3,
column: 2,
source: "uninstall :quit => 'com.example.foo'," \
"\n :kext => 'com.example.foo.kext'",
}, {
message: "`version` stanza out of order",
severity: :convention,
line: 5,
column: 2,
source: "version :latest",
}, {
message: "`sha256` stanza out of order",
severity: :convention,
line: 7,
column: 2,
source: "sha256 :no_check",
}]
end
include_examples "reports offenses" it "does not reorder multiple stanzas of the same type" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when a stanza appears multiple times" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
name 'Foo' name 'Foo'
^^^^^^^^^^ `name` stanza out of order
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `url` stanza out of order
name 'FancyFoo' name 'FancyFoo'
^^^^^^^^^^^^^^^ `name` stanza out of order
version :latest version :latest
^^^^^^^^^^^^^^^ `version` stanza out of order
app 'Foo.app' app 'Foo.app'
^^^^^^^^^^^^^ `app` stanza out of order
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
name 'FunkyFoo' name 'FunkyFoo'
^^^^^^^^^^^^^^^ `name` stanza out of order
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -289,28 +167,23 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
CASK CASK
end end
it "preserves the original order" do it "keeps associated comments when auto-correcting" do
expect_autocorrected_source(source, correct_source) expect_offense <<~CASK
end
end
context "when a stanza has a comment" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
# comment with an empty line between # comment with an empty line between
# comment directly above # comment directly above
postflight do postflight do
^^^^^^^^^^^^^ `postflight` stanza out of order
puts 'We have liftoff!' puts 'We have liftoff!'
end end
sha256 :no_check # comment on same line sha256 :no_check # comment on same line
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK, loop: false
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check # comment on same line sha256 :no_check # comment on same line
@ -324,27 +197,26 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
CASK CASK
end end
include_examples "autocorrects source" it "reports an offense when an `on_arch_conditional` variable assignment with a comment is out of order" do
end expect_offense <<~CASK
context "when a variable assignment is out of order with a comment" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
^^^^^^^^^^^^^^^ `version` stanza out of order
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
# comment with an empty line between # comment with an empty line between
# comment directly above # comment directly above
postflight do postflight do
^^^^^^^^^^^^^ `postflight` stanza out of order
puts 'We have liftoff!' puts 'We have liftoff!'
end end
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" # comment on same line folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" # comment on same line
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `on_arch_conditional` stanza out of order
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" # comment on same line folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" # comment on same line
version :latest version :latest
@ -359,45 +231,58 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
CASK CASK
end end
include_examples "autocorrects source" shared_examples "caveats" do
end it "reports an offense when a `caveats` stanza is out of order" do
# Indent all except the first line.
interpolated_caveats = caveats.lines.map { |l| " #{l}" }.join.strip
context "when the caveats stanza is out of order" do expect_offense <<~CASK
let(:source) do
format(<<~CASK, caveats: caveats.strip)
cask 'foo' do cask 'foo' do
name 'Foo' name 'Foo'
^^^^^^^^^^ `name` stanza out of order
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
%<caveats>s ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `url` stanza out of order
#{interpolated_caveats}
version :latest version :latest
^^^^^^^^^^^^^^^ `version` stanza out of order
app 'Foo.app' app 'Foo.app'
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
end end
CASK CASK
end
let(:correct_source) do # Remove offense annotations.
format(<<~CASK, caveats: caveats.strip) corrected_caveats = interpolated_caveats.gsub(/\n\s*\^+\s+.*$/, "")
expect_correction <<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
name 'Foo' name 'Foo'
app 'Foo.app' app 'Foo.app'
%<caveats>s #{corrected_caveats}
end end
CASK CASK
end end
end
context "when caveats is a one-line string" do context "when caveats is a one-line string" do
let(:caveats) { "caveats 'This is a one-line caveat.'" } let(:caveats) do
<<~CAVEATS
caveats 'This is a one-line caveat.'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `caveats` stanza out of order
CAVEATS
end
include_examples "autocorrects source" include_examples "caveats"
end end
context "when caveats is a heredoc" do context "when caveats is a heredoc" do
let(:caveats) do let(:caveats) do
<<~CAVEATS <<~CAVEATS
caveats <<~EOS caveats <<~EOS
^^^^^^^^^^^^^^ `caveats` stanza out of order
This is a multiline caveat. This is a multiline caveat.
Let's hope it doesn't cause any problems! Let's hope it doesn't cause any problems!
@ -405,13 +290,14 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
CAVEATS CAVEATS
end end
include_examples "autocorrects source" include_examples "caveats"
end end
context "when caveats is a block" do context "when caveats is a block" do
let(:caveats) do let(:caveats) do
<<~CAVEATS <<~CAVEATS
caveats do caveats do
^^^^^^^^^^ `caveats` stanza out of order
puts 'This is a multiline caveat.' puts 'This is a multiline caveat.'
puts "Let's hope it doesn't cause any problems!" puts "Let's hope it doesn't cause any problems!"
@ -419,27 +305,29 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
CAVEATS CAVEATS
end end
include_examples "autocorrects source" include_examples "caveats"
end
end end
context "when the postflight stanza is out of order" do it "reports an offense when the `postflight` stanza is out of order" do
let(:source) do expect_offense <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
name 'Foo' name 'Foo'
^^^^^^^^^^ `name` stanza out of order
url 'https://foo.brew.sh/foo.zip' url 'https://foo.brew.sh/foo.zip'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `url` stanza out of order
postflight do postflight do
^^^^^^^^^^^^^ `postflight` stanza out of order
puts 'We have liftoff!' puts 'We have liftoff!'
end end
version :latest version :latest
^^^^^^^^^^^^^^^ `version` stanza out of order
app 'Foo.app' app 'Foo.app'
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -453,20 +341,21 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
CASK CASK
end end
include_examples "autocorrects source" it "supports `on_arch` blocks and their contents" do
end expect_offense <<~CASK
context "when `on_arch` blocks and their contents are out of order" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
on_intel do on_intel do
^^^^^^^^^^^ `on_intel` stanza out of order
url "https://foo.brew.sh/foo-intel.zip" url "https://foo.brew.sh/foo-intel.zip"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `url` stanza out of order
version :latest version :latest
^^^^^^^^^^^^^^^ `version` stanza out of order
sha256 :no_check sha256 :no_check
^^^^^^^^^^^^^^^^ `sha256` stanza out of order
end end
on_arm do on_arm do
^^^^^^^^^ `on_arm` stanza out of order
version :latest version :latest
sha256 :no_check sha256 :no_check
@ -474,10 +363,8 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
end end
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
on_arm do on_arm do
version :latest version :latest
@ -495,9 +382,6 @@ describe RuboCop::Cop::Cask::StanzaOrder, :config do
CASK CASK
end end
include_examples "autocorrects source"
end
it "registers an offense when `on_os` stanzas and their contents are out of order" do it "registers an offense when `on_os` stanzas and their contents are out of order" do
expect_offense <<~CASK expect_offense <<~CASK
cask "foo" do cask "foo" do

View File

@ -1,102 +1,57 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators, :config do
include CaskCop it "accepts a simple `version` interpolation" do
expect_no_offenses <<~'CASK'
subject(:cop) { described_class.new }
context "when url version interpolation does not include version.before_comma or version.after_comma" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version '1.1' version '1.1'
url 'https://foo.brew.sh/foo-\#{version}.dmg' url 'https://foo.brew.sh/foo-#{version}.dmg'
end end
CASK CASK
end end
include_examples "does not report any offenses" it "accepts an interpolation using `version.csv`" do
end expect_no_offenses <<~'CASK'
context "when the url uses csv" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version '1.1,111' version '1.1,111'
url 'https://foo.brew.sh/foo-\#{version.csv.first}.dmg' url 'https://foo.brew.sh/foo-#{version.csv.first}.dmg'
end end
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense for an interpolation using `version.before_comma`" do
end expect_offense <<~'CASK'
context "when the url uses version.before_comma" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version '1.1,111' version '1.1,111'
url 'https://foo.brew.sh/foo-\#{version.before_comma}.dmg' url 'https://foo.brew.sh/foo-#{version.before_comma}.dmg'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `version.csv.first` instead of `version.before_comma` and `version.csv.second` instead of `version.after_comma`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~'CASK'
<<~CASK
cask 'foo' do cask 'foo' do
version '1.1,111' version '1.1,111'
url 'https://foo.brew.sh/foo-\#{version.csv.first}.dmg' url 'https://foo.brew.sh/foo-#{version.csv.first}.dmg'
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "Cask/UrlLegacyCommaSeparators: Use 'version.csv.first' instead of 'version.before_comma' " \
"and 'version.csv.second' instead of 'version.after_comma'",
severity: :convention,
line: 3,
column: 6,
source: "'https://foo.brew.sh/foo-\#{version.before_comma}.dmg'",
}]
end
include_examples "reports offenses" it "reports an offense for an interpolation using `version.after_comma`" do
expect_offense <<~'CASK'
include_examples "autocorrects source"
end
context "when the url uses version.after_comma" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
version '1.1,111' version '1.1,111'
url 'https://foo.brew.sh/foo-\#{version.after_comma}.dmg' url 'https://foo.brew.sh/foo-#{version.after_comma}.dmg'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `version.csv.first` instead of `version.before_comma` and `version.csv.second` instead of `version.after_comma`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~'CASK'
<<~CASK
cask 'foo' do cask 'foo' do
version '1.1,111' version '1.1,111'
url 'https://foo.brew.sh/foo-\#{version.csv.second}.dmg' url 'https://foo.brew.sh/foo-#{version.csv.second}.dmg'
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "Cask/UrlLegacyCommaSeparators: Use 'version.csv.first' instead of 'version.before_comma' " \
"and 'version.csv.second' instead of 'version.after_comma'",
severity: :convention,
line: 3,
column: 6,
source: "'https://foo.brew.sh/foo-\#{version.after_comma}.dmg'",
}]
end
include_examples "reports offenses"
include_examples "autocorrects source"
end
end end

View File

@ -1,16 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::Url do describe RuboCop::Cop::Cask::Url, :config do
include CaskCop it "accepts a `verified` value that does not start with a protocol" do
expect_no_offenses <<~CASK
subject(:cop) { described_class.new }
context "when url 'verified' value does not start with a protocol" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg", url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/" verified: "example.com/download/"
@ -18,31 +12,16 @@ describe RuboCop::Cop::Cask::Url do
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense for a `verified` value that starts with a protocol" do
end expect_offense <<~CASK
context "when url 'verified' value starts with a protocol" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg", url "https://example.com/download/foo-v1.2.0.dmg",
verified: "https://example.com/download/" verified: "https://example.com/download/"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Verified URL parameter value should not contain a URL scheme.
end end
CASK CASK
end
let(:expected_offenses) do expect_correction <<~CASK
[{
message: "Cask/Url: Verified URL parameter value should not contain a URL scheme.",
severity: :convention,
line: 3,
column: 16,
source: "\"https://example.com/download/\"",
}]
end
let(:correct_source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg", url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/" verified: "example.com/download/"
@ -50,14 +29,9 @@ describe RuboCop::Cop::Cask::Url do
CASK CASK
end end
include_examples "reports offenses" context "when then URL does not have a path and ends with a /" do
include_examples "autocorrects source" it "accepts a `verified` value ending with a /" do
end expect_no_offenses <<~CASK
context "when url 'verified' value does not have a path component" do
context "when the URL ends with a slash" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://example.org/", url "https://example.org/",
verified: "example.org/" verified: "example.org/"
@ -65,47 +39,27 @@ describe RuboCop::Cop::Cask::Url do
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense for a `verified` value not ending a /" do
end expect_offense <<~CASK
context "when the URL does not end with a slash" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://example.org/", url "https://example.org/",
verified: "example.org" verified: "example.org"
^^^^^^^^^^^^^ Verified URL parameter value should end with a /.
end end
CASK CASK
end
let(:expected_offenses) do expect_correction <<~CASK
[{
message: "Cask/Url: Verified URL parameter value should end with a /.",
severity: :convention,
line: 3,
column: 16,
source: "\"example.org\"",
}]
end
let(:correct_source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://example.org/", url "https://example.org/",
verified: "example.org/" verified: "example.org/"
end end
CASK CASK
end end
include_examples "reports offenses"
include_examples "autocorrects source"
end
end end
context "when the URL does not end with a slash" do context "when the URL has a path and does not end with a /" do
describe "and it has one path component" do it "accepts a `verified` value with one path component" do
let(:source) do expect_no_offenses <<~CASK
<<~CASK
cask "foo" do cask "foo" do
url "https://github.com/Foo", url "https://github.com/Foo",
verified: "github.com/Foo" verified: "github.com/Foo"
@ -113,26 +67,19 @@ describe RuboCop::Cop::Cask::Url do
CASK CASK
end end
include_examples "does not report any offenses" it "accepts a `verified` value with two path components" do
end expect_no_offenses <<~CASK
describe "and it has two path components" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://github.com/foo/foo.git", url "https://github.com/foo/foo.git",
verified: "github.com/foo/foo" verified: "github.com/foo/foo"
end end
CASK CASK
end end
include_examples "does not report any offenses"
end
end end
context "when the url ends with a / and the verified value does too" do context "when the url ends with a /" do
let(:source) do it "accepts a `verified` value ending with a /" do
<<~CASK expect_no_offenses <<~CASK
cask "foo" do cask "foo" do
url "https://github.com/", url "https://github.com/",
verified: "github.com/" verified: "github.com/"
@ -140,45 +87,26 @@ describe RuboCop::Cop::Cask::Url do
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense for a `verified` value not ending with a /" do
end expect_offense <<~CASK
context "when the url ends with a / and the verified value does not" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://github.com/", url "https://github.com/",
verified: "github.com" verified: "github.com"
^^^^^^^^^^^^ Verified URL parameter value should end with a /.
end end
CASK CASK
end
let(:expected_offenses) do expect_correction <<~CASK
[{
message: "Cask/Url: Verified URL parameter value should end with a /.",
severity: :convention,
line: 3,
column: 16,
source: "\"github.com\"",
}]
end
let(:correct_source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://github.com/", url "https://github.com/",
verified: "github.com/" verified: "github.com/"
end end
CASK CASK
end end
include_examples "reports offenses"
include_examples "autocorrects source"
end end
context "when url 'verified' value has a path component that ends with a /" do it "accepts a `verified` value with a path ending with a /" do
let(:source) do expect_no_offenses <<~CASK
<<~CASK
cask "foo" do cask "foo" do
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg", url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
verified: "github.com/Foo/foo/" verified: "github.com/Foo/foo/"
@ -186,12 +114,9 @@ describe RuboCop::Cop::Cask::Url do
CASK CASK
end end
include_examples "does not report any offenses" context "when the URL uses interpolation" do
end it "accepts a `verified` value with a path ending with a /" do
expect_no_offenses <<~CASK
context "when the url has interpolation in it and the verified url ends with a /" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
version "1.2.3" version "1.2.3"
url "Cask/Url: https://example.com/download/foo-v\#{version}.dmg", url "Cask/Url: https://example.com/download/foo-v\#{version}.dmg",
@ -199,40 +124,22 @@ describe RuboCop::Cop::Cask::Url do
end end
CASK CASK
end end
include_examples "does not report any offenses"
end end
context "when the url 'verified' value has a path component that doesn't end with a /" do it "reports an offense for a `verified` value with a path component that doesn't end with a /" do
let(:source) do expect_offense <<~CASK
<<~CASK
cask "foo" do cask "foo" do
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg", url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
verified: "github.com/Foo/foo" verified: "github.com/Foo/foo"
^^^^^^^^^^^^^^^^^^^^ Verified URL parameter value should end with a /.
end end
CASK CASK
end
let(:expected_offenses) do expect_correction <<~CASK
[{
message: "Cask/Url: Verified URL parameter value should end with a /.",
severity: :convention,
line: 3,
column: 16,
source: "\"github.com/Foo/foo\"",
}]
end
let(:correct_source) do
<<~CASK
cask "foo" do cask "foo" do
url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg", url "https://github.com/Foo/foo/releases/download/v1.2.0/foo-v1.2.0.dmg",
verified: "github.com/Foo/foo/" verified: "github.com/Foo/foo/"
end end
CASK CASK
end end
include_examples "reports offenses"
include_examples "autocorrects source"
end
end end

View File

@ -1,281 +1,140 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
require "test/rubocops/cask/shared_examples/cask_cop"
describe RuboCop::Cop::Cask::Variables do describe RuboCop::Cop::Cask::Variables, :config do
include CaskCop it "accepts when there are no variables" do
expect_no_offenses <<~CASK
subject(:cop) { described_class.new }
context "when there are no variables" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
version :latest version :latest
end end
CASK CASK
end end
include_examples "does not report any offenses" it "accepts when there is an `arch` stanza" do
end expect_no_offenses <<~CASK
context "when there is an arch stanza" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
arch arm: "darwin-arm64", intel: "darwin" arch arm: "darwin-arm64", intel: "darwin"
end end
CASK CASK
end end
include_examples "does not report any offenses" it "accepts an `on_arch_conditional` variable" do
end expect_no_offenses <<~CASK
context "when there is a non-arch variable that uses the arch conditional" do
let(:source) do
<<~CASK
cask "foo" do cask "foo" do
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
end end
CASK CASK
end end
include_examples "does not report any offenses" it "reports an offense for an `arch` variable using strings" do
end expect_offense <<~CASK
context "when there is an arch variable" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch = Hardware::CPU.intel? ? "darwin" : "darwin-arm64" arch = Hardware::CPU.intel? ? "darwin" : "darwin-arm64"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `arch arm: "darwin-arm64", intel: "darwin"` instead of `arch = Hardware::CPU.intel? ? "darwin" : "darwin-arm64"`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "darwin-arm64", intel: "darwin" arch arm: "darwin-arm64", intel: "darwin"
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: 'Cask/Variables: Use `arch arm: "darwin-arm64", intel: "darwin"` instead of ' \
'`arch = Hardware::CPU.intel? ? "darwin" : "darwin-arm64"`',
severity: :convention,
line: 2,
column: 2,
source: 'arch = Hardware::CPU.intel? ? "darwin" : "darwin-arm64"',
}]
end
include_examples "reports offenses" it "reports an offense for an `arch` variable using symbols" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when there is an arch variable that doesn't use strings" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch = Hardware::CPU.intel? ? :darwin : :darwin_arm64 arch = Hardware::CPU.intel? ? :darwin : :darwin_arm64
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `arch arm: :darwin_arm64, intel: :darwin` instead of `arch = Hardware::CPU.intel? ? :darwin : :darwin_arm64`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: :darwin_arm64, intel: :darwin arch arm: :darwin_arm64, intel: :darwin
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: "Cask/Variables: Use `arch arm: :darwin_arm64, intel: :darwin` instead of " \
"`arch = Hardware::CPU.intel? ? :darwin : :darwin_arm64`",
severity: :convention,
line: 2,
column: 2,
source: "arch = Hardware::CPU.intel? ? :darwin : :darwin_arm64",
}]
end
include_examples "reports offenses" it "reports an offense for an `arch` variable with an empty string" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when there is an arch with an empty string" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch = Hardware::CPU.intel? ? "" : "arm64" arch = Hardware::CPU.intel? ? "" : "arm64"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `arch arm: "arm64"` instead of `arch = Hardware::CPU.intel? ? "" : "arm64"`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "arm64" arch arm: "arm64"
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: 'Cask/Variables: Use `arch arm: "arm64"` instead of ' \
'`arch = Hardware::CPU.intel? ? "" : "arm64"`',
severity: :convention,
line: 2,
column: 2,
source: 'arch = Hardware::CPU.intel? ? "" : "arm64"',
}]
end
include_examples "reports offenses" it "reports an offense for a non-`arch` variable using strings" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when there is a non-arch variable" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
folder = Hardware::CPU.intel? ? "darwin" : "darwin-arm64" folder = Hardware::CPU.intel? ? "darwin" : "darwin-arm64"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"` instead of `folder = Hardware::CPU.intel? ? "darwin" : "darwin-arm64"`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: 'Cask/Variables: Use `folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"` ' \
'instead of `folder = Hardware::CPU.intel? ? "darwin" : "darwin-arm64"`',
severity: :convention,
line: 2,
column: 2,
source: 'folder = Hardware::CPU.intel? ? "darwin" : "darwin-arm64"',
}]
end
include_examples "reports offenses" it "reports an offense for a non-`arch` variable with an empty string" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when there is a non-arch variable with an empty string" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
folder = Hardware::CPU.intel? ? "amd64" : "" folder = Hardware::CPU.intel? ? "amd64" : ""
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `folder = on_arch_conditional intel: "amd64"` instead of `folder = Hardware::CPU.intel? ? "amd64" : ""`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
folder = on_arch_conditional intel: "amd64" folder = on_arch_conditional intel: "amd64"
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: 'Cask/Variables: Use `folder = on_arch_conditional intel: "amd64"` instead of ' \
'`folder = Hardware::CPU.intel? ? "amd64" : ""`',
severity: :convention,
line: 2,
column: 2,
source: 'folder = Hardware::CPU.intel? ? "amd64" : ""',
}]
end
include_examples "reports offenses" it "reports an offense for consecutive `arch` and non-`arch` variables" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when there is an arch and a non-arch variable" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
arch = Hardware::CPU.arm? ? "darwin-arm64" : "darwin" arch = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `arch arm: "darwin-arm64", intel: "darwin"` instead of `arch = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"`.
folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin" folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"` instead of `folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
arch arm: "darwin-arm64", intel: "darwin" arch arm: "darwin-arm64", intel: "darwin"
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: 'Cask/Variables: Use `arch arm: "darwin-arm64", intel: "darwin"` instead of ' \
'`arch = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"`',
severity: :convention,
line: 2,
column: 2,
source: 'arch = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"',
}, {
message: 'Cask/Variables: Use `folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"` ' \
'instead of `folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"`',
severity: :convention,
line: 3,
column: 2,
source: 'folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"',
}]
end
include_examples "reports offenses" it "reports an offense for two consecutive non-`arch` variables" do
expect_offense <<~CASK
include_examples "autocorrects source"
end
context "when there are two non-arch variables" do
let(:source) do
<<~CASK
cask 'foo' do cask 'foo' do
folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin" folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"` instead of `folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"`.
platform = Hardware::CPU.intel? ? "darwin": "darwin-arm64" platform = Hardware::CPU.intel? ? "darwin": "darwin-arm64"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `platform = on_arch_conditional arm: "darwin-arm64", intel: "darwin"` instead of `platform = Hardware::CPU.intel? ? "darwin": "darwin-arm64"`.
end end
CASK CASK
end
let(:correct_source) do expect_correction <<~CASK
<<~CASK
cask 'foo' do cask 'foo' do
folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin" folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
platform = on_arch_conditional arm: "darwin-arm64", intel: "darwin" platform = on_arch_conditional arm: "darwin-arm64", intel: "darwin"
end end
CASK CASK
end end
let(:expected_offenses) do
[{
message: 'Cask/Variables: Use `folder = on_arch_conditional arm: "darwin-arm64", intel: "darwin"` instead ' \
'of `folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"`',
severity: :convention,
line: 2,
column: 2,
source: 'folder = Hardware::CPU.arm? ? "darwin-arm64" : "darwin"',
}, {
message: 'Cask/Variables: Use `platform = on_arch_conditional arm: "darwin-arm64", intel: "darwin"` ' \
'instead of `platform = Hardware::CPU.intel? ? "darwin": "darwin-arm64"`',
severity: :convention,
line: 3,
column: 2,
source: 'platform = Hardware::CPU.intel? ? "darwin": "darwin-arm64"',
}]
end
include_examples "reports offenses"
include_examples "autocorrects source"
end
end end