From 82ba0475addddbc03e42b6711a8ecc7c65413ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matha=CC=88us=20Zingerle?= Date: Sun, 17 May 2020 12:52:39 -0500 Subject: [PATCH] audit: Port version checks that do not rely on `Formula` state to RuboCop and add tests --- Library/Homebrew/dev-cmd/audit.rb | 11 ------ Library/Homebrew/rubocops.rb | 1 + Library/Homebrew/rubocops/version.rb | 26 +++++++++++++ .../Homebrew/test/rubocops/version_spec.rb | 39 +++++++++++++++++++ 4 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 Library/Homebrew/rubocops/version.rb create mode 100644 Library/Homebrew/test/rubocops/version_spec.rb diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 87faf0b725..33c157f6ee 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -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 diff --git a/Library/Homebrew/rubocops.rb b/Library/Homebrew/rubocops.rb index 6b43b6f937..7a0f2c70e8 100644 --- a/Library/Homebrew/rubocops.rb +++ b/Library/Homebrew/rubocops.rb @@ -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" diff --git a/Library/Homebrew/rubocops/version.rb b/Library/Homebrew/rubocops/version.rb new file mode 100644 index 0000000000..f4ed91c18d --- /dev/null +++ b/Library/Homebrew/rubocops/version.rb @@ -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 diff --git a/Library/Homebrew/test/rubocops/version_spec.rb b/Library/Homebrew/test/rubocops/version_spec.rb new file mode 100644 index 0000000000..45ac7ecf1e --- /dev/null +++ b/Library/Homebrew/test/rubocops/version_spec.rb @@ -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