-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Introduction This pre-commit hook runs the Sorbet type-checker. [More information about Sorbet here](https://sorbet.org/docs/overview) Fixes #825 ## Requirements [Sorbet](https://github.com/sorbet/sorbet) Notes: not sure if this should be a pre-commit hook or pre-push hook to be honest. Co-authored-by: Shane da Silva <shane@dasilva.io>
- Loading branch information
Showing
4 changed files
with
124 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = /^(?<file>[^:]+):(?<line>\d+): (?<message>.*)$/.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |