path_coverage 0.0.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.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +330 -84
  3. data/exe/path_coverage_collector +12 -1
  4. data/lib/path_coverage/calls.rb +106 -0
  5. data/lib/path_coverage/collector.rb +88 -22
  6. data/lib/path_coverage/condition.rb +77 -3
  7. data/lib/path_coverage/configuration.rb +52 -12
  8. data/lib/path_coverage/configuration_paths.rb +56 -0
  9. data/lib/path_coverage/console_summary.rb +77 -0
  10. data/lib/path_coverage/gaps.rb +25 -9
  11. data/lib/path_coverage/html/index.html +3 -14
  12. data/lib/path_coverage/html/side.js +36 -6
  13. data/lib/path_coverage/html/sort.js +27 -21
  14. data/lib/path_coverage/html/style.css +32 -1
  15. data/lib/path_coverage/html/tabs.js +15 -0
  16. data/lib/path_coverage/html_index.rb +105 -19
  17. data/lib/path_coverage/html_index_head.rb +49 -0
  18. data/lib/path_coverage/html_report.rb +44 -12
  19. data/lib/path_coverage/html_tabs.rb +26 -0
  20. data/lib/path_coverage/html_tree.rb +47 -17
  21. data/lib/path_coverage/json_reader.rb +94 -0
  22. data/lib/path_coverage/json_writer.rb +44 -8
  23. data/lib/path_coverage/kernel.rb +53 -45
  24. data/lib/path_coverage/kinds/base.rb +71 -0
  25. data/lib/path_coverage/kinds/block.rb +136 -0
  26. data/lib/path_coverage/kinds/case.rb +89 -0
  27. data/lib/path_coverage/kinds/chain.rb +45 -0
  28. data/lib/path_coverage/kinds/choice.rb +68 -0
  29. data/lib/path_coverage/kinds/contains.rb +111 -0
  30. data/lib/path_coverage/kinds/safe.rb +151 -0
  31. data/lib/path_coverage/kinds/write.rb +54 -0
  32. data/lib/path_coverage/kinds.rb +37 -0
  33. data/lib/path_coverage/manifest.rb +93 -26
  34. data/lib/path_coverage/manifest_blocks.rb +96 -0
  35. data/lib/path_coverage/manifest_case.rb +54 -0
  36. data/lib/path_coverage/manifest_chains.rb +76 -0
  37. data/lib/path_coverage/manifest_choices.rb +64 -32
  38. data/lib/path_coverage/manifest_folding.rb +91 -0
  39. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  40. data/lib/path_coverage/manifest_writes.rb +67 -0
  41. data/lib/path_coverage/merge.rb +19 -3
  42. data/lib/path_coverage/post.rb +8 -8
  43. data/lib/path_coverage/recorder.rb +43 -0
  44. data/lib/path_coverage/recorder_blocks.rb +68 -0
  45. data/lib/path_coverage/recorder_cases.rb +23 -0
  46. data/lib/path_coverage/recorder_chains.rb +40 -0
  47. data/lib/path_coverage/recorder_writes.rb +26 -0
  48. data/lib/path_coverage/render.rb +58 -46
  49. data/lib/path_coverage/result.rb +19 -0
  50. data/lib/path_coverage/rewriter.rb +64 -37
  51. data/lib/path_coverage/rewriter_nested.rb +62 -0
  52. data/lib/path_coverage/site.rb +6 -1
  53. data/lib/path_coverage/site_paths.rb +9 -46
  54. data/lib/path_coverage/site_tree.rb +31 -36
  55. data/lib/path_coverage/summary.rb +64 -17
  56. data/lib/path_coverage/totals.rb +29 -0
  57. data/lib/path_coverage/version.rb +1 -1
  58. metadata +35 -7
@@ -0,0 +1,96 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the manifest's visitor that builds the site of a
5
+ # call to a block method, one that calls its block an item at
6
+ # a time, until it decides, eg any?, or for every item, eg
7
+ # select: its leaves are its receiver, whose items the block
8
+ # is called with, and its block's one expression, whose
9
+ # answers decide. It relies on its includer's extent_of and
10
+ # @notes.
11
+ module ManifestBlocks
12
+ # True for a call whose block's every call can be seen: to a
13
+ # block method, on a receiver, with no arguments, which can
14
+ # make it ignore its block, not by &., which skips it for
15
+ # nil, and with a block of one expression that is a value,
16
+ # so can be wrapped: not break, next or return.
17
+ def block_site?(node)
18
+ return false unless BLOCK_PATHS.key?(node.name.to_s)
19
+
20
+ plain_call?(node) && value?(block_expression(node.block))
21
+ end
22
+
23
+ # True for a call on a receiver, with no arguments, and not
24
+ # by &.
25
+ def plain_call?(node)
26
+ !node.receiver.nil? && node.arguments.nil? &&
27
+ !node.safe_navigation?
28
+ end
29
+
30
+ # True for a block's one expression that is a value, so can
31
+ # be wrapped: nil, for no one expression, is not, nor is a
32
+ # jump.
33
+ def value?(body)
34
+ !body.nil? && !JUMPS.include?(body.type)
35
+ end
36
+
37
+ # The expressions that leave a block, so are no value.
38
+ JUMPS = %i[break_node next_node return_node].freeze
39
+
40
+ # The one expression of block, nil for no block, a block
41
+ # passed as an argument, or one of other than one expression.
42
+ def block_expression(block)
43
+ return nil unless block&.type == :block_node
44
+
45
+ statements = block.body
46
+ return nil unless statements&.type == :statements_node
47
+
48
+ statements.body.size == 1 ? statements.body.first : nil
49
+ end
50
+
51
+ # A block method's call as a Condition, named by its method,
52
+ # its extent the whole call. Its block's expression is noted
53
+ # among the commands when it is no bare argument.
54
+ def block_condition(node)
55
+ node.receiver.accept(self)
56
+ body = block_body(node.block)
57
+ Condition.new(
58
+ operator: node.name.to_s,
59
+ operands: [extent_of(node.receiver), body],
60
+ extent: extent_of(node)
61
+ )
62
+ end
63
+
64
+ # The byte range of block's one expression, any site in it
65
+ # visited as one of its own, as the block runs once per item;
66
+ # noted among the commands when it is no bare argument.
67
+ def block_body(block)
68
+ body = block_expression(block)
69
+ scoped { body.accept(self) }
70
+ range = extent_of(body)
71
+ @notes[:commands] << range if bare_less?(body)
72
+ range
73
+ end
74
+
75
+ # True for an expression that is no bare argument: a command
76
+ # call, an and or an or, or a modifier, eg x unless y.
77
+ def bare_less?(node)
78
+ return true if Manifest.command?(node) || modifier?(node)
79
+
80
+ %i[and_node or_node].include?(node.type) &&
81
+ %w[and or].include?(node.operator)
82
+ end
83
+
84
+ # True for a modifier if, unless or rescue, eg x if y: one
85
+ # with no end keyword, a ternary being no modifier.
86
+ def modifier?(node)
87
+ case node.type
88
+ when :rescue_modifier_node then true
89
+ when :unless_node then node.end_keyword_loc.nil?
90
+ when :if_node
91
+ !node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
92
+ else false
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,54 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the manifest's visitor that builds a case site,
5
+ # named case: its arms are its whens, each holding a when
6
+ # condition of its patterns, and its else. It relies on its
7
+ # includer's extent_of and statement_arm.
8
+ module ManifestCase
9
+ # True for a case whose paths can be seen: one with a
10
+ # subject, as each pattern is then matched against it, and
11
+ # no splat among its patterns, whose items cannot each be
12
+ # stood in for.
13
+ def case_site?(node)
14
+ return false if node.predicate.nil?
15
+
16
+ node.conditions.none? do |clause|
17
+ clause.conditions.any? { |each| each.type == :splat_node }
18
+ end
19
+ end
20
+
21
+ # A case as a Condition, its extent its case keyword to the
22
+ # end of its subject, as its arms are statements.
23
+ def case_condition(node)
24
+ node.predicate.accept(self)
25
+ whens = node.conditions.map { |clause| when_arm(clause) }
26
+ start = node.case_keyword_loc.start_offset
27
+ Condition.new(
28
+ operator: 'case',
29
+ operands: [*whens, *case_else(node.else_clause)],
30
+ extent: start...node.predicate.location.end_offset
31
+ )
32
+ end
33
+
34
+ # One when as an arm, its body a when condition whose leaves
35
+ # are its patterns' byte ranges. A pattern is matched, not
36
+ # tested for truth, so none is ever fixed.
37
+ def when_arm(clause)
38
+ patterns = clause.conditions.map do |pattern|
39
+ pattern.accept(self)
40
+ extent_of(pattern)
41
+ end
42
+ body = Condition.new(
43
+ operator: 'when', operands: patterns,
44
+ extent: patterns.first.begin...patterns.last.end
45
+ )
46
+ statement_arm(:when, clause.statements).with(body:)
47
+ end
48
+
49
+ # A case's else arm, none when it has no else.
50
+ def case_else(clause)
51
+ clause ? [statement_arm(:else, clause.statements)] : []
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,76 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the manifest's visitor that builds the sites of
5
+ # safe-navigation chains, eg user&.account&.owner. It relies
6
+ # on its includer's extent_of.
7
+ module ManifestChains
8
+ # The calls of node's chain, outermost first, eg owner then
9
+ # account for user&.account&.owner; none for a node that is
10
+ # no call.
11
+ def chain_calls(node)
12
+ calls = []
13
+ while node&.type == :call_node
14
+ calls << node
15
+ node = node.receiver
16
+ end
17
+ calls
18
+ end
19
+
20
+ # True for a call whose chain has a &. in it, eg
21
+ # user&.name.upcase.
22
+ def safe_chain?(node)
23
+ chain_calls(node).any?(&:safe_navigation?)
24
+ end
25
+
26
+ # A call that is a site as a Condition: a &. chain's, or a
27
+ # block method call's. It relies on its includer's
28
+ # block_condition.
29
+ def call_condition(node)
30
+ return block_condition(node) unless safe_chain?(node)
31
+
32
+ chain_condition(node, value: false)
33
+ end
34
+
35
+ # A &. chain as a Condition named &.: its leaves are the
36
+ # receivers of its &. calls, innermost first, each nil or
37
+ # not nil, and, with value, the whole chain's value last, as
38
+ # when it is an operand of an && or an if's predicate. Any
39
+ # site in a call's arguments or block, or in the chain's
40
+ # first receiver, is a site of its own. A chain that is no
41
+ # bare argument, eg a command call, is noted among the
42
+ # site's commands, to be parenthesised.
43
+ def chain_condition(node, value:)
44
+ calls = chain_calls(node)
45
+ visit_chain(calls)
46
+ safe = calls.select(&:safe_navigation?).reverse
47
+ receivers = safe.map { |call| receiver_of(call) }
48
+ extent = extent_of(node)
49
+ @notes[:commands] << extent if Manifest.command?(node)
50
+ operands = value ? [*receivers, extent] : receivers
51
+ Condition.new(operator: SAFE, operands:, extent:)
52
+ end
53
+
54
+ # The byte range of a &. call's receiver, noted among its
55
+ # site's fixed leaves as :present, never nil, when it is a
56
+ # call to a method treated as present.
57
+ def receiver_of(call)
58
+ receiver = call.receiver
59
+ range = extent_of(receiver)
60
+ named = receiver.type == :call_node &&
61
+ @present.include?(receiver.name.to_s)
62
+ @notes[:fixed][range] = :present if named
63
+ range
64
+ end
65
+
66
+ # Visits the sites inside a chain's calls' arguments and
67
+ # blocks, and inside its first receiver, eg (a || b) in
68
+ # (a || b)&.size, but never a call of the chain itself.
69
+ def visit_chain(calls)
70
+ calls.each do |call|
71
+ [call.arguments, call.block].compact.each { _1.accept(self) }
72
+ end
73
+ calls.last.receiver&.accept(self)
74
+ end
75
+ end
76
+ end
@@ -2,20 +2,39 @@ require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
4
  # The part of the manifest's visitor that builds the sites
5
- # whose predicate picks an arm: a ternary, named ?:, and an
6
- # if, named if. It relies on its includer's condition_of,
7
- # extent_of, grouped, leaf and operator?.
5
+ # whose predicate picks an arm: a ternary, named ?:, an if,
6
+ # named if, and an unless, named unless. It relies on its
7
+ # includer's condition_of, extent_of, grouped, leaf and
8
+ # operator?.
8
9
  module ManifestChoices
9
- # True for a modifier if, eg `return if done?`, whose
10
- # statement comes before its if keyword.
11
- def modifier?(node)
12
- return false if ternary?(node) || node.statements.nil?
10
+ # The node types whose predicate picks an arm, a ternary
11
+ # being an if_node with no if keyword.
12
+ CHOICE_TYPES = %i[if_node unless_node].freeze
13
13
 
14
- node.statements.location.start_offset <
15
- node.if_keyword_loc.start_offset
14
+ # Where node's site starts: a &. chain's start; a block
15
+ # method's call at its method; an if's or an unless's
16
+ # keyword, which a modifier's statement comes before;
17
+ # anything else's own start.
18
+ def site_loc(node)
19
+ return node.location if safe_chain?(node)
20
+ return node.message_loc if node.type == :call_node
21
+ return node.location unless CHOICE_TYPES.include?(node.type)
22
+ return node.location if ternary?(node)
23
+
24
+ keyword_loc(node)
25
+ end
26
+
27
+ # The location of an if's or an unless's keyword.
28
+ def keyword_loc(node)
29
+ unless?(node) ? node.keyword_loc : node.if_keyword_loc
16
30
  end
17
31
 
18
- # A ternary or an if as a Condition.
32
+ # True for an unless.
33
+ def unless?(node)
34
+ node.type == :unless_node
35
+ end
36
+
37
+ # A ternary, an if or an unless as a Condition.
19
38
  def choice_of(node)
20
39
  ternary?(node) ? ternary_condition(node) : if_condition(node)
21
40
  end
@@ -28,33 +47,35 @@ module PathCoverage
28
47
  )
29
48
  end
30
49
 
31
- # An if as a Condition, its extent its if keyword to the end
32
- # of its predicate, as its arms are statements, not part of
33
- # the expression.
50
+ # An if or an unless as a Condition, named by its keyword,
51
+ # its extent its keyword to the end of its predicate, as its
52
+ # arms are statements, not part of the expression.
34
53
  def if_condition(node)
35
- start = node.if_keyword_loc.start_offset
54
+ start = keyword_loc(node).start_offset
36
55
  Condition.new(
37
- operator: 'if', operands: if_of(node),
56
+ operator: unless?(node) ? 'unless' : 'if',
57
+ operands: if_of(node),
38
58
  extent: start...node.predicate.location.end_offset
39
59
  )
40
60
  end
41
61
 
42
- # An if's predicate, then its then arm and, when it has
43
- # one, its else arm.
62
+ # An if's or an unless's predicate, then its then arm and,
63
+ # when it has one, its else arm.
44
64
  def if_of(node)
65
+ rest = unless?(node) ? node.else_clause : node.subsequent
45
66
  [
46
67
  predicate_of(node),
47
68
  statement_arm(:then, node.statements),
48
- *else_arm(node.subsequent)
69
+ *else_arm(rest)
49
70
  ]
50
71
  end
51
72
 
52
73
  # An if's arm, statements its code, which never folds into
53
- # the if's site; any site inside it is a site of its own.
54
- # An arm with no statements has no extent.
74
+ # the if's site; as a new scope, any site inside it is a site
75
+ # of its own. An arm with no statements has no extent.
55
76
  def statement_arm(kind, statements)
56
- statements&.accept(self)
57
- extent = statements && extent_of(statements)
77
+ scoped { statements&.accept(self) }
78
+ extent = statements ? extent_of(statements) : nil
58
79
  Arm.new(kind:, extent:, body: nil)
59
80
  end
60
81
 
@@ -66,7 +87,7 @@ module PathCoverage
66
87
  return [statement_arm(:else, subsequent.statements)]
67
88
  end
68
89
 
69
- subsequent.accept(self)
90
+ scoped { subsequent.accept(self) }
70
91
  extent = extent_of(subsequent)
71
92
  [Arm.new(kind: :else, extent:, body: nil)]
72
93
  end
@@ -81,19 +102,25 @@ module PathCoverage
81
102
  end
82
103
 
83
104
  # One arm of a ternary, statements its code: a condition
84
- # folds in as its body, being part of the one expression;
85
- # any site inside a plain arm is a site of its own.
105
+ # folds in as its body, being part of the one expression, as
106
+ # does a plain arm with sites inside it, its value its last
107
+ # leaf; a plain arm with none has no body.
86
108
  def arm_of(kind, statements)
87
109
  written = grouped(statements.body.first)
88
- body = condition_of(written) if foldable?(written)
89
- statements.accept(self) if body.nil?
90
- Arm.new(kind:, extent: extent_of(statements), body:)
110
+ extent = extent_of(statements)
111
+ body = if foldable?(written)
112
+ condition_of(written)
113
+ else
114
+ inner = collected { statements.accept(self) }
115
+ inner.empty? ? nil : contained(inner, extent)
116
+ end
117
+ Arm.new(kind:, extent:, body:)
91
118
  end
92
119
 
93
120
  # True for a condition that folds into a ternary's site:
94
- # an &&, ||, and, or, or a ternary.
121
+ # an &&, ||, and, or, or a ternary, none of them ignored.
95
122
  def foldable?(node)
96
- operator?(node) || ternary?(node)
123
+ (operator?(node) || ternary?(node)) && !ignored?(node)
97
124
  end
98
125
 
99
126
  # True for a ternary, an if with no if keyword.
@@ -102,10 +129,15 @@ module PathCoverage
102
129
  end
103
130
 
104
131
  # A ternary's or an if's predicate as its operand: an && or
105
- # || nested as a Condition, anything else a leaf.
132
+ # || nested as a Condition, as is a &. chain, its value its
133
+ # last leaf, anything else a leaf.
106
134
  def predicate_of(node)
107
- predicate = node.predicate
135
+ predicate = grouped(node.predicate)
136
+ return leaf(predicate) if ignored?(predicate)
108
137
  return condition_of(predicate) if operator?(predicate)
138
+ if safe_chain?(predicate)
139
+ return chain_condition(predicate, value: true)
140
+ end
109
141
 
110
142
  leaf(predicate)
111
143
  end
@@ -0,0 +1,91 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the manifest's visitor that folds every site in one
5
+ # expression into one site. While a leaf is visited, each site
6
+ # found in it, the leaf itself included, is collected as a nested
7
+ # Condition of that leaf, rather than noted as a site of its own.
8
+ # A new scope, eg a block's body or an if's arm, stops the
9
+ # collecting, so a site in it is a site of its own. It relies on
10
+ # its includer's condition_of, chain_condition, safe_chain? and
11
+ # extent_of.
12
+ module ManifestFolding
13
+ # node's Condition as a site nested in a leaf: a &. chain's
14
+ # with its value as its last leaf, as its leaf's outcome
15
+ # follows from it.
16
+ def nested_condition_of(node)
17
+ return chain_condition(node, value: true) if safe_chain?(node)
18
+
19
+ condition_of(node)
20
+ end
21
+
22
+ # The Conditions of the sites found while yielding, each one
23
+ # collected rather than noted as a site of its own.
24
+ def collected
25
+ outer = @inner
26
+ @inner = []
27
+ yield
28
+ @inner
29
+ ensure
30
+ @inner = outer
31
+ end
32
+
33
+ # Yields with no collecting, so a site found is one of its own:
34
+ # for a new scope, eg a block's body or an if's arm.
35
+ def scoped
36
+ outer = @inner
37
+ @inner = nil
38
+ yield
39
+ ensure
40
+ @inner = outer
41
+ end
42
+
43
+ # range as a leaf, or, with inner sites found in it, a Condition
44
+ # named contains: those sites, then range, the leaf's outcome,
45
+ # fixed as :nonempty when nonempty. A leaf that is wholly one
46
+ # block method's call whose value follows from its path is
47
+ # that call alone, needing no outcome of its own.
48
+ def contained(inner, range, nonempty: false)
49
+ return range if inner.empty?
50
+
51
+ only = inner.first
52
+ if inner.size == 1 && only.extent == range &&
53
+ VALUED.include?(only.operator)
54
+ return only
55
+ end
56
+
57
+ fixed = nonempty ? { range => :nonempty } : {}
58
+ Condition.new(operator: CONTAINS, operands: [*inner, range],
59
+ extent: range, fixed:)
60
+ end
61
+
62
+ # True for a call, with no arguments or block, to a method
63
+ # treated as nonempty, eg any?, whose receiver, parenthesised
64
+ # or not, is one of inner, the sites found in it: its outcome
65
+ # then follows from that site's path. It relies on its
66
+ # includer's grouped.
67
+ def nonempty_call?(node, inner)
68
+ return false unless node.type == :call_node
69
+ return false if node.arguments || node.block || !node.receiver
70
+
71
+ receiver = extent_of(grouped(node.receiver))
72
+ @nonempty.include?(node.name.to_s) &&
73
+ inner.any? { |site| site.extent == receiver }
74
+ end
75
+
76
+ # A block's body is a new scope: it runs once per item.
77
+ def visit_block_node(node)
78
+ scoped { super }
79
+ end
80
+
81
+ # A lambda's body is a new scope: it runs when called.
82
+ def visit_lambda_node(node)
83
+ scoped { super }
84
+ end
85
+
86
+ # A def's body is a new scope: it runs when called.
87
+ def visit_def_node(node)
88
+ scoped { super }
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,35 @@
1
+ module PathCoverage
2
+ # The part of the manifest's visitor that leaves out the sites
3
+ # of ignored operators, eg ignore '&&='. An ignored node is no
4
+ # site, though a site inside it may be; nested in a site, it
5
+ # is a leaf. It relies on its includer's @ignored, ternary?
6
+ # and safe_chain?.
7
+ module ManifestIgnoring
8
+ # The operator of each node type that always names one kind
9
+ # of site.
10
+ NAMED = { unless_node: 'unless', case_node: 'case' }.freeze
11
+
12
+ # True for a node whose operator is ignored.
13
+ def ignored?(node)
14
+ @ignored.include?(operator_of(node))
15
+ end
16
+
17
+ # The operator naming node's kind of site, eg ?: for a
18
+ # ternary or any? for a block method's call; nil for a
19
+ # node with none.
20
+ def operator_of(node)
21
+ type = node.type
22
+ return NAMED[type] if NAMED.key?(type)
23
+ return ternary?(node) ? '?:' : 'if' if type == :if_node
24
+ return call_operator(node) if type == :call_node
25
+
26
+ node.respond_to?(:operator) ? node.operator : nil
27
+ end
28
+
29
+ # The operator of a call: &. for a &. chain, else its
30
+ # method's name, eg any?.
31
+ def call_operator(node)
32
+ safe_chain?(node) ? '&.' : node.name.to_s
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,67 @@
1
+ require_relative 'condition'
2
+
3
+ module PathCoverage
4
+ # The part of the manifest's visitor that builds the sites of
5
+ # ||= and &&= writes, eg @clock ||= Clock.new. It relies on
6
+ # its includer's add_site, bare_less? and extent_of.
7
+ module ManifestWrites
8
+ # Every node type of a ||= or &&= write, by its target: a
9
+ # variable of each kind, a constant, a constant path, an
10
+ # attribute, eg self.size, and an index, eg store[key].
11
+ WRITE_TYPES = %w[
12
+ local_variable instance_variable class_variable
13
+ global_variable constant constant_path call index
14
+ ].flat_map do |target|
15
+ %W[#{target}_or_write_node #{target}_and_write_node]
16
+ end.map(&:to_sym).freeze
17
+
18
+ # Notes each kind of write as a site, eg
19
+ # visit_index_or_write_node for store[key] ||= 0.
20
+ WRITE_TYPES.each do |type|
21
+ define_method(:"visit_#{type}") do |node|
22
+ write?(node) ? add_site(node) : visit_child_nodes(node)
23
+ end
24
+ end
25
+
26
+ # True for a ||= or &&= write that is a site: any but one
27
+ # through &., eg box&.size ||= 3, which a nil box skips whole,
28
+ # its target never looked at, so neither of its paths taken.
29
+ def write?(node)
30
+ WRITE_TYPES.include?(node.type) && !safe_write?(node)
31
+ end
32
+
33
+ # True for a write through &., eg box&.size ||= 3.
34
+ def safe_write?(node)
35
+ node.respond_to?(:safe_navigation?) && node.safe_navigation?
36
+ end
37
+
38
+ # A write as a Condition, named by its operator: its one leaf
39
+ # its target, then its right side as an arm, taken when the
40
+ # target is false for ||=, true for &&=. The target is a
41
+ # leaf the rewriter never reads again, as reading it could
42
+ # call a method twice; its outcome follows from whether the
43
+ # arm ran. Any site inside the write, eg in an index or on
44
+ # the right side, is a site of its own. A right side that is
45
+ # no bare argument, eg a command call, is noted among the
46
+ # site's commands, to be parenthesised.
47
+ def write_condition(node)
48
+ visit_child_nodes(node)
49
+ value = extent_of(node.value)
50
+ @notes[:commands] << value if bare_less?(node.value)
51
+ arm = Arm.new(kind: :then, extent: value, body: nil)
52
+ Condition.new(
53
+ operator: node.operator, operands: [target_of(node), arm],
54
+ extent: extent_of(node)
55
+ )
56
+ end
57
+
58
+ # The byte range of a write's target: from its start to its
59
+ # operator, less any space between, eg `store[key]`.
60
+ def target_of(node)
61
+ start = node.location.start_offset
62
+ before = node.operator_loc.start_offset - start
63
+ text = node.location.slice.byteslice(0, before)
64
+ start...(start + text.rstrip.bytesize)
65
+ end
66
+ end
67
+ end
@@ -12,11 +12,15 @@ module PathCoverage
12
12
  end
13
13
 
14
14
  # One site paths hash, as Result#paths gives them, each
15
- # path's count the sum of every run's; runs of the same
16
- # source list the same paths in the same order.
15
+ # path's count the sum of every run's. Runs that list a
16
+ # site's paths differently raise, as adding their counts by
17
+ # position would credit one path's count to another.
17
18
  def self.paths(runs)
18
19
  runs.reduce do |total, run|
19
- total.merge(run) do |_site, mine, theirs|
20
+ total.merge(run) do |site, mine, theirs|
21
+ same = mine.map { _1[:path] } == theirs.map { _1[:path] }
22
+ raise ArgumentError, "runs differ at #{site}" unless same
23
+
20
24
  mine.zip(theirs).map do |a, b|
21
25
  { path: a[:path], count: a[:count] + b[:count] }
22
26
  end
@@ -24,6 +28,18 @@ module PathCoverage
24
28
  end
25
29
  end
26
30
 
31
+ # One method paths hash, { site => { path => count } }, as
32
+ # the recorder counts them, each path's count the sum of
33
+ # every run's, eg [true, false] taken once in each of two
34
+ # runs is taken twice.
35
+ def self.method_paths(runs)
36
+ runs.reduce do |total, run|
37
+ total.merge(run) do |_site, mine, theirs|
38
+ mine.merge(theirs) { |_path, a, b| a + b }
39
+ end
40
+ end
41
+ end
42
+
27
43
  # One operand's counts from two runs, added; a fixed leaf's
28
44
  # only comes from the source, so either run's will do.
29
45
  def self.sum(mine, theirs)
@@ -6,18 +6,18 @@ module PathCoverage
6
6
  # for runs whose own files are thrown away, eg a failing
7
7
  # docker build step.
8
8
  module Post
9
- # POSTs operands and paths, as the JSON report, to url,
10
- # never through an http_proxy, which could not reach a
11
- # collector local to the run. A collector that cannot be
12
- # reached, or does not answer within a second, is passed
13
- # over in silence: the run's own output and exit status
14
- # must stay as they are.
15
- def self.deliver(operands, url, paths = {})
9
+ # POSTs operands and paths, as the JSON report with its
10
+ # fingerprint, to url, never through an http_proxy,
11
+ # which could not reach a collector local to the run. A
12
+ # collector that cannot be reached, or does not answer
13
+ # within a second, is passed over in silence: the run's own
14
+ # output and exit status must stay as they are.
15
+ def self.deliver(operands, url, paths = {}, fingerprint: {})
16
16
  uri = URI(url)
17
17
  http = Net::HTTP.new(uri.host, uri.port, nil)
18
18
  http.open_timeout = http.read_timeout = 1
19
19
  headers = { 'Content-Type' => 'application/json' }
20
- body = JsonWriter.json(operands, paths)
20
+ body = JsonWriter.json(operands, paths, fingerprint:)
21
21
  http.post(uri.path, body, headers)
22
22
  rescue StandardError
23
23
  nil