Skip to content
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

Check commit rake task #7984

Merged
merged 2 commits into from May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
7 changes: 4 additions & 3 deletions tasks/spec_runner.rake
Expand Up @@ -11,7 +11,10 @@ module RuboCop
# The specs will be run in parallel if the system implements `fork`.
# If ENV['COVERAGE'] is truthy, code coverage will be measured.
class SpecRunner
def initialize(external_encoding: 'UTF-8', internal_encoding: nil)
attr_reader :rspec_args

def initialize(rspec_args = %w[spec], external_encoding: 'UTF-8', internal_encoding: nil)
@rspec_args = rspec_args
@previous_external_encoding = Encoding.default_external
@previous_internal_encoding = Encoding.default_internal

Expand All @@ -20,8 +23,6 @@ module RuboCop
end

def run_specs
rspec_args = %w[spec]

n_failures = with_encoding do
if Process.respond_to?(:fork)
parallel_runner_klass.new(rspec_args).execute
Expand Down