dirtycop 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 195c9c805b5211cbeb8c7f4ab931b1dd7fa34c1b
4
- data.tar.gz: 863300400e793bb3cd4d39bd8a2a7e7ef2fcd9f5
3
+ metadata.gz: 121d4582aab6c06fa5479d106e623ac34dfe9b43
4
+ data.tar.gz: 8f4d5b5330c2954bf5d0667b7d96908b59ea73bd
5
5
  SHA512:
6
- metadata.gz: 53bad38e9728c722af674d1f3d1290b5496a02f3e21697eb108c1ab7e13b2a3f4275f2e63f65d192af72e9d9445068adda9d28a88799a2a48ae0b59154b848e9
7
- data.tar.gz: caa55e47bd56b73c07c0d3ef01f767e5523447dffb14197343837ebb929d7d7a8d7ca7d3c5ef2d73ae5db90243b96f900d4bcaf2c13a9cc0bd60d912708e5165
6
+ metadata.gz: b443bb242d5444c345df81c0c5438e7fe59cda4fcefdddbf2c6f005dcef42c43f1f7fa3fa887c0ce0c3d8cee212bee011377041347d6f0cc4ee3811bd54b671e
7
+ data.tar.gz: 9e956db18397072297a03017d888ea41e9b4ddf915bbd79ae42cb71eefe056303cbffed06e3b4a6fee72b288465ae6c640f6ab412cdc68782dd1518233826c4d
data/.gitignore CHANGED
@@ -31,3 +31,4 @@
31
31
  .rvmrc
32
32
 
33
33
  *.swp
34
+ *.swo
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dirtycop (0.0.2)
4
+ dirtycop (0.0.3)
5
5
  rubocop
6
6
 
7
7
  GEM
data/bin/dirty CHANGED
@@ -5,13 +5,11 @@ $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib/dirty')
5
5
  require 'benchmark'
6
6
  require 'cop'
7
7
 
8
- changed_files = `git diff --name-only | grep \.rb$`.split("\n") # list of changed ruby files
9
-
10
8
  cli = RuboCop::CLI.new
11
9
  result = 0
12
10
 
13
11
  time = Benchmark.realtime do
14
- my_args = ARGV + changed_files
12
+ my_args = ARGV
15
13
  result = cli.run(my_args)
16
14
  end
17
15
 
data/dirtycop.gemspec CHANGED
@@ -6,7 +6,7 @@ require 'dirty/cop/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "dirtycop"
8
8
  spec.version = Dirty::Cop::VERSION
9
- spec.authors = ["Adam Hess"]
9
+ spec.authors = ["Adam Hess", 'Melch']
10
10
  spec.email = ["adamhess1991@gmail.com"]
11
11
  spec.summary = %q{Corrupt cop that avoids unchanged violations}
12
12
  spec.description = %q{Takes your current diff as the input for rubocop}
data/lib/dirty/cop.rb CHANGED
@@ -38,25 +38,32 @@
38
38
  # unmodified code if they are reported in modified lines.
39
39
 
40
40
  require 'rubocop'
41
+ # require 'pry'
41
42
 
42
43
  module DirtyCop
43
44
  extend self # In your face, style guide!
44
45
 
45
46
  def bury_evidence?(file, line)
46
- process_bribe
47
47
  !report_offense_at?(file, line)
48
48
  end
49
49
 
50
- def uncovered_targets
51
- @files
50
+ def ref
51
+ 'HEAD'
52
+ end
53
+
54
+ def files_to_inspect(whitelisted_files, args)
55
+ return @files ||= args unless args.empty?
56
+
57
+ @files ||= (changed_files(ref) & whitelisted_files)
52
58
  end
53
59
 
54
60
  def cover_up_unmodified(ref, only_changed_lines = true)
55
- @files ||= files_modified_since(ref)
56
- @line_filter ||= build_line_filter(@files, ref) if only_changed_lines
61
+ @line_filter ||= changed_files_and_lines(ref) if only_changed_lines
57
62
  end
58
63
 
59
64
  def process_bribe
65
+ # leaving this unused method as a placeholder of flags that purportedly work
66
+ # (potential feature set)
60
67
  only_changed_lines = true
61
68
 
62
69
  # I am specifying the ref above instead of getting it from args since
@@ -83,52 +90,62 @@ module DirtyCop
83
90
  # end
84
91
  # return unless ref
85
92
 
86
- ref = 'HEAD'
87
-
88
93
  cover_up_unmodified ref, only_changed_lines
89
94
  end
90
95
 
91
- private
92
-
93
96
  def report_offense_at?(file, line)
94
- !@line_filter || @line_filter.fetch(file)[line] if @line_filter[file]
97
+ changed_lines_for_file(file).include? line
95
98
  end
96
99
 
97
- def files_modified_since(ref)
98
- `git diff --diff-filter=AM --name-only #{ref}`
100
+ def changed_lines_for_file(file)
101
+ changed_files_and_lines(ref)[file] || []
102
+ end
103
+
104
+ def changed_files(ref)
105
+ @changed_files ||= git_diff_name_only
99
106
  .lines
100
107
  .map(&:chomp)
101
108
  .grep(/\.rb$/)
102
109
  .map { |file| File.absolute_path(file) }
103
110
  end
104
111
 
105
- def build_line_filter(_files, ref)
112
+ def git_diff_name_only
113
+ `git diff --diff-filter=AM --name-only #{ref}`
114
+ end
115
+
116
+ def changed_files_and_lines(ref)
106
117
  result = {}
107
118
 
108
- suspects = files_modified_since(ref)
109
- suspects.each do |file|
110
- result[file] = lines_modified_since(file, ref)
119
+ changed_files(ref).each do |file|
120
+ result[file] = changed_lines(file, ref)
111
121
  end
112
122
 
113
123
  result
114
124
  end
115
125
 
116
- def lines_modified_since(file, ref)
117
- ranges =
118
- `git diff -p -U0 #{ref} #{file}`
119
- .lines
120
- .grep(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/) { Regexp.last_match[1].to_i..(Regexp.last_match[1].to_i + Regexp.last_match[2].to_i) }
121
- .reverse
122
-
123
- mask = Array.new(ranges.first.end)
126
+ def git_diff(file, ref)
127
+ `git diff -p -U0 #{ref} #{file}`
128
+ end
124
129
 
125
- ranges.each do |range|
126
- range.each do |line|
127
- mask[line] = true
130
+ def changed_lines(file, ref)
131
+ ranges = git_diff(file, ref)
132
+ .each_line
133
+ .grep(/@@ -(\d+)(?:,)?(\d+)? \+(\d+)(?:,)?(\d+)? @@/) {
134
+ [
135
+ Regexp.last_match[3].to_i,
136
+ (Regexp.last_match[4] || 1).to_i
137
+ ]
138
+ }.reverse
139
+
140
+ mask = Set.new
141
+
142
+ ranges.each do |changed_line_number, number_of_changed_lines|
143
+ number_of_changed_lines.times do |line_delta|
144
+ mask << changed_line_number + line_delta
128
145
  end
129
146
  end
130
147
 
131
- mask
148
+ mask.to_a
132
149
  end
133
150
 
134
151
  def eat_a_donut
@@ -144,10 +161,7 @@ module RuboCop
144
161
  alias_method :find_unpatched, :find
145
162
 
146
163
  def find(args)
147
- replacement = DirtyCop.uncovered_targets
148
- return replacement if replacement
149
-
150
- find_unpatched(args)
164
+ DirtyCop.files_to_inspect(find_unpatched(args), args)
151
165
  end
152
166
  end
153
167
 
@@ -1,5 +1,5 @@
1
1
  module Dirty
2
2
  module Cop
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -0,0 +1,19 @@
1
+ diff --git a/app/models/submittal_package.rb b/app/models/submittal_package.rb
2
+ index 9ec391b..b39fff7 100755
3
+ --- a/app/models/submittal_package.rb
4
+ +++ b/app/models/submittal_package.rb
5
+ @@ -4 +4 @@
6
+ -#
7
+ +# foo
8
+ @@ -6,4 +6,4 @@
9
+ -# project_id :integer
10
+ -# specification_section_id :integer
11
+ -# created_at :datetime
12
+ -# deleted_at :datetime
13
+ +# project_id :integer foo
14
+ +# specification_section_id :integer foo
15
+ +# created_at :datetime foo
16
+ +# deleted_at :datetime foo
17
+ @@ -12 +12 @@
18
+ -# created_by_id :integer
19
+ +# created_by_id :integer foo
@@ -0,0 +1,2 @@
1
+ lib/dirty/cop.rb
2
+ spec/dirty/cop_spec.rb
@@ -0,0 +1,12 @@
1
+ diff --git a/spec/models/submittal_log_spec.rb b/spec/models/submittal_log_spec.rb
2
+ index f9c743f..de36350 100755
3
+ --- a/spec/models/submittal_log_spec.rb
4
+ +++ b/spec/models/submittal_log_spec.rb
5
+ @@ -2,2 +2,2 @@ require 'rails_helper'
6
+ -require 'shared_examples_for_location_name_setter'
7
+ -
8
+ +require 'shared_examples_for_location_name_setter' bar
9
+ + bar
10
+ @@ -6 +6 @@ RSpec.describe SubmittalLog, type: :model do
11
+ - let(:project) { FactoryGirl.create(:project, time_zone: "Pacific Time (US & Canada)") }
12
+ + let(:project) { FactoryGirl.create(:project, time_zone: "Pacific Time (US & Canada)") } bar
@@ -0,0 +1,101 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ class RubocopTest
6
+ include DirtyCop
7
+ end
8
+
9
+ describe DirtyCop do
10
+ let(:rubocop) { RubocopTest.new }
11
+ let(:ref) { 'HEAD' }
12
+
13
+ describe '#files_to_inspect' do
14
+ context 'files passed in' do
15
+ it 'returns files passed into rubocop that have changes' do
16
+ files = ['company', 'user']
17
+ rubocop_allowed_files = ['dont_care']
18
+ result = rubocop.files_to_inspect(rubocop_allowed_files, files)
19
+ expect(result).to eq(files)
20
+ end
21
+ end
22
+
23
+ context 'no files passed in' do
24
+ it 'returns all files that have changes' do
25
+ changed_files = ['company', 'user']
26
+ rubocop_allowed_files = changed_files
27
+ rubocop.stub(:changed_files) { changed_files }
28
+
29
+ result = rubocop.files_to_inspect(rubocop_allowed_files, [])
30
+ expect(result).to eq(changed_files)
31
+ end
32
+
33
+ it 'returns files that have changes that are okayed by rubocop' do
34
+ changed_files = ['company', 'user', 'project']
35
+ rubocop_allowed_files = ['app', 'company', 'user']
36
+ rubocop.stub(:changed_files) { changed_files }
37
+
38
+ result = rubocop.files_to_inspect(rubocop_allowed_files, [])
39
+ expect(result).to eq(['company', 'user'])
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#changed_files' do
45
+ it 'returns an array of full path of files changed' do
46
+ rubocop.stub(:git_diff_name_only) { File.open('spec/diff_mocks/name_only') }
47
+ expected = [
48
+ "#{Dir.pwd}/lib/dirty/cop.rb",
49
+ "#{Dir.pwd}/spec/dirty/cop_spec.rb"
50
+ ]
51
+ expect(rubocop.changed_files(ref)).to eq(expected)
52
+ end
53
+ end
54
+
55
+ describe '#changed_lines' do
56
+ let(:file) { 'company' }
57
+
58
+ it 'returns an array, containing line numbers that have changed' do
59
+ rubocop.stub(:git_diff) { File.open('spec/diff_mocks/company') }
60
+ result = rubocop.changed_lines(file, ref)
61
+ expected = [4, 6, 7, 8, 9, 12]
62
+ expect(result.sort).to eq(expected)
63
+ end
64
+ end
65
+
66
+ describe '#report_offense_at?' do
67
+ it 'should respond if a line was changed at past file and line' do
68
+ changed_files_and_lines = {
69
+ 'company' => [4, 6, 7, 8, 9, 12],
70
+ 'user' => [2, 3, 6]
71
+ }
72
+ rubocop.stub(:changed_files_and_lines) { changed_files_and_lines }
73
+ expect(rubocop.report_offense_at?('company', 6)).to be_truthy
74
+ expect(rubocop.report_offense_at?('company', 1)).to be_falsy
75
+ expect(rubocop.report_offense_at?('other_file', 6)).to be_falsy
76
+ end
77
+
78
+ it 'should not blow up if no files were changed' do
79
+ changed_files_and_lines = {}
80
+ rubocop.stub(:changed_files_and_lines) { changed_files_and_lines }
81
+ expect(rubocop.report_offense_at?('company', 1)).to be_falsy
82
+ end
83
+ end
84
+
85
+ describe '#changed_files_and_lines' do
86
+ it 'should return a hash with filenames and changed lines' do
87
+ rubocop.stub(:changed_files) { %w(company user) }
88
+ rubocop.stub(:git_diff).with('company', 'HEAD') { File.open('spec/diff_mocks/company') }
89
+ rubocop.stub(:git_diff).with('user', 'HEAD') { File.open('spec/diff_mocks/user') }
90
+ expected = {
91
+ 'company' => [4, 6, 7, 8, 9, 12],
92
+ 'user' => [2, 3, 6]
93
+ }
94
+ result = rubocop.changed_files_and_lines(ref)
95
+ expect(result.keys).to eq(expected.keys)
96
+ result.keys.each do |key|
97
+ expect(result[key].sort).to eq(expected[key].sort)
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dirty/cop'
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dirtycop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hess
8
+ - Melch
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-09-27 00:00:00.000000000 Z
12
+ date: 2015-12-24 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rubocop
@@ -70,6 +71,11 @@ files:
70
71
  - dirtycop.gemspec
71
72
  - lib/dirty/cop.rb
72
73
  - lib/dirty/cop/version.rb
74
+ - spec/diff_mocks/company
75
+ - spec/diff_mocks/name_only
76
+ - spec/diff_mocks/user
77
+ - spec/dirty/cop_spec.rb
78
+ - spec/spec_helper.rb
73
79
  homepage: ''
74
80
  licenses:
75
81
  - MIT
@@ -90,9 +96,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
96
  version: '0'
91
97
  requirements: []
92
98
  rubyforge_project:
93
- rubygems_version: 2.4.5
99
+ rubygems_version: 2.2.2
94
100
  signing_key:
95
101
  specification_version: 4
96
102
  summary: Corrupt cop that avoids unchanged violations
97
- test_files: []
98
- has_rdoc:
103
+ test_files:
104
+ - spec/diff_mocks/company
105
+ - spec/diff_mocks/name_only
106
+ - spec/diff_mocks/user
107
+ - spec/dirty/cop_spec.rb
108
+ - spec/spec_helper.rb