Refactor Checksum cop to add autocorrect method
This commit is contained in:
parent
77da75e7d6
commit
0e1c88e7ae
@ -15,6 +15,9 @@ FormulaAudit/Caveats:
|
|||||||
FormulaAudit/Checksum:
|
FormulaAudit/Checksum:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
|
FormulaAudit/ChecksumCase:
|
||||||
|
Enabled: true
|
||||||
|
|
||||||
FormulaAuditStrict/BottleBlock:
|
FormulaAuditStrict/BottleBlock:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
|
|||||||
@ -21,15 +21,6 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_checksum_node(call)
|
|
||||||
return if parameters(call).empty? || parameters(call).nil?
|
|
||||||
if parameters(call).first.str_type?
|
|
||||||
parameters(call).first
|
|
||||||
elsif parameters(call).first.hash_type?
|
|
||||||
parameters(call).first.keys.first
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def audit_sha256(checksum)
|
def audit_sha256(checksum)
|
||||||
return if checksum.nil?
|
return if checksum.nil?
|
||||||
if regex_match_group(checksum, /^$/)
|
if regex_match_group(checksum, /^$/)
|
||||||
@ -41,12 +32,31 @@ module RuboCop
|
|||||||
problem "sha256 should be 64 characters"
|
problem "sha256 should be 64 characters"
|
||||||
end
|
end
|
||||||
|
|
||||||
if regex_match_group(checksum, /[^a-f0-9]+/i)
|
return unless regex_match_group(checksum, /[^a-f0-9]+/i)
|
||||||
problem "sha256 contains invalid characters"
|
problem "sha256 contains invalid characters"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return unless regex_match_group(checksum, /[A-F]+/)
|
class ChecksumCase < FormulaCop
|
||||||
problem "sha256 should be lowercase"
|
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
||||||
|
return if body_node.nil?
|
||||||
|
sha256_calls = find_every_method_call_by_name(body_node, :sha256)
|
||||||
|
sha256_calls.each do |sha256_call|
|
||||||
|
checksum = get_checksum_node(sha256_call)
|
||||||
|
next if checksum.nil?
|
||||||
|
next unless regex_match_group(checksum, /[A-F]+/)
|
||||||
|
problem "sha256 should be lowercase"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -277,6 +277,17 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the sha256 str node given a sha256 call node
|
||||||
|
def get_checksum_node(call)
|
||||||
|
return if parameters(call).empty? || parameters(call).nil?
|
||||||
|
if parameters(call).first.str_type?
|
||||||
|
parameters(call).first
|
||||||
|
# sha256 is passed as a key-value pair in bottle blocks
|
||||||
|
elsif parameters(call).first.hash_type?
|
||||||
|
parameters(call).first.keys.first
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Returns the begin position of the node's line in source code
|
# Returns the begin position of the node's line in source code
|
||||||
def line_start_column(node)
|
def line_start_column(node)
|
||||||
node.source_range.source_buffer.line_range(node.loc.line).begin_pos
|
node.source_range.source_buffer.line_range(node.loc.line).begin_pos
|
||||||
|
|||||||
@ -108,7 +108,13 @@ describe RuboCop::Cop::FormulaAudit::Checksum do
|
|||||||
expect_offense(expected, actual)
|
expect_offense(expected, actual)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe RuboCop::Cop::FormulaAudit::ChecksumCase do
|
||||||
|
subject(:cop) { described_class.new }
|
||||||
|
|
||||||
|
context "When auditing spec checksums" do
|
||||||
it "When the checksum has upper case characters" do
|
it "When the checksum has upper case characters" do
|
||||||
source = <<-EOS.undent
|
source = <<-EOS.undent
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
@ -176,4 +182,41 @@ describe RuboCop::Cop::FormulaAudit::Checksum do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "When auditing checksum with autocorrect" do
|
||||||
|
it "When there is uppercase sha256" do
|
||||||
|
source = <<-EOS.undent
|
||||||
|
class Foo < Formula
|
||||||
|
url 'http://example.com/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"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
EOS
|
||||||
|
|
||||||
|
corrected_source = <<-EOS.undent
|
||||||
|
class Foo < Formula
|
||||||
|
url 'http://example.com/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"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
EOS
|
||||||
|
|
||||||
|
new_source = autocorrect_source(cop, source)
|
||||||
|
expect(new_source).to eq(corrected_source)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user