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
|
@@ -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
|
|
11
|
-
# record call, each
|
|
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,
|
|
17
|
-
inserts =
|
|
18
|
-
|
|
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
|
|
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.
|
|
43
|
-
Kinds.of(condition.operator).
|
|
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
|
|
47
|
-
# call, innermost of all; a leaf of a nested
|
|
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
|
|
61
|
+
# RewriterNested gives it, or in none, and that decision's own
|
|
50
62
|
# inserts too.
|
|
51
|
-
def self.record_inserts(
|
|
52
|
-
nested = RewriterNested.calls(
|
|
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(
|
|
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(
|
|
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
|
|
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
|
|
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(
|
|
71
|
-
enter = "PathCoverage.enter(#{
|
|
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, :
|
|
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
|
|
82
|
-
def self.record_call(
|
|
83
|
-
"PathCoverage.record(#{
|
|
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
|
|
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
|
|
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
|
|
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
|
|
6
|
-
# another, eg the &. chain in ok && user&.admin?. The
|
|
7
|
-
# around them enters and leaves; each nested
|
|
8
|
-
# noted as that
|
|
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
|
|
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(
|
|
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(
|
|
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
|
|
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(
|
|
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(
|
|
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
|
|
36
|
-
# then noted at their places from 0; else the
|
|
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
|
|
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
|
|
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,
|
|
12
|
-
marks = marks(source,
|
|
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
|
|
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,
|
|
26
|
-
blocks = merged(spans_of(source,
|
|
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
|
|
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,
|
|
35
|
-
|
|
34
|
+
def self.spans_of(source, decisions, operands)
|
|
35
|
+
decisions.map do |decision, condition|
|
|
36
36
|
last = condition.leaves.last
|
|
37
|
-
span = line_of(
|
|
38
|
-
[span, status_of(operands.fetch(
|
|
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
|
|
44
|
-
# covered
|
|
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
|
|
70
|
-
def self.line_of(
|
|
71
|
-
|
|
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.
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
require_relative 'code_tokens'
|
|
2
|
+
require_relative 'fold_rows'
|
|
3
|
+
require_relative 'decision_locations'
|
|
4
|
+
require_relative 'decision_tree'
|
|
5
|
+
require_relative 'source_lines'
|
|
6
|
+
require_relative 'summary'
|
|
7
|
+
|
|
8
|
+
module PathCoverage
|
|
9
|
+
# What a renderer needs of each covered file beyond the JSON
|
|
10
|
+
# report's counts, worked out from its source, so it can draw
|
|
11
|
+
# the file's page without parsing the source itself.
|
|
12
|
+
module SourceReport
|
|
13
|
+
# The version of the source report's shape.
|
|
14
|
+
SCHEMA = 1
|
|
15
|
+
|
|
16
|
+
# The URL of the schema the source report follows.
|
|
17
|
+
SCHEMA_URL = 'https://path-coverage.github.io/schemas/v1/' \
|
|
18
|
+
'path_coverage.source.schema.json'.freeze
|
|
19
|
+
|
|
20
|
+
# The source report of a run, headed by its schema's URL and
|
|
21
|
+
# version: config's ignored operators, the
|
|
22
|
+
# covered files of each of config's groups, each top of the
|
|
23
|
+
# rows of cards, { def decision => card }, coloured, and each of
|
|
24
|
+
# files', { full path => {source:, decisions:} }, lines and
|
|
25
|
+
# decisions, each decision's paths as decision_paths has
|
|
26
|
+
# them, its fold headed by its row's top.
|
|
27
|
+
def self.of(files, config, decision_paths, cards)
|
|
28
|
+
rows = cards.values.flat_map { _1['rows'] }
|
|
29
|
+
tops = rows.to_h { [_1['decision'], _1['top']] }
|
|
30
|
+
shown = files.to_h do |full_path, parsed|
|
|
31
|
+
[config.relative(full_path),
|
|
32
|
+
file(parsed, decision_paths, tops)]
|
|
33
|
+
end
|
|
34
|
+
{ '$schema': SCHEMA_URL, schema: SCHEMA,
|
|
35
|
+
ignored: config.ignored, groups: groups(config, shown.keys),
|
|
36
|
+
labels: labels(rows), files: shown }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# { top => it coloured } of each distinct top of rows, a
|
|
40
|
+
# decision's or a statement's, eg return.
|
|
41
|
+
def self.labels(rows)
|
|
42
|
+
rows.map { _1['top'] }.uniq.to_h do |top|
|
|
43
|
+
[top, CodeTokens.label_code(top)]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# { name => the paths its globs match } of each of config's
|
|
48
|
+
# groups.
|
|
49
|
+
def self.groups(config, paths)
|
|
50
|
+
config.groups.transform_values do |globs|
|
|
51
|
+
paths.select { Summary.matches?(_1, globs) }
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# One file's lines and decisions, parsed being its {source:,
|
|
56
|
+
# decisions:}.
|
|
57
|
+
def self.file(parsed, decision_paths, tops)
|
|
58
|
+
source, decisions = parsed.values_at(:source, :decisions)
|
|
59
|
+
shown = decisions.keys.to_h do |name|
|
|
60
|
+
paths = decision_paths.fetch(name)
|
|
61
|
+
[name,
|
|
62
|
+
decision(source, decisions, name, paths, tops.fetch(name))]
|
|
63
|
+
end
|
|
64
|
+
{ lines: lines(source), decisions: shown }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Example: "ok?(1)\n" gives [{ text: 'ok?(1)', tokens:
|
|
68
|
+
# [[4, 5, 'n']] }].
|
|
69
|
+
# Each line of source: its text and its coloured tokens.
|
|
70
|
+
def self.lines(source)
|
|
71
|
+
tokens = CodeTokens.of(source)
|
|
72
|
+
source.lines.each_with_index.map do |text, index|
|
|
73
|
+
{ text: text.chomp, tokens: tokens.fetch(index + 1, []) }
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Example: ok?(id) && new?(id) on line 2 gives span [2, 2].
|
|
78
|
+
# The decision named name, of decisions, { name => Condition }, in
|
|
79
|
+
# source: the [first, last] lines it spans, and its fold's
|
|
80
|
+
# rows, headed by top, its row's top, a column for each of
|
|
81
|
+
# paths, [{path:, count:}], in their order, with no counts.
|
|
82
|
+
def self.decision(source, decisions, name, paths, top)
|
|
83
|
+
condition = decisions.fetch(name)
|
|
84
|
+
last = SourceLines.end_line(source, condition.leaves.last)
|
|
85
|
+
span = [SourceLines.line_of(name), last]
|
|
86
|
+
{ span:, fold: fold(source, condition, paths, top) }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# The rows of condition's fold, headed by top, its decision row
|
|
90
|
+
# with no counts.
|
|
91
|
+
def self.fold(source, condition, paths, top)
|
|
92
|
+
outcomes = paths.map { _1[:path] }
|
|
93
|
+
tree = DecisionTree.built(source, condition, outcomes)
|
|
94
|
+
located = DecisionLocations.located(source, condition, tree)
|
|
95
|
+
else_at = DecisionLocations.else_at(source, condition)
|
|
96
|
+
own, *rest = FoldRows.rows(located.merge(else_at:, label: top))
|
|
97
|
+
[own.except(:counts), *rest]
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require_relative 'condition'
|
|
3
|
+
|
|
4
|
+
module PathCoverage
|
|
5
|
+
# The leaves of a card whose outcomes cannot differ within one
|
|
6
|
+
# call: each a read of a local variable that its method never
|
|
7
|
+
# writes, eg a parameter, where the leaf's outcome is the
|
|
8
|
+
# variable's truthiness. Every such read in a call has the same
|
|
9
|
+
# outcome, so a way needing two outcomes of it is no way.
|
|
10
|
+
module StableLeaves
|
|
11
|
+
# The operators whose leaf operands' outcomes are their
|
|
12
|
+
# truthiness. A when's leaf is whether its pattern matched, a
|
|
13
|
+
# &. receiver's whether it was nil, false included in not nil,
|
|
14
|
+
# and a block method's leaves count items or answers.
|
|
15
|
+
TRUTHY = (%w[&& and || or] + CHOICES).freeze
|
|
16
|
+
|
|
17
|
+
# The calls through which code can reach a method's locals
|
|
18
|
+
# and rewrite them out of its sight: a binding, a string eval,
|
|
19
|
+
# or any call by name, which could be either.
|
|
20
|
+
REWRITERS = %i[
|
|
21
|
+
binding eval instance_eval class_eval module_eval
|
|
22
|
+
send __send__ public_send method
|
|
23
|
+
].freeze
|
|
24
|
+
|
|
25
|
+
# Example: { 32...33 => :a, 54...55 => :a } for def f(a),
|
|
26
|
+
# return 0 if a, then a ? 1 : 2.
|
|
27
|
+
# { leaf byte range => local name } for each truthy leaf of
|
|
28
|
+
# the decisions of rows, a card's in source, decisions being
|
|
29
|
+
# { decision => Condition }, that reads, outside any block, a
|
|
30
|
+
# local its def never writes, for each name two or more such
|
|
31
|
+
# leaves read; none for a card in no def, or one whose def
|
|
32
|
+
# calls any of REWRITERS.
|
|
33
|
+
def self.of(source, decisions, rows)
|
|
34
|
+
conditions = decisions.values_at(*rows.map(&:decision).compact)
|
|
35
|
+
ranges = conditions.flat_map { truthy_leaves(_1) }
|
|
36
|
+
return {} if ranges.empty?
|
|
37
|
+
|
|
38
|
+
method = def_of(source, ranges.first)
|
|
39
|
+
return {} if method.nil?
|
|
40
|
+
|
|
41
|
+
shared(read_in(method).slice(*ranges))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The leaves of condition, a leaf a byte range, whose outcome
|
|
45
|
+
# is their truthiness, its nested conditions' included.
|
|
46
|
+
def self.truthy_leaves(condition)
|
|
47
|
+
operands = condition.operands
|
|
48
|
+
own = TRUTHY.include?(condition.operator) ? operands : []
|
|
49
|
+
own.grep(Range) + operands.flat_map { inner_leaves(_1) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The truthy leaves of operand, a leaf, a Condition or an Arm:
|
|
53
|
+
# none for a leaf, which its condition counts, or a plain arm.
|
|
54
|
+
def self.inner_leaves(operand)
|
|
55
|
+
case operand
|
|
56
|
+
when Condition then truthy_leaves(operand)
|
|
57
|
+
when Arm then operand.body ? truthy_leaves(operand.body) : []
|
|
58
|
+
else []
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The innermost def in source holding the bytes range, nil
|
|
63
|
+
# when none does.
|
|
64
|
+
def self.def_of(source, range)
|
|
65
|
+
defs = defs_in(Prism.parse(source).value)
|
|
66
|
+
holding = defs.select do |node|
|
|
67
|
+
loc = node.location
|
|
68
|
+
(loc.start_offset...loc.end_offset).cover?(range)
|
|
69
|
+
end
|
|
70
|
+
holding.min_by { _1.location.length }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Every def node in node, node itself included.
|
|
74
|
+
def self.defs_in(node)
|
|
75
|
+
own = node.type == :def_node ? [node] : []
|
|
76
|
+
own + node.compact_child_nodes.flat_map { defs_in(_1) }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# { read byte range => local name } of each read in the def
|
|
80
|
+
# node method, outside any block, of a local it never writes;
|
|
81
|
+
# none when it calls any of REWRITERS.
|
|
82
|
+
def self.read_in(method)
|
|
83
|
+
reads = Reads.new
|
|
84
|
+
reads.visit_child_nodes(method)
|
|
85
|
+
return {} if reads.rewrites
|
|
86
|
+
|
|
87
|
+
written = reads.written
|
|
88
|
+
reads.reads.each_with_object({}) do |(range, name), kept|
|
|
89
|
+
kept[range] = name unless written.include?(name)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The entries of named, { range => name }, whose name two or
|
|
94
|
+
# more of them share.
|
|
95
|
+
def self.shared(named)
|
|
96
|
+
counts = named.values.tally
|
|
97
|
+
named.each_with_object({}) do |(range, name), shared|
|
|
98
|
+
shared[range] = name if counts[name] > 1
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Notes, in a def's body, each read of a local outside any
|
|
103
|
+
# block, def or lambda, where a local of the same name may be
|
|
104
|
+
# another variable; each local written anywhere; and whether
|
|
105
|
+
# any of REWRITERS is called.
|
|
106
|
+
class Reads < Prism::Visitor
|
|
107
|
+
# Starts outside any block, having noted nothing.
|
|
108
|
+
def initialize
|
|
109
|
+
@reads = {}
|
|
110
|
+
@written = []
|
|
111
|
+
@rewrites = false
|
|
112
|
+
@depth = 0
|
|
113
|
+
super
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# { byte range => name } of each read noted; the names of
|
|
117
|
+
# the locals written; and true when a rewriter is called.
|
|
118
|
+
attr_reader :reads, :written, :rewrites
|
|
119
|
+
|
|
120
|
+
# Notes a read, outside any block.
|
|
121
|
+
def visit_local_variable_read_node(node)
|
|
122
|
+
loc = node.location
|
|
123
|
+
range = loc.start_offset...loc.end_offset
|
|
124
|
+
@reads[range] = node.name if @depth.zero?
|
|
125
|
+
super
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Notes the name of a write of a local, eg a = 1, a += 1, a
|
|
129
|
+
# ||= 1, or a target, eg in a, b = 1, 2 or rescue => a.
|
|
130
|
+
def visit_written(node)
|
|
131
|
+
@written << node.name
|
|
132
|
+
visit_child_nodes(node)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
%i[
|
|
136
|
+
local_variable_write_node local_variable_operator_write_node
|
|
137
|
+
local_variable_or_write_node local_variable_and_write_node
|
|
138
|
+
local_variable_target_node
|
|
139
|
+
].each do |type|
|
|
140
|
+
alias_method :"visit_#{type}", :visit_written
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Notes a call of a rewriter.
|
|
144
|
+
def visit_call_node(node)
|
|
145
|
+
@rewrites = true if REWRITERS.include?(node.name)
|
|
146
|
+
super
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Visits a block, a lambda or a def a level deeper, so its
|
|
150
|
+
# reads are not noted.
|
|
151
|
+
def visit_inner(node)
|
|
152
|
+
@depth += 1
|
|
153
|
+
visit_child_nodes(node)
|
|
154
|
+
@depth -= 1
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
%i[block_node lambda_node def_node].each do |type|
|
|
158
|
+
alias_method :"visit_#{type}", :visit_inner
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|