path_coverage 0.4.0 → 0.5.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 (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +286 -199
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +88 -0
  5. data/lib/path_coverage/calls.rb +49 -43
  6. data/lib/path_coverage/card_context.rb +104 -0
  7. data/lib/path_coverage/card_forks.rb +44 -0
  8. data/lib/path_coverage/card_gaps.rb +125 -0
  9. data/lib/path_coverage/card_scopes.rb +37 -0
  10. data/lib/path_coverage/card_tree.rb +79 -66
  11. data/lib/path_coverage/card_way_lists.rb +72 -0
  12. data/lib/path_coverage/card_ways.rb +123 -67
  13. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  14. data/lib/path_coverage/condition.rb +14 -14
  15. data/lib/path_coverage/configuration.rb +7 -4
  16. data/lib/path_coverage/configuration_limits.rb +52 -0
  17. data/lib/path_coverage/configuration_paths.rb +7 -7
  18. data/lib/path_coverage/console_summary.rb +24 -22
  19. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  20. data/lib/path_coverage/decision_locations.rb +76 -0
  21. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  22. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  23. data/lib/path_coverage/fold_branches.rb +18 -0
  24. data/lib/path_coverage/fold_cells.rb +42 -0
  25. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +42 -71
  26. data/lib/path_coverage/gaps.rb +33 -15
  27. data/lib/path_coverage/json_reader.rb +54 -19
  28. data/lib/path_coverage/json_reports.rb +102 -0
  29. data/lib/path_coverage/json_writer.rb +27 -12
  30. data/lib/path_coverage/kernel.rb +56 -58
  31. data/lib/path_coverage/kinds/base.rb +16 -16
  32. data/lib/path_coverage/kinds/block.rb +16 -15
  33. data/lib/path_coverage/kinds/case.rb +14 -12
  34. data/lib/path_coverage/kinds/chain.rb +40 -11
  35. data/lib/path_coverage/kinds/choice.rb +4 -3
  36. data/lib/path_coverage/kinds/contains.rb +40 -32
  37. data/lib/path_coverage/kinds/safe.rb +38 -26
  38. data/lib/path_coverage/kinds/write.rb +6 -6
  39. data/lib/path_coverage/kinds.rb +2 -2
  40. data/lib/path_coverage/leaving.rb +44 -0
  41. data/lib/path_coverage/loop_forks.rb +57 -0
  42. data/lib/path_coverage/loop_nodes.rb +55 -0
  43. data/lib/path_coverage/manifest.rb +92 -58
  44. data/lib/path_coverage/manifest_blocks.rb +5 -26
  45. data/lib/path_coverage/manifest_case.rb +2 -2
  46. data/lib/path_coverage/manifest_chains.rb +8 -8
  47. data/lib/path_coverage/manifest_choices.rb +23 -20
  48. data/lib/path_coverage/manifest_disabled.rb +97 -0
  49. data/lib/path_coverage/manifest_folding.rb +43 -40
  50. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  51. data/lib/path_coverage/manifest_writes.rb +11 -10
  52. data/lib/path_coverage/measured_by.rb +20 -0
  53. data/lib/path_coverage/merge.rb +7 -7
  54. data/lib/path_coverage/method_loops.rb +128 -0
  55. data/lib/path_coverage/method_statements.rb +130 -127
  56. data/lib/path_coverage/method_ways.rb +142 -0
  57. data/lib/path_coverage/paths_report.rb +16 -16
  58. data/lib/path_coverage/recorder.rb +57 -36
  59. data/lib/path_coverage/recorder_blocks.rb +17 -17
  60. data/lib/path_coverage/recorder_cases.rb +12 -11
  61. data/lib/path_coverage/recorder_chains.rb +12 -12
  62. data/lib/path_coverage/recorder_writes.rb +9 -9
  63. data/lib/path_coverage/render.rb +58 -51
  64. data/lib/path_coverage/result.rb +32 -31
  65. data/lib/path_coverage/rewriter.rb +39 -27
  66. data/lib/path_coverage/rewriter_nested.rb +13 -13
  67. data/lib/path_coverage/runs_report.rb +84 -0
  68. data/lib/path_coverage/source_lines.rb +16 -16
  69. data/lib/path_coverage/source_report.rb +100 -0
  70. data/lib/path_coverage/stable_leaves.rb +162 -0
  71. data/lib/path_coverage/statement_arms.rb +31 -30
  72. data/lib/path_coverage/summary.rb +4 -62
  73. data/lib/path_coverage/totals.rb +10 -8
  74. data/lib/path_coverage/version.rb +1 -1
  75. data/lib/path_coverage/walk.rb +106 -0
  76. data/lib/path_coverage/way_algebras.rb +127 -0
  77. data/lib/path_coverage/ways_report.rb +91 -0
  78. data/lib/path_coverage/ways_totals.rb +46 -0
  79. data/lib/path_coverage/within_methods.rb +135 -80
  80. metadata +33 -28
  81. data/lib/path_coverage/card.rb +0 -14
  82. data/lib/path_coverage/html/file.html +0 -33
  83. data/lib/path_coverage/html/index.html +0 -29
  84. data/lib/path_coverage/html/settings.js +0 -50
  85. data/lib/path_coverage/html/side.js +0 -599
  86. data/lib/path_coverage/html/sort.js +0 -34
  87. data/lib/path_coverage/html/style.css +0 -484
  88. data/lib/path_coverage/html/tabs.js +0 -15
  89. data/lib/path_coverage/html/theme.js +0 -27
  90. data/lib/path_coverage/html_branch.rb +0 -30
  91. data/lib/path_coverage/html_cards.rb +0 -88
  92. data/lib/path_coverage/html_cells.rb +0 -71
  93. data/lib/path_coverage/html_file.rb +0 -123
  94. data/lib/path_coverage/html_index.rb +0 -128
  95. data/lib/path_coverage/html_index_head.rb +0 -29
  96. data/lib/path_coverage/html_labels.rb +0 -16
  97. data/lib/path_coverage/html_methods.rb +0 -66
  98. data/lib/path_coverage/html_nav.rb +0 -67
  99. data/lib/path_coverage/html_report.rb +0 -131
  100. data/lib/path_coverage/html_statements.rb +0 -186
  101. data/lib/path_coverage/html_tabs.rb +0 -26
  102. data/lib/path_coverage/html_template.rb +0 -26
@@ -1,88 +1,95 @@
1
+ require_relative 'card_context'
1
2
  require_relative 'console_summary'
2
3
  require_relative 'gaps'
3
- require_relative 'html_report'
4
4
  require_relative 'json_reader'
5
+ require_relative 'json_reports'
5
6
  require_relative 'manifest'
6
7
  require_relative 'paths_report'
7
8
  require_relative 'post'
8
- require_relative 'site_paths'
9
+ require_relative 'decision_paths'
9
10
  require_relative 'within_methods'
10
11
 
11
12
  module PathCoverage
12
- # The reporters, run from operands alone: needing nothing the
13
- # run recorded beyond them, they can run in another process.
13
+ # The JSON reports and the reporters, run from operands alone:
14
+ # needing nothing the run recorded beyond them, they can run in
15
+ # another process.
14
16
  module Render
15
- # Runs each reporter chosen in config. operands, and taken's
16
- # site_paths: and outside:, are as Result gives them, and its
17
- # reached: as Recorder#reached gives it; files is
18
- # { full path => {source:, sites:} } for each covered file.
17
+ # Writes the JSON reports, then runs each reporter chosen in
18
+ # config. operands, and taken's decision_paths: and outside:, are
19
+ # as Result gives them, and its reached: as Recorder#reached
20
+ # gives it; files is { full path => {source:, decisions:} } for
21
+ # each covered file.
19
22
  def self.run(operands, files, config, **taken)
20
- site_paths = every_site(taken.fetch(:site_paths, {}), files)
21
- reporters = config.reporters
22
- if reporters.include?(:gaps)
23
- gaps(operands, files, site_paths, taken.fetch(:outside, {}))
24
- end
25
- html_and_summary(operands, files, config,
26
- [site_paths, taken[:reached]])
27
- return unless reporters.include?(:post)
23
+ recorded = taken.merge(
24
+ decision_paths: every_decision(
25
+ taken.fetch(:decision_paths, {}), files
26
+ ),
27
+ outside: taken.fetch(:outside, {})
28
+ )
29
+ JsonReports.write(operands, files, config, **recorded)
30
+ reporters(operands, files, config, recorded)
31
+ end
28
32
 
29
- post(operands, files, config, site_paths)
33
+ # Runs each reporter chosen in config, from operands, files,
34
+ # and recorded, as run has them.
35
+ def self.reporters(operands, files, config, recorded)
36
+ paths, reached = recorded.values_at(:decision_paths, :reached)
37
+ runs = {
38
+ gaps: -> { gaps(operands, files, config, recorded) },
39
+ summary: -> { summary(files, config, paths, reached) },
40
+ ways: -> { print File.read(JsonReports.ways_path(config)) },
41
+ post: -> { post(operands, files, config, paths) }
42
+ }
43
+ runs.slice(*config.reporters).each_value(&:call)
30
44
  end
31
45
 
32
46
  # Runs the :post reporter: operands, and the paths report of
33
- # site_paths in files, with config's fingerprint.
34
- def self.post(operands, files, config, site_paths)
35
- report = PathsReport.of(site_paths, files)
47
+ # decision_paths in files, with config's fingerprint.
48
+ def self.post(operands, files, config, decision_paths)
49
+ report = PathsReport.of(decision_paths, files)
36
50
  url = config.collector_url
37
51
  fingerprint = config.fingerprint
38
52
  Post.deliver(operands, url, report, fingerprint:)
39
53
  end
40
54
 
41
- # Runs the :html and :summary reporters, each when chosen.
42
- def self.html_and_summary(operands, files, config, recorded)
43
- site_paths, reached = recorded
44
- reporters = config.reporters
45
- if reporters.include?(:html)
46
- HtmlReport.write(operands, files, config, recorded)
47
- end
48
- return unless reporters.include?(:summary)
49
-
50
- cards = WithinMethods.cards(files, config, site_paths, reached)
51
- summary(config, site_paths, cards)
52
- end
53
-
54
55
  # Prints the summary lines: the paths taken of all possible
55
- # within decisions, and, when cards are given, as they are
56
- # when methods are measured, within methods; then any
57
- # operators ignored.
58
- def self.summary(config, site_paths, cards)
59
- groups = config.groups
60
- puts ConsoleSummary.lines(site_paths, cards, groups) +
56
+ # alone, and combined, each def's card of files as decision_paths
57
+ # and reached have it; then any operators ignored.
58
+ def self.summary(files, config, decision_paths, reached)
59
+ cards = WithinMethods.cards(files, config, decision_paths,
60
+ reached)
61
+ puts ConsoleSummary.lines(decision_paths, cards,
62
+ config.groups) +
61
63
  ConsoleSummary.ignored_lines(config.ignored)
62
64
  end
63
65
 
64
- # site_paths with every site in files that it lacks, eg
66
+ # decision_paths with every decision in files that it lacks, eg
65
67
  # from a JSON report with no paths, given each possible
66
68
  # path at a count of zero: unable to say which paths such
67
- # a site took, the report claims none.
68
- def self.every_site(site_paths, files)
69
+ # a decision took, the report claims none.
70
+ def self.every_decision(decision_paths, files)
69
71
  none = files.values.each_with_object({}) do |parsed, all|
70
- parsed[:sites].each do |site, condition|
71
- all[site] = SitePaths.possible(condition).map do |path|
72
- { path:, count: 0 }
73
- end
72
+ parsed[:decisions].each do |decision, condition|
73
+ possible = DecisionPaths.possible(condition)
74
+ all[decision] = possible.map { |path| { path:, count: 0 } }
74
75
  end
75
76
  end
76
- none.merge(site_paths)
77
+ none.merge(decision_paths)
77
78
  end
78
79
 
79
- # Prints the gap lines: each path a site never took, and each
80
+ # Prints the gap lines: each path a decision never took, and each
80
81
  # path taken that a treatment rules out, as outside lists
81
- # them, each leaf named by its text in files.
82
- def self.gaps(operands, files, site_paths, outside)
82
+ # them, each leaf named by its text in files; then each path
83
+ # taken, or return or raise reached, that its method rules
84
+ # out, as CardContext.ruled_out finds them in files under
85
+ # config. recorded is as run has it.
86
+ def self.gaps(operands, files, config, recorded)
83
87
  texts = PathsReport.texts(files)
84
- paths = site_paths
88
+ paths, outside, reached =
89
+ recorded.values_at(:decision_paths, :outside, :reached)
85
90
  lines = Gaps.lines(operands, paths:, texts:, outside:)
91
+ ruled = CardContext.ruled_out(files, config, paths, reached)
92
+ lines += Gaps.ruled_by_method(ruled, texts)
86
93
  lines.each { |line| puts line }
87
94
  end
88
95
 
@@ -1,78 +1,79 @@
1
- require_relative 'site_paths'
1
+ require_relative 'decision_paths'
2
2
 
3
3
  module PathCoverage
4
- # What a run found: every site the manifest knows of, with
4
+ # What a run found: every decision the manifest knows of, with
5
5
  # the counts its operands were recorded with. Every
6
6
  # reporter builds on this.
7
7
  class Result
8
8
  NEVER_RAN = { truthy: 0, falsy: 0 }.freeze
9
9
 
10
- # sites is { site => Condition }, from the manifest;
11
- # counts is { [site, index] => counts } and site_paths is
12
- # { site => { path => evaluations } }, both from the
10
+ # decisions is { decision => Condition }, from the manifest;
11
+ # counts is { [decision, index] => counts } and decision_paths is
12
+ # { decision => { path => evaluations } }, both from the
13
13
  # recorder.
14
- def initialize(sites, counts, site_paths = {})
15
- @sites = sites
14
+ def initialize(decisions, counts, decision_paths = {})
15
+ @decisions = decisions
16
16
  @counts = counts
17
- @site_paths = site_paths
17
+ @decision_paths = decision_paths
18
18
  end
19
19
 
20
- # { site => [{ path:, count: }, ...] }: every possible path
21
- # of each site, as SitePaths gives them, with the number of
22
- # evaluations that took it, zero for one never taken.
20
+ # { decision => [{ path:, count: }, ...] }: every possible path
21
+ # of each decision, as DecisionPaths gives them, with the
22
+ # number of evaluations that took it, zero for one never
23
+ # taken.
23
24
  def paths
24
- @sites.to_h do |site, condition|
25
- taken = @site_paths.fetch(site, {})
26
- all = SitePaths.possible(condition).map do |path|
25
+ @decisions.to_h do |decision, condition|
26
+ taken = @decision_paths.fetch(decision, {})
27
+ all = DecisionPaths.possible(condition).map do |path|
27
28
  { path:, count: taken.fetch(path, 0) }
28
29
  end
29
- [site, all]
30
+ [decision, all]
30
31
  end
31
32
  end
32
33
 
33
- # { site => { path => evaluations } } of each path taken
34
- # that is none of its site's possible paths, eg a &. receiver
35
- # nil though treated as present; only sites with one. Such a
34
+ # { decision => { path => evaluations } } of each path taken
35
+ # that is none of its decision's possible paths, eg a &. receiver
36
+ # nil though treated as present; only decisions with one. Such a
36
37
  # path shows a treatment to be wrong, so it is reported, never
37
38
  # dropped.
38
39
  def outside
39
- all = @sites.to_h do |site, condition|
40
- taken = @site_paths.fetch(site, {})
41
- [site, taken.except(*SitePaths.possible(condition))]
40
+ all = @decisions.to_h do |decision, condition|
41
+ taken = @decision_paths.fetch(decision, {})
42
+ [decision, taken.except(*DecisionPaths.possible(condition))]
42
43
  end
43
44
  all.reject { |_, paths| paths.empty? }
44
45
  end
45
46
 
46
- # The paths taken, as Render.run takes them: site_paths:,
47
+ # The paths taken, as Render.run takes them: decision_paths:,
47
48
  # as paths gives them, and outside:.
48
49
  def taken
49
- { site_paths: paths, outside: }
50
+ { decision_paths: paths, outside: }
50
51
  end
51
52
 
52
- # { site => [counts of operand 0, counts of operand 1, ...] },
53
+ # { decision => [counts of operand 0, counts of operand 1, ...] },
53
54
  # with zero counts for an operand that never ran, and
54
55
  # only: naming the one outcome a fixed leaf can have.
55
56
  def operands
56
- @sites.to_h do |site, condition|
57
+ @decisions.to_h do |decision, condition|
57
58
  leaves = condition.leaves.each_with_index.map do |range, i|
58
- leaf(site, condition, range, i)
59
+ leaf(decision, condition, range, i)
59
60
  end
60
- [site, leaves]
61
+ [decision, leaves]
61
62
  end
62
63
  end
63
64
 
64
65
  private
65
66
 
66
67
  # The counts of one leaf, with only: when it is fixed.
67
- def leaf(site, condition, range, index)
68
- counts = counts_of(site, index)
68
+ def leaf(decision, condition, range, index)
69
+ counts = counts_of(decision, index)
69
70
  only = condition.fixed[range]
70
71
  only ? counts.merge(only:) : counts
71
72
  end
72
73
 
73
74
  # The recorded counts of one operand, or zeros.
74
- def counts_of(site, index)
75
- @counts.fetch([site, index], NEVER_RAN)
75
+ def counts_of(decision, index)
76
+ @counts.fetch([decision, index], NEVER_RAN)
76
77
  end
77
78
  end
78
79
  end
@@ -7,17 +7,28 @@ module PathCoverage
7
7
  # outcome when the code runs. It inserts text at the byte
8
8
  # ranges the manifest found, and never parses.
9
9
  module Rewriter
10
- # Returns source with each operand in sites wrapped in a
11
- # record call, each site as a whole in a leave call that
10
+ # Returns source with each operand in decisions wrapped in a
11
+ # record call, each decision as a whole in a leave call that
12
12
  # enters it first, and each statement in reaches, { name =>
13
13
  # offset }, after a reach call that counts its runs. Only
14
14
  # text is inserted, never a newline, so backtrace line
15
15
  # numbers still match the file on disk.
16
- def self.rewrite(source, sites, reaches = {})
17
- inserts = sites.flat_map do |site, condition|
18
- site_inserts(site, condition)
16
+ def self.rewrite(source, decisions, reaches = {}, passes = {})
17
+ inserts = decisions.flat_map do |decision, condition|
18
+ decision_inserts(decision, condition)
19
+ end
20
+ apply(source.dup, inserts + reach_inserts(reaches) +
21
+ pass_inserts(passes))
22
+ end
23
+
24
+ # Example: { "k.rb:4:16" => 60 } puts a pass call at 60.
25
+ # The inserts that put a pass call, a statement of its own,
26
+ # first in each loop body in passes, { name => offset }.
27
+ def self.pass_inserts(passes)
28
+ rank = [Float::INFINITY, LAYERS.index(:reach)]
29
+ passes.map do |name, at|
30
+ [at, "PathCoverage.pass(#{name.inspect}); ", rank]
19
31
  end
20
- apply(source.dup, inserts + reach_inserts(reaches))
21
32
  end
22
33
 
23
34
  # Example: { "k.rb:2:2" => 18 } puts a reach call at 18.
@@ -36,58 +47,59 @@ module PathCoverage
36
47
  end
37
48
  end
38
49
 
39
- # The inserts for one site, as its kind gives them, eg its
50
+ # The inserts for one decision, as its kind gives them, eg its
40
51
  # leaves' records and its leave; a block method's call or a
41
52
  # write is noted as if nested at leaf 0.
42
- def self.site_inserts(site, condition)
43
- Kinds.of(condition.operator).site_inserts(site, condition)
53
+ def self.decision_inserts(decision, condition)
54
+ Kinds.of(condition.operator).decision_inserts(decision,
55
+ condition)
44
56
  end
45
57
 
46
- # The inserts that wrap each of site's leaves in a record
47
- # call, innermost of all; a leaf of a nested site that needs
58
+ # The inserts that wrap each of decision's leaves in a record
59
+ # call, innermost of all; a leaf of a nested decision that needs
48
60
  # a call of its own, eg a &. chain's, in the call
49
- # RewriterNested gives it, or in none, and that site's own
61
+ # RewriterNested gives it, or in none, and that decision's own
50
62
  # inserts too.
51
- def self.record_inserts(site, condition)
52
- nested = RewriterNested.calls(site, condition)
63
+ def self.record_inserts(decision, condition)
64
+ nested = RewriterNested.calls(decision, condition)
53
65
  leaves = condition.leaves
54
66
  records = leaves.each_with_index.flat_map do |range, i|
55
- call = nested.fetch(range) { record_call(site, i) }
67
+ call = nested.fetch(range) { record_call(decision, i) }
56
68
  next [] unless call
57
69
 
58
70
  wrap(call, range, condition.commands.include?(range), :leaf)
59
71
  end
60
- records + RewriterNested.inserts(site, condition)
72
+ records + RewriterNested.inserts(decision, condition)
61
73
  end
62
74
 
63
75
  WORD_OPERATORS = %w[and or].freeze
64
76
 
65
- # The inserts that wrap site as a whole in a leave call,
77
+ # The inserts that wrap decision as a whole in a leave call,
66
78
  # outside its leaves' record calls, whose first argument
67
- # enters the site before any leaf runs, so each evaluation
79
+ # enters the decision before any leaf runs, so each evaluation
68
80
  # fills in a trail of its own. An and or or is held in
69
81
  # parentheses, being, like a command call, no bare argument.
70
- def self.trail_inserts(site, condition)
71
- enter = "PathCoverage.enter(#{site.inspect}, " \
82
+ def self.trail_inserts(decision, condition)
83
+ enter = "PathCoverage.enter(#{decision.inspect}, " \
72
84
  "#{condition.leaves.size})"
73
85
  call = "PathCoverage.leave(#{enter}, "
74
86
  wrapped = Kinds.of(condition.operator).trailed(condition)
75
87
  nested = wrapped.is_a?(Condition)
76
88
  word = nested && WORD_OPERATORS.include?(wrapped.operator)
77
- wrap(call, nested ? wrapped.extent : wrapped, word, :site)
89
+ wrap(call, nested ? wrapped.extent : wrapped, word, :decision)
78
90
  end
79
91
 
80
92
  # The opening of the record call around operand index of
81
- # the site.
82
- def self.record_call(site, index)
83
- "PathCoverage.record(#{site.inspect}, #{index}, "
93
+ # the decision.
94
+ def self.record_call(decision, index)
95
+ "PathCoverage.record(#{decision.inspect}, #{index}, "
84
96
  end
85
97
 
86
98
  # The layers of inserts, innermost first, that break a tie
87
99
  # between inserts wrapping the same range: a leaf's call, a
88
- # nested kind's call around its whole, eg end_items, a site's
100
+ # nested kind's call around its whole, eg end_items, a decision's
89
101
  # leave, and a statement's reach.
90
- LAYERS = %i[leaf kind site reach].freeze
102
+ LAYERS = %i[leaf kind decision reach].freeze
91
103
 
92
104
  # The two inserts that wrap the code at range in call. Their
93
105
  # order, [size, rank], nests them among inserts at the same
@@ -112,7 +124,7 @@ module PathCoverage
112
124
  # opening inserts go in lowest order first, leaving the
113
125
  # highest outermost, then closing ones highest order first,
114
126
  # so the innermost call closes first. Eg a ternary whose
115
- # predicate is a leaf: the site's leave, wrapping the whole
127
+ # predicate is a leaf: the decision's leave, wrapping the whole
116
128
  # ternary, opens outside the record wrapping its predicate.
117
129
  def self.apply(source, inserts)
118
130
  sorted = inserts.sort_by do |offset, _, order, close|
@@ -2,38 +2,38 @@ require_relative 'condition'
2
2
  require_relative 'kinds'
3
3
 
4
4
  module PathCoverage
5
- # The part of the rewriter that instruments the sites nested in
6
- # another, eg the &. chain in ok && user&.admin?. The site
7
- # around them enters and leaves; each nested site's leaves are
8
- # noted as that site's, at their place among its leaves, by the
5
+ # The part of the rewriter that instruments the decisions nested in
6
+ # another, eg the &. chain in ok && user&.admin?. The decision
7
+ # around them enters and leaves; each nested decision's leaves are
8
+ # noted as that decision's, at their place among its leaves, by the
9
9
  # calls its kind gives in place of plain records.
10
10
  module RewriterNested
11
11
  # { leaf range => the opening of its call, nil for none } for
12
- # the leaves of every site nested in condition whose kind has
12
+ # the leaves of every decision nested in condition whose kind has
13
13
  # calls of its own, condition itself included.
14
- def self.calls(site, condition)
14
+ def self.calls(decision, condition)
15
15
  leaves = condition.leaves
16
16
  rooted(condition).reduce({}) do |calls, inner|
17
17
  kind = Kinds.of(inner.operator)
18
- calls.merge(kind.calls(site, inner, leaves))
18
+ calls.merge(kind.calls(decision, inner, leaves))
19
19
  end
20
20
  end
21
21
 
22
- # The inserts, besides its leaves' calls, for every site
22
+ # The inserts, besides its leaves' calls, for every decision
23
23
  # nested in condition whose kind has calls of its own,
24
24
  # condition itself included, as its kind gives them.
25
- def self.inserts(site, condition)
25
+ def self.inserts(decision, condition)
26
26
  leaves = condition.leaves
27
27
  rooted(condition).flat_map do |inner|
28
28
  at = leaves.index(inner.operands.first)
29
29
  kind = Kinds.of(inner.operator)
30
- kind.inserts(site, inner, at, condition.commands)
30
+ kind.inserts(decision, inner, at, condition.commands)
31
31
  end
32
32
  end
33
33
 
34
34
  # condition alone when its kind has calls of its own, eg a
35
- # block method's call that is a site of its own, its leaves
36
- # then noted at their places from 0; else the sites nested in
35
+ # block method's call that is a decision of its own, its leaves
36
+ # then noted at their places from 0; else the decisions nested in
37
37
  # it.
38
38
  def self.rooted(condition)
39
39
  return [condition] if own_calls?(condition)
@@ -41,7 +41,7 @@ module PathCoverage
41
41
  nested(condition)
42
42
  end
43
43
 
44
- # The sites nested in condition whose kind has calls of its
44
+ # The decisions nested in condition whose kind has calls of its
45
45
  # own, in its operands and in its arms' conditions.
46
46
  def self.nested(condition)
47
47
  condition.operands.flat_map do |operand|
@@ -0,0 +1,84 @@
1
+ require 'json'
2
+ require_relative 'configuration'
3
+ require_relative 'render'
4
+ require_relative 'ways_totals'
5
+
6
+ module PathCoverage
7
+ # The path_coverage_report command: reports saved runs' JSON
8
+ # reports as one, each loop a decision only when asked.
9
+ module RunsReport
10
+ HELP = <<~HELP.freeze
11
+ Usage: path_coverage_report [--loops] [--max-paths N] SOURCE_ROOT GLOBS REPORT_DIR RUN_JSON...
12
+
13
+ Reports the saved runs RUN_JSON... as one, writing the report into
14
+ REPORT_DIR, then prints each method's untested paths and the totals.
15
+ Exits 0 when every path is taken, and 1 when any is not.
16
+
17
+ SOURCE_ROOT the directory holding the covered source
18
+ GLOBS comma-separated globs of the covered files, relative to SOURCE_ROOT
19
+ REPORT_DIR the directory it writes its report into
20
+ RUN_JSON... one or more saved runs' JSON files, space-separated
21
+
22
+ Options:
23
+ -h Show this help
24
+ --loops Count each loop as a decision: 0 items or 1+ items
25
+ --max-paths N Fail each method with more than N paths
26
+ (default 4096, at most 65536)
27
+
28
+ Example:
29
+ path_coverage_report --max-paths 8 /app 'lib/**/*.rb' report runs/*.json
30
+ HELP
31
+
32
+ # Reports the runs argv names, [--loops] [--max-paths N]
33
+ # SOURCE_ROOT GLOBS REPORT_DIR RUN_JSON..., as HELP says,
34
+ # printing the untested paths, then the totals, read from the
35
+ # JSON it wrote, so a run with no untested path still prints
36
+ # them; -h prints HELP. Returns the exit status: 1 when the
37
+ # report fails, as WaysTotals.failed? says, else 0.
38
+ def self.main(argv)
39
+ return help if argv.first == '-h'
40
+
41
+ options, (source_root, globs, report_dir, *runs) = options(argv)
42
+ config = configured(source_root, globs, report_dir, options)
43
+ Render.from_json(config, runs)
44
+ written = JSON.parse(File.read(config.json_path))
45
+ puts WaysTotals.lines(written)
46
+ WaysTotals.failed?(written) ? 1 : 0
47
+ end
48
+
49
+ # Prints HELP; returns 0.
50
+ def self.help
51
+ print HELP
52
+ 0
53
+ end
54
+
55
+ # [{ loops:, max_paths: }, the rest of argv] of argv's leading
56
+ # options; loops false and max_paths the default when not
57
+ # given.
58
+ def self.options(argv)
59
+ options = { loops: false, max_paths: Configuration::MAX_PATHS }
60
+ rest = argv.dup
61
+ while %w[--loops --max-paths].include?(rest.first)
62
+ flag = rest.shift
63
+ next options[:loops] = true if flag == '--loops'
64
+
65
+ options[:max_paths] = Integer(rest.shift)
66
+ end
67
+ [options, rest]
68
+ end
69
+
70
+ # The configuration reporting the files globs, comma-separated,
71
+ # match under source_root into report_dir, with the :ways
72
+ # reporter and options' loops and max_paths.
73
+ def self.configured(source_root, globs, report_dir, options)
74
+ Configuration.new do
75
+ root source_root
76
+ globs.split(',').each { cover _1 }
77
+ coverage_dir report_dir
78
+ report :ways
79
+ max_paths options[:max_paths]
80
+ loops if options[:loops]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -5,11 +5,11 @@ module PathCoverage
5
5
  # conditions on it.
6
6
  module SourceLines
7
7
  # [{number:, text:, status:, span:}] for each line of
8
- # source. A line in a site's span shares one button with
8
+ # source. A line in a decision's span shares one button with
9
9
  # the span's other lines; status and span are nil on a line
10
10
  # with no condition.
11
- def self.of(source, sites, operands)
12
- marks = marks(source, sites, operands)
11
+ def self.of(source, decisions, operands)
12
+ marks = marks(source, decisions, operands)
13
13
  source.lines.each_with_index.map do |text, index|
14
14
  number = index + 1
15
15
  mark = marks.fetch(number, {})
@@ -20,28 +20,28 @@ module PathCoverage
20
20
  end
21
21
  end
22
22
 
23
- # { line number => {status:, span:} } for each line a site
23
+ # { line number => {status:, span:} } for each line a decision
24
24
  # covers, every line of a merged span sharing that span.
25
- def self.marks(source, sites, operands)
26
- blocks = merged(spans_of(source, sites, operands))
25
+ def self.marks(source, decisions, operands)
26
+ blocks = merged(spans_of(source, decisions, operands))
27
27
  blocks.each_with_object({}) do |(span, status), marks|
28
28
  span.each { |number| marks[number] = { status:, span: } }
29
29
  end
30
30
  end
31
31
 
32
- # [span, status] for each site: from the line it starts on
32
+ # [span, status] for each decision: from the line it starts on
33
33
  # to the line its last operand ends on.
34
- def self.spans_of(source, sites, operands)
35
- sites.map do |site, condition|
34
+ def self.spans_of(source, decisions, operands)
35
+ decisions.map do |decision, condition|
36
36
  last = condition.leaves.last
37
- span = line_of(site)..end_line(source, last)
38
- [span, status_of(operands.fetch(site))]
37
+ span = line_of(decision)..end_line(source, last)
38
+ [span, status_of(operands.fetch(decision))]
39
39
  end
40
40
  end
41
41
 
42
42
  # The spans merged wherever they overlap, in line order. A
43
- # merged span has :gaps if any of its sites has, so a
44
- # covered site never hides another's gaps.
43
+ # merged span has :gaps if any of its decisions has, so a
44
+ # covered decision never hides another's gaps.
45
45
  def self.merged(spans)
46
46
  sorted = spans.sort_by { |span, _| span.begin }
47
47
  sorted.each_with_object([]) do |(span, status), blocks|
@@ -66,9 +66,9 @@ module PathCoverage
66
66
  gaps.empty? ? :covered : :gaps
67
67
  end
68
68
 
69
- # The line a site starts on, from its path:line:column name.
70
- def self.line_of(site)
71
- site[/:(\d+):\d+\z/, 1].to_i
69
+ # The line a decision starts on, from its path:line:column name.
70
+ def self.line_of(decision)
71
+ decision[/:(\d+):\d+\z/, 1].to_i
72
72
  end
73
73
 
74
74
  # The line of source on which byte range ends.