Add Rubocop cop to enforce default_version in software definitions

Prevent empty build caches caused by a lack of default version.

Closes https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/8476

Signed-off-by: Balasankar 'Balu' C <balasankar@gitlab.com>
This commit is contained in:
Balasankar 'Balu' C 2024-04-08 16:29:41 +00:00 committed by Robert Marshall
parent 72c2e00be6
commit 22ae898b24
4 changed files with 85 additions and 0 deletions

View File

@ -78,6 +78,21 @@ Cop/AvoidUsingEnv:
- lib/gitlab/util.rb
- spec/lib/gitlab/util_spec.rb
Cop/SpecifyDefaultVersion:
Enabled: true
Include:
- config/software/*.rb
Exclude:
# These files either use `path` source and doesn't need a default_version
# or is just a wrapper around other definitions
- config/software/gitlab-config-template.rb
- config/software/gitlab-cookbooks.rb
- config/software/gitlab-ctl-ee.rb
- config/software/gitlab-ctl.rb
- config/software/gitlab-scripts.rb
- config/software/gitlab-selinux.rb
- config/software/openssl.rb
Style/MultilineIfModifier:
Enabled: false

View File

@ -0,0 +1,28 @@
module Rubocop
module Cop
class SpecifyDefaultVersion < RuboCop::Cop::Base
NOTICE_REGEXP = '^ *default_version'.freeze
MSG = 'Specify default_version for the component.'.freeze
def on_new_investigation
return if notice_found?(processed_source)
add_global_offense(format(MSG))
end
private
def notice_found?(processed_source)
notice_regexp = Regexp.new(NOTICE_REGEXP)
notice_found = false
processed_source.lines.each do |line|
notice_found = notice_regexp.match?(line)
break if notice_found
end
notice_found
end
end
end
end

View File

@ -1 +1,2 @@
require_relative 'cop/avoid_using_env'
require_relative 'cop/specify_default_version'

View File

@ -0,0 +1,41 @@
require 'spec_helper'
require 'rubocop/rspec/cop_helper'
require 'rubocop/rspec/expect_offense'
require 'rubocop/cop/specify_default_version'
RSpec.describe Rubocop::Cop::SpecifyDefaultVersion do
include CopHelper
include RuboCop::RSpec::ExpectOffense
subject(:cop) { described_class.new }
it 'flags violation for software definition files without default_version set' do
expect_offense(<<~RUBY)
name 'sample'
^{} Specify default_version for the component.
license 'MIT'
build do
make
make install
end
RUBY
end
it 'does not flag violation for software definition files with default_version set' do
expect_no_offenses(<<~RUBY)
name 'sample'
license 'MIT'
default_version '1.0.0'
build do
make
make install
end
RUBY
end
end