Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: fix deprecation date check #8533

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Library/Homebrew/rubocops/deprecate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
module RuboCop
module Cop
module FormulaAudit
# This cop audits deprecate!
class Deprecate < FormulaCop
# This cop audits deprecate! date
class DeprecateDate < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, body_node)
deprecate_node = find_node_method_by_name(body_node, :deprecate!)

return if deprecate_node.nil? || deprecate_node.children.length < 3
return if deprecate_node.nil?

date_node = find_strings(deprecate_node).first

begin
deprecate_date(deprecate_node) do |date_node|
Date.iso8601(string_content(date_node))
rescue ArgumentError
fixed_date_string = Date.parse(string_content(date_node)).iso8601
Expand All @@ -29,6 +27,10 @@ def autocorrect(node)
corrector.replace(node.source_range, "\"#{fixed_fixed_date_string}\"")
end
end

def_node_search :deprecate_date, <<~EOS
(pair (sym :date) $str)
EOS
end
end
end
Expand Down
61 changes: 54 additions & 7 deletions Library/Homebrew/test/rubocops/deprecate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

require "rubocops/deprecate"

describe RuboCop::Cop::FormulaAudit::Deprecate do
describe RuboCop::Cop::FormulaAudit::DeprecateDate do
subject(:cop) { described_class.new }

context "When auditing formula for deprecate!" do
context "When auditing formula for deprecate! date:" do
it "deprecation date is not ISO 8601 compliant" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! :date => "June 25, 2020"
^^^^^^^^^^^^^^^ Use `2020-06-25` to comply with ISO 8601
deprecate! date: "June 25, 2020"
^^^^^^^^^^^^^^^ Use `2020-06-25` to comply with ISO 8601
end
RUBY
end

it "deprecation date is not ISO 8601 compliant with reason" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! because: "is broken", date: "June 25, 2020"
^^^^^^^^^^^^^^^ Use `2020-06-25` to comply with ISO 8601
end
RUBY
end
Expand All @@ -20,7 +30,16 @@ class Foo < Formula
expect_no_offenses(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! :date => "2020-06-25"
deprecate! date: "2020-06-25"
end
RUBY
end

it "deprecation date is ISO 8601 compliant with reason" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! because: "is broken", date: "2020-06-25"
end
RUBY
end
Expand All @@ -34,18 +53,46 @@ class Foo < Formula
RUBY
end

it "no deprecation date with reason" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! because: "is broken"
end
RUBY
end

it "auto corrects to ISO 8601 format" do
source = <<~RUBY
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! :date => "June 25, 2020"
deprecate! date: "June 25, 2020"
end
RUBY

corrected_source = <<~RUBY
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! date: "2020-06-25"
end
RUBY

new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end

it "auto corrects to ISO 8601 format with reason" do
source = <<~RUBY
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! because: "is broken", date: "June 25, 2020"
end
RUBY

corrected_source = <<~RUBY
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
deprecate! :date => "2020-06-25"
deprecate! because: "is broken", date: "2020-06-25"
end
RUBY

Expand Down