rubocop-changes 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37daad64414834e07edb588962bdf3916e67fc450a2e3746ab5e45272f5fb72e
4
- data.tar.gz: 5737ea89aa55ce98e89019537e4f54774729b1a19292f40fa2755591721f2763
3
+ metadata.gz: 4c897309c9346d0df738a04adecd0c9f340b26170b5a64884604dec19510afe2
4
+ data.tar.gz: 1e1c3ceeba53c55debf3b4eda2929a4f95b4a013ffd310b63e3c32acb6492da5
5
5
  SHA512:
6
- metadata.gz: 96a3aa06fd3bcc04f9556b73a88adc391c1ef4509070aa5386b73442a7b5c1092a02cb904f3f1d34cd679cb90e77d42632181c567ddcd2d38f33e451ad28694a
7
- data.tar.gz: f9511dabaa54358cfb3760ca31dbdb4f351d6e1c86d4a0cc3ad5385569a10a2ebda81c801b7d8536ebb67ee80e5e654a12a1be477b94df5280141872d275f702
6
+ metadata.gz: 62707c10b740a9ba47bc2b816790178bd09c1be20d6db3a402dafb49b1b49ec65de247adf52aa46c2dd51935ec95854e818f4bbfcf078ab5f62d3af0d6d97a9e
7
+ data.tar.gz: edf2057141d1e7425ca57c728e3e8258de031b6a64dcd401de5627a07921bd0688ce505861b4ffdf47c5406165dd73af7c5549683848e95c81a98bc14df367b4
@@ -1,2 +1,5 @@
1
1
  Style/Documentation:
2
2
  Enabled: false
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-changes (0.1.2)
4
+ rubocop-changes (0.2.0)
5
5
  git_diff_parser (~> 3.2)
6
6
  rubocop (~> 0.59)
7
7
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Rubocop::Changes
2
2
 
3
- [![Gem Version](http://img.shields.io/gem/v/rubocop-changes.svg?style=flat)](http://badge.fury.io/rb/rubocop-changes)
4
- [![Build Status](http://img.shields.io/travis/fcsonline/rubocop-changes/master.svg?style=flat)](https://travis-ci.org/fcsonline/rubocop-changes)
3
+ [![Gem Version](https://img.shields.io/gem/v/rubocop-changes)](https://rubygems.org/gems/rubocop-changes)
4
+ [![Build Status](https://img.shields.io/travis/com/fcsonline/rubocop-changes/master)](https://travis-ci.com/fcsonline/rubocop-changes)
5
5
 
6
6
  `rubocop-changes` runs rubocop and shows only the offenses you introduced since
7
7
  the fork point of your git branch. Will not complain about existing offenses in
@@ -34,21 +34,25 @@ Or install it yourself as:
34
34
 
35
35
  $ bundle exec rubocop-changes
36
36
 
37
- ## Alternatives
37
+ ## Other gems
38
38
 
39
- There are similar projects out there, but all of them show all Rubocop offenses for all changes files:
39
+ There are similar projects out there, like
40
+ [rubocop-git](https://github.com/m4i/rubocop-git),
41
+ [diffcop](https://github.com/yohira0616/diffcop),
42
+ [nexocop](https://github.com/SimpleNexus/nexocop), but not all of them offer
43
+ differences at line level. Only
44
+ [rubocop-git](https://github.com/m4i/rubocop-git) offer this nice feature but
45
+ you have to craft the commit id to get the proper fork point of your pull
46
+ request.
40
47
 
41
- - https://github.com/m4i/rubocop-git
42
- - https://github.com/packsaddle/rubocop-select
43
- - https://github.com/mcgain/rubocop-diff
44
- - https://github.com/yohira0616/diffcop
45
- - https://github.com/SimpleNexus/nexocop
48
+ rubocop-changes does this diff out of the box without specify any commit id. If
49
+ you want to get the offense comparing from one specific commit, you can pass
50
+ the argument `commit` to the command.
46
51
 
47
52
  ## Ideas
48
53
 
49
54
  Those are some ideas to improve `rubocop-changes`:
50
55
 
51
- - [ ] Let users specify which formatter to use for the output
52
56
  - [ ] Let users specify the rubocop config file
53
57
 
54
58
  ## Development
@@ -1,9 +1,17 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'rubocop'
5
+
4
6
  require 'rubocop/changes/checker'
7
+ require 'rubocop/changes/options'
8
+
9
+ args = Rubocop::Changes::Options.new.parse!
5
10
 
6
- checker = Rubocop::Changes::Checker.new
7
- offenses = checker.run
11
+ offenses = Rubocop::Changes::Checker.new(
12
+ format: args.format,
13
+ quiet: args.quiet,
14
+ commit: args.commit
15
+ ).run
8
16
 
9
17
  exit offenses.count.positive? ? 1 : 0
@@ -9,28 +9,37 @@ require 'rubocop/changes/shell'
9
9
 
10
10
  module Rubocop
11
11
  module Changes
12
+ class UnknownFormat < StandardError; end
12
13
  class UnknownForkPointError < StandardError; end
13
14
 
14
15
  class Checker
16
+ def initialize(format:, quiet:, commit:)
17
+ @format = format
18
+ @quiet = quiet
19
+ @commit = commit
20
+ end
21
+
15
22
  def run
16
23
  raise UnknownForkPointError if fork_point.empty?
24
+ raise UnknownFormat if formatter_klass.nil?
17
25
 
18
- # TODO: Let users choose the formatter
19
- formatter = RuboCop::Formatter::SimpleTextFormatter.new($stdout)
20
-
21
- formatter.started('')
22
-
23
- print_offenses(formatter)
24
-
25
- formatter.finished(ruby_changed_files)
26
+ print_offenses! unless quiet
26
27
 
27
28
  checks.map(&:offenses).flatten
28
29
  end
29
30
 
30
31
  private
31
32
 
33
+ attr_reader :format, :quiet, :commit
34
+
32
35
  def fork_point
33
- @fork_point ||= Shell.run('git merge-base HEAD origin/master')
36
+ @fork_point ||= Shell.run(command)
37
+ end
38
+
39
+ def command
40
+ return 'git merge-base HEAD origin/master' unless commit
41
+
42
+ "git log -n 1 --pretty=format:\"%h\" #{commit}"
34
43
  end
35
44
 
36
45
  def diff
@@ -76,13 +85,35 @@ module Rubocop
76
85
  checks.map { |check| check.offended_lines.size }.inject(0, :+)
77
86
  end
78
87
 
79
- def print_offenses(formatter)
88
+ def print_offenses!
89
+ formatter.started(checks)
90
+
80
91
  checks.each do |check|
81
- print_offenses_for_check(formatter, check)
92
+ print_offenses_for_check(check)
82
93
  end
94
+
95
+ formatter.finished(ruby_changed_files)
96
+ end
97
+
98
+ def formatter
99
+ @formatter ||= formatter_klass.new($stdout)
100
+ end
101
+
102
+ def formatter_klass
103
+ @formatter_klass ||= formatters[format]
104
+ end
105
+
106
+ def formatters
107
+ rubocop_formatters.map do |key, value|
108
+ [key.gsub(/[\[\]]/, '').to_sym, value]
109
+ end.to_h
110
+ end
111
+
112
+ def rubocop_formatters
113
+ RuboCop::Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS
83
114
  end
84
115
 
85
- def print_offenses_for_check(formatter, check)
116
+ def print_offenses_for_check(check)
86
117
  offenses = check.offenses.map do |offense|
87
118
  RuboCop::Cop::Offense.new(
88
119
  offense.severity,
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+
5
+ module Rubocop
6
+ module Changes
7
+ class Options
8
+ Options = Struct.new(:format, :quiet, :commit)
9
+
10
+ def initialize
11
+ @args = Options.new(:simple, false, nil) # Defaults
12
+ end
13
+
14
+ def parse!
15
+ OptionParser.new do |opts|
16
+ opts.banner = 'Usage: rubocop-changes [options]'
17
+
18
+ parse_formatter!(opts)
19
+ parse_commit!(opts)
20
+ parse_quiet!(opts)
21
+ parse_help!(opts)
22
+ parse_version!(opts)
23
+ end.parse!
24
+
25
+ args
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :args
31
+
32
+ def formatter_values
33
+ formatters = RuboCop::Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS
34
+
35
+ formatters.keys.map do |key|
36
+ key.gsub(/[\[\]]/, '').to_sym
37
+ end
38
+ end
39
+
40
+ def parse_formatter!(opts)
41
+ opts.on(
42
+ '-f',
43
+ '--formatter [FORMATER]',
44
+ formatter_values,
45
+ "Select format type (#{formatter_values.join(', ')})"
46
+ ) do |t|
47
+ args.format = t
48
+ end
49
+ end
50
+
51
+ def parse_commit!(opts)
52
+ opts.on(
53
+ '-c',
54
+ '--commit [COMMIT_ID]',
55
+ 'Compare from some specific point on git history'
56
+ ) do |c|
57
+ args.commit = c
58
+ end
59
+ end
60
+
61
+ def parse_quiet!(opts)
62
+ opts.on('-q', '--quiet', 'Be quiet') do |v|
63
+ args.quiet = v
64
+ end
65
+ end
66
+
67
+ def parse_help!(opts)
68
+ opts.on('-h', '--help', 'Prints this help') do
69
+ puts opts
70
+ exit
71
+ end
72
+ end
73
+
74
+ def parse_version!(opts)
75
+ opts.on('--version', 'Display version') do
76
+ puts Rubocop::Changes::VERSION
77
+ exit 0
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rubocop
4
4
  module Changes
5
- VERSION = '0.1.2'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -10,10 +10,10 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Ferran Basora']
11
11
  spec.email = ['fcsonline@gmail.com']
12
12
 
13
- spec.summary = "Rubocop on changed lines from forked point"
13
+ spec.summary = 'Rubocop on changed lines from git fork point'
14
14
  spec.description = <<-DESCRIPTION
15
15
  rubocop-changes will run rubocop on changed lines from forked point in your main branch.
16
- It will not complain about existing offenses in master branch.
16
+ It will not complain about existing offenses in master branch on your git prioject.
17
17
  This gem is perfect as a Continuous Integration tool
18
18
  DESCRIPTION
19
19
 
@@ -26,8 +26,9 @@ Gem::Specification.new do |spec|
26
26
  }
27
27
 
28
28
  # Specify which files should be added to the gem when it is released.
29
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
30
+ # into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
32
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
33
  end
33
34
  spec.bindir = 'exe'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-changes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ferran Basora
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-29 00:00:00.000000000 Z
11
+ date: 2019-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git_diff_parser
@@ -96,7 +96,7 @@ dependencies:
96
96
  version: '3.0'
97
97
  description: |2
98
98
  rubocop-changes will run rubocop on changed lines from forked point in your main branch.
99
- It will not complain about existing offenses in master branch.
99
+ It will not complain about existing offenses in master branch on your git prioject.
100
100
  This gem is perfect as a Continuous Integration tool
101
101
  email:
102
102
  - fcsonline@gmail.com
@@ -123,6 +123,7 @@ files:
123
123
  - lib/rubocop/changes.rb
124
124
  - lib/rubocop/changes/check.rb
125
125
  - lib/rubocop/changes/checker.rb
126
+ - lib/rubocop/changes/options.rb
126
127
  - lib/rubocop/changes/shell.rb
127
128
  - lib/rubocop/changes/version.rb
128
129
  - rubocop-changes.gemspec
@@ -151,5 +152,5 @@ rubyforge_project:
151
152
  rubygems_version: 2.7.6
152
153
  signing_key:
153
154
  specification_version: 4
154
- summary: Rubocop on changed lines from forked point
155
+ summary: Rubocop on changed lines from git fork point
155
156
  test_files: []