From 9885a5f6f95db20a74dfa9751411b8ca3263a2d9 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Fri, 25 Feb 2022 20:42:12 -0800 Subject: [PATCH] Fix configuration loading on Ruby 3.1 --- .github/workflows/lint.yml | 5 +- .github/workflows/tests.yml | 6 +- .projections.json | 10 --- .simplecov | 6 -- Gemfile | 4 +- appveyor.yml | 61 ------------------- lib/overcommit/cli.rb | 4 +- lib/overcommit/hook/pre_commit/php_lint.rb | 4 +- spec/overcommit/default_configuration_spec.rb | 6 +- .../hook/pre_commit/php_lint_spec.rb | 4 +- spec/overcommit/hook/pre_commit/phpcs_spec.rb | 8 +-- spec/overcommit/utils_spec.rb | 10 +-- spec/spec_helper.rb | 25 +++++--- template-dir/hooks/commit-msg | 9 ++- template-dir/hooks/overcommit-hook | 9 ++- template-dir/hooks/post-checkout | 9 ++- template-dir/hooks/post-commit | 9 ++- template-dir/hooks/post-merge | 9 ++- template-dir/hooks/post-rewrite | 9 ++- template-dir/hooks/pre-commit | 9 ++- template-dir/hooks/pre-push | 9 ++- template-dir/hooks/pre-rebase | 9 ++- template-dir/hooks/prepare-commit-msg | 9 ++- 23 files changed, 126 insertions(+), 117 deletions(-) delete mode 100644 .projections.json delete mode 100644 .simplecov delete mode 100644 appveyor.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 24baefc6..b1879f09 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -21,9 +21,10 @@ jobs: - name: Prepare environment run: | - git config --local user.email "gh-actions@example.com" - git config --local user.name "GitHub Actions" + git config --global user.email "gh-actions@example.com" + git config --global user.name "GitHub Actions" bundle exec overcommit --sign + bundle exec overcommit --sign pre-commit - name: Run pre-commit checks run: bundle exec overcommit --run diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 91ddb5b9..e57f5204 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -19,7 +19,6 @@ jobs: - '3.1' os: - ubuntu - - windows steps: - uses: actions/checkout@v2 @@ -31,7 +30,10 @@ jobs: bundler-cache: true - name: Run tests - run: bundle exec rspec + run: | + git config --global user.email "gh-actions@example.com" + git config --global user.name "GitHub Actions" + bundle exec rspec - name: Code coverage reporting uses: coverallsapp/github-action@master diff --git a/.projections.json b/.projections.json deleted file mode 100644 index 24f5ce34..00000000 --- a/.projections.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "lib/overcommit/*.rb": { - "alternate": "spec/overcommit/{}_spec.rb", - "type": "source" - }, - "spec/overcommit/*_spec.rb": { - "alternate": "lib/overcommit/{}.rb", - "type": "test" - } -} diff --git a/.simplecov b/.simplecov deleted file mode 100644 index 3cc40052..00000000 --- a/.simplecov +++ /dev/null @@ -1,6 +0,0 @@ -SimpleCov.start do - add_filter 'bin/' - add_filter 'libexec/' - add_filter 'spec/' - add_filter 'template-dir/' -end diff --git a/Gemfile b/Gemfile index d1cd3c37..ff22be0c 100644 --- a/Gemfile +++ b/Gemfile @@ -8,8 +8,8 @@ gemspec gem 'rspec', '~> 3.0' -# Generate coverage information in Travis builds -gem 'coveralls', '~> 0.8' +gem 'simplecov', '~> 0.21.0' +gem 'simplecov-lcov', '~> 0.8.0' # Pin RuboCop for Travis builds. gem 'rubocop', '0.82.0' diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index fcb90b95..00000000 --- a/appveyor.yml +++ /dev/null @@ -1,61 +0,0 @@ -version: "{build}" - -clone_depth: 1 - -environment: - matrix: - - RUBY_FOLDER_VERSION: "24-x64" - - RUBY_FOLDER_VERSION: "25-x64" - - RUBY_FOLDER_VERSION: "26-x64" -matrix: - fast_finish: true - -cache: - # Cache installed gems unless dependencies change - - vendor\bundle -> Gemfile,overcommit.gemspec - -install: - # Add ruby executables to PATH - - set PATH=C:\Ruby%RUBY_FOLDER_VERSION%\bin;%PATH% - - echo %PATH% - - # Print version and location for pre-installed grep - - grep --version - - where grep - - # Print version and location for pre-installed git - - git --version - - where git - - # Print version and location for pre-installed ruby - - ruby --version - - where ruby - - # Install latest version of RubyGems - - gem update --system --no-document - - gem --version - - where gem - - # Install latest Bundler to work around version issues - - gem install bundler:'< 2' --force - - where bundler - - # Install ruby dependencies - - bundle install --retry 3 --path vendor\bundle - -build: off - -before_test: - # Necessary for AuthorName and AuthorEmail pre-commit hooks to pass - - git config --global user.email "appveyor@appveyor.ci" - - git config --global user.name "Appveyor CI" - - # Ignore CRLF conversion warnings - - git config --global core.autocrlf true - - git config --global core.safecrlf false - -test_script: - - bundle exec rspec --tty --backtrace --color - - bundle exec overcommit --sign - - bundle exec overcommit --sign pre-commit - - bundle exec overcommit --run diff --git a/lib/overcommit/cli.rb b/lib/overcommit/cli.rb index d8b743a3..bc2ec994 100644 --- a/lib/overcommit/cli.rb +++ b/lib/overcommit/cli.rb @@ -94,7 +94,7 @@ def add_installation_options(opts) @options[:force] = true end - opts.on('-r [hook]', '--run [hook]', 'Run specified hook against all git tracked files. Defaults to `pre_commit`.') do |arg| + opts.on('-r [hook]', '--run [hook]', 'Run specified hook against all git tracked files. Defaults to `pre_commit`.') do |arg| # rubocop:disable Layout/LineLength @options[:action] = :run_all @options[:hook_to_run] = arg ? arg.to_s : 'run-all' end @@ -200,7 +200,7 @@ def sign def run_all empty_stdin = File.open(File::NULL) # pre-commit hooks don't take input - context = Overcommit::HookContext.create(@options[:hook_to_run], config, @arguments, empty_stdin) + context = Overcommit::HookContext.create(@options[:hook_to_run], config, @arguments, empty_stdin) # rubocop:disable Layout/LineLength config.apply_environment!(context, ENV) printer = Overcommit::Printer.new(config, log, context) diff --git a/lib/overcommit/hook/pre_commit/php_lint.rb b/lib/overcommit/hook/pre_commit/php_lint.rb index 3f70bfb2..f2ba2f24 100644 --- a/lib/overcommit/hook/pre_commit/php_lint.rb +++ b/lib/overcommit/hook/pre_commit/php_lint.rb @@ -4,9 +4,9 @@ module Overcommit::Hook::PreCommit # Runs `php -l` against any modified PHP files. class PhpLint < Base # Sample String - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength # PHP Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE) in site/sumo.php on line 12 - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength MESSAGE_REGEX = /^(?.+)\:\s+(?.+) in (?.+) on line (?\d+)/.freeze def run diff --git a/spec/overcommit/default_configuration_spec.rb b/spec/overcommit/default_configuration_spec.rb index a86b1fe3..706af872 100644 --- a/spec/overcommit/default_configuration_spec.rb +++ b/spec/overcommit/default_configuration_spec.rb @@ -4,7 +4,11 @@ describe 'default configuration' do default_config = - YAML.load_file(Overcommit::ConfigurationLoader::DEFAULT_CONFIG_PATH).to_hash + begin + YAML.load_file(Overcommit::ConfigurationLoader::DEFAULT_CONFIG_PATH, aliases: true).to_hash + rescue ArgumentError + YAML.load_file(Overcommit::ConfigurationLoader::DEFAULT_CONFIG_PATH).to_hash + end Overcommit::Utils.supported_hook_types.each do |hook_type| hook_class = Overcommit::Utils.camel_case(hook_type) diff --git a/spec/overcommit/hook/pre_commit/php_lint_spec.rb b/spec/overcommit/hook/pre_commit/php_lint_spec.rb index aff0b97e..ec755345 100644 --- a/spec/overcommit/hook/pre_commit/php_lint_spec.rb +++ b/spec/overcommit/hook/pre_commit/php_lint_spec.rb @@ -26,13 +26,13 @@ context 'when php lint exits unsuccessfully' do before do # php -l prints the same to both stdout and stderr - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength sample_output = [ '', "Parse error: syntax error, unexpected '0' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$' in sample.php on line 3 ", 'Errors parsing invalid.php', ].join("\n") - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength result = double('result') result.stub(:status).and_return(255) diff --git a/spec/overcommit/hook/pre_commit/phpcs_spec.rb b/spec/overcommit/hook/pre_commit/phpcs_spec.rb index 3df6cfb5..6982a297 100644 --- a/spec/overcommit/hook/pre_commit/phpcs_spec.rb +++ b/spec/overcommit/hook/pre_commit/phpcs_spec.rb @@ -39,12 +39,12 @@ context 'and it reports a warning' do before do - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength sample_output = [ 'File,Line,Column,Type,Message,Source,Severity,Fixable', '"/Users/craig/HelpScout/overcommit-testing/invalid.php",5,1,warning,"Possible parse error: FOREACH has no AS statement",Squiz.ControlStructures.ForEachLoopDeclaration.MissingAs,5,0' ].join("\n") - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength result.stub(:stdout).and_return(sample_output) end @@ -53,12 +53,12 @@ context 'and it reports an error' do before do - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength sample_output = [ 'File,Line,Column,Type,Message,Source,Severity,Fixable', '"/Users/craig/HelpScout/overcommit-testing/invalid.php",5,1,error,"Inline control structures are not allowed",Generic.ControlStructures.InlineControlStructure.NotAllowed,5,1' ].join("\n") - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength result.stub(:stdout).and_return(sample_output) end diff --git a/spec/overcommit/utils_spec.rb b/spec/overcommit/utils_spec.rb index 3be592ff..6bf75923 100644 --- a/spec/overcommit/utils_spec.rb +++ b/spec/overcommit/utils_spec.rb @@ -119,17 +119,17 @@ describe '.supported_hook_types' do subject { described_class.supported_hook_types } - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength it { should =~ %w[commit-msg pre-commit post-checkout post-commit post-merge post-rewrite pre-push pre-rebase prepare-commit-msg] } - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength end describe '.supported_hook_type_classes' do subject { described_class.supported_hook_type_classes } - # rubocop:disable Metrics/LineLength + # rubocop:disable Layout/LineLength it { should =~ %w[CommitMsg PreCommit PostCheckout PostCommit PostMerge PostRewrite PrePush PreRebase PrepareCommitMsg] } - # rubocop:enable Metrics/LineLength + # rubocop:enable Layout/LineLength end describe '.parent_command' do @@ -190,7 +190,7 @@ it 'invokes CommandSplitter.execute' do Overcommit::CommandSplitter. should_receive(:execute). - with(arguments, args: splittable_args). + with(arguments, { args: splittable_args }). and_return(double(status: 0, stdout: '', stderr: '')) subject end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fe6f82da..89d30fbe 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,13 +1,22 @@ # frozen_string_literal: true -if ENV['TRAVIS'] - # When running in Travis, report coverage stats to Coveralls. - require 'coveralls' - Coveralls.wear! -else - # Otherwise render coverage information in coverage/index.html and display - # coverage percentage in the console. - require 'simplecov' +require 'simplecov' +SimpleCov.start do + add_filter 'bin/' + add_filter 'libexec/' + add_filter 'spec/' + add_filter 'template-dir/' + + if ENV['CI'] + require 'simplecov-lcov' + + SimpleCov::Formatter::LcovFormatter.config do |c| + c.report_with_single_file = true + c.single_report_path = 'coverage/lcov.info' + end + + formatter SimpleCov::Formatter::LcovFormatter + end end require 'overcommit' diff --git a/template-dir/hooks/commit-msg b/template-dir/hooks/commit-msg index 197dc198..7f8023de 100755 --- a/template-dir/hooks/commit-msg +++ b/template-dir/hooks/commit-msg @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/overcommit-hook b/template-dir/hooks/overcommit-hook index 197dc198..7f8023de 100755 --- a/template-dir/hooks/overcommit-hook +++ b/template-dir/hooks/overcommit-hook @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/post-checkout b/template-dir/hooks/post-checkout index 197dc198..7f8023de 100755 --- a/template-dir/hooks/post-checkout +++ b/template-dir/hooks/post-checkout @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/post-commit b/template-dir/hooks/post-commit index 197dc198..7f8023de 100755 --- a/template-dir/hooks/post-commit +++ b/template-dir/hooks/post-commit @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/post-merge b/template-dir/hooks/post-merge index 197dc198..7f8023de 100755 --- a/template-dir/hooks/post-merge +++ b/template-dir/hooks/post-merge @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/post-rewrite b/template-dir/hooks/post-rewrite index 197dc198..7f8023de 100755 --- a/template-dir/hooks/post-rewrite +++ b/template-dir/hooks/post-rewrite @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/pre-commit b/template-dir/hooks/pre-commit index 197dc198..7f8023de 100755 --- a/template-dir/hooks/pre-commit +++ b/template-dir/hooks/pre-commit @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/pre-push b/template-dir/hooks/pre-push index 197dc198..7f8023de 100755 --- a/template-dir/hooks/pre-push +++ b/template-dir/hooks/pre-push @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/pre-rebase b/template-dir/hooks/pre-rebase index 197dc198..7f8023de 100755 --- a/template-dir/hooks/pre-rebase +++ b/template-dir/hooks/pre-rebase @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' diff --git a/template-dir/hooks/prepare-commit-msg b/template-dir/hooks/prepare-commit-msg index 197dc198..7f8023de 100755 --- a/template-dir/hooks/prepare-commit-msg +++ b/template-dir/hooks/prepare-commit-msg @@ -29,7 +29,14 @@ end # Check if Overcommit should invoke a Bundler context for loading gems require 'yaml' # rubocop:disable Style/RescueModifier -if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil +gemfile = + begin + YAML.load_file('.overcommit.yml', aliases: true)['gemfile'] + rescue ArgumentError + YAML.load_file('.overcommit.yml')['gemfile'] + end rescue nil + +if gemfile ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler'