-
Notifications
You must be signed in to change notification settings - Fork 280
750: Teach ad-hoc hooks line parsing #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guillaume-d
wants to merge
5
commits into
sds:main
Choose a base branch
from
guillaume-d:guillaume-d/bug/750-teach-ad-hoc-hooks-line-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e9c87f
Add some tests for ad-hoc (pre-commit) Git hooks
guillaume-d a359629
Add "ad-hoc" line-aware command hooks
guillaume-d ad88bf7
Flatten YAML configuration as per review comments
guillaume-d 9f1c8ba
Link to feature more + avoid "ad-hoc" term in docs
guillaume-d 3dc1d14
Remove magic default value as per review comment
guillaume-d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,199 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'ad-hoc pre-commit hook' do | ||
subject do | ||
hook_loader = Overcommit::HookLoader::PluginHookLoader.new( | ||
config, | ||
context, | ||
Overcommit::Logger.new(STDOUT) | ||
) | ||
hooks = hook_loader.load_hooks | ||
hooks.find { |h| h.name == hook_name } | ||
end | ||
let(:config) do | ||
config = Overcommit::Configuration.new( | ||
YAML.safe_load(config_contents, [Regexp]), { | ||
validate: false | ||
} | ||
) | ||
Overcommit::ConfigurationLoader.default_configuration.merge(config) | ||
end | ||
let(:context) do | ||
empty_stdin = File.open(File::NULL) # pre-commit hooks don't take input | ||
context = Overcommit::HookContext.create('pre-commit', config, applicable_files, empty_stdin) | ||
context | ||
end | ||
|
||
around do |example| | ||
repo do | ||
example.run | ||
end | ||
end | ||
|
||
describe 'if not line-aware' do | ||
let(:config_contents) do | ||
<<-'YML' | ||
PreCommit: | ||
FooGitHook: | ||
enabled: true | ||
command: "foocmd" | ||
YML | ||
end | ||
let(:hook_name) { 'FooGitHook' } | ||
let(:applicable_files) { nil } | ||
|
||
before do | ||
context.stub(:execute_hook).with(%w[foocmd]). | ||
and_return(result) | ||
end | ||
|
||
context 'when command succeeds' do | ||
let(:result) do | ||
double(success?: true, stdout: '') | ||
end | ||
|
||
it { should pass } | ||
end | ||
|
||
context 'when command fails' do | ||
let(:result) do | ||
double(success?: false, stdout: '', stderr: '') | ||
end | ||
|
||
it { should fail_hook } | ||
end | ||
end | ||
|
||
describe 'if line-aware' do | ||
let(:config_contents) do | ||
<<-'YML' | ||
PreCommit: | ||
FooLint: | ||
enabled: true | ||
command: ["foo", "lint"] | ||
ad_hoc: | ||
message_pattern: !ruby/regexp /^(?<file>[^:]+):(?<line>[0-9]+):(?<type>[^ ]+)/ | ||
warning_message_type_pattern: warning | ||
flags: | ||
- "--format=emacs" | ||
include: '**/*.foo' | ||
FooLintDefault: | ||
enabled: true | ||
command: ["foo", "lint"] | ||
ad_hoc: | ||
warning_message_type_pattern: warning | ||
flags: | ||
- "--format=emacs" | ||
include: '**/*.foo' | ||
FooLintDefaultNoWarnings: | ||
enabled: true | ||
command: ["foo", "lint"] | ||
ad_hoc: | ||
flags: | ||
- "--format=emacs" | ||
include: '**/*.foo' | ||
YML | ||
end | ||
let(:hook_name) { 'FooLint' } | ||
let(:applicable_files) { %w[file.foo] } | ||
|
||
before do | ||
subject.stub(:applicable_files).and_return(applicable_files) | ||
subject.stub(:execute).with(%w[foo lint --format=emacs], args: applicable_files). | ||
and_return(result) | ||
end | ||
|
||
context 'when command succeeds' do | ||
let(:result) do | ||
double(success?: true, stdout: '') | ||
end | ||
|
||
it { should pass } | ||
end | ||
|
||
context 'when command fails with empty stdout' do | ||
let(:result) do | ||
double(success?: false, stdout: '', stderr: '') | ||
end | ||
|
||
it { should pass } | ||
end | ||
|
||
context 'when command fails with some warning message' do | ||
let(:result) do | ||
double( | ||
success?: false, | ||
stdout: "A:1:warning...\n", | ||
stderr: '' | ||
) | ||
end | ||
|
||
it { should warn } | ||
end | ||
|
||
context 'when command fails with some error message' do | ||
let(:result) do | ||
double( | ||
success?: false, | ||
stdout: "A:1:???\n", | ||
stderr: '' | ||
) | ||
end | ||
|
||
it { should fail_hook } | ||
end | ||
|
||
describe '(using default pattern)' do | ||
let(:hook_name) { 'FooLintDefault' } | ||
|
||
context 'when command fails with some warning message' do | ||
let(:result) do | ||
double( | ||
success?: false, | ||
stdout: <<-MSG, | ||
B:1: warning: ??? | ||
MSG | ||
stderr: '' | ||
) | ||
end | ||
|
||
it { should warn } | ||
end | ||
|
||
context 'when command fails with some error message' do | ||
let(:result) do | ||
double( | ||
success?: false, | ||
stdout: <<-MSG, | ||
A:1:80: error | ||
MSG | ||
stderr: '' | ||
) | ||
end | ||
|
||
it { should fail_hook } | ||
end | ||
end | ||
|
||
describe '(using defaults)' do | ||
let(:hook_name) { 'FooLintDefaultNoWarnings' } | ||
|
||
context 'when command fails with some messages' do | ||
let(:result) do | ||
double( | ||
success?: false, | ||
stdout: <<-MSG, | ||
A:1:80: error | ||
B:1: warning: ??? | ||
MSG | ||
stderr: '' | ||
) | ||
end | ||
|
||
it { should fail_hook } | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.