varar-core 0.8.0 → 0.9.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/lib/varar/core/ast.rb +4 -1
- data/lib/varar/core/cell_diff.rb +23 -9
- data/lib/varar/core/conformance.rb +12 -6
- data/lib/varar/core/diagnostics.rb +57 -2
- data/lib/varar/core/drift.rb +158 -84
- data/lib/varar/core/execute.rb +12 -8
- data/lib/varar/core/failure.rb +2 -1
- data/lib/varar/core/failure_anchor.rb +17 -0
- data/lib/varar/core/hash.rb +1 -1
- data/lib/varar/core/matcher.rb +10 -13
- data/lib/varar/core/parse.rb +2 -1
- data/lib/varar/core/plan.rb +232 -50
- data/lib/varar/core/reference.rb +142 -0
- data/lib/varar/core/registry.rb +7 -3
- data/lib/varar/core/result.rb +22 -6
- data/lib/varar/core/scanner.rb +54 -37
- data/lib/varar/core/sentences.rb +41 -34
- data/lib/varar/core/span.rb +111 -38
- data/lib/varar/core/step_index.rb +124 -0
- data/lib/varar/core/structurer.rb +6 -3
- data/lib/varar/core/table_cells.rb +4 -3
- data/lib/varar/core.rb +1 -2
- metadata +6 -5
- data/lib/varar/core/canonical_json.rb +0 -47
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6b3afe2eac41dbbd5e500dd3e0e845c77a8bdf3c8d6fd80dd61b63c95abbbf03
|
|
4
|
+
data.tar.gz: 9e683dc8f095c659aeea657a9dc93b35b6ab561bf5b08feab08f242d41423ead
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1635a2d5f71a1714286f4bd167fbca6792a8097673e652d0979ae939fc44fe8a279ed0ef1cea3ed54f0cf7325fe8a367ecc5cc989e5b9fd15053d27c78d96ba3
|
|
7
|
+
data.tar.gz: 387588fbc0526a7c99fe8281f2993c1f6097e1edeca095aaf5816e36906a79722d8753d10b7cde65b4cba176f8a1067af2f3d0bd949d78c2d6e2c99de8aa0ede
|
data/lib/varar/core/ast.rb
CHANGED
|
@@ -46,6 +46,9 @@ module Varar
|
|
|
46
46
|
# one example. See ADR 0012.
|
|
47
47
|
Example = Data.define(:scope_stack, :span, :body, :preceded_by_delimiter)
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
# +headings+ is every heading in the document in source order — the same
|
|
50
|
+
# Heading values the scanner produced. The planner uses it to tell whether
|
|
51
|
+
# a reference anchor names one section or several (ADR 0016).
|
|
52
|
+
Doc = Data.define(:path, :source, :examples, :orphan_attachments, :headings)
|
|
50
53
|
end
|
|
51
54
|
end
|
data/lib/varar/core/cell_diff.rb
CHANGED
|
@@ -55,6 +55,7 @@ module Varar
|
|
|
55
55
|
def compare_row(returned, checks)
|
|
56
56
|
return [] unless returned.is_a?(Hash)
|
|
57
57
|
|
|
58
|
+
check_record_columns(returned, checks.map(&:column), '')
|
|
58
59
|
checks.filter_map do |check|
|
|
59
60
|
next unless returned.key?(check.column)
|
|
60
61
|
|
|
@@ -64,9 +65,25 @@ module Varar
|
|
|
64
65
|
end
|
|
65
66
|
end
|
|
66
67
|
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
68
|
+
# A returned record checks only the columns it has (#114), so its keys must
|
|
69
|
+
# be columns of the table — a misspelt key would otherwise check nothing —
|
|
70
|
+
# and it must have at least one, or it checks nothing at all.
|
|
71
|
+
def check_record_columns(record, columns, where)
|
|
72
|
+
unknown = record.keys.find { |key| !columns.include?(key) }
|
|
73
|
+
raise ReturnShapeError, "#{where}unknown column \"#{unknown}\"" unless unknown.nil?
|
|
74
|
+
return if columns.any? { |column| record.key?(column) }
|
|
75
|
+
|
|
76
|
+
raise ReturnShapeError, "#{where}returned no column of the table"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Compare a whole-table step's returned table against the input table (the
|
|
80
|
+
# header row is labels, never compared). +returned+: nil (no checks), Array
|
|
81
|
+
# of Arrays (positional), which reproduces every column of every row, or
|
|
82
|
+
# Array of Hashes (keyed by header), which checks only the columns each
|
|
83
|
+
# record has — so a table can carry columns for people alone, such as a
|
|
84
|
+
# "why" column, that no step echoes back (#114). Cells compare as exact
|
|
85
|
+
# strings. A record key that is no column, or a record with no column at
|
|
86
|
+
# all, is a ReturnShapeError.
|
|
70
87
|
def compare_table(returned, input_table)
|
|
71
88
|
return [] if returned.nil?
|
|
72
89
|
raise ReturnShapeError, "expected a table (array of rows), got #{returned.class}" unless returned.is_a?(Array)
|
|
@@ -84,18 +101,15 @@ module Varar
|
|
|
84
101
|
diffs = []
|
|
85
102
|
data_rows.each_with_index do |row, i|
|
|
86
103
|
ret = returned[i]
|
|
104
|
+
check_record_columns(ret, columns, "row #{i}: ") if all_records
|
|
87
105
|
if all_arrays && ret.length != columns.length
|
|
88
106
|
raise ReturnShapeError, "row #{i}: expected #{columns.length} column(s), got #{ret.length}"
|
|
89
107
|
end
|
|
90
108
|
|
|
91
109
|
columns.each_with_index do |column, j|
|
|
92
|
-
if
|
|
93
|
-
actual_value = ret[j]
|
|
94
|
-
else
|
|
95
|
-
raise ReturnShapeError, "row #{i}: missing column \"#{column}\"" unless ret.key?(column)
|
|
110
|
+
next if all_records && !ret.key?(column)
|
|
96
111
|
|
|
97
|
-
|
|
98
|
-
end
|
|
112
|
+
actual_value = all_arrays ? ret[j] : ret[column]
|
|
99
113
|
expected = j < row.cells.length ? row.cells[j] : ''
|
|
100
114
|
actual = render_cell_value(actual_value)
|
|
101
115
|
span = j < row.cell_spans.length ? row.cell_spans[j] : row.span
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'varar/core/ast'
|
|
4
4
|
require 'varar/core/plan'
|
|
5
|
+
require 'varar/core/reference'
|
|
5
6
|
require 'varar/core/execute'
|
|
6
7
|
require 'varar/core/failure_anchor'
|
|
7
8
|
|
|
@@ -103,7 +104,8 @@ module Varar
|
|
|
103
104
|
{
|
|
104
105
|
'path' => doc.path,
|
|
105
106
|
'examples' => doc.examples.map { |ex| example_hash(ex) },
|
|
106
|
-
'orphanAttachments' => doc.orphan_attachments.map { |b| block_hash(b) }
|
|
107
|
+
'orphanAttachments' => doc.orphan_attachments.map { |b| block_hash(b) },
|
|
108
|
+
'headings' => doc.headings.map { |h| block_hash(h) }
|
|
107
109
|
}
|
|
108
110
|
end
|
|
109
111
|
|
|
@@ -149,6 +151,7 @@ module Varar
|
|
|
149
151
|
def planned_example_hash(example, source)
|
|
150
152
|
result = {
|
|
151
153
|
'name' => example.name,
|
|
154
|
+
'key' => example.key,
|
|
152
155
|
'scopeStack' => example.scope_stack,
|
|
153
156
|
'span' => span_hash(example.span),
|
|
154
157
|
'expectedOutcome' => example.expected_outcome || 'pass'
|
|
@@ -158,20 +161,23 @@ module Varar
|
|
|
158
161
|
result
|
|
159
162
|
end
|
|
160
163
|
|
|
161
|
-
def planned_step_hash(step,
|
|
164
|
+
def planned_step_hash(step, _source)
|
|
162
165
|
step_names = parameter_type_names(step.step_def.compiled)
|
|
163
166
|
result = {
|
|
164
167
|
'text' => step.text,
|
|
165
168
|
'matchSpan' => span_hash(step.match_span),
|
|
166
169
|
'paramSpans' => step.param_spans.map { |s| span_hash(s) },
|
|
167
170
|
'matchedExpression' => step.step_def.expression,
|
|
168
|
-
'args' => step.
|
|
171
|
+
'args' => step.param_texts.each_with_index.map do |value, i|
|
|
169
172
|
{
|
|
170
|
-
'value' =>
|
|
173
|
+
'value' => value,
|
|
171
174
|
'parameterType' => i < step_names.length ? step_names[i] : nil
|
|
172
175
|
}
|
|
173
176
|
end
|
|
174
177
|
}
|
|
178
|
+
# Present only on a step a reference block spliced in from another oath
|
|
179
|
+
# (ADR 0016): the document its spans belong to.
|
|
180
|
+
result['docPath'] = step.doc_path if step.doc_path
|
|
175
181
|
result['dataTable'] = block_hash(step.data_table) if step.data_table
|
|
176
182
|
result['docString'] = doc_string_hash(step.doc_string) if step.doc_string
|
|
177
183
|
result
|
|
@@ -206,8 +212,8 @@ module Varar
|
|
|
206
212
|
end
|
|
207
213
|
|
|
208
214
|
# Run all examples and return the four-artifact bundle. Port of runConformance.
|
|
209
|
-
def run_conformance(doc, registry, create_context, parameter_types = [])
|
|
210
|
-
execution = Plan.plan(doc, registry)
|
|
215
|
+
def run_conformance(doc, registry, create_context, parameter_types = [], workspace = nil)
|
|
216
|
+
execution = Plan.plan(doc, registry, workspace || Reference.empty_workspace)
|
|
211
217
|
observed = Hash.new { |h, k| h[k] = [] }
|
|
212
218
|
observer = ->(o) { observed[o.example_index] << o }
|
|
213
219
|
queue = Execute.collect_examples(execution, create_context: create_context, observer: observer)
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
module Varar
|
|
4
4
|
module Core
|
|
5
5
|
# A planning/run diagnostic on the shared rail. code is one of
|
|
6
|
-
# "ambiguous-match", "error-fence-without-step", "drift"
|
|
7
|
-
#
|
|
6
|
+
# "ambiguous-match", "error-fence-without-step", "drift",
|
|
7
|
+
# "reference-not-found", "reference-empty", "reference-cycle",
|
|
8
|
+
# "ambiguous-anchor". Port of diagnostics.ts.
|
|
8
9
|
Diagnostic = Data.define(:code, :severity, :message, :span)
|
|
9
10
|
Candidate = Data.define(:expression, :source_file, :source_line)
|
|
10
11
|
AmbiguousInput = Data.define(:text, :span, :candidates)
|
|
@@ -43,6 +44,60 @@ module Varar
|
|
|
43
44
|
span: span
|
|
44
45
|
)
|
|
45
46
|
end
|
|
47
|
+
|
|
48
|
+
# A reference block (ADR 0016) points at an oath the workspace does not
|
|
49
|
+
# hold. Never prose: a link-only block that resolves to nothing has no
|
|
50
|
+
# other reading, so it fails the run rather than degrading silently.
|
|
51
|
+
def reference_not_found(text, path, span)
|
|
52
|
+
Diagnostic.new(
|
|
53
|
+
severity: 'error',
|
|
54
|
+
code: 'reference-not-found',
|
|
55
|
+
message: %(Reference to "#{text}" points at "#{path}", which is not an oath in this ) +
|
|
56
|
+
"workspace.\nCheck the path, and that the file is matched by the `docs` globs " \
|
|
57
|
+
'in varar.config.json.',
|
|
58
|
+
span: span
|
|
59
|
+
)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The referenced document exists but the section contributes no steps — a
|
|
63
|
+
# mistyped anchor, or a section that is pure prose.
|
|
64
|
+
def reference_empty(text, path, slug, span)
|
|
65
|
+
where = slug.empty? ? path : "#{path}##{slug}"
|
|
66
|
+
Diagnostic.new(
|
|
67
|
+
severity: 'error',
|
|
68
|
+
code: 'reference-empty',
|
|
69
|
+
message: %(Reference to "#{text}" resolves to "#{where}", which contributes no steps.\n) +
|
|
70
|
+
'Check the heading the anchor names, and that its section contains a matching ' \
|
|
71
|
+
'paragraph.',
|
|
72
|
+
span: span
|
|
73
|
+
)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The anchor names more than one heading in the referenced document:
|
|
77
|
+
# GitHub would disambiguate with a numeric suffix, but a reference that
|
|
78
|
+
# could mean either section is an error, not a guess (ADR 0016).
|
|
79
|
+
def ambiguous_anchor(text, path, slug, heading_lines, span)
|
|
80
|
+
Diagnostic.new(
|
|
81
|
+
severity: 'error',
|
|
82
|
+
code: 'ambiguous-anchor',
|
|
83
|
+
message: "Reference to \"#{text}\" is ambiguous: \"#{path}\" has #{heading_lines.length} headings " \
|
|
84
|
+
"with the anchor \"##{slug}\" (lines #{heading_lines.join(', ')}).\n" \
|
|
85
|
+
'Rename the headings so each has an anchor of its own.',
|
|
86
|
+
span: span
|
|
87
|
+
)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# References may nest to any depth (depth is a style question, not a
|
|
91
|
+
# rule), so a chain that reaches a section already on it must be reported
|
|
92
|
+
# rather than recursed into.
|
|
93
|
+
def reference_cycle(chain, span)
|
|
94
|
+
Diagnostic.new(
|
|
95
|
+
severity: 'error',
|
|
96
|
+
code: 'reference-cycle',
|
|
97
|
+
message: "Reference cycle: #{chain.join(' → ')}.",
|
|
98
|
+
span: span
|
|
99
|
+
)
|
|
100
|
+
end
|
|
46
101
|
end
|
|
47
102
|
end
|
|
48
103
|
end
|
data/lib/varar/core/drift.rb
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
|
-
require 'varar/core/hash'
|
|
5
4
|
require 'varar/core/diagnostics'
|
|
6
5
|
require 'varar/core/plan'
|
|
7
|
-
require 'varar/core/canonical_json'
|
|
8
6
|
|
|
9
7
|
module Varar
|
|
10
8
|
module Core
|
|
11
|
-
#
|
|
12
|
-
|
|
13
|
-
#
|
|
14
|
-
|
|
15
|
-
#
|
|
9
|
+
# The committed baseline for one oath file (ADR 0017): the normalised text
|
|
10
|
+
# of each example-producing paragraph, in document order — the same text as
|
|
11
|
+
# an example's key (Plan.derive_example_key). A text may repeat; the k-th
|
|
12
|
+
# occurrence breaks ties toward the k-th equally similar paragraph. Empty
|
|
13
|
+
# for an oath that was baselined with nothing to record.
|
|
14
|
+
OathBaseline = Data.define(:paragraphs)
|
|
15
|
+
# The whole varar.lock: every oath keyed by its POSIX path.
|
|
16
16
|
LockFile = Data.define(:version, :oaths)
|
|
17
17
|
# A paragraph the baseline says was an example and now matches no step.
|
|
18
18
|
Drift = Data.define(:name, :line, :span)
|
|
19
|
+
# A paragraph that became an example since the baseline was recorded (#120).
|
|
20
|
+
NewExample = Data.define(:text, :line)
|
|
19
21
|
|
|
20
|
-
# Oath drift detection: a paragraph the committed varar.lock
|
|
22
|
+
# Oath drift detection: a paragraph the committed varar.lock baseline
|
|
21
23
|
# recorded as an example that now matches no step. Pure, byte-identical to
|
|
22
|
-
# the TS port so varar.lock
|
|
24
|
+
# the TS port so varar.lock is shared across languages. Port of drift.ts.
|
|
23
25
|
#
|
|
24
26
|
# BaselineStore is a duck-typed port: #read -> String|nil, #write(contents).
|
|
27
|
+
# A filesystem store reads varar.lock, falling back to a version 2
|
|
28
|
+
# varar.lock.json while that is all there is (parse_lock_file reads both),
|
|
29
|
+
# and #write writes varar.lock and deletes the legacy file.
|
|
25
30
|
module Drifts
|
|
26
31
|
# A paragraph may be moved anywhere and reworded up to ~half its words
|
|
27
32
|
# and still be recognized; edit it past this and it reads as remove+add,
|
|
@@ -29,6 +34,16 @@ module Varar
|
|
|
29
34
|
SIMILARITY_THRESHOLD = 0.5
|
|
30
35
|
TOKEN_RE = /[[:alnum:]]+/
|
|
31
36
|
|
|
37
|
+
LOCK_VERSION = 3
|
|
38
|
+
# The lock file's name, and the version 2 name it replaces. A store reads
|
|
39
|
+
# the legacy file only while the new one is absent, and deletes it on its
|
|
40
|
+
# first write (ADR 0017).
|
|
41
|
+
LOCK_FILE_NAME = 'varar.lock'
|
|
42
|
+
LEGACY_LOCK_FILE_NAME = 'varar.lock.json'
|
|
43
|
+
# The first line of every varar.lock. Exact: a file without it is not a lock.
|
|
44
|
+
LOCK_HEADER = '# varar.lock v3 — generated by Varar; commit it. ' \
|
|
45
|
+
'See https://varar.dev/reference/examples/#drift-detection'
|
|
46
|
+
|
|
32
47
|
module_function
|
|
33
48
|
|
|
34
49
|
# Do the two spans overlap at all (offset ranges intersect)? A candidate
|
|
@@ -61,52 +76,105 @@ module Varar
|
|
|
61
76
|
union.zero? ? 0.0 : intersection.to_f / union
|
|
62
77
|
end
|
|
63
78
|
|
|
64
|
-
# The current example-producing paragraphs, in document order
|
|
79
|
+
# The current example-producing paragraphs, in document order — what a
|
|
80
|
+
# clean run records as the new baseline for an oath.
|
|
65
81
|
def live_examples(doc, plan)
|
|
66
|
-
doc.examples.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
BaselineExample.new(name: Plan.derive_example_name(candidate.body), line: candidate.span.start_line)
|
|
70
|
-
end
|
|
82
|
+
doc.examples.select { |candidate| live?(candidate.span, plan) }
|
|
83
|
+
.map { |candidate| Plan.derive_example_key(candidate.body) }
|
|
71
84
|
end
|
|
72
85
|
|
|
73
|
-
|
|
74
|
-
|
|
86
|
+
# The baseline record for an oath: its live example-producing paragraphs.
|
|
87
|
+
def derive_oath_baseline(doc, plan)
|
|
88
|
+
OathBaseline.new(paragraphs: live_examples(doc, plan))
|
|
75
89
|
end
|
|
76
90
|
|
|
77
91
|
# Paragraphs the baseline recorded as examples that now match zero steps.
|
|
78
|
-
# Each re-identified by the most word-similar current paragraph at
|
|
79
|
-
# the threshold (exact
|
|
92
|
+
# Each is re-identified by the most word-similar current paragraph at or
|
|
93
|
+
# above the threshold (an exact text scores 1), so moving a paragraph never
|
|
94
|
+
# looks like drift. Among equally similar paragraphs, the k-th occurrence
|
|
95
|
+
# of a text in the baseline takes the k-th of them in document order (the
|
|
96
|
+
# last, if fewer), so repeated paragraphs pair up in order.
|
|
97
|
+
#
|
|
98
|
+
# A paragraph in a section a reference block consumes (ADR 0016) is
|
|
99
|
+
# neither: it is not live here, but it still runs where it is referenced,
|
|
100
|
+
# and a broken step there fails as `reference-empty`. So a baseline
|
|
101
|
+
# paragraph that now lands in a consumed section — setup hoisted into a
|
|
102
|
+
# section the example links to — is not drift (#117), the same as when the
|
|
103
|
+
# whole file is referenced.
|
|
104
|
+
# matched & live → not drift · matched & consumed → not drift
|
|
105
|
+
# matched & dead → DRIFT · no match → remove+add
|
|
80
106
|
def detect_drift(baseline, doc, plan)
|
|
81
107
|
return [] if baseline.nil?
|
|
82
108
|
|
|
83
109
|
candidates = doc.examples
|
|
84
|
-
tokens = candidates.map { |c| tokenize(Plan.
|
|
110
|
+
tokens = candidates.map { |c| tokenize(Plan.derive_example_key(c.body)) }
|
|
85
111
|
live = candidates.map { |c| live?(c.span, plan) }
|
|
112
|
+
consumed = candidates.map do |c|
|
|
113
|
+
plan.consumed.any? { |s| s.start_offset == c.span.start_offset && s.end_offset == c.span.end_offset }
|
|
114
|
+
end
|
|
115
|
+
occurrences = Hash.new(0)
|
|
86
116
|
|
|
87
|
-
baseline.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
score = similarity(b_tokens, tokens[i])
|
|
93
|
-
next if score < SIMILARITY_THRESHOLD
|
|
117
|
+
baseline.paragraphs.filter_map do |text|
|
|
118
|
+
occurrences[text] += 1
|
|
119
|
+
best = best_candidates(tokenize(text), tokens)
|
|
120
|
+
# No recognizable paragraph — a rewrite/removal, not drift.
|
|
121
|
+
next if best.empty?
|
|
94
122
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
(score == best_score && (line - b.line).abs < (best_line - b.line).abs)
|
|
123
|
+
best_idx = best[[occurrences[text], best.length].min - 1]
|
|
124
|
+
next if live[best_idx] # still an example — not drift
|
|
125
|
+
next if consumed[best_idx] # runs where it is referenced — not drift (#117)
|
|
99
126
|
|
|
100
|
-
|
|
127
|
+
Drift.new(name: text, line: candidates[best_idx].span.start_line, span: candidates[best_idx].span)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Indexes of every candidate sharing the best similarity at or above the
|
|
132
|
+
# threshold, in document order.
|
|
133
|
+
def best_candidates(b_tokens, tokens)
|
|
134
|
+
best_score = 0.0
|
|
135
|
+
best = []
|
|
136
|
+
tokens.each_with_index do |candidate_tokens, i|
|
|
137
|
+
score = similarity(b_tokens, candidate_tokens)
|
|
138
|
+
next if score < SIMILARITY_THRESHOLD
|
|
139
|
+
|
|
140
|
+
if best.empty? || score > best_score
|
|
101
141
|
best_score = score
|
|
142
|
+
best = [i]
|
|
143
|
+
elsif score == best_score
|
|
144
|
+
best << i
|
|
102
145
|
end
|
|
103
|
-
|
|
104
|
-
|
|
146
|
+
end
|
|
147
|
+
best
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The inverse of drift (#120): paragraphs that are examples now but that
|
|
151
|
+
# the baseline does not recognise — no baseline paragraph is at least
|
|
152
|
+
# SIMILARITY_THRESHOLD similar. Usually a new example someone wrote; but it
|
|
153
|
+
# is also how a step rename or a new step definition turns a sentence of
|
|
154
|
+
# explanation into an example nobody meant. Never gated, only reported
|
|
155
|
+
# when the baseline is rewritten, so it is seen when it is introduced.
|
|
156
|
+
# Empty when there is no baseline yet: on a first run everything is new,
|
|
157
|
+
# and saying so is noise.
|
|
158
|
+
def new_examples(baseline, doc, plan)
|
|
159
|
+
return [] if baseline.nil?
|
|
105
160
|
|
|
106
|
-
|
|
161
|
+
known = baseline.paragraphs.map { |text| tokenize(text) }
|
|
162
|
+
doc.examples.select { |candidate| live?(candidate.span, plan) }.filter_map do |candidate|
|
|
163
|
+
text = Plan.derive_example_key(candidate.body)
|
|
164
|
+
tokens = tokenize(text)
|
|
165
|
+
next if known.any? { |k| similarity(k, tokens) >= SIMILARITY_THRESHOLD }
|
|
166
|
+
|
|
167
|
+
NewExample.new(text: text, line: candidate.span.start_line)
|
|
107
168
|
end
|
|
108
169
|
end
|
|
109
170
|
|
|
171
|
+
# The one-line notice every adapter prints for a new example when it
|
|
172
|
+
# rewrites the baseline. Shared so the wording is the same in every port.
|
|
173
|
+
def new_example_notice(oath_path, example)
|
|
174
|
+
"varar: #{oath_path}:#{example.line} became an example: \"#{example.text}\" " \
|
|
175
|
+
'— if that sentence is explanation, reword it so it matches no step'
|
|
176
|
+
end
|
|
177
|
+
|
|
110
178
|
def drift_diagnostics(drifts)
|
|
111
179
|
drifts.map { |d| Diagnostics.drift_detected(d.name, d.span) }
|
|
112
180
|
end
|
|
@@ -114,16 +182,20 @@ module Varar
|
|
|
114
182
|
# One oath's baseline reconciliation against a BaselineStore. In update
|
|
115
183
|
# mode, accept all drift (re-record, report nothing); otherwise detect
|
|
116
184
|
# drift and rewrite the baseline only on a clean run, so an unacknowledged
|
|
117
|
-
# drift keeps its old entry (and stays red).
|
|
118
|
-
|
|
185
|
+
# drift keeps its old entry (and stays red). When the baseline is
|
|
186
|
+
# rewritten, +notify+ (if given) receives one new_example_notice line per
|
|
187
|
+
# paragraph that became an example (#120) — the adapter prints it; it
|
|
188
|
+
# never fails or gates anything.
|
|
189
|
+
def reconcile_drift(store, oath_path, doc, plan, update: false, notify: nil)
|
|
119
190
|
text = store.read
|
|
120
191
|
lock = text ? parse_lock_file(text) : nil
|
|
121
192
|
baseline = lock ? lock.oaths[oath_path] : nil
|
|
122
193
|
drifts = update ? [] : detect_drift(baseline, doc, plan)
|
|
123
194
|
if update || drifts.empty?
|
|
124
195
|
oaths = lock ? lock.oaths.dup : {}
|
|
125
|
-
oaths[oath_path] = derive_oath_baseline(
|
|
126
|
-
store.write(stringify_lock_file(LockFile.new(version:
|
|
196
|
+
oaths[oath_path] = derive_oath_baseline(doc, plan)
|
|
197
|
+
store.write(stringify_lock_file(LockFile.new(version: LOCK_VERSION, oaths: oaths)))
|
|
198
|
+
new_examples(baseline, doc, plan).each { |ex| notify&.call(new_example_notice(oath_path, ex)) }
|
|
127
199
|
end
|
|
128
200
|
drifts
|
|
129
201
|
end
|
|
@@ -134,7 +206,7 @@ module Varar
|
|
|
134
206
|
# exists" means.
|
|
135
207
|
def prune_lock_file(lock, keep_paths)
|
|
136
208
|
keep = keep_paths.to_a
|
|
137
|
-
LockFile.new(version:
|
|
209
|
+
LockFile.new(version: LOCK_VERSION, oaths: lock.oaths.slice(*keep))
|
|
138
210
|
end
|
|
139
211
|
|
|
140
212
|
# The whole-lock counterpart of reconcile_drift, run ONCE per run rather
|
|
@@ -161,65 +233,67 @@ module Varar
|
|
|
161
233
|
stale
|
|
162
234
|
end
|
|
163
235
|
|
|
236
|
+
# Parse a lock: varar.lock (version 3), or a version 2 varar.lock.json
|
|
237
|
+
# read during migration, whose example names become paragraph texts.
|
|
238
|
+
# Returns nil on malformed input (treated as "no baseline yet").
|
|
164
239
|
def parse_lock_file(text)
|
|
165
|
-
|
|
166
|
-
return nil unless parsed.is_a?(::Hash) && parsed['version'] == 2
|
|
240
|
+
return parse_legacy_lock_file(text) if text.lstrip.start_with?('{')
|
|
167
241
|
|
|
168
|
-
|
|
169
|
-
return nil unless
|
|
242
|
+
lines = text.split("\n", -1).map { |line| line.delete_suffix("\r") }
|
|
243
|
+
return nil unless lines.first == LOCK_HEADER
|
|
170
244
|
|
|
171
245
|
oaths = {}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
246
|
+
lines.drop(1).each do |line|
|
|
247
|
+
next if line.empty? || line.start_with?('#')
|
|
248
|
+
|
|
249
|
+
path, paragraph = line.split("\t", 2)
|
|
250
|
+
return nil if path.empty?
|
|
175
251
|
|
|
176
|
-
oaths[path]
|
|
252
|
+
paragraphs = (oaths[path] ||= [])
|
|
253
|
+
paragraphs << paragraph unless paragraph.nil?
|
|
177
254
|
end
|
|
178
|
-
LockFile.new(version:
|
|
179
|
-
rescue JSON::ParserError, TypeError
|
|
180
|
-
nil
|
|
255
|
+
LockFile.new(version: LOCK_VERSION, oaths: oaths.transform_values { |p| OathBaseline.new(paragraphs: p) })
|
|
181
256
|
end
|
|
182
257
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
examples << parsed
|
|
258
|
+
# Serialize a varar.lock deterministically: the header, then oath paths
|
|
259
|
+
# sorted, each oath's paragraphs in document order, one per line, and a
|
|
260
|
+
# bare path for an oath with none. Byte-stable across runs so a clean
|
|
261
|
+
# re-run produces no git diff, and line-oriented so git merges it line by
|
|
262
|
+
# line.
|
|
263
|
+
def stringify_lock_file(lock)
|
|
264
|
+
lines = [LOCK_HEADER]
|
|
265
|
+
lock.oaths.keys.sort.each do |path|
|
|
266
|
+
paragraphs = lock.oaths[path].paragraphs
|
|
267
|
+
lines << path if paragraphs.empty?
|
|
268
|
+
paragraphs.each { |text| lines << "#{path}\t#{text}" }
|
|
196
269
|
end
|
|
197
|
-
|
|
270
|
+
"#{lines.join("\n")}\n"
|
|
198
271
|
end
|
|
199
272
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
273
|
+
# A version 2 varar.lock.json: { version: 2, oaths: { path: { sourceHash,
|
|
274
|
+
# examples: [{ name, line }] } } }. Only the names survive: sourceHash was
|
|
275
|
+
# never read, and line only broke ties (ADR 0017). Read during migration
|
|
276
|
+
# only.
|
|
277
|
+
def parse_legacy_lock_file(text)
|
|
278
|
+
parsed = JSON.parse(text)
|
|
279
|
+
return nil unless parsed.is_a?(::Hash) && parsed['version'] == 2
|
|
206
280
|
|
|
207
|
-
|
|
208
|
-
|
|
281
|
+
oaths_raw = parsed['oaths']
|
|
282
|
+
return nil unless oaths_raw.is_a?(::Hash)
|
|
209
283
|
|
|
210
|
-
# Serialize varar.lock.json deterministically: oath paths sorted, examples
|
|
211
|
-
# in document order, insertion-order keys otherwise (version, oaths;
|
|
212
|
-
# sourceHash, examples; name, line) — NOT canonical JSON's key sort.
|
|
213
|
-
def stringify_lock_file(lock)
|
|
214
284
|
oaths = {}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
285
|
+
oaths_raw.each do |path, value|
|
|
286
|
+
examples = value.is_a?(::Hash) ? value['examples'] : nil
|
|
287
|
+
return nil unless examples.is_a?(Array)
|
|
288
|
+
|
|
289
|
+
names = examples.map { |e| e.is_a?(::Hash) ? e['name'] : nil }
|
|
290
|
+
return nil unless names.all?(String)
|
|
291
|
+
|
|
292
|
+
oaths[path] = OathBaseline.new(paragraphs: names)
|
|
221
293
|
end
|
|
222
|
-
|
|
294
|
+
LockFile.new(version: LOCK_VERSION, oaths: oaths)
|
|
295
|
+
rescue JSON::ParserError
|
|
296
|
+
nil
|
|
223
297
|
end
|
|
224
298
|
end
|
|
225
299
|
end
|
data/lib/varar/core/execute.rb
CHANGED
|
@@ -48,11 +48,11 @@ module Varar
|
|
|
48
48
|
seen_lines = {}
|
|
49
49
|
ex.steps.each { |s| seen_lines[s.match_span.start_line] = true }
|
|
50
50
|
info = { lines: seen_lines.keys }
|
|
51
|
-
sink.call(ex.name, build_run(
|
|
51
|
+
sink.call(ex.name, build_run(ex, example_index, create_ctx, observer, oath_path), info)
|
|
52
52
|
end
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
def build_run(
|
|
55
|
+
def build_run(ex, example_index, create_ctx, observer, oath_path)
|
|
56
56
|
lambda do
|
|
57
57
|
state_by_file = {}
|
|
58
58
|
last_return = nil
|
|
@@ -92,7 +92,7 @@ module Varar
|
|
|
92
92
|
state_by_file[file] = state
|
|
93
93
|
end
|
|
94
94
|
when 'sensor'
|
|
95
|
-
compare_sensor_return(
|
|
95
|
+
compare_sensor_return(ex, step, returned, extra) if ex.row_checks.nil?
|
|
96
96
|
else
|
|
97
97
|
raise ReturnShapeError, "unknown step kind: #{step.step_def.kind}"
|
|
98
98
|
end
|
|
@@ -145,7 +145,7 @@ module Varar
|
|
|
145
145
|
# return is REQUIRED — returning nothing used to skip the comparison
|
|
146
146
|
# silently, so a typo turned an assertion into a no-op. Raises the
|
|
147
147
|
# appropriate diff error.
|
|
148
|
-
def compare_sensor_return(
|
|
148
|
+
def compare_sensor_return(_ex, step, returned, extra)
|
|
149
149
|
slot_count = step.args.length + extra.length
|
|
150
150
|
if slot_count.zero?
|
|
151
151
|
return if returned.nil?
|
|
@@ -176,10 +176,10 @@ module Varar
|
|
|
176
176
|
end
|
|
177
177
|
|
|
178
178
|
inline_returned = slots[0...step.args.length]
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
param_diffs = ParamDiff.compare_params(inline_returned, step.args, step.param_spans,
|
|
179
|
+
# The text each parameter matched, sliced at plan time from the
|
|
180
|
+
# document the step was written in — which, for a step spliced in by
|
|
181
|
+
# a reference block, is not plan.doc.
|
|
182
|
+
param_diffs = ParamDiff.compare_params(inline_returned, step.args, step.param_spans, step.param_texts,
|
|
183
183
|
step.formats).reject(&:ok)
|
|
184
184
|
raise CellMismatchError, param_diffs unless param_diffs.empty?
|
|
185
185
|
|
|
@@ -205,6 +205,10 @@ module Varar
|
|
|
205
205
|
# so a renderer underlines the step and not its whole line.
|
|
206
206
|
def augment_stack(error, step, _var_path)
|
|
207
207
|
FailureAnchor.attach_anchor(error, FailureAnchor.failure_anchor(error, step.match_span))
|
|
208
|
+
# A step spliced in by a reference block has spans in the document it
|
|
209
|
+
# was WRITTEN in, so the payload must name that file — otherwise a
|
|
210
|
+
# renderer points at the running oath's line N, some other sentence.
|
|
211
|
+
FailureAnchor.attach_doc_path(error, step.doc_path) if step.doc_path
|
|
208
212
|
error
|
|
209
213
|
end
|
|
210
214
|
end
|
data/lib/varar/core/failure.rb
CHANGED
|
@@ -29,7 +29,8 @@ module Varar
|
|
|
29
29
|
message: error.message,
|
|
30
30
|
stack: render_stack(error),
|
|
31
31
|
cells: failing_cells(error),
|
|
32
|
-
anchor: anchor && AnchorRange.new(from: anchor.start_offset, to: anchor.end_offset)
|
|
32
|
+
anchor: anchor && AnchorRange.new(from: anchor.start_offset, to: anchor.end_offset),
|
|
33
|
+
doc_path: FailureAnchor.attached_doc_path(error)
|
|
33
34
|
)
|
|
34
35
|
end
|
|
35
36
|
|