diff --git a/.rubocop.yml b/.rubocop.yml index 8c779097..1b4e7ad7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,9 @@ inherit_from: .rubocop_todo.yml AllCops: - TargetRubyVersion: 2.4 + TargetRubyVersion: 2.6 + NewCops: disable + SuggestExtensions: false Layout/ClosingParenthesisIndentation: Enabled: false diff --git a/README.md b/README.md index afcbcbbf..769c1cf1 100644 --- a/README.md +++ b/README.md @@ -17,34 +17,43 @@ are stored in source control. You can also easily [add your existing hook scripts](#adding-existing-git-hooks) without writing any Ruby code. -* [Requirements](#requirements) - * [Dependencies](#dependencies) -* [Installation](#installation) - * [Automatically Install Overcommit Hooks](#automatically-install-overcommit-hooks) -* [Usage](#usage) -* [Continuous Integration](#continuous-integration) -* [Configuration](#configuration) - * [Hook Options](#hook-options) - * [Hook Categories](#hook-categories) - * [Gemfile](#gemfile) - * [Plugin Directory](#plugin-directory) - * [Signature Verification](#signature-verification) -* [Built-In Hooks](#built-in-hooks) - * [CommitMsg](#commitmsg) - * [PostCheckout](#postcheckout) - * [PostCommit](#postcommit) - * [PostMerge](#postmerge) - * [PostRewrite](#postrewrite) - * [PreCommit](#precommit) - * [PrePush](#prepush) - * [PreRebase](#prerebase) -* [Repo-Specific Hooks](#repo-specific-hooks) - * [Adding Existing Git Hooks](#adding-existing-git-hooks) -* [Security](#security) -* [Contributing](#contributing) -* [Community](#community) -* [Changelog](#changelog) -* [License](#license) +- [Requirements](#requirements) + - [Windows](#windows) + - [Dependencies](#dependencies) +- [Installation](#installation) + - [Automatically Install Overcommit Hooks](#automatically-install-overcommit-hooks) +- [Usage](#usage) + - [Skipping Hooks](#skipping-hooks) + - [Disabling Overcommit](#disabling-overcommit) + - [Disabling Colorized Output](#disabling-colorized-output) +- [Continuous Integration](#continuous-integration) +- [Configuration](#configuration) + - [Hook Options](#hook-options) + - [Hook Categories](#hook-categories) + - [The `ALL` Hook](#the-all-hook) + - [Gemfile](#gemfile) + - [Plugin Directory](#plugin-directory) + - [Quiet Hook Runs](#quiet-hook-runs) + - [Concurrency](#concurrency) + - [Signature Verification](#signature-verification) +- [Built-In Hooks](#built-in-hooks) + - [CommitMsg](#commitmsg) + - [PostCheckout](#postcheckout) + - [PostCommit](#postcommit) + - [PostMerge](#postmerge) + - [PostRewrite](#postrewrite) + - [PreCommit](#precommit) + - [WARNING: pre-commit hooks cannot have side effects](#warning-pre-commit-hooks-cannot-have-side-effects) + - [PrePush](#prepush) + - [PreRebase](#prerebase) +- [Repo-Specific hooks](#repo-specific-hooks) + - [Adding Existing Git Hooks](#adding-existing-git-hooks) +- [Security](#security) + - [Disabling Signature Checking](#disabling-signature-checking) +- [Contributing](#contributing) +- [Community](#community) +- [Changelog](#changelog) +- [License](#license) ## Requirements @@ -561,6 +570,7 @@ issue](https://github.com/sds/overcommit/issues/238) for more details. * [SemiStandard](lib/overcommit/hook/pre_commit/semi_standard.rb) * [ShellCheck](lib/overcommit/hook/pre_commit/shell_check.rb) * [SlimLint](lib/overcommit/hook/pre_commit/slim_lint.rb) +* [Sorbet](lib/overcommit/hook/pre_commit/sorbet.rb) * [Sqlint](lib/overcommit/hook/pre_commit/sqlint.rb) * [Standard](lib/overcommit/hook/pre_commit/standard.rb) * [Stylelint](lib/overcommit/hook/pre_commit/stylelint.rb) diff --git a/config/default.yml b/config/default.yml index 10a1dc7a..70ae0b2d 100644 --- a/config/default.yml +++ b/config/default.yml @@ -795,6 +795,14 @@ PreCommit: install_command: 'gem install slim_lint' include: '**/*.slim' + Sorbet: + enabled: false + description: 'Analyze with Sorbet' + required_executable: 'srb' + install_command: 'gem install sorbet' + command: ['srb', 'tc'] + include: '**/*.rb' + Sqlint: enabled: false description: 'Analyze with sqlint' diff --git a/lib/overcommit/cli.rb b/lib/overcommit/cli.rb index bc2ec994..f8127125 100644 --- a/lib/overcommit/cli.rb +++ b/lib/overcommit/cli.rb @@ -124,15 +124,13 @@ def install_or_uninstall end @options[:targets].each do |target| - begin - Installer.new(log).run(target, @options) - rescue Overcommit::Exceptions::InvalidGitRepo => e - log.warning "Invalid repo #{target}: #{e}" - halt 69 # EX_UNAVAILABLE - rescue Overcommit::Exceptions::PreExistingHooks => e - log.warning "Unable to install into #{target}: #{e}" - halt 73 # EX_CANTCREAT - end + Installer.new(log).run(target, @options) + rescue Overcommit::Exceptions::InvalidGitRepo => e + log.warning "Invalid repo #{target}: #{e}" + halt 69 # EX_UNAVAILABLE + rescue Overcommit::Exceptions::PreExistingHooks => e + log.warning "Unable to install into #{target}: #{e}" + halt 73 # EX_CANTCREAT end end diff --git a/lib/overcommit/hook/base.rb b/lib/overcommit/hook/base.rb index 37696d08..3c2eaeb3 100644 --- a/lib/overcommit/hook/base.rb +++ b/lib/overcommit/hook/base.rb @@ -228,14 +228,12 @@ def check_for_libraries output = [] required_libraries.each do |library| - begin - require library - rescue LoadError - install_command = @config['install_command'] - install_command = " -- install via #{install_command}" if install_command - - output << "Unable to load '#{library}'#{install_command}" - end + require library + rescue LoadError + install_command = @config['install_command'] + install_command = " -- install via #{install_command}" if install_command + + output << "Unable to load '#{library}'#{install_command}" end return if output.empty? diff --git a/lib/overcommit/hook/commit_msg/text_width.rb b/lib/overcommit/hook/commit_msg/text_width.rb index 63addbcf..52de3bd7 100644 --- a/lib/overcommit/hook/commit_msg/text_width.rb +++ b/lib/overcommit/hook/commit_msg/text_width.rb @@ -41,7 +41,7 @@ def find_errors_in_body(lines) max_body_width = config['max_body_width'] - lines[2..-1].each_with_index do |line, index| + lines[2..].each_with_index do |line, index| if line.chomp.size > max_body_width @errors << "Line #{index + 3} of commit message has > " \ "#{max_body_width} characters" diff --git a/lib/overcommit/hook/pre_commit/erb_lint.rb b/lib/overcommit/hook/pre_commit/erb_lint.rb index a903b10b..ae5af164 100644 --- a/lib/overcommit/hook/pre_commit/erb_lint.rb +++ b/lib/overcommit/hook/pre_commit/erb_lint.rb @@ -12,7 +12,7 @@ def run return :pass if result.success? extract_messages( - result.stdout.split("\n\n")[1..-1], + result.stdout.split("\n\n")[1..], MESSAGE_REGEX ) end diff --git a/lib/overcommit/hook/pre_commit/html_hint.rb b/lib/overcommit/hook/pre_commit/html_hint.rb index e8cb4a68..ddbe37d1 100644 --- a/lib/overcommit/hook/pre_commit/html_hint.rb +++ b/lib/overcommit/hook/pre_commit/html_hint.rb @@ -14,7 +14,7 @@ def run lines = group.split("\n").map(&:strip) file = lines[0][/(.+):/, 1] extract_messages( - lines[1..-1].map { |msg| "#{file}: #{msg}" }, + lines[1..].map { |msg| "#{file}: #{msg}" }, /^(?(?:\w:)?[^:]+): line (?\d+)/ ) end.flatten diff --git a/lib/overcommit/hook/pre_commit/json_syntax.rb b/lib/overcommit/hook/pre_commit/json_syntax.rb index 04972b91..bd162f7d 100644 --- a/lib/overcommit/hook/pre_commit/json_syntax.rb +++ b/lib/overcommit/hook/pre_commit/json_syntax.rb @@ -7,12 +7,10 @@ def run messages = [] applicable_files.each do |file| - begin - JSON.parse(IO.read(file)) - rescue JSON::ParserError => e - error = "#{e.message} parsing #{file}" - messages << Overcommit::Hook::Message.new(:error, file, nil, error) - end + JSON.parse(IO.read(file)) + rescue JSON::ParserError => e + error = "#{e.message} parsing #{file}" + messages << Overcommit::Hook::Message.new(:error, file, nil, error) end messages diff --git a/lib/overcommit/hook/pre_commit/sorbet.rb b/lib/overcommit/hook/pre_commit/sorbet.rb new file mode 100644 index 00000000..57988a6e --- /dev/null +++ b/lib/overcommit/hook/pre_commit/sorbet.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Overcommit::Hook::PreCommit + # Runs 'srb tc' against any modified files. + # + # @see https://github.com/sorbet/sorbet + class Sorbet < Base + # example of output: + # sorbet.rb:1: Method `foo` does not exist on `T.class_of(Bar)` https://srb.help/7003 + MESSAGE_REGEX = /^(?[^:]+):(?\d+): (?.*)$/.freeze + + def run + result = execute(command, args: applicable_files) + return :pass if result.success? + + output = result.stderr.split("\n").grep(MESSAGE_REGEX) + + extract_messages( + output, + MESSAGE_REGEX + ) + end + end +end diff --git a/lib/overcommit/hook/pre_commit/xml_syntax.rb b/lib/overcommit/hook/pre_commit/xml_syntax.rb index 7ac05360..99465182 100644 --- a/lib/overcommit/hook/pre_commit/xml_syntax.rb +++ b/lib/overcommit/hook/pre_commit/xml_syntax.rb @@ -7,12 +7,10 @@ def run messages = [] applicable_files.each do |file| - begin - REXML::Document.new(IO.read(file)) - rescue REXML::ParseException => e - error = "Error parsing #{file}: #{e.message}" - messages << Overcommit::Hook::Message.new(:error, file, nil, error) - end + REXML::Document.new(IO.read(file)) + rescue REXML::ParseException => e + error = "Error parsing #{file}: #{e.message}" + messages << Overcommit::Hook::Message.new(:error, file, nil, error) end messages diff --git a/lib/overcommit/hook/pre_commit/yaml_syntax.rb b/lib/overcommit/hook/pre_commit/yaml_syntax.rb index dd685450..83ff6789 100644 --- a/lib/overcommit/hook/pre_commit/yaml_syntax.rb +++ b/lib/overcommit/hook/pre_commit/yaml_syntax.rb @@ -7,17 +7,15 @@ def run messages = [] applicable_files.each do |file| + YAML.load_file(file, aliases: true) + rescue ArgumentError begin - YAML.load_file(file, aliases: true) - rescue ArgumentError - begin - YAML.load_file(file) - rescue ArgumentError, Psych::SyntaxError => e - messages << Overcommit::Hook::Message.new(:error, file, nil, e.message) - end - rescue Psych::DisallowedClass => e - messages << error_message(file, e) + YAML.load_file(file) + rescue ArgumentError, Psych::SyntaxError => e + messages << Overcommit::Hook::Message.new(:error, file, nil, e.message) end + rescue Psych::DisallowedClass => e + messages << error_message(file, e) end messages diff --git a/lib/overcommit/utils/messages_utils.rb b/lib/overcommit/utils/messages_utils.rb index c92a6010..31c0f8db 100644 --- a/lib/overcommit/utils/messages_utils.rb +++ b/lib/overcommit/utils/messages_utils.rb @@ -26,7 +26,7 @@ def extract_messages(output_messages, regex, type_categorizer = nil) raise Overcommit::Exceptions::MessageProcessingError, 'Unexpected output: unable to determine line number or type ' \ "of error/warning for output:\n" \ - "#{output_messages[index..-1].join("\n")}" + "#{output_messages[index..].join("\n")}" end file = extract_file(match, message) diff --git a/overcommit.gemspec b/overcommit.gemspec index 8bc70845..268a9fc1 100644 --- a/overcommit.gemspec +++ b/overcommit.gemspec @@ -25,7 +25,7 @@ Gem::Specification.new do |s| Dir['libexec/**/*'] + Dir['template-dir/**/*'] - s.required_ruby_version = '>= 2.4' + s.required_ruby_version = '>= 2.6' s.add_dependency 'childprocess', '>= 0.6.3', '< 6' s.add_dependency 'iniparse', '~> 1.4' diff --git a/spec/overcommit/hook/pre_commit/sorbet_spec.rb b/spec/overcommit/hook/pre_commit/sorbet_spec.rb new file mode 100644 index 00000000..60f811cb --- /dev/null +++ b/spec/overcommit/hook/pre_commit/sorbet_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Overcommit::Hook::PreCommit::Sorbet do + let(:config) { Overcommit::ConfigurationLoader.default_configuration } + let(:context) { double('context') } + subject { described_class.new(config, context) } + + before do + subject.stub(:applicable_files).and_return(%w[file1.rb file2.rb]) + end + + context 'when Sorbet exits successfully' do + let(:result) { double('result') } + + before do + result.stub(:success?).and_return(true) + subject.stub(:execute).and_return(result) + end + + it { should pass } + + context 'and it printed a message to stderr' do + before do + result.stub(:stderr).and_return("No errors! Great job.\n") + end + + it { should pass } + end + end + + context 'when Sorbet exits unsucessfully' do + let(:result) { double('result') } + + before do + result.stub(:success?).and_return(false) + subject.stub(:execute).and_return(result) + end + + context 'and it reports an error' do + before do + result.stub(:stderr).and_return(normalize_indent(<<-MSG)) + sorbet.rb:1: Method `foo` does not exist on `T.class_of(Bar)` https://srb.help/7003 + 5 | foo 'bar' + ^^^ + Errors: 1 + MSG + end + + it { should fail_hook } + end + end +end