what_to_run 0.0.0 → 0.0.1.pre

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8627bc44ac70f2e2911589b7a59e1c7bfac33bac
4
- data.tar.gz: 26b2e777319a8d6ebcac6e7552aa95a23b8ce94d
3
+ metadata.gz: 6fd560629ae7b8791f59fe5cfb47d9049febbe04
4
+ data.tar.gz: 58c24411992a29a737103d1072e62594e311b0eb
5
5
  SHA512:
6
- metadata.gz: af570365e27f5a2c07e9debf012e7d2962e8c44849ae3fd10a8308de46b30aff32578d4c9fcef809aaf725d9da2aa36e195e0745aa172d3b875b336ebed3552e
7
- data.tar.gz: f338f32e7886d66b61f2f816e4176287d2944ba4a7a107e57aad97705f9df3401307837790be6945c9979f82339f46fc4329902b9b4496c97bac3eb6770c8dcc
6
+ metadata.gz: 3861db56557e5f50e273a43caf790aa19163068b69a0e27f9cdc7ef8bf79189a3f9878017b3eda49c1aa9241778395e86e7e068fc622446f3c5c127de90b1159
7
+ data.tar.gz: 87d098e0915aad867239ebf93280d44d1487dab7b288294fbcc46fb05fb2908d48e6dba83c8481a1199b2c60628a4d51978e2a021bba98735d4f90b3d7b64c7d
data/README.md CHANGED
@@ -1,67 +1,77 @@
1
- # Regression Test Selection
1
+ # What To Run
2
2
 
3
- This is a demo repository for regression test selection. You will need Ruby 2.3 to use this (which is currently trunk Ruby).
3
+ What To Run is a lib for regression test selection, use it to predict which tests you should run when you make any modification on your codebase.
4
4
 
5
- ## Make it go with Minitest
5
+ This lib is based on [@tenderlove](https://github.com/tenderlove) idea and guidance, make sure to read his [blog post](http://tenderlovemaking.com/2015/02/13/predicting-test-failues.html) on the subject.
6
6
 
7
- To try it with Minitest, do:
7
+ ## Requirements
8
+
9
+ - Project must be inside a Git repository
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
8
14
 
9
15
  ```
10
- $ COLLECTION=1 ruby -I lib spec/whatever_test.rb
16
+ gem 'what_to_run'
11
17
  ```
12
18
 
13
- This will create the initial coverage information. Then modify `lib/my_thing.rb` so that the diff looks like this:
19
+ And then execute
14
20
 
15
- ```patch
16
- diff --git a/lib/my_thing.rb b/lib/my_thing.rb
17
- index 806deff..eb057b9 100644
18
- --- a/lib/my_thing.rb
19
- +++ b/lib/my_thing.rb
20
- @@ -4,7 +4,7 @@ class Whatever
21
- end
22
-
23
- def bar
24
- - "bar #{@foo}"
25
- + raise
26
- end
27
-
28
- def baz
21
+ ```
22
+ $ bundle
29
23
  ```
30
24
 
31
- Now to predict which tests will fail, do this:
25
+ Or install it yourself as:
32
26
 
33
27
  ```
34
- $ ruby what_to_run.rb
28
+ gem install what_to_run
35
29
  ```
36
30
 
37
- ## Make it go with RSpec
31
+ ## Usage
32
+
33
+ Require the lib with:
38
34
 
39
- To try it with RSpec, do:
35
+ Minitest
40
36
 
41
37
  ```
42
- $ COLLECTION=1 rspec spec/whatever_spec.rb
38
+ require 'what_to_run/minitest'
43
39
  ```
44
40
 
45
- This will create the initial coverage information. Then modify `lib/my_thing.rb` so that the diff looks like this:
41
+ RSpec
46
42
 
47
- ```patch
48
- diff --git a/lib/my_thing.rb b/lib/my_thing.rb
49
- index 806deff..eb057b9 100644
50
- --- a/lib/my_thing.rb
51
- +++ b/lib/my_thing.rb
52
- @@ -4,7 +4,7 @@ class Whatever
53
- end
54
-
55
- def bar
56
- - "bar #{@foo}"
57
- + raise
58
- end
59
-
60
- def baz
43
+ ```
44
+ require 'what_to_run/rspec'
45
+ ```
46
+
47
+ Run your tests on a clean git branch
48
+
49
+ Minitest
50
+
51
+ ```
52
+ $ COLLECTION=1 bundle exec rake test
61
53
  ```
62
54
 
63
- Now to predict which tests will fail, do this:
55
+ RSpec
64
56
 
65
57
  ```
66
- $ ruby what_to_run.rb
58
+ $ COLLECTION=1 bundle exec rspec
67
59
  ```
60
+
61
+ This will create the initial coverage information. Then make your desired modifications on your code.
62
+
63
+ Now to predict which tests is likely fail, run this:
64
+
65
+ ```
66
+ $ what_to_run
67
+ ```
68
+
69
+ :warning: A `run_log.json` file will be created in the current directory, you might want to include it in your `.gitignore`.
70
+
71
+ ## Contributing
72
+
73
+ Open an [issue](https://github.com/DyegoCosta/what_to_run/issues) or fork it and submit a [pull-request](https://help.github.com/articles/using-pull-requests/).
74
+
75
+ ## License
76
+
77
+ What To Run is released under the [MIT License](http://www.opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('coverage_peeker/coverage_peeker')
@@ -0,0 +1,5 @@
1
+ require 'coverage_peeker/coverage_peeker'
2
+
3
+ module CoveragePeeker
4
+ VERSION = '1.0.0'
5
+ end
data/lib/what_to_run.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'json'
2
- require 'shellwords'
3
2
  require 'rugged'
4
3
  require 'set'
5
4
 
@@ -8,22 +7,20 @@ module WhatToRun
8
7
 
9
8
  def predict
10
9
  lines_to_run.inject([]) do |tests, (file, line)|
11
- cov_map[File.expand_path(file)][line].each do |desc|
12
- tests += Array(desc)
13
- end
14
- tests
10
+ path = File.expand_path(file)
11
+ tests += Array cov_map[path][line]
15
12
  end
16
13
  end
17
14
 
18
15
  def lines_to_run
19
- repo = Rugged::Repository.new '.'
16
+ repo = Rugged::Repository.new('.')
20
17
  lines_to_run = Set.new
21
18
 
22
- repo.index.diff.each_patch { |patch|
19
+ repo.index.diff.each_patch do |patch|
23
20
  file = patch.delta.old_file[:path]
24
21
 
25
- patch.each_hunk { |hunk|
26
- hunk.each_line { |line|
22
+ patch.each_hunk do |hunk|
23
+ hunk.each_line do |line|
27
24
  case line.line_origin
28
25
  when :addition
29
26
  lines_to_run << [file, line.new_lineno]
@@ -32,31 +29,25 @@ module WhatToRun
32
29
  when :context
33
30
  # do nothing
34
31
  end
35
- }
36
- }
37
- }
32
+ end
33
+ end
34
+ end
38
35
 
39
36
  lines_to_run
40
37
  end
41
38
 
42
- def diff before, after
43
- after.each_with_object({}) do |(file_name,line_cov), res|
44
- before_line_cov = before[file_name]
39
+ def cov_delta(before, after)
40
+ after.each_with_object({}) do |(file_name, lines_cov), delta|
41
+ before_lines_cov = before[file_name]
45
42
 
46
43
  # skip arrays that are exactly the same
47
- next if before_line_cov == line_cov
44
+ next if before_lines_cov == lines_cov
48
45
 
49
46
  # subtract the old coverage from the new coverage
50
- cov = line_cov.zip(before_line_cov).map do |line_after, line_before|
51
- if line_after
52
- line_after - line_before
53
- else
54
- line_after
55
- end
56
- end
47
+ cov = lines_cov_delta(before_lines_cov, lines_cov)
57
48
 
58
49
  # add the "diffed" coverage to the hash
59
- res[file_name] = cov
50
+ delta[file_name] = cov
60
51
  end
61
52
  end
62
53
 
@@ -64,29 +55,17 @@ module WhatToRun
64
55
  cov_map = Hash.new { |h, file| h[file] = Hash.new { |i, line| i[line] = [] } }
65
56
 
66
57
  File.open('run_log.json') do |f|
67
- # Read in the coverage info
68
- JSON.parse(f.read).each do |args|
69
- if args.length == 4 # for Minitest
70
- desc = args.first(2).join('#')
71
- else # for RSpec
72
- desc = args.first
73
- end
74
-
75
- before, after = args.last(2)
58
+ JSON.parse(f.read).each do |cov_info|
59
+ before, after = cov_info.last(2)
60
+ desc = build_test_desc(cov_info)
76
61
 
77
- # calculate the per test coverage
78
- delta = diff before, after
62
+ delta = cov_delta(before, after)
79
63
 
80
64
  delta.each_pair do |file, lines|
81
65
  file_map = cov_map[file]
82
66
 
83
67
  lines.each_with_index do |val, i|
84
- # skip lines that weren't executed
85
- next unless val && val > 0
86
-
87
- # add the test name to the map. Multiple tests can execute the same
88
- # line, so we need to use an array.
89
- file_map[i + 1] << desc
68
+ file_map[i + 1] << desc if line_executed?(val)
90
69
  end
91
70
  end
92
71
  end
@@ -94,4 +73,19 @@ module WhatToRun
94
73
 
95
74
  cov_map
96
75
  end
76
+
77
+ def build_test_desc(cov_info)
78
+ using_minitest = cov_info.length == 4
79
+ using_minitest ? cov_info.first(2).join('#') : cov_info.first
80
+ end
81
+
82
+ def line_executed?(line)
83
+ line.to_i > 0
84
+ end
85
+
86
+ def lines_cov_delta(before_lines_cov, after_lines_cov)
87
+ after_lines_cov.zip(before_lines_cov).map do |lines_after, lines_before|
88
+ lines_after ? lines_after - lines_before : lines_after
89
+ end
90
+ end
97
91
  end
@@ -1,5 +1,6 @@
1
- require 'coverage'
2
1
  require 'json'
2
+ require 'coverage'
3
+ require 'coverage_peeker'
3
4
 
4
5
  Coverage.start
5
6
 
@@ -16,9 +17,9 @@ class Minitest::Runnable
16
17
  alias :old_run_one_method :run_one_method
17
18
 
18
19
  def run_one_method klass, method_name, reporter
19
- before = Coverage.peek_result
20
+ before = CoveragePeeker.peek_result
20
21
  old_run_one_method klass, method_name, reporter
21
- after = Coverage.peek_result
22
+ after = CoveragePeeker.peek_result
22
23
  LOGS << [ klass.name, method_name.to_s, before, after ]
23
24
  end
24
25
  end
@@ -1,7 +1,9 @@
1
- require 'coverage'
2
1
  require 'json'
3
2
  require 'rspec'
4
3
 
4
+ require 'coverage'
5
+ require 'coverage_peeker'
6
+
5
7
  LOGS = []
6
8
  Coverage.start
7
9
 
@@ -10,8 +12,8 @@ RSpec.configuration.after(:suite) {
10
12
  }
11
13
 
12
14
  RSpec.configuration.around(:example) do |example|
13
- before = Coverage.peek_result
15
+ before = CoveragePeeker.peek_result
14
16
  example.call
15
- after = Coverage.peek_result
17
+ after = CoveragePeeker.peek_result
16
18
  LOGS << [ example.full_description, before, after ]
17
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: what_to_run
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson
@@ -35,11 +35,15 @@ description: Predict which tests are likely to fail after you’ve changed the c
35
35
  email: dyego@dyegocosta.com
36
36
  executables:
37
37
  - what_to_run
38
- extensions: []
38
+ extensions:
39
+ - ext/coverage_peeker/extconf.rb
39
40
  extra_rdoc_files: []
40
41
  files:
41
42
  - README.md
42
43
  - bin/what_to_run
44
+ - ext/coverage_peeker/extconf.rb
45
+ - lib/coverage_peeker.rb
46
+ - lib/coverage_peeker/coverage_peeker.bundle
43
47
  - lib/what_to_run.rb
44
48
  - lib/what_to_run/minitest.rb
45
49
  - lib/what_to_run/rspec.rb
@@ -53,17 +57,17 @@ require_paths:
53
57
  - lib
54
58
  required_ruby_version: !ruby/object:Gem::Requirement
55
59
  requirements:
56
- - - ">="
60
+ - - "~>"
57
61
  - !ruby/object:Gem::Version
58
- version: '0'
62
+ version: '2.3'
59
63
  required_rubygems_version: !ruby/object:Gem::Requirement
60
64
  requirements:
61
- - - ">="
65
+ - - ">"
62
66
  - !ruby/object:Gem::Version
63
- version: '0'
67
+ version: 1.3.1
64
68
  requirements: []
65
69
  rubyforge_project:
66
- rubygems_version: 2.4.6
70
+ rubygems_version: 2.2.2
67
71
  signing_key:
68
72
  specification_version: 4
69
73
  summary: Regression test selection