path_coverage 0.4.0 → 0.6.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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +260 -300
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +109 -0
  5. data/lib/path_coverage/calls.rb +73 -43
  6. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  7. data/lib/path_coverage/condition.rb +14 -14
  8. data/lib/path_coverage/configuration.rb +7 -4
  9. data/lib/path_coverage/configuration_limits.rb +52 -0
  10. data/lib/path_coverage/configuration_paths.rb +7 -7
  11. data/lib/path_coverage/console_summary.rb +28 -26
  12. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  13. data/lib/path_coverage/decision_locations.rb +76 -0
  14. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  15. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  16. data/lib/path_coverage/fold_branches.rb +18 -0
  17. data/lib/path_coverage/fold_cells.rb +42 -0
  18. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
  19. data/lib/path_coverage/gaps.rb +33 -15
  20. data/lib/path_coverage/json_reader.rb +54 -19
  21. data/lib/path_coverage/json_reports.rb +109 -0
  22. data/lib/path_coverage/json_writer.rb +29 -14
  23. data/lib/path_coverage/kernel.rb +77 -58
  24. data/lib/path_coverage/kinds/base.rb +16 -16
  25. data/lib/path_coverage/kinds/block.rb +16 -15
  26. data/lib/path_coverage/kinds/case.rb +14 -12
  27. data/lib/path_coverage/kinds/chain.rb +40 -11
  28. data/lib/path_coverage/kinds/choice.rb +4 -3
  29. data/lib/path_coverage/kinds/contains.rb +40 -32
  30. data/lib/path_coverage/kinds/safe.rb +38 -26
  31. data/lib/path_coverage/kinds/write.rb +6 -6
  32. data/lib/path_coverage/kinds.rb +2 -2
  33. data/lib/path_coverage/leaving.rb +52 -0
  34. data/lib/path_coverage/loop_forks.rb +57 -0
  35. data/lib/path_coverage/loop_nodes.rb +91 -0
  36. data/lib/path_coverage/manifest.rb +92 -58
  37. data/lib/path_coverage/manifest_blocks.rb +5 -26
  38. data/lib/path_coverage/manifest_case.rb +2 -2
  39. data/lib/path_coverage/manifest_chains.rb +8 -8
  40. data/lib/path_coverage/manifest_choices.rb +23 -20
  41. data/lib/path_coverage/manifest_disabled.rb +97 -0
  42. data/lib/path_coverage/manifest_folding.rb +43 -40
  43. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  44. data/lib/path_coverage/manifest_writes.rb +11 -10
  45. data/lib/path_coverage/measured_by.rb +20 -0
  46. data/lib/path_coverage/merge.rb +7 -7
  47. data/lib/path_coverage/method_loops.rb +172 -0
  48. data/lib/path_coverage/method_statements.rb +150 -134
  49. data/lib/path_coverage/method_ways.rb +170 -0
  50. data/lib/path_coverage/paths_context.rb +104 -0
  51. data/lib/path_coverage/paths_fitting.rb +31 -0
  52. data/lib/path_coverage/paths_forks.rb +72 -0
  53. data/lib/path_coverage/paths_gaps.rb +137 -0
  54. data/lib/path_coverage/paths_leaving.rb +40 -0
  55. data/lib/path_coverage/paths_report.rb +16 -16
  56. data/lib/path_coverage/paths_rescues.rb +146 -0
  57. data/lib/path_coverage/paths_scopes.rb +37 -0
  58. data/lib/path_coverage/paths_tree.rb +203 -0
  59. data/lib/path_coverage/paths_way_lists.rb +72 -0
  60. data/lib/path_coverage/paths_ways.rb +193 -0
  61. data/lib/path_coverage/recorder.rb +71 -36
  62. data/lib/path_coverage/recorder_blocks.rb +17 -17
  63. data/lib/path_coverage/recorder_cases.rb +12 -11
  64. data/lib/path_coverage/recorder_chains.rb +12 -12
  65. data/lib/path_coverage/recorder_writes.rb +9 -9
  66. data/lib/path_coverage/render.rb +58 -51
  67. data/lib/path_coverage/rescue_forks.rb +174 -0
  68. data/lib/path_coverage/result.rb +32 -31
  69. data/lib/path_coverage/rewriter.rb +116 -35
  70. data/lib/path_coverage/rewriter_nested.rb +13 -13
  71. data/lib/path_coverage/runs_report.rb +84 -0
  72. data/lib/path_coverage/source_lines.rb +16 -16
  73. data/lib/path_coverage/source_report.rb +100 -0
  74. data/lib/path_coverage/stable_leaves.rb +162 -0
  75. data/lib/path_coverage/statement_arms.rb +53 -38
  76. data/lib/path_coverage/summary.rb +4 -62
  77. data/lib/path_coverage/totals.rb +12 -10
  78. data/lib/path_coverage/version.rb +1 -1
  79. data/lib/path_coverage/walk.rb +106 -0
  80. data/lib/path_coverage/way_algebras.rb +127 -0
  81. data/lib/path_coverage/ways_report.rb +91 -0
  82. data/lib/path_coverage/ways_totals.rb +46 -0
  83. data/lib/path_coverage/within_methods.rb +152 -97
  84. metadata +39 -30
  85. data/lib/path_coverage/card.rb +0 -14
  86. data/lib/path_coverage/card_tree.rb +0 -186
  87. data/lib/path_coverage/card_ways.rb +0 -140
  88. data/lib/path_coverage/html/file.html +0 -33
  89. data/lib/path_coverage/html/index.html +0 -29
  90. data/lib/path_coverage/html/settings.js +0 -50
  91. data/lib/path_coverage/html/side.js +0 -599
  92. data/lib/path_coverage/html/sort.js +0 -34
  93. data/lib/path_coverage/html/style.css +0 -484
  94. data/lib/path_coverage/html/tabs.js +0 -15
  95. data/lib/path_coverage/html/theme.js +0 -27
  96. data/lib/path_coverage/html_branch.rb +0 -30
  97. data/lib/path_coverage/html_cards.rb +0 -88
  98. data/lib/path_coverage/html_cells.rb +0 -71
  99. data/lib/path_coverage/html_file.rb +0 -123
  100. data/lib/path_coverage/html_index.rb +0 -128
  101. data/lib/path_coverage/html_index_head.rb +0 -29
  102. data/lib/path_coverage/html_labels.rb +0 -16
  103. data/lib/path_coverage/html_methods.rb +0 -66
  104. data/lib/path_coverage/html_nav.rb +0 -67
  105. data/lib/path_coverage/html_report.rb +0 -131
  106. data/lib/path_coverage/html_statements.rb +0 -186
  107. data/lib/path_coverage/html_tabs.rb +0 -26
  108. data/lib/path_coverage/html_template.rb +0 -26
@@ -1,11 +1,13 @@
1
1
  require 'json'
2
2
  require_relative 'kinds'
3
+ require_relative 'measured_by'
3
4
  require_relative 'merge'
5
+ require_relative 'ways_report'
4
6
 
5
7
  module PathCoverage
6
8
  # Reads back the JSON reports JsonWriter writes, merging the
7
9
  # counts of several runs, and refusing any report whose
8
- # sites' paths are listed differently from a render's.
10
+ # decisions' paths are listed differently from a render's.
9
11
  module JsonReader
10
12
  # The keys of a JSON report's fingerprint.
11
13
  FINGERPRINT = %w[ignored treats].freeze
@@ -18,13 +20,42 @@ module PathCoverage
18
20
  end
19
21
 
20
22
  # The paths taken of the JSON reports at paths, as
21
- # Render.run takes them: site_paths:, each site's possible
22
- # paths, outside:, those a treatment rules out, and reached:,
23
- # each statement's reaches, each merged.
23
+ # Render.run takes them: decision_paths:, each decision's possible
24
+ # paths, outside:, those a treatment rules out, reached:,
25
+ # each statement's reaches, and ways:, each method's calls'
26
+ # ways and each loop's passes', each merged.
24
27
  def self.taken(paths)
25
- site_paths = Merge.paths(paths.map { |path| paths_of(path) })
28
+ decision_paths = Merge.paths(paths.map do |path|
29
+ paths_of(path)
30
+ end)
26
31
  outside = merged(paths, 'outside')
27
- { site_paths:, outside:, reached: reached(paths) }
32
+ { decision_paths:, outside:, reached: reached(paths),
33
+ ways: ways(paths) }
34
+ end
35
+
36
+ # The ways of the JSON reports at paths, as WaysReport.ways
37
+ # reads them, each unit's way's counts summed; nil when none
38
+ # lists any, eg reports the :post reporter sent, so a render
39
+ # cannot say which ways were taken.
40
+ def self.ways(paths)
41
+ runs = paths.map do |path|
42
+ report = JSON.parse(File.read(path))
43
+ listed_ways?(report) ? WaysReport.ways(report) : nil
44
+ end.compact
45
+ return nil if runs.empty?
46
+
47
+ runs.reduce do |all, run|
48
+ all.merge(run) do |_, ways, more|
49
+ ways.merge(more) { |_, a, b| a + b }
50
+ end
51
+ end
52
+ end
53
+
54
+ # True when report, a JSON report, lists its flows' ways: one a
55
+ # run wrote, which names the max_paths its flows were listed
56
+ # with; not one the :post reporter sent, which has no flows.
57
+ def self.listed_ways?(report)
58
+ report.key?('max_paths')
28
59
  end
29
60
 
30
61
  # Example: { "k.rb:2:2" => 2 } and { "k.rb:2:2" => 1 } give
@@ -41,15 +72,19 @@ module PathCoverage
41
72
  end
42
73
 
43
74
  # Raises unless json, the JSON report at path, measured the
44
- # kinds a render does, with fingerprint; one that measured
45
- # others, or names none, lists its sites' paths differently,
46
- # so its counts cannot be trusted to the paths a render
47
- # lists.
48
- def self.same_kinds(path, json, fingerprint)
75
+ # kinds a render does, with fingerprint, and was measured by
76
+ # the same tools at the same versions; one that differs in
77
+ # either, or names none, can list its decisions' paths
78
+ # differently, so its counts cannot be trusted to the paths a
79
+ # render lists.
80
+ def self.same_measuring(path, json, fingerprint)
49
81
  kinds = json['kinds'] == Kinds.names
50
- return if kinds && json.slice(*FINGERPRINT) == fingerprint
82
+ unless kinds && json.slice(*FINGERPRINT) == fingerprint
83
+ raise ArgumentError, "#{path} measured other kinds"
84
+ end
85
+ return if json['measured_by'] == MeasuredBy.versions
51
86
 
52
- raise ArgumentError, "#{path} measured other kinds"
87
+ raise ArgumentError, "#{path} was measured by other tools"
53
88
  end
54
89
 
55
90
  # The key lists of the JSON reports at paths, as listed_of
@@ -59,7 +94,7 @@ module PathCoverage
59
94
  end
60
95
 
61
96
  # The JSON report at path's list under key, eg its paths a
62
- # treatment rules out, { site => { path => count } }; none
97
+ # treatment rules out, { decision => { path => count } }; none
63
98
  # for a report that has none, so the render claims none.
64
99
  def self.listed_of(path, key)
65
100
  json = JSON.parse(File.read(path))
@@ -68,12 +103,12 @@ module PathCoverage
68
103
  end
69
104
  end
70
105
 
71
- # The site paths of the JSON report at path, as
106
+ # The decision paths of the JSON report at path, as
72
107
  # Result#paths gives them; none for a report that has none.
73
108
  def self.paths_of(path)
74
109
  json = JSON.parse(File.read(path))
75
- json.fetch('paths', {}).transform_values do |site|
76
- site['taken'].map do |each|
110
+ json.fetch('paths', {}).transform_values do |decision|
111
+ decision['taken'].map do |each|
77
112
  { path: each['path'], count: each['count'] }
78
113
  end
79
114
  end
@@ -84,8 +119,8 @@ module PathCoverage
84
119
  # fingerprint.
85
120
  def self.read(path, fingerprint)
86
121
  json = JSON.parse(File.read(path))
87
- same_kinds(path, json, fingerprint)
88
- json['sites'].transform_values do |leaves|
122
+ same_measuring(path, json, fingerprint)
123
+ json['decisions'].transform_values do |leaves|
89
124
  leaves.map { |leaf| counts(leaf) }
90
125
  end
91
126
  end
@@ -0,0 +1,109 @@
1
+ require 'fileutils'
2
+ require 'json'
3
+ require_relative 'paths_gaps'
4
+ require_relative 'console_summary'
5
+ require_relative 'json_writer'
6
+ require_relative 'paths_report'
7
+ require_relative 'source_report'
8
+ require_relative 'within_methods'
9
+
10
+ module PathCoverage
11
+ # The two JSON reports every run writes, and every render of
12
+ # runs merged, into coverage_dir: path_coverage.json, the
13
+ # counts, and path_coverage.source.json, what drawing each
14
+ # file's code needs, for html-reporter to draw the pages from.
15
+ module JsonReports
16
+ # What WithinMethods reads of a run: operands, decision paths,
17
+ # and paths a treatment rules out, as Result gives them.
18
+ Recorded = Data.define(:operands, :paths, :outside)
19
+
20
+ # Writes both reports into config's coverage_dir, created
21
+ # first if need be. operands, and taken's decision_paths: and
22
+ # outside:, are as Result gives them, taken's reached: as
23
+ # Recorder#reached gives it, nil when not known; files is
24
+ # { full path => {source:, decisions:} } for each covered file.
25
+ def self.write(operands, files, config, **taken)
26
+ FileUtils.mkdir_p(config.output_dir)
27
+ paths = taken.values_at(:decision_paths, :outside)
28
+ recorded = Recorded.new(operands, *paths)
29
+ reached = taken[:reached]
30
+ flows = WithinMethods.listed(files, config, recorded,
31
+ [reached, taken[:ways]])
32
+ tree = WithinMethods.tree(files, config, recorded)
33
+ write_json(recorded, files, config, [flows, reached, tree])
34
+ write_ways(flows, files, config, tree)
35
+ write_source(files, config, recorded.paths, flows)
36
+ end
37
+
38
+ # Writes recorded as the JSON report at config's json_path:
39
+ # its operands, decision paths, paths a treatment rules out,
40
+ # totals, flows, each def's flow, as WithinMethods.listed
41
+ # gives them, tree, how each flow's rows nest, the statements
42
+ # reached, when known, and config's fingerprint and limits.
43
+ def self.write_json(recorded, files, config, listed)
44
+ flows, reached, tree = listed
45
+ report = PathsReport.of(recorded.paths, files)
46
+ totals = totals(recorded.paths, files, config, reached)
47
+ limits = { 'loops' => config.loops?,
48
+ 'max_paths' => config.path_limit }
49
+ parts = { totals:, outside: recorded.outside, flows:, tree:,
50
+ fingerprint: config.fingerprint, limits: }
51
+ parts[:reached] = reached if reached
52
+ path = config.json_path
53
+ JsonWriter.write(recorded.operands, path, report, **parts)
54
+ end
55
+
56
+ # Writes the ways lines, each method's untested paths, as
57
+ # PathsGaps.lines gives them for flows, beside the JSON report,
58
+ # at ways_path, so a later check, or the :ways reporter, can
59
+ # show them; tree is how each flow's rows nest, as
60
+ # WithinMethods.tree gives it.
61
+ def self.write_ways(flows, files, config, tree)
62
+ texts = PathsReport.texts(files)
63
+ lines = PathsGaps.lines(flows, texts, parents_of(tree))
64
+ File.write(ways_path(config), lines.map { "#{_1}\n" }.join)
65
+ end
66
+
67
+ # { row's decision => its parent's decision } of each row in
68
+ # tree that runs inside another row, as WithinMethods.tree
69
+ # gives it. Each decision is named path:line:column.
70
+ def self.parents_of(tree)
71
+ tree.flat_map do |path, nested|
72
+ nested.values.flat_map do |rows|
73
+ rows.filter_map do |at, place|
74
+ next unless place['parent']
75
+
76
+ ["#{path}:#{at}", "#{path}:#{place['parent']}"]
77
+ end
78
+ end
79
+ end.to_h
80
+ end
81
+
82
+ # Where the ways lines go, beside the JSON report.
83
+ def self.ways_path(config)
84
+ config.json_path.sub(/\.json\z/, '.ways.txt')
85
+ end
86
+
87
+ # Writes the source report beside the JSON report, from
88
+ # files, decision_paths and flows, as write has them.
89
+ def self.write_source(files, config, decision_paths, flows)
90
+ source = SourceReport.of(files, config, decision_paths, flows)
91
+ path = config.json_path.sub(/\.json\z/, '.source.json')
92
+ File.write(path, JSON.generate(source))
93
+ end
94
+
95
+ # The JSON report's totals, each [taken, possible] paths:
96
+ # within each decision, and within each method; with groups
97
+ # configured, each group's own too, under groups.
98
+ def self.totals(decision_paths, files, config, reached)
99
+ flows = WithinMethods.flows(files, config, decision_paths,
100
+ reached)
101
+ totals = ConsoleSummary.totals(decision_paths, flows)
102
+ groups = config.groups
103
+ return totals if groups.empty?
104
+
105
+ own = ConsoleSummary.group_totals(decision_paths, flows, groups)
106
+ totals.merge('groups' => own)
107
+ end
108
+ end
109
+ end
@@ -1,45 +1,60 @@
1
1
  require 'json'
2
2
  require_relative 'kinds'
3
+ require_relative 'measured_by'
3
4
 
4
5
  module PathCoverage
5
6
  # The base reporter: the result as raw JSON, which every
6
7
  # other reporter can read back.
7
8
  module JsonWriter
9
+ # The URL of the schema the JSON report follows.
10
+ SCHEMA = 'https://path-coverage.github.io/schemas/v2/' \
11
+ 'path_coverage.schema.json'.freeze
12
+
8
13
  # Writes operands, as Result gives them, paths, as
9
- # PathsReport gives them, and totals, as Kernel.totals gives
10
- # them, to path as {"sites": {site: [counts, ...]}, "paths":
11
- # {site: {leaves:, taken:}}, "totals": {"decisions": [taken,
12
- # possible]}}.
14
+ # PathsReport gives them, and totals, as JsonReports.totals
15
+ # gives them, to path as {"decisions": {decision: [counts, ...]},
16
+ # "paths": {decision: {leaves:, taken:}}, "totals": {"alone":
17
+ # [taken, possible]}}.
13
18
  # parts are json's.
14
19
  def self.write(operands, path, paths = {}, **parts)
15
20
  File.write(path, json(operands, paths, **parts))
16
21
  end
17
22
 
18
- # The keys of parts listed as { site => [{path:, count:}] }:
23
+ # The keys of parts listed as { decision => [{path:, count:}] }:
19
24
  # each path taken that a treatment rules out, as
20
25
  # Result#outside gives them.
21
26
  LISTED = %i[outside].freeze
22
27
 
23
28
  # operands, and any paths, and parts' outside:, totals:,
24
- # cards:, as WithinMethods.listed gives them, and tree:, as
29
+ # flows:, as WithinMethods.listed gives them, and tree:, as
25
30
  # WithinMethods.tree gives it, as the JSON report's text, with
26
31
  # parts' fingerprint:, as Configuration#fingerprint gives
27
- # it, the names of the kinds measured, and parts' reached:,
32
+ # it, the names of the kinds measured, the tools that measured
33
+ # it and their versions, and parts' reached:,
28
34
  # as Recorder#reached gives it, even empty, as a run that
29
- # reached no statement says so; none when not given.
35
+ # reached no statement says so; none when not given; parts'
36
+ # limits:, { "loops" =>, "max_paths" => } the flows were
37
+ # listed with; headed by its schema's URL.
30
38
  def self.json(operands, paths = {}, **parts)
31
39
  fingerprint = parts.fetch(:fingerprint, {})
32
- report = { 'sites' => operands, **fingerprint }
40
+ report = { '$schema' => SCHEMA, 'decisions' => operands,
41
+ **fingerprint }
33
42
  report['paths'] = paths unless paths.empty?
34
- report.merge!(lists(parts))
35
- report['reached'] = parts[:reached] if parts.key?(:reached)
36
- report.merge!(kept(parts))
43
+ report.merge!(lists(parts), counted(parts), kept(parts))
37
44
  report['kinds'] = Kinds.names
45
+ report['measured_by'] = MeasuredBy.versions
38
46
  JSON.pretty_generate(report)
39
47
  end
40
48
 
49
+ # { "reached" =>, "loops" =>, "max_paths" => } of parts'
50
+ # reached:, when given, and limits:, when given.
51
+ def self.counted(parts)
52
+ reached = parts.slice(:reached).transform_keys(&:to_s)
53
+ reached.merge(parts.fetch(:limits, {}))
54
+ end
55
+
41
56
  # The keys of parts written as they are, when not empty.
42
- KEPT = %i[totals cards tree].freeze
57
+ KEPT = %i[totals flows tree].freeze
43
58
 
44
59
  # { key => its value } of each of parts' KEPT keys that has
45
60
  # a value not empty.
@@ -59,7 +74,7 @@ module PathCoverage
59
74
  end.to_h
60
75
  end
61
76
 
62
- # taken, { site => { path => count } }, as JSON lists it,
77
+ # taken, { decision => { path => count } }, as JSON lists it,
63
78
  # eg { 'k.rb:2:2' => [{ path: [true, false], count: 1 }] }.
64
79
  def self.listed(taken)
65
80
  taken.transform_values do |counts|
@@ -1,12 +1,10 @@
1
- require 'fileutils'
2
1
  require_relative 'calls'
3
2
  require_relative 'configuration'
4
- require_relative 'console_summary'
5
- require_relative 'json_writer'
6
3
  require_relative 'manifest'
7
- require_relative 'paths_report'
4
+ require_relative 'method_loops'
8
5
  require_relative 'recorder'
9
6
  require_relative 'render'
7
+ require_relative 'rescue_forks'
10
8
  require_relative 'result'
11
9
  require_relative 'rewriter'
12
10
  require_relative 'within_methods'
@@ -17,7 +15,7 @@ require_relative 'within_methods'
17
15
  module PathCoverage
18
16
  @recorder = Recorder.new
19
17
  @config = nil
20
- # { full path => {source:, sites:} } of each covered file
18
+ # { full path => {source:, decisions:} } of each covered file
21
19
  # parsed as it loaded, so the exit report need not read it
22
20
  # again.
23
21
  @parsed = {}
@@ -88,71 +86,90 @@ module PathCoverage
88
86
  end
89
87
 
90
88
  # The compiled, rewritten code of path, or nil for a path
91
- # config does not cover, which Ruby then loads as normal.
89
+ # config does not cover, which Ruby then loads as normal. Each
90
+ # call of a method in it is traced, as its way is recorded.
92
91
  def self.instrumented(path, config)
93
92
  return unless config.covers?(path)
94
93
 
95
94
  parsed = @parsed[path] = Manifest.parse(path, config)
96
- source = parsed[:source]
97
- reaches = WithinMethods.reaches(path, parsed, config)
98
- rewritten = Rewriter.rewrite(source, parsed[:sites], reaches)
99
- RubyVM::InstructionSequence.compile(rewritten, path, path)
95
+ args = [parsed[:source], config.relative(path),
96
+ parsed[:decisions]]
97
+ loops = MethodLoops.of(*args)
98
+ rescues = RescueForks.found(*args.first(2))
99
+ rewritten = rewritten(args, [loops, rescues])
100
+ iseq = RubyVM::InstructionSequence
101
+ traced(iseq.compile(rewritten, path, path), known(args, loops))
100
102
  end
101
103
 
102
- # Writes the JSON report, then runs the chosen reporters, for
103
- # every covered file whether or not the run loaded it, into
104
- # coverage_dir, created first if need be.
105
- def self.report(config)
106
- files = parsed_files(config)
107
- result = collect(files, config)
108
- Render.run(result.operands, files, config,
109
- **result.taken, reached: @recorder.reached)
104
+ # [loops, repeats, restarts] of the file of args, [source, path,
105
+ # decisions], as traced takes them, loops as MethodLoops gives
106
+ # them.
107
+ def self.known(args, loops)
108
+ [loops, MethodLoops.repeats(*args), MethodLoops.restarts(args[0])]
110
109
  end
111
110
 
112
- # The Result of every covered file, with the counts and site
113
- # paths this run recorded, once its operands and paths are
114
- # written as the JSON report.
115
- def self.collect(files, config)
116
- sites = merged_sites(files)
117
- result = Result.new(sites, @recorder.counts, @recorder.site_paths)
118
- FileUtils.mkdir_p(config.output_dir)
119
- write_json(result, files, config)
120
- result
111
+ # source rewritten, the file at relative path, with decisions
112
+ # as Manifest gives them. Each decision is recorded. Each
113
+ # statement's reach is counted. Each pass of each of loops,
114
+ # { entry => loops }, is noted. Each def's entry is counted,
115
+ # before any statement's reach there. Each rescue running, and
116
+ # each body completing, is noted where rescues, as
117
+ # RescueForks.found gives them, finds them.
118
+ def self.rewritten((source, relative, decisions), (loops, rescues))
119
+ found = MethodStatements.collect(source, relative, decisions)
120
+ passes = MethodLoops.passes(source, relative, loops)
121
+ marks = { reaches: found.reaches, passes:,
122
+ entries: found.entries,
123
+ completions: rescues.completions,
124
+ rescues: rescues.starts, values: rescues.values }
125
+ Rewriter.rewrite(source, decisions, marks)
121
126
  end
122
127
 
123
- # Writes result as the JSON report at config's json_path: its
124
- # operands, site paths, paths a treatment rules out, totals,
125
- # each def's card, how each card's rows nest, the statements
126
- # the recorder reached, so a render from the JSON alone has
127
- # them, and config's fingerprint.
128
- def self.write_json(result, files, config)
129
- report = PathsReport.of(result.paths, files)
130
- reached = @recorder.reached
131
- cards = WithinMethods.listed(files, config, result, reached)
132
- tree = WithinMethods.tree(files, config, result)
133
- parts = {
134
- totals: totals(result, files, config), outside: result.outside,
135
- cards:, tree:, fingerprint: config.fingerprint, reached:
136
- }
137
- path = config.json_path
138
- JsonWriter.write(result.operands, path, report, **parts)
128
+ # compiled, after noting each of its methods' loops and repeats,
129
+ # [{ entry => loops }, { entry => repeats }], as MethodLoops
130
+ # gives them, on the recorder's trace. Each call of them, and
131
+ # only of them, is traced on the trace of whichever start is
132
+ # current as it runs. restarts is the lines of the defs a retry
133
+ # restarts, as MethodLoops gives them. Code with no method has
134
+ # no call to trace, and TracePoint refuses to trace it.
135
+ def self.traced(compiled, (loops, repeats, restarts))
136
+ return compiled if loops.empty?
137
+
138
+ loops.each do |entry, held|
139
+ @recorder.trace.know(entry, held, repeats[entry])
140
+ end
141
+ hook = ->(point) { on_call_event(point, restarts) }
142
+ TracePoint.new(:call, :return, &hook).enable(target: compiled)
143
+ compiled
139
144
  end
140
145
 
141
- # The JSON report's totals, each [taken, possible] paths:
142
- # within each decision, and within each method; with groups
143
- # configured, each group's own too, under groups.
144
- def self.totals(result, files, config)
145
- reached = @recorder.reached
146
- cards = WithinMethods.cards(files, config, result.paths, reached)
147
- totals = ConsoleSummary.totals(result.paths, cards)
148
- groups = config.groups
149
- return totals if groups.empty?
146
+ # Notes point, a TracePoint's :call or :return of a covered
147
+ # method, on the current start's trace. A call of a def a retry
148
+ # restarts, its line in restarts, is noted with its frame's
149
+ # depth, so a restart is not taken for a new call.
150
+ def self.on_call_event(point, restarts)
151
+ trace = @recorder.trace
152
+ return trace.return if point.event == :return
153
+ return trace.call unless restarts.include?(point.lineno)
150
154
 
151
- own = ConsoleSummary.group_totals(result.paths, cards, groups)
152
- totals.merge('groups' => own)
155
+ trace.restartable_call(caller_locations.size)
156
+ end
157
+
158
+ # Writes the JSON reports, then runs the chosen reporters, for
159
+ # every covered file whether or not the run loaded it, from
160
+ # the counts and decision paths this run recorded.
161
+ def self.report(config)
162
+ files = parsed_files(config)
163
+ decisions = merged_decisions(files)
164
+ result = Result.new(decisions, @recorder.counts,
165
+ @recorder.decision_paths)
166
+ reached = @recorder.reached
167
+ ways = @recorder.trace.ways
168
+ Render.run(result.operands, files, config,
169
+ **result.taken, reached:, ways:)
153
170
  end
154
171
 
155
- # { full path => {source:, sites:} } for every covered file:
172
+ # { full path => {source:, decisions:} } for every covered file:
156
173
  # as parsed when it loaded, or parsed now for a file the run
157
174
  # never loaded.
158
175
  def self.parsed_files(config)
@@ -161,8 +178,10 @@ module PathCoverage
161
178
  end
162
179
  end
163
180
 
164
- # The sites of every covered file, in one manifest.
165
- def self.merged_sites(files)
166
- files.values.map { |parsed| parsed[:sites] }.reduce({}, :merge)
181
+ # The decisions of every covered file, in one manifest.
182
+ def self.merged_decisions(files)
183
+ files.values.map do |parsed|
184
+ parsed[:decisions]
185
+ end.reduce({}, :merge)
167
186
  end
168
187
  end
@@ -3,48 +3,48 @@ module PathCoverage
3
3
  # The hooks every kind has, each as most kinds need it; a
4
4
  # kind extends Base and defines its own in place of any.
5
5
  module Base
6
- # The ways a site of this kind as a whole can be evaluated:
6
+ # The ways a decision of this kind as a whole can be evaluated:
7
7
  # as it can be nested in another.
8
8
  def root_ways(node, fixed)
9
9
  ways(node, fixed)
10
10
  end
11
11
 
12
- # Whether its leaves, nested in a site, need calls of their
12
+ # Whether its leaves, nested in a decision, need calls of their
13
13
  # own in place of record calls; false for most.
14
14
  def own_calls?
15
15
  false
16
16
  end
17
17
 
18
- # The inserts for a site of this kind: its leaves' records,
18
+ # The inserts for a decision of this kind: its leaves' records,
19
19
  # and its leave around them.
20
- def site_inserts(site, condition)
21
- Rewriter.record_inserts(site, condition) +
22
- Rewriter.trail_inserts(site, condition)
20
+ def decision_inserts(decision, condition)
21
+ Rewriter.record_inserts(decision, condition) +
22
+ Rewriter.trail_inserts(decision, condition)
23
23
  end
24
24
 
25
- # What a site of this kind's leave call wraps: the whole of
25
+ # What a decision of this kind's leave call wraps: the whole of
26
26
  # it, for most.
27
27
  def trailed(condition)
28
28
  condition
29
29
  end
30
30
 
31
- # Its node in a site's tree, each leaf taking the next index
31
+ # Its node in a decision's tree, each leaf taking the next index
32
32
  # of leaves: its operands' nodes, then its arms', if any.
33
33
  def node(source, condition, outcomes, leaves)
34
34
  built = [outcomes, leaves]
35
- operands = SiteTree.operands_of(source, condition, *built)
36
- SiteTree.arms(source, condition, operands, *built)
35
+ operands = DecisionTree.operands_of(source, condition, *built)
36
+ DecisionTree.arms(source, condition, operands, *built)
37
37
  end
38
38
 
39
39
  # The nodes its node stands for among its siblings' in a
40
- # site's tree on a file's page: itself, for most.
40
+ # decision's tree on a file's page: itself, for most.
41
41
  def unfold(node)
42
42
  [node]
43
43
  end
44
44
 
45
45
  # The inserts, besides its leaves' calls, for one nested in
46
- # a site at its at-th leaf: none for most.
47
- def inserts(_site, _inner, _at, _commands)
46
+ # a decision at its at-th leaf: none for most.
47
+ def inserts(_decision, _inner, _at, _commands)
48
48
  []
49
49
  end
50
50
 
@@ -54,14 +54,14 @@ module PathCoverage
54
54
  nodes
55
55
  end
56
56
 
57
- # The text a site of this kind opens and closes with, as
57
+ # The text a decision of this kind opens and closes with, as
58
58
  # [opening, closing], eg ['{', '}'] for a block's braces.
59
59
  # Most kinds have none, so ['', ''].
60
- def brackets(_source, _site)
60
+ def brackets(_source, _decision)
61
61
  ['', '']
62
62
  end
63
63
 
64
- # Its operands' rows in a site's tree on a file's page, nil
64
+ # Its operands' rows in a decision's tree on a file's page, nil
65
65
  # for most, whose operands branch from its operator.
66
66
  def rows(_condition, _indent)
67
67
  nil
@@ -58,12 +58,12 @@ module PathCoverage
58
58
  end
59
59
  }.freeze
60
60
 
61
- # Whether site's result has items on the path that took
62
- # outcomes; nil when its kind cannot tell, eg for a site
61
+ # Whether decision's result has items on the path that took
62
+ # outcomes; nil when its kind cannot tell, eg for a decision
63
63
  # that is no block method's call.
64
- def self.items?(site, outcomes)
65
- receiver, body = site.operands
66
- rule = HAS_ITEMS[site.operator]
64
+ def self.items?(decision, outcomes)
65
+ receiver, body = decision.operands
66
+ rule = HAS_ITEMS[decision.operator]
67
67
  rule&.call(outcomes[receiver], outcomes[body])
68
68
  end
69
69
 
@@ -97,10 +97,10 @@ module PathCoverage
97
97
  # Its block's opening and closing in source, ['{', '}'] or
98
98
  # ['do', 'end']: the opening between its receiver and its
99
99
  # block's expression, the closing after that expression.
100
- def self.brackets(source, site)
101
- receiver, body = site.operands
100
+ def self.brackets(source, decision)
101
+ receiver, body = decision.operands
102
102
  between = source.byteslice(receiver.end...body.begin)
103
- closing = source.byteslice(body.end...site.extent.end)
103
+ closing = source.byteslice(body.end...decision.extent.end)
104
104
  [between[/\{|\bdo\b/], closing.strip]
105
105
  end
106
106
 
@@ -115,19 +115,20 @@ module PathCoverage
115
115
  # item_at call, noting its last answer, or for a method that
116
116
  # calls it with every item an answer_at call, noting every
117
117
  # answer, for its block's expression.
118
- def self.calls(site, inner, leaves)
118
+ def self.calls(decision, inner, leaves)
119
119
  receiver, body = inner.operands
120
120
  at = leaves.index(receiver)
121
121
  every = EVERY_ITEM.include?(inner.operator)
122
122
  noter = every ? 'answer_at' : 'item_at'
123
- { receiver => nil,
124
- body => "PathCoverage.#{noter}(#{site.inspect}, #{at}, " }
123
+ opening = "PathCoverage.#{noter}(#{decision.inspect}, #{at}, "
124
+ { receiver => nil, body => opening }
125
125
  end
126
126
 
127
- # Its call wrapped whole, its receiver the site's at-th leaf,
128
- # so its items are begun before it runs and counted after.
129
- def self.inserts(site, inner, at, _commands)
130
- name = site.inspect
127
+ # Its call wrapped whole, its receiver the decision's at-th
128
+ # leaf, so its items are begun before it runs and counted
129
+ # after.
130
+ def self.inserts(decision, inner, at, _commands)
131
+ name = decision.inspect
131
132
  begun = "PathCoverage.begin_items(#{name}, #{at})"
132
133
  call = "PathCoverage.end_items(#{name}, #{at}, #{begun}, "
133
134
  Rewriter.wrap(call, inner.extent, false, :kind)