path_coverage 0.3.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.
- checksums.yaml +4 -4
- data/README.md +316 -177
- data/exe/path_coverage_report +4 -0
- data/lib/path_coverage/call_trace.rb +88 -0
- data/lib/path_coverage/calls.rb +49 -43
- data/lib/path_coverage/card_context.rb +104 -0
- data/lib/path_coverage/card_forks.rb +44 -0
- data/lib/path_coverage/card_gaps.rb +125 -0
- data/lib/path_coverage/card_scopes.rb +37 -0
- data/lib/path_coverage/card_tree.rb +199 -0
- data/lib/path_coverage/card_way_lists.rb +72 -0
- data/lib/path_coverage/card_ways.rb +196 -0
- data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +38 -25
- data/lib/path_coverage/condition.rb +14 -14
- data/lib/path_coverage/configuration.rb +7 -4
- data/lib/path_coverage/configuration_limits.rb +52 -0
- data/lib/path_coverage/configuration_paths.rb +7 -7
- data/lib/path_coverage/console_summary.rb +24 -22
- data/lib/path_coverage/{site.rb → decision.rb} +2 -2
- data/lib/path_coverage/decision_locations.rb +76 -0
- data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +18 -8
- data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
- data/lib/path_coverage/fold_branches.rb +18 -0
- data/lib/path_coverage/fold_cells.rb +42 -0
- data/lib/path_coverage/fold_rows.rb +140 -0
- data/lib/path_coverage/gaps.rb +33 -15
- data/lib/path_coverage/json_reader.rb +54 -19
- data/lib/path_coverage/json_reports.rb +102 -0
- data/lib/path_coverage/json_writer.rb +31 -16
- data/lib/path_coverage/kernel.rb +56 -57
- data/lib/path_coverage/kinds/base.rb +16 -16
- data/lib/path_coverage/kinds/block.rb +16 -15
- data/lib/path_coverage/kinds/case.rb +14 -17
- data/lib/path_coverage/kinds/chain.rb +40 -11
- data/lib/path_coverage/kinds/choice.rb +7 -10
- data/lib/path_coverage/kinds/contains.rb +43 -33
- data/lib/path_coverage/kinds/safe.rb +44 -46
- data/lib/path_coverage/kinds/write.rb +6 -12
- data/lib/path_coverage/kinds.rb +2 -2
- data/lib/path_coverage/leaving.rb +44 -0
- data/lib/path_coverage/loop_forks.rb +57 -0
- data/lib/path_coverage/loop_nodes.rb +55 -0
- data/lib/path_coverage/manifest.rb +94 -59
- data/lib/path_coverage/manifest_blocks.rb +5 -26
- data/lib/path_coverage/manifest_case.rb +2 -2
- data/lib/path_coverage/manifest_chains.rb +8 -8
- data/lib/path_coverage/manifest_choices.rb +52 -29
- data/lib/path_coverage/manifest_disabled.rb +97 -0
- data/lib/path_coverage/manifest_folding.rb +66 -26
- data/lib/path_coverage/manifest_ignoring.rb +6 -6
- data/lib/path_coverage/manifest_writes.rb +11 -10
- data/lib/path_coverage/measured_by.rb +20 -0
- data/lib/path_coverage/merge.rb +7 -7
- data/lib/path_coverage/method_loops.rb +128 -0
- data/lib/path_coverage/method_statements.rb +147 -139
- data/lib/path_coverage/method_ways.rb +142 -0
- data/lib/path_coverage/paths_report.rb +16 -16
- data/lib/path_coverage/recorder.rb +57 -36
- data/lib/path_coverage/recorder_blocks.rb +17 -17
- data/lib/path_coverage/recorder_cases.rb +12 -11
- data/lib/path_coverage/recorder_chains.rb +12 -12
- data/lib/path_coverage/recorder_writes.rb +9 -9
- data/lib/path_coverage/render.rb +58 -51
- data/lib/path_coverage/result.rb +32 -31
- data/lib/path_coverage/rewriter.rb +39 -27
- data/lib/path_coverage/rewriter_nested.rb +13 -13
- data/lib/path_coverage/runs_report.rb +84 -0
- data/lib/path_coverage/source_lines.rb +16 -16
- data/lib/path_coverage/source_report.rb +100 -0
- data/lib/path_coverage/stable_leaves.rb +162 -0
- data/lib/path_coverage/statement_arms.rb +61 -27
- data/lib/path_coverage/summary.rb +4 -62
- data/lib/path_coverage/totals.rb +10 -8
- data/lib/path_coverage/version.rb +1 -1
- data/lib/path_coverage/walk.rb +106 -0
- data/lib/path_coverage/way_algebras.rb +127 -0
- data/lib/path_coverage/ways_report.rb +91 -0
- data/lib/path_coverage/ways_totals.rb +46 -0
- data/lib/path_coverage/within_methods.rb +184 -44
- metadata +36 -29
- data/lib/path_coverage/card.rb +0 -14
- data/lib/path_coverage/html/file.html +0 -33
- data/lib/path_coverage/html/index.html +0 -29
- data/lib/path_coverage/html/settings.js +0 -50
- data/lib/path_coverage/html/side.js +0 -470
- data/lib/path_coverage/html/sort.js +0 -34
- data/lib/path_coverage/html/style.css +0 -470
- data/lib/path_coverage/html/tabs.js +0 -15
- data/lib/path_coverage/html/theme.js +0 -27
- data/lib/path_coverage/html_branch.rb +0 -30
- data/lib/path_coverage/html_cards.rb +0 -111
- data/lib/path_coverage/html_cells.rb +0 -66
- data/lib/path_coverage/html_file.rb +0 -123
- data/lib/path_coverage/html_index.rb +0 -128
- data/lib/path_coverage/html_index_head.rb +0 -29
- data/lib/path_coverage/html_labels.rb +0 -23
- data/lib/path_coverage/html_methods.rb +0 -66
- data/lib/path_coverage/html_nav.rb +0 -67
- data/lib/path_coverage/html_report.rb +0 -131
- data/lib/path_coverage/html_statements.rb +0 -166
- data/lib/path_coverage/html_tabs.rb +0 -26
- data/lib/path_coverage/html_template.rb +0 -26
- data/lib/path_coverage/html_tree.rb +0 -131
|
@@ -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
|
-
#
|
|
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:
|
|
22
|
-
# paths, outside:, those a treatment rules out,
|
|
23
|
-
# each statement's reaches, each
|
|
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
|
-
|
|
28
|
+
decision_paths = Merge.paths(paths.map do |path|
|
|
29
|
+
paths_of(path)
|
|
30
|
+
end)
|
|
26
31
|
outside = merged(paths, 'outside')
|
|
27
|
-
{
|
|
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 cards' ways: one a
|
|
55
|
+
# run wrote, which names the max_paths its cards were listed
|
|
56
|
+
# with; not one the :post reporter sent, which has no cards.
|
|
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
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
#
|
|
48
|
-
|
|
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
|
-
|
|
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
|
|
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, {
|
|
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
|
|
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 |
|
|
76
|
-
|
|
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
|
-
|
|
88
|
-
json['
|
|
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,102 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
require 'json'
|
|
3
|
+
require_relative 'card_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
|
+
cards = WithinMethods.listed(files, config, recorded,
|
|
31
|
+
[reached, taken[:ways]])
|
|
32
|
+
tree = WithinMethods.tree(files, config, recorded)
|
|
33
|
+
write_json(recorded, files, config, [cards, reached, tree])
|
|
34
|
+
write_ways(cards, files, config, tree)
|
|
35
|
+
write_source(files, config, recorded.paths, cards)
|
|
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, cards, each def's card, as WithinMethods.listed
|
|
41
|
+
# gives them, tree, how each card'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
|
+
cards, 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, cards:, 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
|
+
# CardGaps.lines gives them for cards, beside the JSON report,
|
|
58
|
+
# at ways_path, so a later check, or the :ways reporter, can
|
|
59
|
+
# show them; tree is how each card's rows nest, as
|
|
60
|
+
# WithinMethods.tree gives it.
|
|
61
|
+
def self.write_ways(cards, files, config, tree)
|
|
62
|
+
parents = tree.flat_map do |path, nested|
|
|
63
|
+
nested.values.flat_map do |rows|
|
|
64
|
+
rows.filter_map do |at, place|
|
|
65
|
+
next unless place['parent']
|
|
66
|
+
|
|
67
|
+
["#{path}:#{at}", "#{path}:#{place['parent']}"]
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end.to_h
|
|
71
|
+
lines = CardGaps.lines(cards, PathsReport.texts(files), parents)
|
|
72
|
+
File.write(ways_path(config), lines.map { "#{_1}\n" }.join)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Where the ways lines go, beside the JSON report.
|
|
76
|
+
def self.ways_path(config)
|
|
77
|
+
config.json_path.sub(/\.json\z/, '.ways.txt')
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Writes the source report beside the JSON report, from
|
|
81
|
+
# files, decision_paths and cards, as write has them.
|
|
82
|
+
def self.write_source(files, config, decision_paths, cards)
|
|
83
|
+
source = SourceReport.of(files, config, decision_paths, cards)
|
|
84
|
+
path = config.json_path.sub(/\.json\z/, '.source.json')
|
|
85
|
+
File.write(path, JSON.generate(source))
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The JSON report's totals, each [taken, possible] paths:
|
|
89
|
+
# within each decision, and within each method; with groups
|
|
90
|
+
# configured, each group's own too, under groups.
|
|
91
|
+
def self.totals(decision_paths, files, config, reached)
|
|
92
|
+
cards = WithinMethods.cards(files, config, decision_paths,
|
|
93
|
+
reached)
|
|
94
|
+
totals = ConsoleSummary.totals(decision_paths, cards)
|
|
95
|
+
groups = config.groups
|
|
96
|
+
return totals if groups.empty?
|
|
97
|
+
|
|
98
|
+
own = ConsoleSummary.group_totals(decision_paths, cards, groups)
|
|
99
|
+
totals.merge('groups' => own)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
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/v1/' \
|
|
11
|
+
'path_coverage.schema.json'.freeze
|
|
12
|
+
|
|
8
13
|
# Writes operands, as Result gives them, paths, as
|
|
9
|
-
# PathsReport gives them, and totals, as
|
|
10
|
-
# them, to path as {"
|
|
11
|
-
# {
|
|
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 {
|
|
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
|
-
# operands, and any paths, and parts' outside:, totals
|
|
24
|
-
# cards:, as WithinMethods.listed gives them,
|
|
25
|
-
# report's text, with
|
|
28
|
+
# operands, and any paths, and parts' outside:, totals:,
|
|
29
|
+
# cards:, as WithinMethods.listed gives them, and tree:, as
|
|
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,
|
|
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 cards 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 = { '
|
|
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].freeze
|
|
57
|
+
KEPT = %i[totals cards 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, {
|
|
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|
|
data/lib/path_coverage/kernel.rb
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
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 '
|
|
4
|
+
require_relative 'method_loops'
|
|
8
5
|
require_relative 'recorder'
|
|
9
6
|
require_relative 'render'
|
|
10
7
|
require_relative 'result'
|
|
@@ -17,7 +14,7 @@ require_relative 'within_methods'
|
|
|
17
14
|
module PathCoverage
|
|
18
15
|
@recorder = Recorder.new
|
|
19
16
|
@config = nil
|
|
20
|
-
# { full path => {source:,
|
|
17
|
+
# { full path => {source:, decisions:} } of each covered file
|
|
21
18
|
# parsed as it loaded, so the exit report need not read it
|
|
22
19
|
# again.
|
|
23
20
|
@parsed = {}
|
|
@@ -88,70 +85,70 @@ module PathCoverage
|
|
|
88
85
|
end
|
|
89
86
|
|
|
90
87
|
# The compiled, rewritten code of path, or nil for a path
|
|
91
|
-
# config does not cover, which Ruby then loads as normal.
|
|
88
|
+
# config does not cover, which Ruby then loads as normal. Each
|
|
89
|
+
# call of a method in it is traced, as its way is recorded.
|
|
92
90
|
def self.instrumented(path, config)
|
|
93
91
|
return unless config.covers?(path)
|
|
94
92
|
|
|
95
93
|
parsed = @parsed[path] = Manifest.parse(path, config)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
94
|
+
args = [parsed[:source], config.relative(path),
|
|
95
|
+
parsed[:decisions]]
|
|
96
|
+
loops = MethodLoops.of(*args)
|
|
97
|
+
rewritten = rewritten(path, parsed, config, loops)
|
|
98
|
+
iseq = RubyVM::InstructionSequence
|
|
99
|
+
traced(iseq.compile(rewritten, path, path),
|
|
100
|
+
[loops, MethodLoops.repeats(*args)])
|
|
100
101
|
end
|
|
101
102
|
|
|
102
|
-
#
|
|
103
|
-
#
|
|
104
|
-
#
|
|
105
|
-
def self.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
103
|
+
# The source of parsed, the file at path, rewritten: each of its
|
|
104
|
+
# decisions recorded, each statement reach counted, and each
|
|
105
|
+
# pass of each of loops, { entry => loops }, noted.
|
|
106
|
+
def self.rewritten(path, parsed, config, loops)
|
|
107
|
+
source = parsed[:source]
|
|
108
|
+
reaches = WithinMethods.reaches(path, parsed, config)
|
|
109
|
+
passes = MethodLoops.passes(source, config.relative(path), loops)
|
|
110
|
+
Rewriter.rewrite(source, parsed[:decisions], reaches, passes)
|
|
110
111
|
end
|
|
111
112
|
|
|
112
|
-
#
|
|
113
|
-
#
|
|
114
|
-
#
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
113
|
+
# compiled, after noting each of its methods' loops and repeats,
|
|
114
|
+
# [{ entry => loops }, { entry => repeats }], as MethodLoops
|
|
115
|
+
# gives them, on the recorder's trace, and tracing each call of
|
|
116
|
+
# them, and only of them, on the trace of whichever start is
|
|
117
|
+
# current as it runs. Code with no method has no call to
|
|
118
|
+
# trace, and TracePoint refuses to trace it.
|
|
119
|
+
def self.traced(compiled, (loops, repeats))
|
|
120
|
+
return compiled if loops.empty?
|
|
121
|
+
|
|
122
|
+
loops.each do |entry, held|
|
|
123
|
+
@recorder.trace.know(entry, held, repeats[entry])
|
|
124
|
+
end
|
|
125
|
+
hook = method(:on_call_event)
|
|
126
|
+
TracePoint.new(:call, :return, &hook).enable(target: compiled)
|
|
127
|
+
compiled
|
|
121
128
|
end
|
|
122
129
|
|
|
123
|
-
#
|
|
124
|
-
#
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
parts = {
|
|
133
|
-
totals: totals(result, files, config), outside: result.outside,
|
|
134
|
-
cards:, fingerprint: config.fingerprint, reached:
|
|
135
|
-
}
|
|
136
|
-
path = config.json_path
|
|
137
|
-
JsonWriter.write(result.operands, path, report, **parts)
|
|
130
|
+
# Notes point, a TracePoint's :call or :return of a covered
|
|
131
|
+
# method, on the current start's trace.
|
|
132
|
+
def self.on_call_event(point)
|
|
133
|
+
trace = @recorder.trace
|
|
134
|
+
point.event == :call ? trace.call : trace.return
|
|
138
135
|
end
|
|
139
136
|
|
|
140
|
-
#
|
|
141
|
-
#
|
|
142
|
-
#
|
|
143
|
-
def self.
|
|
137
|
+
# Writes the JSON reports, then runs the chosen reporters, for
|
|
138
|
+
# every covered file whether or not the run loaded it, from
|
|
139
|
+
# the counts and decision paths this run recorded.
|
|
140
|
+
def self.report(config)
|
|
141
|
+
files = parsed_files(config)
|
|
142
|
+
decisions = merged_decisions(files)
|
|
143
|
+
result = Result.new(decisions, @recorder.counts,
|
|
144
|
+
@recorder.decision_paths)
|
|
144
145
|
reached = @recorder.reached
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
return totals if groups.empty?
|
|
149
|
-
|
|
150
|
-
own = ConsoleSummary.group_totals(result.paths, cards, groups)
|
|
151
|
-
totals.merge('groups' => own)
|
|
146
|
+
ways = @recorder.trace.ways
|
|
147
|
+
Render.run(result.operands, files, config,
|
|
148
|
+
**result.taken, reached:, ways:)
|
|
152
149
|
end
|
|
153
150
|
|
|
154
|
-
# { full path => {source:,
|
|
151
|
+
# { full path => {source:, decisions:} } for every covered file:
|
|
155
152
|
# as parsed when it loaded, or parsed now for a file the run
|
|
156
153
|
# never loaded.
|
|
157
154
|
def self.parsed_files(config)
|
|
@@ -160,8 +157,10 @@ module PathCoverage
|
|
|
160
157
|
end
|
|
161
158
|
end
|
|
162
159
|
|
|
163
|
-
# The
|
|
164
|
-
def self.
|
|
165
|
-
files.values.map
|
|
160
|
+
# The decisions of every covered file, in one manifest.
|
|
161
|
+
def self.merged_decisions(files)
|
|
162
|
+
files.values.map do |parsed|
|
|
163
|
+
parsed[:decisions]
|
|
164
|
+
end.reduce({}, :merge)
|
|
166
165
|
end
|
|
167
166
|
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
|
|
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
|
|
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
|
|
18
|
+
# The inserts for a decision of this kind: its leaves' records,
|
|
19
19
|
# and its leave around them.
|
|
20
|
-
def
|
|
21
|
-
Rewriter.record_inserts(
|
|
22
|
-
Rewriter.trail_inserts(
|
|
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
|
|
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
|
|
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 =
|
|
36
|
-
|
|
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
|
-
#
|
|
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
|
|
47
|
-
def inserts(
|
|
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
|
|
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,
|
|
60
|
+
def brackets(_source, _decision)
|
|
61
61
|
['', '']
|
|
62
62
|
end
|
|
63
63
|
|
|
64
|
-
# Its operands' rows in a
|
|
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
|
|
62
|
-
# outcomes; nil when its kind cannot tell, eg for a
|
|
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?(
|
|
65
|
-
receiver, body =
|
|
66
|
-
rule = HAS_ITEMS[
|
|
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,
|
|
101
|
-
receiver, body =
|
|
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...
|
|
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(
|
|
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
|
-
|
|
124
|
-
|
|
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
|
|
128
|
-
# so its items are begun before it runs and counted
|
|
129
|
-
|
|
130
|
-
|
|
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)
|