rubocops/checksum: use rubocop v1 API
This commit is contained in:
parent
b8fb568021
commit
9046d778e0
@ -38,7 +38,7 @@ module RuboCop
|
|||||||
|
|
||||||
return unless regex_match_group(checksum, /[^a-f0-9]+/i)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -46,6 +46,8 @@ module RuboCop
|
|||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
class ChecksumCase < FormulaCop
|
class ChecksumCase < FormulaCop
|
||||||
|
extend AutoCorrector
|
||||||
|
|
||||||
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
||||||
return if body_node.nil?
|
return if body_node.nil?
|
||||||
|
|
||||||
@ -55,15 +57,11 @@ module RuboCop
|
|||||||
next if checksum.nil?
|
next if checksum.nil?
|
||||||
next unless regex_match_group(checksum, /[A-F]+/)
|
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|
|
||||||
end
|
correction = @offensive_node.source.downcase
|
||||||
end
|
corrector.insert_before(@offensive_node.source_range, correction)
|
||||||
|
corrector.remove(@offensive_node.source_range)
|
||||||
def autocorrect(node)
|
end
|
||||||
lambda do |corrector|
|
|
||||||
correction = node.source.downcase
|
|
||||||
corrector.insert_before(node.source_range, correction)
|
|
||||||
corrector.remove(node.source_range)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -29,6 +29,7 @@ module RuboCop
|
|||||||
@line_no = line_number(node)
|
@line_no = line_number(node)
|
||||||
@source_buf = source_buffer(node)
|
@source_buf = source_buffer(node)
|
||||||
@offensive_node = node
|
@offensive_node = node
|
||||||
|
@offensive_source_range = source_range(@source_buf, @line_no, @column, @length)
|
||||||
match_object
|
match_object
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -33,12 +33,12 @@ describe RuboCop::Cop::FormulaAudit::Checksum do
|
|||||||
stable do
|
stable do
|
||||||
url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz"
|
url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz"
|
||||||
sha256 "5cf6e1ae0a645b426c0474cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9ad"
|
sha256 "5cf6e1ae0a645b426c0474cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9ad"
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters
|
||||||
|
|
||||||
resource "foo-package" do
|
resource "foo-package" do
|
||||||
url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz"
|
url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz"
|
||||||
sha256 "5cf6e1ae0a645b426c047aaa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9"
|
sha256 "5cf6e1ae0a645b426c047aaa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9"
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sha256 should be 64 characters
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -89,7 +89,7 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do
|
|||||||
RUBY
|
RUBY
|
||||||
end
|
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)
|
expect_offense(<<~RUBY)
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
url 'https://brew.sh/foo-1.0.tgz'
|
url 'https://brew.sh/foo-1.0.tgz'
|
||||||
@ -109,27 +109,14 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
end
|
|
||||||
|
|
||||||
it "auto-corrects checksums that contain uppercase letters" do
|
expect_correction(<<~RUBY)
|
||||||
source = <<~RUBY
|
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
url 'https://brew.sh/foo-1.0.tgz'
|
url 'https://brew.sh/foo-1.0.tgz'
|
||||||
stable do
|
resource "foo-outside" do
|
||||||
url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz"
|
url "https://github.com/foo-lang/foo-outside/archive/0.18.0.tar.gz"
|
||||||
sha256 "5cf6e1ae0A645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a"
|
sha256 "a4cc7cd3f7d1605ffa1ac5755cf6e1ae0a645b426b047a6a39a8b2268ddc7ea9"
|
||||||
|
|
||||||
resource "foo-package" do
|
|
||||||
url "https://github.com/foo-lang/foo-package/archive/0.18.0.tar.gz"
|
|
||||||
sha256 "5cf6e1Ae0a645b426b047aa4cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea9"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
|
|
||||||
corrected_source = <<~RUBY
|
|
||||||
class Foo < Formula
|
|
||||||
url 'https://brew.sh/foo-1.0.tgz'
|
|
||||||
stable do
|
stable do
|
||||||
url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz"
|
url "https://github.com/foo-lang/foo-compiler/archive/0.18.0.tar.gz"
|
||||||
sha256 "5cf6e1ae0a645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a"
|
sha256 "5cf6e1ae0a645b426c0a7cc7cd3f7d1605ffa1ac5756a39a8b2268ddc7ea0e9a"
|
||||||
@ -141,9 +128,6 @@ describe RuboCop::Cop::FormulaAudit::ChecksumCase do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
new_source = autocorrect_source(source)
|
|
||||||
expect(new_source).to eq(corrected_source)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user