diff options
| author | Gautham Goli | 2017-06-01 00:57:24 +0530 |
|---|---|---|
| committer | Gautham Goli | 2017-06-08 21:53:46 +0530 |
| commit | d09d5ecc559c4971fae77769cb1a203bb20c7a97 (patch) | |
| tree | f7fff3a14cf6c6fdaa9ec0996c18af7ed16c41e0 /Library/Homebrew/rubocops | |
| parent | 134da5b8c266d0e5f07a1f79bff233f372c7ef30 (diff) | |
| download | brew-d09d5ecc559c4971fae77769cb1a203bb20c7a97.tar.bz2 | |
audit: Port audit_checksum method to rubocop and add tests
Diffstat (limited to 'Library/Homebrew/rubocops')
| -rw-r--r-- | Library/Homebrew/rubocops/checksum_cop.rb | 55 | ||||
| -rw-r--r-- | Library/Homebrew/rubocops/extend/formula_cop.rb | 5 |
2 files changed, 57 insertions, 3 deletions
diff --git a/Library/Homebrew/rubocops/checksum_cop.rb b/Library/Homebrew/rubocops/checksum_cop.rb new file mode 100644 index 000000000..98e6a8bd3 --- /dev/null +++ b/Library/Homebrew/rubocops/checksum_cop.rb @@ -0,0 +1,55 @@ +require_relative "./extend/formula_cop" + +module RuboCop + module Cop + module FormulaAudit + class Checksum < FormulaCop + def audit_formula(_node, _class_node, _parent_class_node, body_node) + %w[Stable Devel HEAD].each do |name| + next unless spec_node = find_block(body_node, name.downcase.to_sym) + _, _, spec_body = *spec_node + audit_checksums(spec_body, name) + resource_blocks = find_all_blocks(spec_body, :resource) + resource_blocks.each do |rb| + _, _, resource_body = *rb + audit_checksums(resource_body, name, string_content(parameters(rb).first)) + end + end + end + + def audit_checksums(node, spec, resource_name = nil) + msg_prefix = if resource_name + "#{spec} resource \"#{resource_name}\": " + else + "#{spec}: " + end + if find_node_method_by_name(node, :md5) + problem "#{msg_prefix}MD5 checksums are deprecated, please use SHA256" + end + + if find_node_method_by_name(node, :sha1) + problem "#{msg_prefix}SHA1 checksums are deprecated, please use SHA256" + end + + checksum_node = find_node_method_by_name(node, :sha256) + checksum = parameters(checksum_node).first + if string_content(checksum).size.zero? + problem "#{msg_prefix}sha256 is empty" + return + end + + if string_content(checksum).size != 64 && regex_match_group(checksum, /^\w*$/) + problem "#{msg_prefix}sha256 should be 64 characters" + end + + unless regex_match_group(checksum, /^[a-f0-9]+$/i) + problem "#{msg_prefix}sha256 contains invalid characters" + end + + return unless regex_match_group(checksum, /^[a-f0-9]+$/) + problem "#{msg_prefix}sha256 should be lowercase" + end + end + end + end +end diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index 6bb406c05..b6fa45d3f 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -176,7 +176,7 @@ module RuboCop # Returns an array of block nodes of any depth below node in AST def find_all_blocks(node, block_name) return if node.nil? - node.each_descendant(:block).select { |block_node| block_name == block_node.method_name} + node.each_descendant(:block).select { |block_node| block_name == block_node.method_name } end # Returns a method definition node with method_name @@ -256,8 +256,7 @@ module RuboCop # Returns the array of arguments of the method_node def parameters(method_node) - return unless method_node.send_type? || method_node.block_type? - method_node.method_args + method_node.method_args if method_node.send_type? || method_node.block_type? end # Returns true if the given parameters are present in method call |
