cs-rubocop-git 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.travis.yml +32 -0
  4. data/Appraisals +31 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE.txt +26 -0
  7. data/README.md +46 -0
  8. data/Rakefile +15 -0
  9. data/bin/rubocop-git +5 -0
  10. data/gemfiles/0.24.gemfile +7 -0
  11. data/gemfiles/0.25.gemfile +7 -0
  12. data/gemfiles/0.26.gemfile +7 -0
  13. data/gemfiles/0.27.gemfile +7 -0
  14. data/gemfiles/0.28.gemfile +7 -0
  15. data/gemfiles/0.29.gemfile +7 -0
  16. data/gemfiles/0.30.gemfile +7 -0
  17. data/gemfiles/0.31.gemfile +7 -0
  18. data/gemfiles/0.32.gemfile +7 -0
  19. data/gemfiles/0.33.gemfile +7 -0
  20. data/gemfiles/0.34.gemfile +7 -0
  21. data/gemfiles/0.35.gemfile +7 -0
  22. data/gemfiles/0.36.gemfile +7 -0
  23. data/gemfiles/0.37.gemfile +7 -0
  24. data/gemfiles/0.38.gemfile +7 -0
  25. data/gemfiles/0.39.gemfile +7 -0
  26. data/gemfiles/0.40.gemfile +7 -0
  27. data/gemfiles/0.41.gemfile +7 -0
  28. data/gemfiles/0.42.gemfile +7 -0
  29. data/gemfiles/0.43.gemfile +7 -0
  30. data/gemfiles/0.44.gemfile +7 -0
  31. data/gemfiles/0.45.gemfile +7 -0
  32. data/gemfiles/0.46.gemfile +7 -0
  33. data/gemfiles/0.47.gemfile +7 -0
  34. data/gemfiles/0.48.gemfile +7 -0
  35. data/hound.yml +254 -0
  36. data/lib/rubocop/git.rb +19 -0
  37. data/lib/rubocop/git/cli.rb +67 -0
  38. data/lib/rubocop/git/commit.rb +22 -0
  39. data/lib/rubocop/git/commit_file.rb +55 -0
  40. data/lib/rubocop/git/diff_parser.rb +31 -0
  41. data/lib/rubocop/git/file_violation.rb +5 -0
  42. data/lib/rubocop/git/line.rb +8 -0
  43. data/lib/rubocop/git/options.rb +84 -0
  44. data/lib/rubocop/git/patch.rb +36 -0
  45. data/lib/rubocop/git/pseudo_pull_request.rb +36 -0
  46. data/lib/rubocop/git/pseudo_resource.rb +24 -0
  47. data/lib/rubocop/git/runner.rb +70 -0
  48. data/lib/rubocop/git/style_checker.rb +43 -0
  49. data/lib/rubocop/git/style_guide.rb +98 -0
  50. data/lib/rubocop/git/version.rb +5 -0
  51. data/rubocop-git.gemspec +27 -0
  52. data/test/rubocop/git/cli_test.rb +12 -0
  53. data/test/rubocop/git/options_test.rb +18 -0
  54. data/test/rubocop/git/runner_test.rb +23 -0
  55. data/test/test_helper.rb +2 -0
  56. metadata +172 -0
@@ -0,0 +1,43 @@
1
+ module RuboCop::Git
2
+ # ref. https://github.com/thoughtbot/hound/blob/d2f3933/app/models/style_checker.rb
3
+ class StyleChecker
4
+ def initialize(modified_files,
5
+ rubocop_options,
6
+ config_file,
7
+ custom_config = nil)
8
+ @modified_files = modified_files
9
+ @rubocop_options = rubocop_options
10
+ @config_file = config_file
11
+ @custom_config = custom_config
12
+ end
13
+
14
+ def violations
15
+ file_violations = @modified_files.map do |modified_file|
16
+ FileViolation.new(modified_file.absolute_path, offenses(modified_file))
17
+ end
18
+
19
+ file_violations.select do |file_violation|
20
+ file_violation.offenses.any?
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def offenses(modified_file)
27
+ violations = style_guide.violations(modified_file)
28
+ violations_on_changed_lines(modified_file, violations)
29
+ end
30
+
31
+ def violations_on_changed_lines(modified_file, violations)
32
+ violations.select do |violation|
33
+ modified_file.relevant_line?(violation.line)
34
+ end
35
+ end
36
+
37
+ def style_guide
38
+ @style_guide ||= StyleGuide.new(@rubocop_options,
39
+ @config_file,
40
+ @custom_config)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,98 @@
1
+ module RuboCop::Git
2
+ # ref. https://github.com/thoughtbot/hound/blob/d2f3933/app/models/style_guide.rb
3
+ class StyleGuide
4
+ def initialize(rubocop_options, config_file, override_config_content = nil)
5
+ @rubocop_options = rubocop_options
6
+ @config_file = config_file
7
+ @override_config_content = override_config_content
8
+ end
9
+
10
+ def violations(file)
11
+ if ignored_file?(file)
12
+ []
13
+ else
14
+ parsed_source = parse_source(file)
15
+ team = RuboCop::Cop::Team.new(all_cops, config, rubocop_options)
16
+ team.inspect_file(parsed_source)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ if Gem::Version.new(RuboCop::Version::STRING) >= Gem::Version.new('0.47.0')
23
+ def all_cops; RuboCop::Cop::Registry.new RuboCop::Cop::Cop.all; end
24
+ else
25
+ def all_cops; RuboCop::Cop::Cop.all; end
26
+ end
27
+
28
+ def ignored_file?(file)
29
+ !file.ruby? || file.removed? || excluded_file?(file)
30
+ end
31
+
32
+ def excluded_file?(file)
33
+ config.file_to_exclude?(file.absolute_path)
34
+ end
35
+
36
+ def parse_source(file)
37
+ rubocop_version = Gem::Version.new(RuboCop::Version::STRING)
38
+ if rubocop_version < Gem::Version.new('0.36.0')
39
+ RuboCop::ProcessedSource.new(file.content, file.absolute_path)
40
+ elsif rubocop_version < Gem::Version.new('0.41.0')
41
+ RuboCop::ProcessedSource.new(file.content,
42
+ target_ruby_version, file.absolute_path)
43
+ else
44
+ RuboCop::ProcessedSource.new(file.content,
45
+ config.target_ruby_version, file.absolute_path)
46
+ end
47
+ end
48
+
49
+ def config
50
+ if @config.nil?
51
+ config = RuboCop::ConfigLoader.configuration_from_file(@config_file)
52
+ combined_config = RuboCop::ConfigLoader.merge(config, override_config)
53
+ @config = RuboCop::Config.new(combined_config, "")
54
+ end
55
+
56
+ @config
57
+ end
58
+
59
+ def rubocop_options
60
+ if config["ShowCopNames"]
61
+ { debug: true }
62
+ else
63
+ {}
64
+ end.merge(@rubocop_options)
65
+ end
66
+
67
+ def override_config
68
+ if @override_config_content
69
+ config_content = YAML.load(@override_config_content)
70
+ override_config = RuboCop::Config.new(config_content, "")
71
+ override_config.add_missing_namespaces
72
+ override_config.make_excludes_absolute
73
+ override_config
74
+ else
75
+ {}
76
+ end
77
+ end
78
+
79
+ # TODO: DELETE ME when we drop support for 0.x releases of rubocop
80
+ #
81
+ # This method exists in RuboCop::Config now (or config in this class) so we
82
+ # should make use of that.
83
+ def target_ruby_version
84
+ @target ||= begin
85
+ target = config['AllCops'] && config['AllCops']['TargetRubyVersion']
86
+
87
+ if !target || !RuboCop::Config::KNOWN_RUBIES.include?(target)
88
+ fail ValidationError, "Unknown Ruby version #{target.inspect} found " \
89
+ 'in `TargetRubyVersion` parameter (in ' \
90
+ "#{loaded_path}).\nKnown versions: " \
91
+ "#{RuboCop::Config::KNOWN_RUBIES.join(', ')}"
92
+ end
93
+
94
+ target
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,5 @@
1
+ module RuboCop
2
+ module Git
3
+ VERSION = '0.1.4'
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubocop/git/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cs-rubocop-git'
8
+ spec.version = RuboCop::Git::VERSION
9
+ spec.authors = ['Masaki Takeuchi']
10
+ spec.email = ['developers@combostrike.com']
11
+ spec.summary = %q{RuboCop for git diff.}
12
+ spec.description = %q{RuboCop for git diff.}
13
+ spec.homepage = 'https://github.com/ComboStrikeHQ/rubocop-git'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'minitest'
24
+ spec.add_development_dependency 'appraisal'
25
+
26
+ spec.add_dependency 'rubocop', '>= 0.24.1'
27
+ end
@@ -0,0 +1,12 @@
1
+ require_relative '../../test_helper'
2
+ require 'rubocop/git/cli'
3
+
4
+ describe RuboCop::Git::CLI do
5
+ it 'fail with invalid options' do
6
+ proc do
7
+ _out, _err = capture_io do
8
+ RuboCop::Git::CLI.new.run(['--gruß'])
9
+ end
10
+ end.must_raise(SystemExit)
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ require_relative '../../test_helper'
2
+ require 'rubocop/git/options'
3
+
4
+ describe RuboCop::Git::Options do
5
+ it 'fail with no options' do
6
+ proc do
7
+ RuboCop::Git::Options.new({})
8
+ end.must_raise(RuboCop::Git::Options::Invalid)
9
+ end
10
+
11
+ it 'can pass string hash options' do
12
+ RuboCop::Git::Options.new('rubocop' => {}, 'commits' => [])
13
+ end
14
+
15
+ it 'can pass symbol hash options' do
16
+ RuboCop::Git::Options.new(rubocop: {}, commits: [])
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ require_relative '../../test_helper'
2
+ require 'rubocop/git/runner'
3
+
4
+ describe RuboCop::Git::Runner do
5
+ it 'exit with violations' do
6
+ options = RuboCop::Git::Options.new
7
+ # lib/rubocop/git/runner.rb:14:1: C: Trailing whitespace detected.
8
+ options.commits = ["v0.0.4", "v0.0.5"]
9
+ proc do
10
+ _out, _err = capture_io do
11
+ RuboCop::Git::Runner.new.run(options)
12
+ end
13
+ end.must_raise(SystemExit)
14
+ end
15
+
16
+ it 'fail with no options' do
17
+ proc do
18
+ _out, _err = capture_io do
19
+ RuboCop::Git::Runner.new.run({})
20
+ end
21
+ end.must_raise(RuboCop::Git::Options::Invalid)
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ require 'minitest/autorun'
2
+ require 'rubocop/git'
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cs-rubocop-git
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Masaki Takeuchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: appraisal
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.24.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.24.1
83
+ description: RuboCop for git diff.
84
+ email:
85
+ - developers@combostrike.com
86
+ executables:
87
+ - rubocop-git
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Appraisals
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/rubocop-git
99
+ - gemfiles/0.24.gemfile
100
+ - gemfiles/0.25.gemfile
101
+ - gemfiles/0.26.gemfile
102
+ - gemfiles/0.27.gemfile
103
+ - gemfiles/0.28.gemfile
104
+ - gemfiles/0.29.gemfile
105
+ - gemfiles/0.30.gemfile
106
+ - gemfiles/0.31.gemfile
107
+ - gemfiles/0.32.gemfile
108
+ - gemfiles/0.33.gemfile
109
+ - gemfiles/0.34.gemfile
110
+ - gemfiles/0.35.gemfile
111
+ - gemfiles/0.36.gemfile
112
+ - gemfiles/0.37.gemfile
113
+ - gemfiles/0.38.gemfile
114
+ - gemfiles/0.39.gemfile
115
+ - gemfiles/0.40.gemfile
116
+ - gemfiles/0.41.gemfile
117
+ - gemfiles/0.42.gemfile
118
+ - gemfiles/0.43.gemfile
119
+ - gemfiles/0.44.gemfile
120
+ - gemfiles/0.45.gemfile
121
+ - gemfiles/0.46.gemfile
122
+ - gemfiles/0.47.gemfile
123
+ - gemfiles/0.48.gemfile
124
+ - hound.yml
125
+ - lib/rubocop/git.rb
126
+ - lib/rubocop/git/cli.rb
127
+ - lib/rubocop/git/commit.rb
128
+ - lib/rubocop/git/commit_file.rb
129
+ - lib/rubocop/git/diff_parser.rb
130
+ - lib/rubocop/git/file_violation.rb
131
+ - lib/rubocop/git/line.rb
132
+ - lib/rubocop/git/options.rb
133
+ - lib/rubocop/git/patch.rb
134
+ - lib/rubocop/git/pseudo_pull_request.rb
135
+ - lib/rubocop/git/pseudo_resource.rb
136
+ - lib/rubocop/git/runner.rb
137
+ - lib/rubocop/git/style_checker.rb
138
+ - lib/rubocop/git/style_guide.rb
139
+ - lib/rubocop/git/version.rb
140
+ - rubocop-git.gemspec
141
+ - test/rubocop/git/cli_test.rb
142
+ - test/rubocop/git/options_test.rb
143
+ - test/rubocop/git/runner_test.rb
144
+ - test/test_helper.rb
145
+ homepage: https://github.com/ComboStrikeHQ/rubocop-git
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubygems_version: 3.0.3
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: RuboCop for git diff.
168
+ test_files:
169
+ - test/rubocop/git/cli_test.rb
170
+ - test/rubocop/git/options_test.rb
171
+ - test/rubocop/git/runner_test.rb
172
+ - test/test_helper.rb