test_diff 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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MWVkMDZlMTVhZWIwMDYzMjk1ZTM1MTc4N2ZlMDQwZjFlZWM4Mzc3Nw==
4
+ MDg5NWQ4MmE3YzUzOGNlZDIwYjdkYTI3YzczNjJhM2VhNmE4NWM2MA==
5
5
  data.tar.gz: !binary |-
6
- ZTAzOGEwOTNkNTRjZGYwM2Q4YTZhODdjN2I4ZGQ1Y2QyZDJkZThkOA==
6
+ MzJjODhhYjg5MmQ0NzNlMzcwNjA1NDE2MWVmYTY1NWE4ZTVmODNhNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZTEyNzk0OTBkZjRkNGIxM2ExOTM2YzRlZmZlZDZlY2NmZmIzNzIyOTFlZmEw
10
- ODUxZTdjYzhlOGJjZGQ2ZDNmZWE3Y2VmNjI5ZjViZmExZWQ1OTM1OGQ5YjI4
11
- OTljNjRmZWVkN2FjYTVlMWEzZGNjNjcyODlkODMyZGE4MjNjMGY=
9
+ NGI0MjdlYjVhZWM2NjQ4ZTk5MmI4OGRiMzllOWY5Y2RhZGQ0YzFhY2UzYmMy
10
+ OTU0MzA3OWZjNzk3NWUwOGY1MjE3ZTYxYmMyMmQwNDU4YjcyMTE0OTI0ZWUz
11
+ OGVhOGQ4ZTBlZjE4OGEzZmQ5MzJmYWNjMTdiNmRmNjVjOTA1ZjQ=
12
12
  data.tar.gz: !binary |-
13
- NGFhYzA4MjU2MmZkZjAyZGFlYWUwZDNlYTY0YmRhNjMwNzExNGRjZmU0Y2M3
14
- NTlmOWE1MzMyYjM3NjM0OGZiNTQ0NWEyMzg2NDRhNTdiMDFmOGJiMzRlN2I4
15
- NWMxZmNiNjI4MzgyOTg3OWY4NGU5ZDU3MzBlMjMwZDdjN2VkYWQ=
13
+ OTM0M2EzZmZjZjVlOTk1NTI1NTlkNTdmODA0MjlkYjQyNTQ1MDgxYTI0ODRk
14
+ OTA5ZmMyNTY0NGYyNmE4Mjk0MGY3NzM1ZWEzODJlZTc5YmExYzBmMWY2NzVm
15
+ MzlmYWNiYzNhODU4MTQwZTBhNWRjMzZjNjg3OGZmNGIwYzE5NTQ=
data/exe/test_diff CHANGED
@@ -20,6 +20,11 @@ class TestDiffBuilder < Thor
20
20
  def run_spec_diff(spec_folder = 'spec', groups_of = nil, group = '0')
21
21
  TestDiff::RunDiff.new(spec_folder, groups_of, group).run
22
22
  end
23
+
24
+ desc 'build_coverage_diff spec master', 'runs the specs difference between the branches'
25
+ def build_coverage_diff(spec_folder = 'spec', pre_load = nil, continue = nil)
26
+ TestDiff::BuildCoverageDiff.new(spec_folder, pre_load, continue).run
27
+ end
23
28
  end
24
29
 
25
30
  TestDiffBuilder.start(ARGV)
@@ -0,0 +1,152 @@
1
+ # TestDiff module
2
+ module TestDiff
3
+ # class used to build the coverage file
4
+ class BuildCoverageDiff
5
+ attr_reader :spec_folder, :pre_load, :sha1
6
+
7
+ def initialize(spec_folder, pre_load, continue)
8
+ @spec_folder = spec_folder
9
+ @sha1 = File.read('test_diff_coverage/sha')
10
+ @specs_to_run = []
11
+ @storage = Storage.new
12
+ @pre_load = pre_load
13
+ @continue = continue
14
+ end
15
+
16
+ def run
17
+ add_changed_files
18
+ remove_tests_that_do_not_exist
19
+ remove_tests_in_wrong_folder
20
+ require 'coverage.so'
21
+ Coverage.start
22
+ require_pre_load
23
+ run_batch
24
+ end
25
+
26
+ private
27
+
28
+ def require_pre_load
29
+ return unless pre_load
30
+ puts "pre_loading #{pre_load}"
31
+ require File.expand_path(pre_load)
32
+ end
33
+
34
+ def run_batch
35
+ puts "Running #{@specs_to_run.size} tests"
36
+ require 'spec'
37
+ timing_thread = start_timing_thread(Time.now, @specs_to_run.size)
38
+ start
39
+ timing_thread.join
40
+ puts 'Test done, compacting db'
41
+ @storage.compact if @storage.respond_to?(:compact)
42
+ end
43
+
44
+ def start_timing_thread(start_time, original_size)
45
+ Thread.new do
46
+ until @specs_to_run.empty?
47
+ last_size = @specs_to_run.size
48
+ completed = original_size - last_size
49
+ if completed > 0
50
+ time_per_spec = (Time.now - start_time).to_f / completed.to_f
51
+ est_time_left = time_per_spec * last_size
52
+ puts "specs left #{last_size}, est time_left: #{est_time_left.to_i}"
53
+ end
54
+ sleep(60)
55
+ end
56
+ end
57
+ end
58
+
59
+ def start
60
+ until @specs_to_run.empty?
61
+ pid = start_process_fork(@specs_to_run.pop)
62
+ pid, status = Process.waitpid2(pid)
63
+ fail 'Test Failed' unless status.success?
64
+ end
65
+ Coverage.result # disable coverage
66
+ end
67
+
68
+ def start_process_fork(main_spec_file)
69
+ Process.fork do
70
+ puts "running #{main_spec_file}"
71
+ ActiveRecord::Base.connection.reconnect! if defined?(ActiveRecord::Base)
72
+ Time.zone_default = (Time.zone = 'UTC') if Time.respond_to?(:zone_default) && Time.zone_default.nil?
73
+ # ARGV = ['-b',main_spec_file]
74
+ if run_tests(main_spec_file)
75
+ save_coverage_data(main_spec_file)
76
+ else
77
+ Coverage.result # disable coverage
78
+ exit!(false) unless @continue
79
+ end
80
+ # ::Spec::Runner::CommandLine.run(options)
81
+ end
82
+ end
83
+
84
+ def run_tests(main_spec_file)
85
+ options ||= begin
86
+ parser = ::Spec::Runner::OptionParser.new($stderr, $stdout)
87
+ parser.order!(['-b', main_spec_file])
88
+ parser.options
89
+ end
90
+ Spec::Runner.use options
91
+ options.run_examples
92
+ end
93
+
94
+ def save_coverage_data(main_spec_file)
95
+ data = {}
96
+ Coverage.result.each do |file_name, stats|
97
+ relative_file_name = file_name.gsub("#{FileUtils.pwd}/", '')
98
+ if file_name.include?(FileUtils.pwd)
99
+ data[relative_file_name] = stats.join(',')
100
+ end
101
+ end
102
+ YAML::ENGINE.yamler = 'psych'
103
+ @storage.set(main_spec_file, data)
104
+ @storage.flush if @storage.respond_to?(:flush)
105
+ end
106
+
107
+ def remove_tests_that_do_not_exist
108
+ @specs_to_run.delete_if do |s|
109
+ !File.exist?(s)
110
+ end
111
+ end
112
+
113
+ def remove_tests_in_wrong_folder
114
+ @specs_to_run.delete_if do |s|
115
+ !s.start_with?("#{spec_folder}/")
116
+ end
117
+ end
118
+
119
+ def add_changed_files
120
+ _build_specs_to_run
121
+
122
+ @specs_to_run.flatten!
123
+ @specs_to_run.sort!
124
+ @specs_to_run.uniq!
125
+ end
126
+
127
+ def _build_specs_to_run
128
+ files = []
129
+ `git diff --name-only #{sha1} HEAD`.split("\n").each do |file_name|
130
+ if file_name.end_with?('spec.rb') || file_name.end_with?('test.rb')
131
+ @specs_to_run << file_name
132
+ elsif !file_name.start_with?(@storage.folder)
133
+ files << file_name
134
+ _add_rails_view_spec(file_name)
135
+ end
136
+ end
137
+ _add_calculated_tests(files)
138
+ end
139
+
140
+ def _add_calculated_tests(files)
141
+ @specs_to_run << @storage.find_for(files, spec_folder)
142
+ end
143
+
144
+ def _add_rails_view_spec(file_name)
145
+ # try and find a matching view spec
146
+ return unless file_name.include?('app/views')
147
+ view_spec_name = file_name.gsub('app/views', "#{spec_folder}/views").gsub('.erb', '.erb_spec.rb')
148
+ return unless File.exist?(view_spec_name)
149
+ @specs_to_run << view_spec_name
150
+ end
151
+ end
152
+ end
@@ -35,7 +35,7 @@ module TestDiff
35
35
  def select_test_group
36
36
  return unless groups_of
37
37
  groups_size = (@specs_to_run.length / groups_of.to_f).ceil
38
- @specs_to_run = @specs_to_run.slice(group.to_i * groups_size, groups_size)
38
+ @specs_to_run = @specs_to_run.slice(group.to_i * groups_size, groups_size) || []
39
39
  end
40
40
 
41
41
  def remove_tests_that_do_not_exist
@@ -1,4 +1,4 @@
1
1
  # version number
2
2
  module TestDiff
3
- VERSION = '0.1.2'
3
+ VERSION = '0.2.0'
4
4
  end
data/lib/test_diff.rb CHANGED
@@ -2,6 +2,7 @@ require 'test_diff/version'
2
2
  require 'test_diff/storage'
3
3
  require 'test_diff/build_coverage'
4
4
  require 'test_diff/run_diff'
5
+ require 'test_diff/build_coverage_diff'
5
6
  require 'test_diff/track_build'
6
7
 
7
8
  # main module
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test_diff
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
  - Grant Speelman
@@ -130,6 +130,7 @@ files:
130
130
  - exe/test_diff
131
131
  - lib/test_diff.rb
132
132
  - lib/test_diff/build_coverage.rb
133
+ - lib/test_diff/build_coverage_diff.rb
133
134
  - lib/test_diff/run_diff.rb
134
135
  - lib/test_diff/storage.rb
135
136
  - lib/test_diff/track_build.rb