overrides_tracker 0.1.12 → 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.
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require_relative '../test_classes/custom_class'
5
+
6
+ describe OverridesTracker::FileObserver do
7
+ it 'calls the method overrides_tracker_finished_file when the file is finished' do
8
+ expect(CustomClass.instance_variable_get(:@overrides_tracker_finished_file_called)).to eq(true)
9
+ end
10
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'overrides_tracker/hash_decorator'
5
+
6
+ describe Hash do
7
+ describe '#deep_merge' do
8
+ let(:hash) { { a: 1, b: { c: 2 } } }
9
+ let(:other_hash) { { a: 3, b: { d: 4 } } }
10
+
11
+ it 'returns a new hash with the contents of both hashes merged' do
12
+ expect(hash.deep_merge(other_hash)).to eq(a: 3, b: { c: 2, d: 4 })
13
+ end
14
+
15
+ it 'does not modify the original hash' do
16
+ expect { hash.deep_merge(other_hash) }.not_to change { hash }
17
+ end
18
+ end
19
+
20
+ describe '#deep_merge!' do
21
+ let(:hash) { { a: 1, b: { c: 2 } } }
22
+ let(:other_hash) { { a: 3, b: { d: 4 } } }
23
+
24
+ it 'modifies the original hash with the contents of the other hash merged' do
25
+ expect { hash.deep_merge!(other_hash) }.to change { hash }.to(a: 3, b: { c: 2, d: 4 })
26
+ end
27
+ end
28
+
29
+ describe '#deep_stringify_keys!' do
30
+ it 'converts all keys in a hash to strings' do
31
+ hash = { a: 1, b: { c: 2, d: 3 } }
32
+ hash.deep_stringify_keys!
33
+ expect(hash).to eq({ 'a' => 1, 'b' => { 'c' => 2, 'd' => 3 } })
34
+ end
35
+
36
+ it 'converts all keys in an array of hashes to strings' do
37
+ array = { a: [{ a: 1, b: 2 }, { c: 3, d: 4 }] }
38
+ array.deep_stringify_keys!
39
+ expect(array).to eq({ 'a' => [{ 'a' => 1, 'b' => 2 }, { 'c' => 3, 'd' => 4 }] })
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,365 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+ require_relative '../test_classes/custom_class'
4
+
5
+
6
+ WORKING_DIR = Dir.pwd
7
+
8
+
9
+ describe OverridesTracker::MethodsCollector do
10
+ let(:obj) { OverridesTracker::MethodsCollector.instance }
11
+
12
+ describe '#add_method_for_class' do
13
+ let(:method) { CustomClass.instance_method(:instance_test_method) }
14
+
15
+ it 'adds the method to the methods collection' do
16
+ expect(obj.instance_variable_get(:@methods_collection)).to eq(nil)
17
+
18
+ obj.add_method_for_class(:instance_methods, 'CustomClass', :instance_test_method, OverridesTracker::Util.method_hash(method))
19
+
20
+ expect(obj.instance_variable_get(:@methods_collection)).to eq({"CustomClass" => {:instance_methods=>{:instance_test_method=>{:body=>"def instance_test_method\n 'instance_test_method'\nend\n", :is_part_of_app=>true, :location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 10], :sha=>"3408e1f1736c6b83bc13f014e5338eec0c67393f"}}, :is_part_of_app=>true, :singleton_methods=>{}}})
21
+ end
22
+ end
23
+
24
+ describe '#mark_method_as_override' do
25
+ let(:method) { CustomClass.instance_method(:instance_test_method) }
26
+ let(:overriding_method) { CustomClass.instance_method(:instance_override_test_method) }
27
+
28
+ before do
29
+ obj.instance_variable_set(:@methods_collection, nil)
30
+ end
31
+
32
+ context 'when overriding location is in working directory' do
33
+ it 'adds the method to the overridden methods collection' do
34
+ expect(obj.instance_variable_get(:@methods_collection)).to eq(nil)
35
+
36
+ obj.add_method_for_class(:instance_methods, 'CustomClass', :instance_test_method, OverridesTracker::Util.method_hash(method))
37
+ obj.mark_method_as_override(:instance_methods, 'CustomClass', :instance_test_method, overriding_method, OverridesTracker::Util.method_hash(overriding_method))
38
+
39
+ expect(obj.instance_variable_get(:@methods_collection)).to eq({"CustomClass" => {:instance_methods=>{:instance_test_method=>{:body=>"def instance_test_method\n 'instance_test_method'\nend\n", :is_part_of_app=>true, :location=>["#{Dir.pwd}/spec/test_classes/custom_class.rb", 10], :overriding_body=>"def instance_override_test_method\n 'instance_override_test_method'\nend\n", :overriding_is_part_of_app=>true, :overriding_location=>["#{Dir.pwd}/spec/test_classes/custom_class.rb", 14], :overriding_sha=>"75cf2b21c3033f33c155a329d8e9110ae3fb0290", :sha=>"3408e1f1736c6b83bc13f014e5338eec0c67393f"}}, :is_part_of_app=>true, :singleton_methods=>{}}})
40
+ expect(obj.instance_variable_get(:@overridden_methods_collection)).to eq({"CustomClass" => {:added_instance_methods=>{}, :added_singleton_methods=>{}, :instance_methods=>{:instance_test_method=>{:body=>"def instance_test_method\n 'instance_test_method'\nend\n", :is_part_of_app=>true, :location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 10], :overriding_body=>"def instance_override_test_method\n 'instance_override_test_method'\nend\n", :overriding_is_part_of_app=>true, :overriding_location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 14], :overriding_sha=>"75cf2b21c3033f33c155a329d8e9110ae3fb0290", :sha=>"3408e1f1736c6b83bc13f014e5338eec0c67393f"}}, :singleton_methods=>{}}})
41
+ end
42
+ end
43
+
44
+ context 'when overriding location is not in working directory' do
45
+ let(:overriding_method) { CustomClass.instance_method(:instance_override_test_method) }
46
+
47
+ before do
48
+ @original_dir = Dir.pwd
49
+ allow(Dir).to receive(:pwd).and_return("another_directory")
50
+ end
51
+
52
+ it 'adds the method to the overridden methods collection' do
53
+ expect(obj.instance_variable_get(:@methods_collection)).to eq(nil)
54
+
55
+ obj.add_method_for_class(:instance_methods, 'CustomClass', :instance_test_method, OverridesTracker::Util.method_hash(method))
56
+ obj.mark_method_as_override(:instance_methods, 'CustomClass', :instance_test_method, overriding_method, OverridesTracker::Util.method_hash(overriding_method))
57
+
58
+ expect(obj.instance_variable_get(:@methods_collection)).to eq({"CustomClass" => {:instance_methods=>{:instance_test_method=>{:body=>"def instance_test_method\n 'instance_test_method'\nend\n", :is_part_of_app=>false, :location=>["#{@original_dir}/spec/test_classes/custom_class.rb", 10], :overriding_body=>"def instance_override_test_method\n 'instance_override_test_method'\nend\n", :overriding_is_part_of_app=>false, :overriding_location=>["#{@original_dir}/spec/test_classes/custom_class.rb", 14], :overriding_sha=>"75cf2b21c3033f33c155a329d8e9110ae3fb0290", :sha=>"3408e1f1736c6b83bc13f014e5338eec0c67393f"}}, :is_part_of_app=>false, :singleton_methods=>{}}})
59
+ expect(obj.instance_variable_get(:@overridden_methods_collection)).to eq({"CustomClass" => {:added_instance_methods=>{}, :added_singleton_methods=>{}, :instance_methods=>{:instance_test_method=>{:body=>"def instance_test_method\n 'instance_test_method'\nend\n", :is_part_of_app=>false, :location=>["#{@original_dir}/spec/test_classes/custom_class.rb", 10], :overriding_body=>"def instance_override_test_method\n 'instance_override_test_method'\nend\n", :overriding_is_part_of_app=>false, :overriding_location=>["#{@original_dir}/spec/test_classes/custom_class.rb", 14], :overriding_sha=>"75cf2b21c3033f33c155a329d8e9110ae3fb0290", :sha=>"3408e1f1736c6b83bc13f014e5338eec0c67393f"}}, :singleton_methods=>{}}})
60
+ end
61
+ end
62
+ end
63
+
64
+ describe '#mark_method_as_added' do
65
+ let(:method) { CustomClass.instance_method(:instance_test_method) }
66
+ let(:overriding_method) { CustomClass.instance_method(:instance_override_test_method) }
67
+
68
+ before do
69
+ obj.instance_variable_set(:@methods_collection, nil)
70
+ obj.instance_variable_set(:@overridden_methods_collection, nil)
71
+ end
72
+
73
+ context 'when overriding location is in working directory' do
74
+ it 'adds the method to the overridden methods collection' do
75
+ expect(obj.instance_variable_get(:@methods_collection)).to eq(nil)
76
+ expect(obj.instance_variable_get(:@overridden_methods_collection)).to eq(nil)
77
+
78
+ obj.mark_method_as_added(:added_instance_methods, 'CustomClass', :instance_test_method, overriding_method, OverridesTracker::Util.method_hash(overriding_method))
79
+
80
+ expect(obj.instance_variable_get(:@overridden_methods_collection)).to eq({"CustomClass" => {:added_instance_methods=>{:instance_test_method=>{:body=>"def instance_override_test_method\n 'instance_override_test_method'\nend\n", :is_part_of_app=>true, :location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 14], :overriding_is_part_of_app=>true, :overriding_location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 14], :sha=>"75cf2b21c3033f33c155a329d8e9110ae3fb0290"}}, :added_singleton_methods=>{}, :instance_methods=>{}, :singleton_methods=>{}}})
81
+ end
82
+ end
83
+
84
+ context 'when overriding location is not in working directory' do
85
+ let(:overriding_method) { CustomClass.instance_method(:instance_override_test_method) }
86
+
87
+ before do
88
+ allow(Dir).to receive(:pwd).and_return("another_directory")
89
+ end
90
+
91
+ it 'adds the method to the overridden methods collection' do
92
+ expect(obj.instance_variable_get(:@methods_collection)).to eq(nil)
93
+ expect(obj.instance_variable_get(:@overridden_methods_collection)).to eq(nil)
94
+
95
+ obj.mark_method_as_added(:added_instance_methods, 'CustomClass', :instance_test_method, overriding_method, OverridesTracker::Util.method_hash(overriding_method))
96
+
97
+ expect(obj.instance_variable_get(:@overridden_methods_collection)).to eq({"CustomClass" => {:added_instance_methods=>{:instance_test_method=>{:body=>"def instance_override_test_method\n 'instance_override_test_method'\nend\n", :is_part_of_app=>false, :location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 14], :overriding_is_part_of_app=>false, :overriding_location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 14], :sha=>"75cf2b21c3033f33c155a329d8e9110ae3fb0290"}}, :added_singleton_methods=>{}, :instance_methods=>{}, :singleton_methods=>{}}})
98
+ end
99
+ end
100
+ end
101
+
102
+ describe '#summarize_overrides' do
103
+ let(:instance_method) { CustomClass.instance_method(:instance_test_method) }
104
+ let(:singleton_method) { CustomClass.singleton_method(:singleton_test_method) }
105
+ let(:added_instance_method) { CustomClass.instance_method(:instance_added_test_method) }
106
+ let(:added_singleton_method) { CustomClass.singleton_method(:singleton_added_test_method) }
107
+
108
+ before do
109
+ obj.instance_variable_set(:@methods_collection, nil)
110
+ obj.instance_variable_set(:@overridden_methods_collection, nil)
111
+ obj.add_method_for_class(:singleton_methods, 'CustomClass', :singleton_test_method, OverridesTracker::Util.method_hash(singleton_method))
112
+ obj.add_method_for_class(:instance_methods, 'CustomClass', :instance_test_method, OverridesTracker::Util.method_hash(instance_method))
113
+ obj.mark_method_as_override(:singleton_methods, 'CustomClass', :singleton_test_method, singleton_method, OverridesTracker::Util.method_hash(singleton_method))
114
+ obj.mark_method_as_override(:instance_methods, 'CustomClass', :instance_test_method, instance_method, OverridesTracker::Util.method_hash(instance_method))
115
+ obj.mark_method_as_added(:added_singleton_methods, 'CustomClass', :singleton_added_test_method, added_singleton_method, OverridesTracker::Util.method_hash(added_singleton_method))
116
+ obj.mark_method_as_added(:added_instance_methods, 'CustomClass', :instance_added_test_method, added_instance_method, OverridesTracker::Util.method_hash(added_instance_method))
117
+ end
118
+
119
+ it 'calls show_override for each overridden method' do
120
+ expect(obj).to receive(:show_override).with('CustomClass', :instance_test_method, {:sha=>"3408e1f1736c6b83bc13f014e5338eec0c67393f", :location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 10], :body=>"def instance_test_method\n 'instance_test_method'\nend\n", :is_part_of_app=>true, :overriding_location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 10], :overriding_body=>"def instance_test_method\n 'instance_test_method'\nend\n", :overriding_sha=>"3408e1f1736c6b83bc13f014e5338eec0c67393f", :overriding_is_part_of_app=>true},'#', 'overridden').once.and_call_original
121
+ expect(obj).to receive(:show_override).with('CustomClass', :singleton_test_method, {:sha=>"1e331d1bb802e5d0a21a6c18f574f6ceecb4bc91", :location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 6], :body=>"def self.singleton_test_method\n 'singleton_test_method'\nend\n", :is_part_of_app=>true, :overriding_location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 6], :overriding_body=>"def self.singleton_test_method\n 'singleton_test_method'\nend\n", :overriding_sha=>"1e331d1bb802e5d0a21a6c18f574f6ceecb4bc91", :overriding_is_part_of_app=>true},'.', 'overridden').once.and_call_original
122
+ expect(obj).to receive(:show_override).with('CustomClass', :instance_added_test_method, {:sha=>"5d7709e9d44e9f718cca1876a4fdec82bbe3cf4e", :location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 18], :body=>"def instance_added_test_method\n 'instance_added_test_method'\nend\n", :is_part_of_app=>true, :overriding_location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 18], :overriding_is_part_of_app=>true},'#', 'added').once.and_call_original
123
+ expect(obj).to receive(:show_override).with('CustomClass', :singleton_added_test_method, {:sha=>"ba6efb2d378551e55ee53660d25b266b3c515da3", :location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 22], :body=>"def self.singleton_added_test_method\n 'singleton_added_test_method'\nend\n", :is_part_of_app=>true, :overriding_location=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 22], :overriding_is_part_of_app=>true},'.', 'added').once.and_call_original
124
+
125
+ obj.summarize_overrides
126
+ end
127
+ end
128
+
129
+ describe 'save and load_from_file' do
130
+ let(:method) { CustomClass.instance_method(:instance_test_method) }
131
+ let(:overriding_method) { CustomClass.instance_method(:instance_override_test_method) }
132
+
133
+ let(:file_name) { "branch_name#last_commit_id.otf"}
134
+
135
+
136
+ before do
137
+ allow(obj).to receive(:branch_name).and_return('branch_name')
138
+ allow(obj).to receive(:last_commit_id).and_return('last_commit_id')
139
+ allow(obj).to receive(:last_commit_name).and_return('last_commit_name')
140
+ allow(obj).to receive(:last_commit_name_to_report).and_return('last_commit_name_to_report')
141
+ allow(obj).to receive(:author_name).and_return('author_name')
142
+ allow(obj).to receive(:committer_name).and_return('committer_name')
143
+
144
+ obj.instance_variable_set(:@methods_collection, nil)
145
+ obj.instance_variable_set(:@overridden_methods_collection, nil)
146
+ obj.add_method_for_class(:instance_methods, 'CustomClass', :instance_test_method, OverridesTracker::Util.method_hash(method))
147
+ obj.mark_method_as_override(:instance_methods, 'CustomClass', :instance_test_method, overriding_method, OverridesTracker::Util.method_hash(overriding_method))
148
+ obj.save_to_file
149
+ end
150
+
151
+ it 'loads the methods collection from the file' do
152
+ obj.instance_variable_set(:@methods_collection, nil)
153
+ obj.instance_variable_set(:@overridden_methods_collection, nil)
154
+
155
+ data = obj.load_from_file(file_name)
156
+
157
+ expect(data).to eq(
158
+ {"branch_name" => "branch_name","branch_name_to_report" => "master","bundle_path" => Bundler.bundle_path.to_s, "author_name" => "author_name", "committer_name" => "committer_name","last_commit_id" => "last_commit_id","last_commit_name" => "last_commit_name","last_commit_name_to_report" => "last_commit_name_to_report","methods_collection" => {"CustomClass"=>{"added_instance_methods"=>{}, "added_singleton_methods"=>{}, "instance_methods"=>{"instance_test_method"=>{"body"=>"def instance_test_method\n 'instance_test_method'\nend\n", "is_part_of_app"=>true, "location"=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 10], "overriding_body"=>"def instance_override_test_method\n 'instance_override_test_method'\nend\n", "overriding_is_part_of_app"=>true, "overriding_location"=>["#{WORKING_DIR}/spec/test_classes/custom_class.rb", 14], "overriding_sha"=>"75cf2b21c3033f33c155a329d8e9110ae3fb0290", "sha"=>"3408e1f1736c6b83bc13f014e5338eec0c67393f"}}, "singleton_methods"=>{}}}, "version" => "#{OverridesTracker::VERSION}", "working_directory" => Dir.pwd,"number_of_classes" => 1, "number_of_classes_in_app_path" => 1, "number_of_methods" => 1, "number_of_methods_in_app_path" => 1})
159
+ end
160
+
161
+ end
162
+
163
+ describe '#build_overrides_hash' do
164
+ let(:method) { CustomClass.instance_method(:instance_test_method) }
165
+
166
+ before do
167
+ obj.instance_variable_set(:@methods_collection, nil)
168
+ obj.add_method_for_class(:instance_methods, 'CustomClass', :instance_test_method, OverridesTracker::Util.method_hash(method))
169
+ @methods_collection = obj.instance_variable_get(:@methods_collection)
170
+
171
+ allow(obj).to receive(:build_overrides_hash_for_method_type).with(CustomClass, @methods_collection["CustomClass"], :instance_methods, WORKING_DIR)
172
+ allow(obj).to receive(:build_overrides_hash_for_method_type).with(CustomClass, @methods_collection["CustomClass"], :singleton_methods, WORKING_DIR)
173
+ end
174
+
175
+ it 'calls build_overrides_hash_for_method_type' do
176
+ obj.build_overrides_hash
177
+
178
+ expect(obj).to have_received(:build_overrides_hash_for_method_type).with(CustomClass, @methods_collection["CustomClass"], :instance_methods, WORKING_DIR)
179
+ expect(obj).to have_received(:build_overrides_hash_for_method_type).with(CustomClass, @methods_collection["CustomClass"], :singleton_methods, WORKING_DIR)
180
+ end
181
+ end
182
+
183
+ describe '#overridden_methods' do
184
+ let(:overridden_methods_collection) { {test: 'test '} }
185
+
186
+ before do
187
+ obj.instance_variable_set(:@overridden_methods_collection, overridden_methods_collection)
188
+ end
189
+
190
+ it 'returns the overridden_methods_collection' do
191
+ expect(obj.overridden_methods).to eq(overridden_methods_collection)
192
+ end
193
+ end
194
+
195
+ describe '#report' do
196
+ let(:api_token) { 'api_token' }
197
+ let(:branch_name_to_report) { 'branch_name_to_report' }
198
+ let(:last_commit_id) { 'last_commit_id' }
199
+ let(:last_commit_name_to_report) { 'last_commit_name_to_report' }
200
+ let(:path_to_report_file) { 'path_to_report_file' }
201
+
202
+ before do
203
+ allow(obj).to receive(:branch_name_to_report).and_return(branch_name_to_report)
204
+ allow(obj).to receive(:last_commit_id).and_return(last_commit_id)
205
+ allow(obj).to receive(:last_commit_name_to_report).and_return(last_commit_name_to_report)
206
+ allow(obj).to receive(:path_to_report_file).and_return(path_to_report_file)
207
+ allow(OverridesTracker::Api).to receive(:report_build).with(api_token, branch_name_to_report, last_commit_id, last_commit_name_to_report, path_to_report_file)
208
+ end
209
+
210
+ it 'call the API' do
211
+ obj.report(api_token)
212
+
213
+ expect(OverridesTracker::Api).to have_received(:report_build).with(api_token, branch_name_to_report, last_commit_id, last_commit_name_to_report, path_to_report_file)
214
+ end
215
+ end
216
+
217
+ describe '#branch_name' do
218
+ let(:branch_name) { `git rev-parse --abbrev-ref HEAD` }
219
+
220
+ it 'returns the branch name' do
221
+ expect(obj.send(:branch_name)).to eq(branch_name.downcase.gsub('/', '_').gsub(/\s+/, ''))
222
+ end
223
+ end
224
+
225
+ describe '#last_commit_id' do
226
+ let(:last_commit_id) { `git log --format="%H" -n 1` }
227
+
228
+ it 'returns the last_commit_id' do
229
+ expect(obj.send(:last_commit_id)).to eq(last_commit_id.gsub(/\s+/, ''))
230
+ end
231
+ end
232
+
233
+ describe '#last_commit_name_to_report' do
234
+ let(:last_commit_name_to_report) { `git log --format="%s" -n 1` }
235
+
236
+ it 'returns the last_commit_name_to_report' do
237
+ expect(obj.send(:last_commit_name_to_report)).to eq(last_commit_name_to_report.chomp)
238
+ end
239
+ end
240
+
241
+ describe '#author_name' do
242
+ let(:author_name) { `git show -s --format='%an'`.chomp }
243
+
244
+ it 'returns the author_name' do
245
+ expect(obj.send(:author_name)).to eq(author_name)
246
+ end
247
+ end
248
+
249
+ describe '#committer_name' do
250
+ let(:committer_name) { `git show -s --format='%cn'`.chomp }
251
+
252
+ it 'returns the committer_name' do
253
+ expect(obj.send(:committer_name)).to eq(committer_name)
254
+ end
255
+ end
256
+
257
+ describe '#build_overrides_hash_for_method_type' do
258
+
259
+ context 'instance_methods' do
260
+ let(:method) { CustomClass.instance_method(:instance_test_method) }
261
+
262
+ before do
263
+ obj.instance_variable_set(:@methods_collection, nil)
264
+ obj.add_method_for_class(:instance_methods, 'CustomClass', :instance_test_method, OverridesTracker::Util.method_hash(method))
265
+ obj.add_method_for_class(:instance_methods, 'CustomClass', :instance_override_test_method, OverridesTracker::Util.method_hash(method))
266
+ @methods_collection = obj.instance_variable_get(:@methods_collection)
267
+ end
268
+
269
+ context 'when the method is not overridden' do
270
+ let(:method_to_check) { CustomClass.instance_method(:instance_added_test_method) }
271
+
272
+ before do
273
+ allow(obj).to receive(:mark_method_as_added).with(:added_instance_methods, 'CustomClass', :instance_added_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
274
+ end
275
+
276
+ context 'when the method is part of the app' do
277
+ it 'calls mark_method_as_added_instance_methods' do
278
+ obj.build_overrides_hash_for_method_type(CustomClass, @methods_collection["CustomClass"], :instance_methods, WORKING_DIR)
279
+ expect(obj).to have_received(:mark_method_as_added).with(:added_instance_methods, 'CustomClass', :instance_added_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
280
+ end
281
+ end
282
+
283
+ context 'when the method is not part of the app' do
284
+ it 'does not call mark_method_as_added_instance_methods' do
285
+ obj.build_overrides_hash_for_method_type(CustomClass, @methods_collection["CustomClass"], :instance_methods, '/not_working_dir')
286
+ expect(obj).to_not have_received(:mark_method_as_added).with(:added_instance_methods, 'CustomClass', :instance_added_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
287
+ end
288
+ end
289
+ end
290
+
291
+ context 'when the method is override' do
292
+ let(:method_to_check) { CustomClass.instance_method(:instance_override_test_method) }
293
+
294
+ before do
295
+ CustomClass.class_eval do
296
+ def instance_override_test_method
297
+ "instance_override_test_method_override"
298
+ end
299
+ end
300
+ allow(obj).to receive(:mark_method_as_override).with(:instance_methods, 'CustomClass', :instance_override_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
301
+ end
302
+
303
+ it 'calls mark_method_as_added_instance_methods' do
304
+ obj.build_overrides_hash_for_method_type(CustomClass, @methods_collection["CustomClass"], :instance_methods, WORKING_DIR)
305
+ expect(obj).to have_received(:mark_method_as_override).with(:instance_methods, 'CustomClass', :instance_override_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
306
+ end
307
+ end
308
+ end
309
+
310
+ context 'singleton_methods' do
311
+ let(:method) { CustomClass.singleton_method(:singleton_test_method) }
312
+
313
+ before do
314
+ obj.instance_variable_set(:@methods_collection, nil)
315
+ obj.add_method_for_class(:singleton_methods, 'CustomClass', :singleton_test_method, OverridesTracker::Util.method_hash(method))
316
+ obj.add_method_for_class(:singleton_methods, 'CustomClass', :singleton_override_test_method, OverridesTracker::Util.method_hash(method))
317
+ @methods_collection = obj.instance_variable_get(:@methods_collection)
318
+ end
319
+
320
+ context 'when the method is not overridden' do
321
+ let(:method_to_check) { CustomClass.singleton_class.instance_method(:singleton_added_test_method) }
322
+
323
+ before do
324
+ allow(obj).to receive(:mark_method_as_added).with(:added_singleton_methods, 'CustomClass', :singleton_added_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
325
+ end
326
+
327
+ context 'when the method is part of the app' do
328
+ it 'calls mark_method_as_added_singleton_methods' do
329
+ obj.build_overrides_hash_for_method_type(CustomClass, @methods_collection["CustomClass"], :singleton_methods, WORKING_DIR)
330
+
331
+ expect(obj).to have_received(:mark_method_as_added).with(:added_singleton_methods, 'CustomClass', :singleton_added_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
332
+ end
333
+ end
334
+
335
+ context 'when the method is not part of the app' do
336
+ it 'does not call mark_method_as_added_singleton_methods' do
337
+ obj.build_overrides_hash_for_method_type(CustomClass, @methods_collection["CustomClass"], :singleton_methods, '/not_working_dir')
338
+
339
+ expect(obj).to_not have_received(:mark_method_as_added).with(:added_singleton_methods, 'CustomClass', :singleton_added_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
340
+ end
341
+ end
342
+ end
343
+
344
+ context 'when the method is override' do
345
+ let(:method_to_check) { CustomClass.singleton_class.instance_method(:singleton_override_test_method) }
346
+
347
+ before do
348
+ CustomClass.class_eval do
349
+ def self.singleton_override_test_method
350
+ "singleton_override_test_method_override"
351
+ end
352
+ end
353
+ allow(obj).to receive(:mark_method_as_override).with(:singleton_methods, 'CustomClass', :singleton_override_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
354
+ end
355
+
356
+ it 'calls mark_method_as_added_singleton_methods' do
357
+ obj.build_overrides_hash_for_method_type(CustomClass, @methods_collection["CustomClass"], :singleton_methods, WORKING_DIR)
358
+
359
+ expect(obj).to have_received(:mark_method_as_override).with(:singleton_methods, 'CustomClass', :singleton_override_test_method, method_to_check, OverridesTracker::Util.method_hash(method_to_check) )
360
+ end
361
+ end
362
+ end
363
+ end
364
+
365
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe String do
6
+ let(:custom_string) { String.new('custom string') }
7
+
8
+ describe '#colorize' do
9
+ it 'returns a string with the color code' do
10
+ expect(custom_string.colorize(31)).to eq "\e[31m#{custom_string}\e[0m"
11
+ end
12
+ end
13
+
14
+ describe '#red' do
15
+ it 'calls colorize with 31' do
16
+ expect(custom_string).to receive(:colorize).with(31)
17
+
18
+ custom_string.red
19
+ end
20
+ end
21
+
22
+ describe '#green' do
23
+ it 'calls colorize with 32' do
24
+ expect(custom_string).to receive(:colorize).with(32)
25
+
26
+ custom_string.green
27
+ end
28
+ end
29
+
30
+ describe '#yellow' do
31
+ it 'calls colorize with 33' do
32
+ expect(custom_string).to receive(:colorize).with(33)
33
+
34
+ custom_string.yellow
35
+ end
36
+ end
37
+
38
+ describe '#blue' do
39
+ it 'calls colorize with 34' do
40
+ expect(custom_string).to receive(:colorize).with(34)
41
+
42
+ custom_string.blue
43
+ end
44
+ end
45
+
46
+ describe '#pink' do
47
+ it 'calls colorize with 35' do
48
+ expect(custom_string).to receive(:colorize).with(35)
49
+
50
+ custom_string.pink
51
+ end
52
+ end
53
+
54
+ describe '#light_blue' do
55
+ it 'calls colorize with 36' do
56
+ expect(custom_string).to receive(:colorize).with(36)
57
+
58
+ custom_string.light_blue
59
+ end
60
+ end
61
+
62
+ describe '#bold' do
63
+ it 'returns a string with the bold code' do
64
+ expect(custom_string.bold).to eq "\e[1m#{custom_string}\e[22m"
65
+ end
66
+ end
67
+
68
+ describe '#italic' do
69
+ it 'returns a string with the italic code' do
70
+ expect(custom_string.italic).to eq "\e[3m#{custom_string}\e[23m"
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require_relative '../test_classes/custom_class'
5
+
6
+ WORKING_DIR = Dir.pwd
7
+
8
+ describe OverridesTracker::Util do
9
+ let(:method) { CustomClass.instance_method(:instance_test_method) }
10
+
11
+ describe '.method_hash' do
12
+ context 'when the method is part of the app' do
13
+ it "returns a hash with the method's name, body and sha" do
14
+ expect(OverridesTracker::Util.method_hash(method)).to eq({ body: "def instance_test_method\n 'instance_test_method'\nend\n",
15
+ is_part_of_app: true,
16
+ location: [
17
+ "#{WORKING_DIR}/spec/test_classes/custom_class.rb", 10
18
+ ],
19
+ sha: '3408e1f1736c6b83bc13f014e5338eec0c67393f' })
20
+ end
21
+ end
22
+
23
+ context 'when the method is not part of the app' do
24
+ before do
25
+ allow(Dir).to receive(:pwd).and_return('another_directory')
26
+ end
27
+
28
+ it "returns a hash with the method's name, body and sha" do
29
+ expect(OverridesTracker::Util.method_hash(method)[:body]).to eq("def instance_test_method\n 'instance_test_method'\nend\n")
30
+ expect(OverridesTracker::Util.method_hash(method)[:is_part_of_app]).to eq(false)
31
+ expect(OverridesTracker::Util.method_hash(method)[:sha]).to eq('3408e1f1736c6b83bc13f014e5338eec0c67393f')
32
+ end
33
+ end
34
+
35
+ context 'when we we run into an error' do
36
+ before do
37
+ allow(method).to receive(:source_location).and_return('/')
38
+ end
39
+
40
+ it "returns a hash with the method's hash" do
41
+ expect(OverridesTracker::Util.method_hash(method)).to eq({ body: nil, is_part_of_app: false, location: nil,
42
+ sha: nil })
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '.outdented_method_body' do
48
+ it 'returns the body of the method without the indentation' do
49
+ expect(OverridesTracker::Util.outdented_method_body(method)).to eq("def instance_test_method\n 'instance_test_method'\nend\n")
50
+ end
51
+ end
52
+
53
+ describe '.method_sha' do
54
+ it 'returns the sha of the method' do
55
+ expect(OverridesTracker::Util.method_sha(method)).to eq('3408e1f1736c6b83bc13f014e5338eec0c67393f')
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe OverridesTracker::VERSION do
6
+ it 'has a version number' do
7
+ expect(OverridesTracker::VERSION).not_to be nil
8
+ end
9
+ end