delta_test 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/README.md +63 -14
  4. data/Rakefile +16 -3
  5. data/circle.yml +8 -1
  6. data/delta_test.gemspec +4 -3
  7. data/ext/delta_test/delta_test_native.c +154 -0
  8. data/ext/delta_test/delta_test_native.h +15 -0
  9. data/ext/delta_test/extconf.rb +12 -0
  10. data/lib/delta_test.rb +8 -2
  11. data/lib/delta_test/cli.rb +108 -21
  12. data/lib/delta_test/configuration.rb +91 -35
  13. data/lib/delta_test/dependencies_table.rb +15 -1
  14. data/lib/delta_test/generator.rb +42 -29
  15. data/lib/delta_test/profiler.rb +5 -0
  16. data/lib/delta_test/related_spec_list.rb +67 -8
  17. data/lib/delta_test/spec_helpers.rb +9 -7
  18. data/lib/delta_test/version.rb +1 -1
  19. data/spec/lib/delta_test/cli_spec.rb +26 -5
  20. data/spec/lib/delta_test/configuration_spec.rb +12 -0
  21. data/spec/lib/delta_test/dependencies_table_spec.rb +35 -0
  22. data/spec/lib/delta_test/generator_spec.rb +34 -17
  23. data/spec/lib/delta_test/profiler_spec.rb +121 -0
  24. data/spec/lib/delta_test/related_spec_list_spec.rb +150 -34
  25. data/spec/lib/delta_test/spec_helpers_spec.rb +11 -5
  26. data/spec/rails/Gemfile +8 -2
  27. data/spec/rails/Gemfile.lock +37 -3
  28. data/spec/rails/app/models/category.rb +14 -0
  29. data/spec/rails/app/models/comment.rb +20 -0
  30. data/spec/rails/app/models/post.rb +23 -0
  31. data/spec/rails/app/models/post_categorizing.rb +14 -0
  32. data/spec/rails/app/models/user.rb +15 -0
  33. data/spec/rails/db/migrate/20150518052022_create_users.rb +9 -0
  34. data/spec/rails/db/migrate/20150518052057_create_posts.rb +11 -0
  35. data/spec/rails/db/migrate/20150518052332_create_comments.rb +11 -0
  36. data/spec/rails/db/migrate/20150518052523_create_categories.rb +9 -0
  37. data/spec/rails/db/migrate/20150518052604_create_post_categorizings.rb +10 -0
  38. data/spec/rails/db/schema.rb +59 -0
  39. data/spec/rails/spec/factories/categories.rb +5 -0
  40. data/spec/rails/spec/factories/comments.rb +8 -0
  41. data/spec/rails/spec/factories/post_categorizings.rb +6 -0
  42. data/spec/rails/spec/factories/posts.rb +7 -0
  43. data/spec/rails/spec/factories/users.rb +5 -0
  44. data/spec/rails/spec/models/category_spec.rb +3 -0
  45. data/spec/rails/spec/models/comment_spec.rb +3 -0
  46. data/spec/rails/spec/models/post_categorizing_spec.rb +3 -0
  47. data/spec/rails/spec/models/post_spec.rb +3 -0
  48. data/spec/rails/spec/models/user_spec.rb +20 -0
  49. data/spec/rails/spec/spec_helper.rb +53 -9
  50. data/spec/spec_helper.rb +2 -0
  51. metadata +79 -19
  52. data/lib/delta_test/analyzer.rb +0 -47
  53. data/spec/lib/delta_test/analyzer_spec.rb +0 -126
@@ -0,0 +1,5 @@
1
+ module DeltaTest
2
+ module Profiler
3
+
4
+ end
5
+ end
@@ -37,27 +37,86 @@ module DeltaTest
37
37
  end
38
38
 
39
39
  ###
40
- # Calculate related spec files
40
+ # Return if full tests are related
41
41
  #
42
- # @return {Set<String>}
42
+ # @return {Boolean}
43
43
  ###
44
- def related_spec_files
45
- spec_files = Set.new
44
+ def full_tests?
45
+ return false unless @changed_files
46
+
47
+ @full_tests ||= if DeltaTest.config.full_test_patterns.empty?
48
+ false
49
+ else
50
+ Utils.files_grep(
51
+ @changed_files,
52
+ DeltaTest.config.full_test_patterns
53
+ ).any?
54
+ end
55
+ end
56
+
57
+ ###
58
+ # Dependent spec files
59
+ #
60
+ # @return {Set|Nil}
61
+ ###
62
+ def dependents
63
+ return nil unless @changed_files && @table
64
+
65
+ return @dependents if @dependents
66
+ @dependents = Set.new
46
67
 
47
68
  @table.each do |spec_file, dependencies|
48
- related = @changed_files.include?(spec_file) \
69
+ dependent = @changed_files.include?(spec_file) \
49
70
  || (dependencies & @changed_files).any?
50
71
 
51
- spec_files << spec_file if related
72
+ @dependents << spec_file if dependent
52
73
  end
53
74
 
75
+ @dependents
76
+ end
77
+
78
+ ###
79
+ # Custom spec files
80
+ #
81
+ # @return {Set|Nil}
82
+ ###
83
+ def customs
84
+ return nil unless @changed_files
85
+
86
+ return @customs if @customs
87
+ @customs = Set.new
88
+
54
89
  DeltaTest.config.custom_mappings.each do |spec_file, patterns|
55
90
  if Utils.files_grep(@changed_files, patterns).any?
56
- spec_files << spec_file
91
+ @customs << spec_file
57
92
  end
58
93
  end
59
94
 
60
- spec_files
95
+ @customs
96
+ end
97
+
98
+ ###
99
+ # Full spec files
100
+ #
101
+ # @return {Set|Nil}
102
+ ###
103
+ def full
104
+ return nil unless @table
105
+
106
+ @full ||= Set.new(@table.keys)
107
+ end
108
+
109
+ ###
110
+ # Calculate related spec files
111
+ #
112
+ # @return {Set<String>}
113
+ ###
114
+ def related_spec_files
115
+ if full_tests?
116
+ full
117
+ else
118
+ dependents | customs
119
+ end
61
120
  end
62
121
 
63
122
  end
@@ -4,19 +4,21 @@ module DeltaTest
4
4
  module SpecHelpers
5
5
 
6
6
  ###
7
- # Setup generator and hook analyzer on contexts
7
+ # Setup generator and hook profiler on contexts
8
8
  ###
9
9
  def use_delta_test(example)
10
- $delta_test_generator ||= DeltaTest::Generator.new
11
- $delta_test_generator.setup!
10
+ generator = DeltaTest::GeneratorSingleton.instance
11
+ generator.setup!
12
12
 
13
- example.before(:context) do
14
- $delta_test_generator.start!(example.metadata[:file_path])
13
+ example.before(:all) do
14
+ generator.start!(example.file_path)
15
15
  end
16
16
 
17
- example.after(:context) do
18
- $delta_test_generator.stop!
17
+ example.after(:all) do
18
+ generator.stop!
19
19
  end
20
+
21
+ generator.hook_on_exit
20
22
  end
21
23
 
22
24
  ###
@@ -1,7 +1,7 @@
1
1
  module DeltaTest
2
2
 
3
3
  MAJOR = 0
4
- MINOR = 1
4
+ MINOR = 2
5
5
  REVISION = 0
6
6
 
7
7
  VERSION = [MAJOR, MINOR, REVISION].compact.join('.')
@@ -154,7 +154,7 @@ describe DeltaTest::CLI do
154
154
 
155
155
  end
156
156
 
157
- describe '#run_full_tests?' do
157
+ describe '#profile_mode?' do
158
158
 
159
159
  let(:map) do
160
160
  {
@@ -179,7 +179,7 @@ describe DeltaTest::CLI do
179
179
  end
180
180
 
181
181
  it 'should return true' do
182
- expect(cli.run_full_tests?).to be(true)
182
+ expect(cli.profile_mode?).to be(true)
183
183
  end
184
184
 
185
185
  end
@@ -194,7 +194,7 @@ describe DeltaTest::CLI do
194
194
  end
195
195
 
196
196
  it 'should return false' do
197
- expect(cli.run_full_tests?).to be(false)
197
+ expect(cli.profile_mode?).to be(false)
198
198
  end
199
199
 
200
200
  end
@@ -306,7 +306,7 @@ describe DeltaTest::CLI do
306
306
  context 'Full tests' do
307
307
 
308
308
  before do
309
- expect(cli).to receive(:run_full_tests?).with(no_args).and_return(true)
309
+ allow(cli).to receive(:profile_mode?).with(no_args).and_return(true)
310
310
  end
311
311
 
312
312
  it 'should run script with a flag' do
@@ -325,7 +325,7 @@ describe DeltaTest::CLI do
325
325
  context 'Partial tests' do
326
326
 
327
327
  before do
328
- expect(cli).to receive(:run_full_tests?).with(no_args).and_return(false)
328
+ allow(cli).to receive(:profile_mode?).with(no_args).and_return(false)
329
329
  end
330
330
 
331
331
  context 'Any related files' do
@@ -365,6 +365,26 @@ describe DeltaTest::CLI do
365
365
 
366
366
  end
367
367
 
368
+ describe '#do_clear' do
369
+
370
+ before do
371
+ allow(Open3).to receive(:capture3).and_return(nil)
372
+ end
373
+
374
+ let(:table_file_path) { '/path/to/table' }
375
+
376
+ it 'should remove table files' do
377
+ allow(DeltaTest.config).to receive(:table_file_path).and_return(table_file_path)
378
+ expect(DeltaTest.config).to receive(:table_file_path).with('')
379
+ expect(Open3).to receive(:capture3).with('rm %s*' % table_file_path)
380
+
381
+ expect {
382
+ cli.do_clear
383
+ }.not_to raise_error
384
+ end
385
+
386
+ end
387
+
368
388
  describe '#do_help' do
369
389
 
370
390
  it 'should print help' do
@@ -394,6 +414,7 @@ describe DeltaTest::CLI do
394
414
  'list' => 'do_list',
395
415
  'table' => 'do_table',
396
416
  'exec' => 'do_exec',
417
+ 'clear' => 'do_clear',
397
418
  'help' => 'do_help',
398
419
  '-v' => 'do_version',
399
420
  }
@@ -218,6 +218,18 @@ describe DeltaTest::Configuration do
218
218
  expect(configuration.table_file_path).to eq(Pathname.new('/somewhere/table_file'))
219
219
  end
220
220
 
221
+ context 'With part' do
222
+
223
+ it 'should return a path with a part extension' do
224
+ configuration.base_path = '/base_path'
225
+ configuration.table_file = 'somewhere/table_file'
226
+
227
+ configuration.precalculate!
228
+ expect(configuration.table_file_path(1)).to eq(Pathname.new('/base_path/somewhere/table_file.part-1'))
229
+ end
230
+
231
+ end
232
+
221
233
  end
222
234
 
223
235
  end
@@ -53,6 +53,41 @@ describe DeltaTest::DependenciesTable do
53
53
 
54
54
  end
55
55
 
56
+ describe '#reverse_merge!' do
57
+
58
+ let(:one_table) { DeltaTest::DependenciesTable.new }
59
+ let(:other_table) { DeltaTest::DependenciesTable.new }
60
+
61
+ it 'should raise an error if other is not an instance of DependenciesTable' do
62
+ expect {
63
+ one_table.reverse_merge!({})
64
+ }.to raise_error(TypeError)
65
+ end
66
+
67
+ it 'should merge other table into self' do
68
+ one_table[:foo] << 1
69
+ one_table[:foo] << 2
70
+ one_table[:bar] << 3
71
+ one_table[:one] << 4
72
+
73
+ other_table[:foo] << 10
74
+ other_table[:foo] << 20
75
+ other_table[:bar] << 30
76
+ other_table[:other] << 40
77
+
78
+ expect {
79
+ one_table.reverse_merge!(other_table)
80
+ }.not_to raise_error
81
+
82
+ expect(one_table.keys).to eq(one_table.keys | other_table.keys)
83
+ expect(one_table[:foo]).to eq(Set[1, 2, 10, 20])
84
+ expect(one_table[:bar]).to eq(Set[3, 30])
85
+ expect(one_table[:one]).to eq(Set[4])
86
+ expect(one_table[:other]).to eq(Set[40])
87
+ end
88
+
89
+ end
90
+
56
91
  describe '#without_default_proc' do
57
92
 
58
93
  it 'should reset default_proc temporary inside a block' do
@@ -31,14 +31,14 @@ describe DeltaTest::Generator do
31
31
 
32
32
  after do
33
33
  DeltaTest.active = false
34
- RubyProf.stop if RubyProf.running?
34
+ DeltaTest::Profiler.clean!
35
35
  end
36
36
 
37
37
  describe '#setup!' do
38
38
 
39
39
  it 'should setup a generator' do
40
40
  expect {
41
- generator.setup!(false) # disable tearadown
41
+ generator.setup!
42
42
  }.not_to raise_error
43
43
 
44
44
  expect(generator).to be_respond_to(:table)
@@ -50,17 +50,17 @@ describe DeltaTest::Generator do
50
50
  describe '#start!' do
51
51
 
52
52
  before do
53
- generator.setup!(false) # disable tearadown
53
+ generator.setup!
54
54
  end
55
55
 
56
- it 'should start ruby-prof' do
57
- expect(RubyProf.running?).to be(false)
56
+ it 'should start the profiler' do
57
+ expect(DeltaTest::Profiler.running?).to be(false)
58
58
 
59
59
  expect {
60
60
  generator.start!(spec_file)
61
61
  }.not_to raise_error
62
62
 
63
- expect(RubyProf.running?).to be(true)
63
+ expect(DeltaTest::Profiler.running?).to be(true)
64
64
  end
65
65
 
66
66
  describe '#current_spec_file' do
@@ -84,15 +84,15 @@ describe DeltaTest::Generator do
84
84
  describe '#stop!' do
85
85
 
86
86
  before do
87
- generator.setup!(false) # disable tearadown
87
+ generator.setup!
88
88
  end
89
89
 
90
- it 'should stop ruby-prof' do
91
- expect(RubyProf.running?).to be(false)
90
+ it 'should stop the profiler' do
91
+ expect(DeltaTest::Profiler.running?).to be(false)
92
92
  generator.start!(spec_file)
93
- expect(RubyProf.running?).to be(true)
93
+ expect(DeltaTest::Profiler.running?).to be(true)
94
94
  generator.stop!
95
- expect(RubyProf.running?).to be(false)
95
+ expect(DeltaTest::Profiler.running?).to be(false)
96
96
  end
97
97
 
98
98
  it 'should unset current_spec_file' do
@@ -108,7 +108,7 @@ describe DeltaTest::Generator do
108
108
  describe '#table' do
109
109
 
110
110
  before do
111
- generator.setup!(false) # disable tearadown
111
+ generator.setup!
112
112
  end
113
113
 
114
114
  it 'should return a set of source files' do
@@ -168,15 +168,15 @@ describe DeltaTest::Generator do
168
168
  context 'When `setup!` is called' do
169
169
 
170
170
  before do
171
- generator.setup!(false) # disable tearadown
171
+ generator.setup!
172
172
  end
173
173
 
174
- it 'should stop ruby-prof if running' do
175
- expect(RubyProf.running?).to be(false)
174
+ it 'should stop the profiler if running' do
175
+ expect(DeltaTest::Profiler.running?).to be(false)
176
176
  generator.start!(spec_file)
177
- expect(RubyProf.running?).to be(true)
177
+ expect(DeltaTest::Profiler.running?).to be(true)
178
178
  generator.teardown!
179
- expect(RubyProf.running?).to be(false)
179
+ expect(DeltaTest::Profiler.running?).to be(false)
180
180
  end
181
181
 
182
182
  it 'should save the table into a file' do
@@ -198,4 +198,21 @@ describe DeltaTest::Generator do
198
198
 
199
199
  end
200
200
 
201
+ describe '#hook_on_exit' do
202
+
203
+ before do
204
+ allow(Kernel).to receive(:at_exit).and_return(nil)
205
+ end
206
+
207
+ it 'should call at_exit' do
208
+ # FIXME
209
+ # expect(Kernel).to receive(:at_exit)
210
+
211
+ expect {
212
+ generator.hook_on_exit
213
+ }.not_to raise_error
214
+ end
215
+
216
+ end
217
+
201
218
  end
@@ -0,0 +1,121 @@
1
+ require 'delta_test/profiler'
2
+
3
+ describe DeltaTest::Profiler do
4
+
5
+ after do
6
+ DeltaTest::Profiler.clean!
7
+ end
8
+
9
+ describe '::start!' do
10
+
11
+ it 'should start profiler' do
12
+ expect(DeltaTest::Profiler.running?).to be(false)
13
+
14
+ expect {
15
+ DeltaTest::Profiler.start!
16
+ }.not_to raise_error
17
+
18
+ expect(DeltaTest::Profiler.running?).to be(true)
19
+ end
20
+
21
+ end
22
+
23
+ describe '::stop!' do
24
+
25
+ it 'should not raise error if the profiler not yet started' do
26
+ expect {
27
+ DeltaTest::Profiler.stop!
28
+ }.not_to raise_error
29
+ end
30
+
31
+ it 'should set result' do
32
+ expect(DeltaTest::Profiler.last_result).to be_a(Array)
33
+
34
+ DeltaTest::Profiler.start!
35
+
36
+ expect {
37
+ DeltaTest::Profiler.stop!
38
+ }.not_to raise_error
39
+
40
+ expect(DeltaTest::Profiler.last_result).to be_a(Array)
41
+ expect(DeltaTest::Profiler.last_result).not_to be_empty
42
+ end
43
+
44
+ end
45
+
46
+ describe '::last_result' do
47
+
48
+ it 'should retrun an array if not yet started' do
49
+ files = DeltaTest::Profiler.last_result
50
+ expect(files).to be_a(Array)
51
+ end
52
+
53
+ it 'should return nil if running' do
54
+ DeltaTest::Profiler.start!
55
+
56
+ files = DeltaTest::Profiler.last_result
57
+ expect(files).to be_nil
58
+ end
59
+
60
+ it 'should return an array of source files' do
61
+ DeltaTest::Profiler.start!
62
+ DeltaTest::Profiler.stop!
63
+ files = DeltaTest::Profiler.last_result
64
+ expect(files).to be_a(Array)
65
+ end
66
+
67
+ describe 'Source files' do
68
+
69
+ context 'Instantiated class in a file' do
70
+
71
+ it 'should not include the file' do
72
+ DeltaTest::Profiler.start!
73
+ Sample::Alpha.new
74
+ DeltaTest::Profiler.stop!
75
+ expect(DeltaTest::Profiler.last_result).not_to include(fixture_path('sample/alpha.rb'))
76
+ expect(DeltaTest::Profiler.last_result).not_to include(fixture_path('sample/beta.rb'))
77
+ expect(DeltaTest::Profiler.last_result).not_to include(fixture_path('sample/gamma.rb'))
78
+ end
79
+
80
+ end
81
+
82
+ context 'Called some instance methods of a class in the file' do
83
+
84
+ it 'should include the file' do
85
+ DeltaTest::Profiler.start!
86
+ Sample::Alpha.new.alpha
87
+ DeltaTest::Profiler.stop!
88
+ expect(DeltaTest::Profiler.last_result).to include(fixture_path('sample/alpha.rb'))
89
+ expect(DeltaTest::Profiler.last_result).not_to include(fixture_path('sample/beta.rb'))
90
+ expect(DeltaTest::Profiler.last_result).not_to include(fixture_path('sample/gamma.rb'))
91
+ end
92
+
93
+ end
94
+
95
+ context 'Called methods that uses extarnal classes' do
96
+
97
+ it 'should include a extarnal file' do
98
+ DeltaTest::Profiler.start!
99
+ Sample::Alpha.new.beta
100
+ DeltaTest::Profiler.stop!
101
+ expect(DeltaTest::Profiler.last_result).to include(fixture_path('sample/alpha.rb'))
102
+ expect(DeltaTest::Profiler.last_result).to include(fixture_path('sample/beta.rb'))
103
+ expect(DeltaTest::Profiler.last_result).not_to include(fixture_path('sample/gamma.rb'))
104
+ end
105
+
106
+ it 'should include extarnal files even if nested' do
107
+ DeltaTest::Profiler.start!
108
+ Sample::Alpha.new.beta_gamma
109
+ DeltaTest::Profiler.stop!
110
+ expect(DeltaTest::Profiler.last_result).to include(fixture_path('sample/alpha.rb'))
111
+ expect(DeltaTest::Profiler.last_result).to include(fixture_path('sample/beta.rb'))
112
+ expect(DeltaTest::Profiler.last_result).to include(fixture_path('sample/gamma.rb'))
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+
121
+ end