rubocop: add uninstall methods order tests

Co-authored-by: Bevan Kay <email@bevankay.me>
This commit is contained in:
Razvan Azamfirei 2024-01-01 17:11:24 +02:00
parent db72295c20
commit 7c540dd3c5
No known key found for this signature in database
GPG Key ID: 62E97BFCB12046BD

View File

@ -2,11 +2,30 @@
require "rubocops/rubocop-cask"
describe RuboCop::Cop::Cask::UninstallMethodsOrder, :config do
context "with order errors in both the uninstall and zap block" do
it "reports an offense and corrects the order" do
expect_offense(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
context "with uninstall blocks" do
context "when methods are incorrectly ordered" do
it "detects and corrects ordering offenses in the uninstall block when each method contains a single item" do
expect_offense(<<~CASK)
cask 'foo' do
uninstall quit: "com.example.foo",
^^^^ `quit` method out of order
launchctl: "com.example.foo"
^^^^^^^^^ `launchctl` method out of order
end
CASK
expect_correction(<<~CASK)
cask 'foo' do
uninstall launchctl: "com.example.foo",
quit: "com.example.foo"
end
CASK
end
it "detects and corrects ordering offenses in the uninstall block when methods contain arrays" do
expect_offense(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall delete: [
^^^^^^ `delete` method out of order
@ -20,21 +39,12 @@ describe RuboCop::Cop::Cask::UninstallMethodsOrder, :config do
},
pkgutil: "org.foo.bar"
^^^^^^^ `pkgutil` method out of order
end
CASK
zap delete: [
"~/Library/Application Support/Bar",
"~/Library/Application Support/Foo",
],
rmdir: "~/Library/Application Support",
^^^^^ `rmdir` method out of order
trash: "~/Library/Application Support/FooBar"
^^^^^ `trash` method out of order
end
CASK
expect_correction(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
expect_correction(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall script: {
executable: "/usr/local/bin/foo",
@ -45,165 +55,301 @@ describe RuboCop::Cop::Cask::UninstallMethodsOrder, :config do
"/usr/local/bin/foo",
"/usr/local/bin/foobar",
]
end
CASK
end
end
zap delete: [
"~/Library/Application Support/Bar",
"~/Library/Application Support/Foo",
],
trash: "~/Library/Application Support/FooBar",
rmdir: "~/Library/Application Support"
context "when methods are correctly ordered" do
it "does not report an offense" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall script: {
executable: "/usr/local/bin/foo",
sudo: false,
},
pkgutil: "org.foo.bar",
delete: [
"/usr/local/bin/foo",
"/usr/local/bin/foobar",
]
end
CASK
end
end
context "with a single method" do
it "does not report an offense when a single item is present in the method" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall delete: "/usr/local/bin/foo"
end
CASK
end
it "does not report an offense when the method contains an array" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall pkgutil: [
"org.foo.bar",
"org.foobar.bar",
]
end
CASK
end
end
end
context "with zap blocks" do
context "when methods are incorrectly ordered" do
it "detects and corrects ordering offenses in the zap block when each method contains a single item" do
expect_offense(<<~CASK)
cask 'foo' do
zap rmdir: "/Library/Foo",
^^^^^ `rmdir` method out of order
trash: "com.example.foo"
^^^^^ `trash` method out of order
end
CASK
expect_correction(<<~CASK)
cask 'foo' do
zap trash: "com.example.foo",
rmdir: "/Library/Foo"
end
CASK
end
it "detects and corrects ordering offenses in the zap block when methods contain arrays" do
expect_offense(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap delete: [
"~/Library/Application Support/Foo",
"~/Library/Application Support/Bar",
],
rmdir: "~/Library/Application Support",
^^^^^ `rmdir` method out of order
trash: "~/Library/Application Support/FooBar"
^^^^^ `trash` method out of order
end
CASK
expect_correction(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap delete: [
"~/Library/Application Support/Foo",
"~/Library/Application Support/Bar",
],
trash: "~/Library/Application Support/FooBar",
rmdir: "~/Library/Application Support"
end
CASK
end
end
context "when methods are correctly ordered" do
it "does not report an offense" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap delete: [
"~/Library/Application Support/Bar",
"~/Library/Application Support/Foo",
],
trash: "~/Library/Application Support/FooBar",
rmdir: "~/Library/Application Support"
end
CASK
end
end
context "with a single method" do
it "does not report an offense when a single item is present in the method" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap trash: "~/Library/Application Support/FooBar"
end
CASK
end
it "does not report an offense when the method contains an array" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap trash: [
"~/Library/Application Support/FooBar",
"~/Library/Application Support/FooBarBar",
]
end
CASK
end
end
end
context "with both uninstall and zap blocks" do
context "when both uninstall and zap methods are incorrectly ordered" do
it "detects offenses and auto-corrects to the correct order" do
expect_offense(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall delete: [
^^^^^^ `delete` method out of order
"/usr/local/bin/foo",
"/usr/local/bin/foobar",
],
script: {
^^^^^^ `script` method out of order
executable: "/usr/local/bin/foo",
sudo: false,
},
pkgutil: "org.foo.bar"
^^^^^^^ `pkgutil` method out of order
zap delete: [
"~/Library/Application Support/Bar",
"~/Library/Application Support/Foo",
],
rmdir: "~/Library/Application Support",
^^^^^ `rmdir` method out of order
trash: "~/Library/Application Support/FooBar"
^^^^^ `trash` method out of order
end
CASK
expect_correction(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall script: {
executable: "/usr/local/bin/foo",
sudo: false,
},
pkgutil: "org.foo.bar",
delete: [
"/usr/local/bin/foo",
"/usr/local/bin/foobar",
]
zap delete: [
"~/Library/Application Support/Bar",
"~/Library/Application Support/Foo",
],
trash: "~/Library/Application Support/FooBar",
rmdir: "~/Library/Application Support"
end
CASK
end
end
context "when uninstall and zap methods are correctly ordered" do
it "does not report an offense" do
expect_no_offenses(<<~CASK)
cask 'foo' do
uninstall early_script: {
executable: "foo.sh",
args: ["--unattended"],
},
launchctl: "com.example.foo",
quit: "com.example.foo",
signal: ["TERM", "com.example.foo"],
login_item: "FooApp",
kext: "com.example.foo",
script: {
executable: "foo.sh",
args: ["--unattended"],
},
pkgutil: "com.example.foo",
delete: "~/Library/Preferences/com.example.foo",
trash: "~/Library/Preferences/com.example.foo",
rmdir: "~/Library/Foo"
zap early_script: {
executable: "foo.sh",
args: ["--unattended"],
},
launchctl: "com.example.foo",
quit: "com.example.foo",
signal: ["TERM", "com.example.foo"],
login_item: "FooApp",
kext: "com.example.foo",
script: {
executable: "foo.sh",
args: ["--unattended"],
},
pkgutil: "com.example.foo",
delete: "~/Library/Preferences/com.example.foo",
trash: "~/Library/Preferences/com.example.foo",
rmdir: "~/Library/Foo"
end
CASK
end
end
end
context "when in-line comments are present" do
it "keeps associated comments when auto-correcting" do
expect_offense <<~CASK
cask 'foo' do
uninstall quit: "com.example.foo", # comment on same line
^^^^ `quit` method out of order
launchctl: "com.example.foo"
^^^^^^^^^ `launchctl` method out of order
end
CASK
expect_correction <<~CASK
cask 'foo' do
uninstall launchctl: "com.example.foo",
quit: "com.example.foo" # comment on same line
end
CASK
end
end
context "with incorrectly ordered uninstall methods" do
it "reports an offense and corrects the order" do
expect_offense(<<~CASK)
context "when methods are inside an `on_os` block" do
it "detects and corrects offenses within OS-specific blocks" do
expect_offense <<~CASK
cask "foo" do
url "https://example.com/foo.zip"
uninstall delete: [
^^^^^^ `delete` method out of order
"/usr/local/bin/foo",
"/usr/local/bin/foobar",
],
script: {
^^^^^^ `script` method out of order
executable: "/usr/local/bin/foo",
sudo: false,
},
pkgutil: "org.foo.bar"
^^^^^^^ `pkgutil` method out of order
on_catalina do
uninstall trash: "com.example.foo",
^^^^^ `trash` method out of order
launchctl: "com.example.foo"
^^^^^^^^^ `launchctl` method out of order
end
on_ventura do
uninstall quit: "com.example.foo",
^^^^ `quit` method out of order
launchctl: "com.example.foo"
^^^^^^^^^ `launchctl` method out of order
end
end
CASK
expect_correction(<<~CASK)
expect_correction <<~CASK
cask "foo" do
url "https://example.com/foo.zip"
uninstall script: {
executable: "/usr/local/bin/foo",
sudo: false,
},
pkgutil: "org.foo.bar",
delete: [
"/usr/local/bin/foo",
"/usr/local/bin/foobar",
]
end
CASK
end
end
context "with correctly ordered uninstall methods" do
it "does not report an offense" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall script: {
executable: "/usr/local/bin/foo",
sudo: false,
},
pkgutil: "org.foo.bar",
delete: [
"/usr/local/bin/foo",
"/usr/local/bin/foobar",
]
end
CASK
end
end
context "with a single method in uninstall block" do
it "does not report an offense" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall delete: "/usr/local/bin/foo"
end
CASK
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
uninstall pkgutil: [
"org.foo.bar",
"org.foobar.bar",
]
end
CASK
end
end
context "with incorrectly ordered zap methods" do
it "reports an offense and corrects the order" do
expect_offense(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap delete: [
"~/Library/Application Support/Foo",
"~/Library/Application Support/Bar",
],
rmdir: "~/Library/Application Support",
^^^^^ `rmdir` method out of order
trash: "~/Library/Application Support/FooBar"
^^^^^ `trash` method out of order
end
CASK
expect_correction(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap delete: [
"~/Library/Application Support/Foo",
"~/Library/Application Support/Bar",
],
trash: "~/Library/Application Support/FooBar",
rmdir: "~/Library/Application Support"
end
CASK
end
end
context "with correctly ordered zap methods" do
it "does not report an offense" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap delete: [
"~/Library/Application Support/Bar",
"~/Library/Application Support/Foo",
],
trash: "~/Library/Application Support/FooBar",
rmdir: "~/Library/Application Support"
end
CASK
end
end
context "with a single method in the zap block" do
it "does not report an offense" do
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap trash: "~/Library/Application Support/FooBar"
end
CASK
expect_no_offenses(<<~CASK)
cask "foo" do
url "https://example.com/foo.zip"
zap trash: [
"~/Library/Application Support/FooBar",
"~/Library/Application Support/FooBarBar",
]
on_catalina do
uninstall launchctl: "com.example.foo",
trash: "com.example.foo"
end
on_ventura do
uninstall launchctl: "com.example.foo",
quit: "com.example.foo"
end
end
CASK
end