Skip to content

Commit

Permalink
Add rake check_commit and check_commit:auto_correct
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed May 21, 2020
1 parent b883f1a commit 8cee1c7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* [#7735](https://github.com/rubocop-hq/rubocop/issues/7735): `NodePattern` and `AST` classes have been moved to the [`rubocop-ast` gem](https://github.com/rubocop-hq/rubocop-ast). ([@marcandre][])
* [#7950](https://github.com/rubocop-hq/rubocop/pull/7950): Add new `Lint/DeprecatedOpenSSLConstant` cop. ([@bdewater][])
* [#7976](https://github.com/rubocop-hq/rubocop/issues/7976): Add `AllowAliasSyntax` and `AllowedMethods` options for `Layout/EmptyLinesAroundAttributeAccessor`. ([@koic][])
* [#7984](https://github.com/rubocop-hq/rubocop/pull/7984): New `rake` task "check_commit" will run `rspec` and `rubocop` on files touched by the last commit. ([@marcandre][])

### Bug fixes

Expand Down
4 changes: 4 additions & 0 deletions manual/contributing.md
Expand Up @@ -17,6 +17,10 @@ Patches in any form are always welcome! GitHub pull requests are even better! :-
Before submitting a patch or a pull request make sure all tests are
passing and that your patch is in line with the [contribution
guidelines](https://github.com/rubocop-hq/rubocop/blob/master/CONTRIBUTING.md).

A handy way to test only the files that you have modified in the last commit
(with `rspec` and `rubocop`) is to use `rake check_commit`.

Also see the [Development section](development.md).

## Documentation
Expand Down
20 changes: 20 additions & 0 deletions tasks/check_commit.rake
@@ -0,0 +1,20 @@
# frozen_string_literal: true

def commit_paths(commit_range)
commit_range = "#{commit_range}~..HEAD" if commit_range.include?('..')
`git diff-tree --no-commit-id --name-only -r #{commit_range}`.split("\n")
ensure
exit($CHILD_STATUS.exitstatus) if $CHILD_STATUS.exitstatus != 0
end

desc 'Check files modified in commit (default: HEAD) with rspec and rubocop'
RuboCop::RakeTask.new(:check_commit, :commit) do |t, args|
commit = args[:commit] || 'HEAD'
paths = commit_paths(commit)
specs = paths.select { |p| p.start_with?('spec') }

puts "Checking: #{paths.join(' ')}"
RuboCop::SpecRunner.new(specs).run_specs

t.patterns = paths
end

0 comments on commit 8cee1c7

Please sign in to comment.