audit: Port version checks that do not rely on Formula state to RuboCop and add tests

This commit is contained in:
Mathäus Zingerle 2020-05-17 12:52:39 -05:00
parent ca5eac845d
commit 82ba0475ad
4 changed files with 66 additions and 11 deletions

View File

@ -1042,9 +1042,6 @@ module Homebrew
def audit_version
if version.nil?
problem "missing version"
elsif version.blank?
# TODO: check could be in RuboCop
problem "version is set to an empty string"
elsif !version.detected_from_url?
version_text = version
version_url = Version.detect(url, specs)
@ -1052,14 +1049,6 @@ module Homebrew
problem "version #{version_text} is redundant with version scanned from URL"
end
end
# TODO: check could be in RuboCop
problem "version #{version} should not have a leading 'v'" if version.to_s.start_with?("v")
return unless version.to_s.match?(/_\d+$/)
# TODO: check could be in RuboCop
problem "version #{version} should not end with an underline and a number"
end
def audit_download_strategy

View File

@ -21,5 +21,6 @@ require "rubocops/class"
require "rubocops/uses_from_macos"
require "rubocops/files"
require "rubocops/keg_only"
require "rubocops/version"
require "rubocops/rubocop-cask"

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
require "rubocops/extend/formula"
module RuboCop
module Cop
module FormulaAudit
class Version < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, body_node)
version_node = find_node_method_by_name(body_node, :version)
return unless version_node
version = string_content(parameters(version_node).first)
problem "version is set to an empty string" if version.empty?
problem "version #{version} should not have a leading 'v'" if version.start_with?("v")
return unless version.match?(/_\d+$/)
problem "version #{version} should not end with an underline and a number"
end
end
end
end
end

View File

@ -0,0 +1,39 @@
# frozen_string_literal: true
require "rubocops/version"
describe RuboCop::Cop::FormulaAudit::Version do
subject(:cop) { described_class.new }
context "When auditing version" do
it "version should not be an empty string" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
version ""
^^^^^^^^^^ version is set to an empty string
end
RUBY
end
it "version should not have a leading 'v'" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
version "v1.0"
^^^^^^^^^^^^^^ version v1.0 should not have a leading 'v'
end
RUBY
end
it "version should not end with underline and number" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
version "1_0"
^^^^^^^^^^^^^ version 1_0 should not end with an underline and a number
end
RUBY
end
end
end