Class: TTNT::TestSelector
- Inherits:
-
Object
- Object
- TTNT::TestSelector
- Defined in:
- lib/ttnt/test_selector.rb
Overview
Select tests using git information and TestToCodeMapping.
Instance Attribute Summary collapse
-
#tests ⇒ Object
readonly
Returns the value of attribute tests.
Instance Method Summary collapse
-
#find_anchored_commit ⇒ Object
private
Find the commit ‘rake ttnt:test:anchor` has been run on.
-
#initialize(repo, target_sha, test_files) ⇒ TestSelector
constructor
A new instance of TestSelector.
- #mapping ⇒ Object private
-
#select_tests! ⇒ Set
Select tests using differences in anchored commit and target commit (or current working tree) and TestToCodeMapping.
-
#select_tests_from_patch(patch) ⇒ Set
private
Select tests which are affected by the change of given patch.
-
#test_file?(filename) ⇒ Boolean
private
Check if given file is a test file.
Constructor Details
#initialize(repo, target_sha, test_files) ⇒ TestSelector
Returns a new instance of TestSelector.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ttnt/test_selector.rb', line 16 def initialize(repo, target_sha, test_files) @repo = repo = MetaData.new(repo, target_sha) @target_obj = @repo.lookup(target_sha) if target_sha # Base should be the commit `ttnt:anchor` has run on. # NOT the one test-to-code mapping was commited to. @base_obj = find_anchored_commit @test_files = test_files end |
Instance Attribute Details
#tests ⇒ Object (readonly)
Returns the value of attribute tests.
10 11 12 |
# File 'lib/ttnt/test_selector.rb', line 10 def tests @tests end |
Instance Method Details
#find_anchored_commit ⇒ Object (private)
Find the commit ‘rake ttnt:test:anchor` has been run on.
85 86 87 |
# File 'lib/ttnt/test_selector.rb', line 85 def find_anchored_commit @repo.lookup(['anchored_commit']) end |
#mapping ⇒ Object (private)
49 50 51 52 |
# File 'lib/ttnt/test_selector.rb', line 49 def mapping sha = @target_obj ? @target_obj.oid : @repo.head.target_id @mapping ||= TTNT::TestToCodeMapping.new(@repo, sha) end |
#select_tests! ⇒ Set
Select tests using differences in anchored commit and target commit (or current working tree) and TTNT::TestToCodeMapping.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ttnt/test_selector.rb', line 32 def select_tests! # TODO: if test-to-code-mapping is not found (ttnt-anchor has not been run) @tests ||= Set.new diff = @target_obj ? @base_obj.diff(@target_obj) : @base_obj.diff_workdir diff.each_patch do |patch| file = patch.delta.old_file[:path] if test_file?(file) @tests << file else select_tests_from_patch(patch) end end @tests.delete(nil) end |
#select_tests_from_patch(patch) ⇒ Set (private)
Select tests which are affected by the change of given patch.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ttnt/test_selector.rb', line 58 def select_tests_from_patch(patch) target_lines = Set.new file = patch.delta.old_file[:path] prev_line = nil patch.each_hunk do |hunk| hunk.each_line do |line| case line.line_origin when :addition if prev_line && !prev_line.addition? target_lines << prev_line.old_lineno elsif prev_line.nil? target_lines << hunk.old_start end when :deletion target_lines << line.old_lineno end prev_line = line end end target_lines.each do |line| @tests += mapping.get_tests(file: file, lineno: line) end end |
#test_file?(filename) ⇒ Boolean (private)
Check if given file is a test file.
92 93 94 |
# File 'lib/ttnt/test_selector.rb', line 92 def test_file?(filename) @test_files.include?(filename) end |