rubocops/checksum: use rubocop v1 API

This commit is contained in:
Jonathan Chang 2021-01-12 11:05:37 +11:00
parent b8fb568021
commit 9046d778e0
3 changed files with 16 additions and 33 deletions

View File

@ -38,7 +38,7 @@ module RuboCop
return unless regex_match_group(checksum, /[^a-f0-9]+/i)
problem "sha256 contains invalid characters"
add_offense(@offensive_source_range, message: "sha256 contains invalid characters")
end
end
@ -46,6 +46,8 @@ module RuboCop
#
# @api private
class ChecksumCase < FormulaCop
extend AutoCorrector
def audit_formula(_node, _class_node, _parent_class_node, body_node)
return if body_node.nil?
@ -55,16 +57,12 @@ module RuboCop
next if checksum.nil?
next unless regex_match_group(checksum, /[A-F]+/)
problem "sha256 should be lowercase"
add_offense(@offensive_source_range, message: "sha256 should be lowercase") do |corrector|
correction = @offensive_node.source.downcase
corrector.insert_before(@offensive_node.source_range, correction)
corrector.remove(@offensive_node.source_range)
end
end
def autocorrect(node)
lambda do |corrector|
correction = node.source.downcase
corrector.insert_before(node.source_range, correction)
corrector.remove(node.source_range)
end
end
end
end

View File

@ -29,6 +29,7 @@ module RuboCop
@line_no = line_number(node)
@source_buf = source_buffer(node)
@offensive_node = node
@offensive_source_range = source_range(@source_buf, @line_no, @column, @length)
match_object
end

View File

@ -33,12 +33,12 @@ describe RuboCop::Cop::FormulaAudit::Checksum do
stable do
url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz"
sha256 "5cf6e1ae0a645b426c0474cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9ad"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters
resource "foo-package" do
url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz"
sha256 "5cf6e1ae0a645b426c047aaa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters
end
end
end
@ -89,7 +89,7 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do
RUBY
end
it "reports an offense if a checksum outside a `stable` block contains uppercase letters" do
it "reports and corrects an offense if a checksum outside a `stable` block contains uppercase letters" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
@ -109,27 +109,14 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do
end
end
RUBY
end
it "auto-corrects checksums that contain uppercase letters" do
source = <<~RUBY
expect_correction(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
stable do
url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz"
sha256 "5cf6e1ae0A645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a"
resource "foo-package" do
url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz"
sha256 "5cf6e1Ae0a645b426b047aa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea9"
resource "foo-outside" do
url "https://github.com/foo-lang/foo-outside/archive/0.18.0.tar.gz"
sha256 "a4cc7cd3f7d1605ffa1ac5755cf6e1ae0a645b426b047a6a39a8b2268ddc7ea9"
end
end
end
RUBY
corrected_source = <<~RUBY
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
stable do
url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz"
sha256 "5cf6e1ae0a645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a"
@ -141,9 +128,6 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do
end
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end
end
end