nukadoko 0.1.0 → 0.3.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/CHANGELOG.md +417 -0
- data/README.ja.md +62 -7
- data/README.md +19 -5
- data/exe/nuka +8 -0
- data/lib/nukadoko/aria_snapshot.rb +1008 -0
- data/lib/nukadoko/aria_tree.rb +150 -0
- data/lib/nukadoko/base.rb +294 -0
- data/lib/nukadoko/check.rb +669 -22
- data/lib/nukadoko/claim_check.rb +177 -0
- data/lib/nukadoko/claim_check_run.rb +293 -0
- data/lib/nukadoko/claim_line.rb +381 -0
- data/lib/nukadoko/claim_rules.rb +519 -0
- data/lib/nukadoko/claim_sides.rb +469 -0
- data/lib/nukadoko/claims.rb +576 -37
- data/lib/nukadoko/cli.rb +814 -22
- data/lib/nukadoko/codes.json +947 -0
- data/lib/nukadoko/codes.rb +19 -0
- data/lib/nukadoko/command.rb +214 -12
- data/lib/nukadoko/criteria.rb +172 -0
- data/lib/nukadoko/definition.rb +33 -41
- data/lib/nukadoko/env_check.rb +88 -0
- data/lib/nukadoko/features.rb +80 -10
- data/lib/nukadoko/fixtures.rb +329 -11
- data/lib/nukadoko/http_recording.rb +68 -0
- data/lib/nukadoko/init.rb +129 -0
- data/lib/nukadoko/js_json.rb +274 -0
- data/lib/nukadoko/js_pattern.rb +531 -0
- data/lib/nukadoko/judge.rb +221 -0
- data/lib/nukadoko/judgment_file.rb +78 -0
- data/lib/nukadoko/judgments.rb +256 -0
- data/lib/nukadoko/literal.rb +129 -0
- data/lib/nukadoko/measured.rb +105 -0
- data/lib/nukadoko/order.rb +84 -0
- data/lib/nukadoko/parts.rb +43 -22
- data/lib/nukadoko/pattern.rb +73 -3
- data/lib/nukadoko/poll.rb +21 -8
- data/lib/nukadoko/program.rb +105 -17
- data/lib/nukadoko/project_files.rb +17 -0
- data/lib/nukadoko/project_fixtures.rb +17 -2
- data/lib/nukadoko/project_program.rb +119 -0
- data/lib/nukadoko/report_allure.rb +751 -0
- data/lib/nukadoko/report_messages.rb +167 -0
- data/lib/nukadoko/report_run.rb +169 -0
- data/lib/nukadoko/run.rb +898 -91
- data/lib/nukadoko/run_record.rb +535 -0
- data/lib/nukadoko/scaffold.rb +1015 -0
- data/lib/nukadoko/schema.rb +120 -21
- data/lib/nukadoko/secrets.rb +375 -0
- data/lib/nukadoko/selection.rb +163 -0
- data/lib/nukadoko/signature.rb +4 -1
- data/lib/nukadoko/skill.rb +36 -0
- data/lib/nukadoko/spaces.rb +28 -0
- data/lib/nukadoko/step.rb +68 -21
- data/lib/nukadoko/summary.rb +662 -0
- data/lib/nukadoko/table.rb +34 -2
- data/lib/nukadoko/text_fit.rb +179 -0
- data/lib/nukadoko/trace.rb +244 -0
- data/lib/nukadoko/trace_actions.rb +116 -0
- data/lib/nukadoko/tree_passes.rb +122 -0
- data/lib/nukadoko/verdict.rb +128 -0
- data/lib/nukadoko/version.rb +2 -1
- data/lib/nukadoko/vocabulary.rb +6 -0
- data/lib/nukadoko/walk.rb +305 -0
- data/lib/nukadoko/walk_files.rb +473 -0
- data/lib/nukadoko/walk_step.rb +149 -0
- data/lib/nukadoko.rb +60 -2
- data/llms.txt +297 -0
- data/sig/generated/nukadoko/aria_snapshot.rbs +331 -0
- data/sig/generated/nukadoko/aria_tree.rbs +142 -0
- data/sig/generated/nukadoko/base.rbs +142 -0
- data/sig/generated/nukadoko/check.rbs +255 -0
- data/sig/generated/nukadoko/claim_check.rbs +118 -0
- data/sig/generated/nukadoko/claim_check_run.rbs +88 -0
- data/sig/generated/nukadoko/claim_line.rbs +158 -0
- data/sig/generated/nukadoko/claim_rules.rbs +172 -0
- data/sig/generated/nukadoko/claim_sides.rbs +207 -0
- data/sig/generated/nukadoko/claims.rbs +208 -0
- data/sig/generated/nukadoko/cli.rbs +182 -0
- data/sig/generated/nukadoko/codes.rbs +16 -0
- data/sig/generated/nukadoko/command.rbs +109 -0
- data/sig/generated/nukadoko/criteria.rbs +83 -0
- data/sig/generated/nukadoko/definition.rbs +30 -0
- data/sig/generated/nukadoko/env_check.rbs +34 -0
- data/sig/generated/nukadoko/features.rbs +101 -0
- data/sig/generated/nukadoko/fixtures.rbs +175 -0
- data/sig/generated/nukadoko/http_recording.rbs +29 -0
- data/sig/generated/nukadoko/init.rbs +64 -0
- data/sig/generated/nukadoko/js_json.rbs +109 -0
- data/sig/generated/nukadoko/js_pattern.rbs +194 -0
- data/sig/generated/nukadoko/judge.rbs +64 -0
- data/sig/generated/nukadoko/judgment_file.rbs +39 -0
- data/sig/generated/nukadoko/judgments.rbs +120 -0
- data/sig/generated/nukadoko/literal.rbs +71 -0
- data/sig/generated/nukadoko/measured.rbs +42 -0
- data/sig/generated/nukadoko/order.rbs +54 -0
- data/sig/generated/nukadoko/parts.rbs +35 -0
- data/sig/generated/nukadoko/pattern.rbs +62 -0
- data/sig/generated/nukadoko/poll.rbs +41 -0
- data/sig/generated/nukadoko/program.rbs +101 -0
- data/sig/generated/nukadoko/project_files.rbs +19 -0
- data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
- data/sig/generated/nukadoko/project_program.rbs +55 -0
- data/sig/generated/nukadoko/report_allure.rbs +268 -0
- data/sig/generated/nukadoko/report_messages.rbs +68 -0
- data/sig/generated/nukadoko/report_run.rbs +102 -0
- data/sig/generated/nukadoko/run.rbs +264 -0
- data/sig/generated/nukadoko/run_record.rbs +175 -0
- data/sig/generated/nukadoko/scaffold.rbs +328 -0
- data/sig/generated/nukadoko/schema.rbs +155 -0
- data/sig/generated/nukadoko/secrets.rbs +172 -0
- data/sig/generated/nukadoko/selection.rbs +73 -0
- data/sig/generated/nukadoko/signature.rbs +17 -0
- data/sig/generated/nukadoko/skill.rbs +20 -0
- data/sig/generated/nukadoko/spaces.rbs +31 -0
- data/sig/generated/nukadoko/step.rbs +115 -0
- data/sig/generated/nukadoko/summary.rbs +204 -0
- data/sig/generated/nukadoko/table.rbs +43 -0
- data/sig/generated/nukadoko/text_fit.rbs +70 -0
- data/sig/generated/nukadoko/trace.rbs +127 -0
- data/sig/generated/nukadoko/trace_actions.rbs +40 -0
- data/sig/generated/nukadoko/tree_passes.rbs +48 -0
- data/sig/generated/nukadoko/verdict.rbs +56 -0
- data/sig/generated/nukadoko/version.rbs +5 -0
- data/sig/generated/nukadoko/vocabulary.rbs +25 -0
- data/sig/generated/nukadoko/walk.rbs +139 -0
- data/sig/generated/nukadoko/walk_files.rbs +189 -0
- data/sig/generated/nukadoko/walk_step.rbs +50 -0
- data/sig/generated/nukadoko.rbs +26 -0
- data/sig/nukadoko.rbs +5 -48
- data/skills/nukadoko/SKILL.md +120 -0
- data/skills/nukadoko/references/checking-steps.md +200 -0
- data/skills/nukadoko/references/diagnosing.md +200 -0
- data/skills/nukadoko/references/done.md +99 -0
- data/skills/nukadoko/references/evidence.md +124 -0
- data/skills/nukadoko/references/examples/cli.md +230 -0
- data/skills/nukadoko/references/examples/http-api.md +342 -0
- data/skills/nukadoko/references/examples/workflow.md +173 -0
- data/skills/nukadoko/references/exploring.md +184 -0
- data/skills/nukadoko/references/fixtures.md +198 -0
- data/skills/nukadoko/references/from-prose.md +51 -0
- data/skills/nukadoko/references/judgments.md +95 -0
- data/skills/nukadoko/references/maintenance.md +97 -0
- data/skills/nukadoko/references/merge-risk.md +74 -0
- data/skills/nukadoko/references/parts.md +152 -0
- data/skills/nukadoko/references/perturbed-worlds.md +109 -0
- data/skills/nukadoko/references/program.md +200 -0
- data/skills/nukadoko/references/reviewing.md +200 -0
- data/skills/nukadoko/references/running.md +200 -0
- data/skills/nukadoko/references/scenario-rules.md +188 -0
- data/skills/nukadoko/references/states.md +66 -0
- data/skills/nukadoko/references/step-files.md +200 -0
- data/skills/nukadoko/references/verdicts.md +200 -0
- data/skills/nukadoko/references/writing-steps.md +200 -0
- data/skills/nukadoko/references/writing-the-feature.md +165 -0
- data/skills/nukadoko/scripts/claude-code-verdict-hook.mjs +99 -0
- data/skills/nukadoko/scripts/merge-risk.jq +56 -0
- data/skills/nukadoko-verification/SKILL.md +263 -0
- metadata +133 -1
|
@@ -0,0 +1,1015 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "pathname"
|
|
6
|
+
require "prism"
|
|
7
|
+
require "cucumber/cucumber_expressions/cucumber_expression_generator"
|
|
8
|
+
|
|
9
|
+
# `nuka scaffold`: a step file a person fills in, written in the TypeScript
|
|
10
|
+
# engine's terms. `nuka scaffold <name> --pattern <p> [--then]` writes one
|
|
11
|
+
# stub; `nuka scaffold --feature <path>` writes one per line no step
|
|
12
|
+
# matches, read as a pattern by cucumber-expressions' own generator, and
|
|
13
|
+
# names each from the pattern's literal words. A stub's body raises
|
|
14
|
+
# "not implemented: <name>" until someone writes it, so a run of it fails
|
|
15
|
+
# instead of passing.
|
|
16
|
+
#
|
|
17
|
+
# `--command <entry point>` writes a When step that runs the program under
|
|
18
|
+
# Ruby in an empty scratch directory and returns its exitCode, stdout, and
|
|
19
|
+
# stderr; `--request "<METHOD> <path>"` one that sends a request through the
|
|
20
|
+
# `request` fixture and returns its status and body; and `--from
|
|
21
|
+
# <step>.<key>` the chain to an earlier step's returns, read from that
|
|
22
|
+
# step's own declaration so the two cannot drift apart. A Then step with one
|
|
23
|
+
# stated and one chained value gets a whole claim, so a feature about a
|
|
24
|
+
# program runs green with no hand edit. A step's class name comes from its
|
|
25
|
+
# file name (how `load_steps` autoloads it), so a name whose class would not
|
|
26
|
+
# be a new Ruby constant is refused.
|
|
27
|
+
module Nukadoko
|
|
28
|
+
module Engine
|
|
29
|
+
class Scaffold
|
|
30
|
+
VALID_NAME = /\A[a-z0-9-]+\z/
|
|
31
|
+
NAME_LIMIT = 60
|
|
32
|
+
NAMED = /\{([^{}:]*)(?::([^{}]*))?\}/
|
|
33
|
+
DESCRIPTION_TODO = "TODO: describe"
|
|
34
|
+
CAPTURED = { "int" => :number, "float" => :number, "string" => :string, "word" => :string }.freeze
|
|
35
|
+
ATTACHMENT = { "docstring" => "string :docstring", "table" => "table :table" }.freeze
|
|
36
|
+
LITERAL_WORDS = { "no" => [0, "", []], "none" => [0, "", []], "nothing" => [0, "", []], "neither" => [0, "", []],
|
|
37
|
+
"never" => [0], "empty" => ["", []] }.freeze
|
|
38
|
+
NUMBER_WORDS = %w[zero one two three four five six seven eight nine ten eleven twelve].freeze
|
|
39
|
+
TIMES_WORDS = { "once" => 1, "twice" => 2 }.freeze
|
|
40
|
+
STATES_NO_VALUE = "a Then step's claim compares what the step reads with a value the line states, and this pattern captures none " \
|
|
41
|
+
"and holds no word that states one (no, none, nothing, neither, never, empty, once, twice, or a number word from zero to twelve)"
|
|
42
|
+
LITERAL_FIX = "Chain the value the line checks with --from <step>.<key> from a step whose returns declares it as z.string(), z.number(), " \
|
|
43
|
+
"or z.array(), or capture the value in --pattern, and scaffold again."
|
|
44
|
+
REDO_HEADER = "next: redo a stub in place with its line below, adding --command, --request, --from, or --description; " \
|
|
45
|
+
"--replace overwrites the stub while nobody has edited it:"
|
|
46
|
+
|
|
47
|
+
Capture = Data.define(
|
|
48
|
+
:key, #: String
|
|
49
|
+
:type, #: String
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
#: (String, ?String, **untyped) -> Refusal
|
|
53
|
+
def self.refusal(message, code = "invalid-value", **hint) = Refusal.new(code, message, **hint)
|
|
54
|
+
|
|
55
|
+
# The captures of `pattern`, or the reason no step could hold it.
|
|
56
|
+
#: (String) -> (Array[Capture] | Refusal)
|
|
57
|
+
def self.read_pattern(pattern)
|
|
58
|
+
return refusal("Refusing to scaffold: the pattern is empty") if pattern.empty?
|
|
59
|
+
|
|
60
|
+
captures = []
|
|
61
|
+
rest = pattern.gsub(/\\./, "")
|
|
62
|
+
open = rest.index("{")
|
|
63
|
+
while open
|
|
64
|
+
close = rest.index("}", open)
|
|
65
|
+
return refusal(%(Refusing to scaffold: Pattern has an unterminated "{" starting at: #{rest[open..]})) unless close
|
|
66
|
+
|
|
67
|
+
key, type = rest[open + 1...close].to_s.split(":", 2)
|
|
68
|
+
return refusal(%(Refusing to scaffold: Parameter "{#{key}}" has no name; every parameter in a pattern must be written {key:type} (docs/spec.md "Typed steps"))) unless type
|
|
69
|
+
|
|
70
|
+
captures << Capture.new(key.to_s, type)
|
|
71
|
+
open = rest.index("{", close)
|
|
72
|
+
end
|
|
73
|
+
repeated = captures.map(&:key).tally.select { _2 > 1 }.keys
|
|
74
|
+
return refusal("Refusing to scaffold: the pattern captures #{repeated.map { %("#{_1}") }.join(", ")} more than once, and one args field holds one value") if repeated.any?
|
|
75
|
+
|
|
76
|
+
types = Pattern::REGISTRY.parameter_types.map(&:name).reject(&:empty?).sort
|
|
77
|
+
unknown = captures.map(&:type).uniq.find { !types.include?(_1) }
|
|
78
|
+
if unknown
|
|
79
|
+
return refusal(%(Refusing to scaffold: the pattern names type "#{unknown}", and this project registers none by that name; registered types: #{types.join(", ")}))
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
begin
|
|
83
|
+
Pattern.new(pattern)
|
|
84
|
+
rescue StandardError => e
|
|
85
|
+
return refusal("Refusing to scaffold: #{e.message}")
|
|
86
|
+
end
|
|
87
|
+
captures
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The class `load_steps` autoloads from the step's file.
|
|
91
|
+
#: (String) -> String
|
|
92
|
+
def self.class_name(name) = name.split("-").map(&:capitalize).join
|
|
93
|
+
|
|
94
|
+
#: (String) -> String?
|
|
95
|
+
def self.class_refusal(name)
|
|
96
|
+
constant = class_name(name)
|
|
97
|
+
return "#{constant} is not a Ruby constant" unless constant.match?(/\A[A-Z]\w*\z/)
|
|
98
|
+
|
|
99
|
+
existing = Object.const_defined?(constant, false) && !Object.autoload?(constant) && Object.const_get(constant)
|
|
100
|
+
"#{constant} is already a Ruby constant that is not a step" if existing && !(existing.is_a?(Class) && existing < Step)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
#: (String) -> String
|
|
104
|
+
def self.file_of(name) = File.join("features", "steps", "#{name.tr("-", "_")}.rb")
|
|
105
|
+
|
|
106
|
+
# --- the stub ---
|
|
107
|
+
|
|
108
|
+
ENTRY_POINT_PLACEHOLDER = "<TODO: the entry point>"
|
|
109
|
+
SENT_METHODS = %w[GET POST PUT PATCH DELETE].freeze
|
|
110
|
+
# Neither a capture nor a docstring can end with the newline a program
|
|
111
|
+
# prints last, so a claim on one of these compares its text view.
|
|
112
|
+
OUTPUT_STREAMS = %w[stdout stderr].freeze
|
|
113
|
+
|
|
114
|
+
# One args key the template writes: `kind` is the class of the value
|
|
115
|
+
# it holds ("string", "number", or "other") and `json` its JSON type
|
|
116
|
+
# ("string", "number", "array", or nil when that is not known).
|
|
117
|
+
Field = Data.define(
|
|
118
|
+
:key, #: String
|
|
119
|
+
:kind, #: String
|
|
120
|
+
:json, #: String?
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
# One args key `from` fills, with every step that can give it, by
|
|
124
|
+
# class name, and the type the first of them declares.
|
|
125
|
+
Chained = Data.define(
|
|
126
|
+
:key, #: String
|
|
127
|
+
:producers, #: Array[String]
|
|
128
|
+
:type, #: untyped
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
#: (String) -> String
|
|
132
|
+
def self.ruby(text) = text.inspect
|
|
133
|
+
|
|
134
|
+
#: (Capture) -> String
|
|
135
|
+
def self.field_line(capture)
|
|
136
|
+
type = CAPTURED.fetch(capture.type, :string)
|
|
137
|
+
"#{type} :#{capture.key}"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
#: (Capture) -> Field
|
|
141
|
+
def self.capture_field(capture)
|
|
142
|
+
kind = CAPTURED[capture.type]&.to_s || "other"
|
|
143
|
+
Field.new(capture.key, kind, kind == "other" ? nil : kind)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
#: (String) -> Field
|
|
147
|
+
def self.attachment_field(attachment) = attachment == "docstring" ? Field.new("docstring", "string", "string") : Field.new("table", "other", "array")
|
|
148
|
+
|
|
149
|
+
# Only a bare string or number counts as one: a nullable one may hold
|
|
150
|
+
# no value of that type at all.
|
|
151
|
+
#: (Chained) -> Field
|
|
152
|
+
def self.chained_field(chained)
|
|
153
|
+
type = chained.type
|
|
154
|
+
kind = type == :string ? "string" : (%i[number integer].include?(type) ? "number" : "other")
|
|
155
|
+
json = kind == "other" ? (type.is_a?(Array) && type.first == :array ? "array" : nil) : kind
|
|
156
|
+
Field.new(chained.key, kind, json)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# `<METHOD> <path>` as [method, segments], each segment a String sent
|
|
160
|
+
# as it is or a Symbol naming an args key; nil when it is neither.
|
|
161
|
+
#: (String) -> [String, Array[String | Symbol]]?
|
|
162
|
+
def self.read_request(text)
|
|
163
|
+
parts = /\A([A-Z]+) (\S+)\z/.match(text) or return nil
|
|
164
|
+
path = parts[2].to_s
|
|
165
|
+
segments = [] #: Array[String | Symbol]
|
|
166
|
+
path.scan(/\{([A-Za-z_]\w*)\}|([^{}]+)/) { segments << ((key = Regexp.last_match(1)) ? key.to_sym : Regexp.last_match(2).to_s) }
|
|
167
|
+
return nil unless segments.map { _1.is_a?(Symbol) ? "{#{_1}}" : _1 }.join == path
|
|
168
|
+
|
|
169
|
+
[parts[1].to_s, segments]
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
#: ([String, Array[String | Symbol]]) -> String
|
|
173
|
+
def self.request_text(request) = "#{request[0]} #{request[1].map { _1.is_a?(Symbol) ? "{#{_1}}" : _1 }.join}"
|
|
174
|
+
|
|
175
|
+
# The path as a Ruby string literal, each key's value put in its place.
|
|
176
|
+
#: (Array[String | Symbol]) -> String
|
|
177
|
+
def self.path_literal(segments)
|
|
178
|
+
%("#{segments.map { _1.is_a?(Symbol) ? "\#{args.#{_1}}" : ruby(_1)[1...-1] }.join}")
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
#: (name: String, pattern: String?, captures: Array[Capture], then_step: bool, attachment: String?, description: String?, ?variant: String?, ?entry: String?, ?cwd: String?, ?argv: String?, ?request: [String, Array[String | Symbol]]?, ?body: String?, ?chained: Array[Chained]) -> String
|
|
182
|
+
def self.template(name:, pattern:, captures:, then_step:, attachment:, description:, variant: nil, entry: nil, cwd: nil, argv: nil, request: nil, body: nil,
|
|
183
|
+
chained: [])
|
|
184
|
+
command = variant == "command"
|
|
185
|
+
sends = variant == "request"
|
|
186
|
+
stated = captures.map { capture_field(_1) } + [attachment].compact.map { attachment_field(_1) }
|
|
187
|
+
from_fields = chained.map { chained_field(_1) }
|
|
188
|
+
fields = captures.map { field_line(_1) } + [ATTACHMENT[attachment]].compact + chained.map { "field :#{_1.key}, #{_1.producers.first}.returns[:#{_1.key}]" }
|
|
189
|
+
keys = (stated + from_fields).map(&:key)
|
|
190
|
+
args = case fields.size
|
|
191
|
+
when 0 then []
|
|
192
|
+
when 1 then [" args { #{fields.first} }"]
|
|
193
|
+
else [" args do", *fields.map { " #{_1}" }, " end"]
|
|
194
|
+
end
|
|
195
|
+
sources = chained.map do |link|
|
|
196
|
+
candidates = link.producers.map { "[#{_1}, :#{link.key}]" }
|
|
197
|
+
"#{link.key}: #{candidates.size == 1 ? candidates.first : "[#{candidates.join(", ")}]"}"
|
|
198
|
+
end
|
|
199
|
+
described = description || default_description(name, variant, cwd, argv, request)
|
|
200
|
+
lines = [
|
|
201
|
+
"# frozen_string_literal: true",
|
|
202
|
+
"",
|
|
203
|
+
"class #{class_name(name)} < Nukadoko::Step",
|
|
204
|
+
*(command ? [" CLI = File.expand_path(#{ruby(entry || ENTRY_POINT_PLACEHOLDER)})", ""] : []),
|
|
205
|
+
" description #{ruby(described)}",
|
|
206
|
+
pattern ? " pattern #{ruby(pattern)}" : ' # pattern "the feature line this step matches" (without one, the step is called by name only)',
|
|
207
|
+
" # Give each field a description: `nuka describe` shows it to whoever picks this step.",
|
|
208
|
+
*args,
|
|
209
|
+
*(sources.empty? ? [] : [" from #{sources.join(", ")}"])
|
|
210
|
+
]
|
|
211
|
+
lines += if then_step then then_lines(name, pattern, keys, stated, from_fields)
|
|
212
|
+
elsif command then command_lines(name, entry, cwd, argv, captures.map { capture_field(_1) }, from_fields)
|
|
213
|
+
elsif sends && request then request_lines(name, request, body, stated + from_fields)
|
|
214
|
+
else when_lines(name, keys)
|
|
215
|
+
end
|
|
216
|
+
[*lines, "end", ""].each_with_index.map { |line, index| line.sub(LINE_MARKER, (index + 1).to_s) }.join("\n")
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Stands for the 1-based number of the line that holds it, filled in
|
|
220
|
+
# once the whole file is assembled.
|
|
221
|
+
LINE_MARKER = "\u0000line"
|
|
222
|
+
|
|
223
|
+
#: (String, String?, String?, String?, [String, Array[String | Symbol]]?) -> String
|
|
224
|
+
def self.default_description(name, variant, cwd, argv, request)
|
|
225
|
+
case variant
|
|
226
|
+
when "command"
|
|
227
|
+
where = cwd ? "in the directory #{cwd} names" : "in an empty scratch directory"
|
|
228
|
+
"Run the command under test #{where}#{", with #{argv} split on whitespace into its arguments" if argv}"
|
|
229
|
+
when "request" then request ? "Send #{request_text(request)} and return the response's status and body" : "#{DESCRIPTION_TODO} #{name}"
|
|
230
|
+
else "#{DESCRIPTION_TODO} #{name}"
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
#: (String, Array[String]) -> Array[String]
|
|
235
|
+
def self.when_lines(name, keys)
|
|
236
|
+
message = keys.empty? ? ruby("not implemented: #{name}") : %("not implemented: #{name}; args \#{JSON.generate(args.to_h)}")
|
|
237
|
+
[
|
|
238
|
+
" # Return what later steps cite and the values this step's correctness rests on;",
|
|
239
|
+
' # beside an absence (false, 0, ""), return proof the read was valid. Declare them here:',
|
|
240
|
+
" # returns { ... }",
|
|
241
|
+
"",
|
|
242
|
+
" def run(args:)",
|
|
243
|
+
" raise #{message}",
|
|
244
|
+
" end"
|
|
245
|
+
]
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# The guard a step raises while a value has nowhere to go yet. The
|
|
249
|
+
# values are in its message, so the first failed run shows what was
|
|
250
|
+
# bound.
|
|
251
|
+
#: (String, Array[Field], String) -> Array[String]
|
|
252
|
+
def self.unplaced_guard(name, unplaced, reader)
|
|
253
|
+
keys = unplaced.map(&:key)
|
|
254
|
+
todo = if reader == "program"
|
|
255
|
+
[" # TODO: pass each chained value where the program reads it: an environment",
|
|
256
|
+
" # variable (run_command's env:), a flag, or --cwd. Then delete the raise."]
|
|
257
|
+
else
|
|
258
|
+
[" # TODO: pass each value where the request reads it: the path, or the",
|
|
259
|
+
" # request's data:, body:, or headers:. Then delete the raise."]
|
|
260
|
+
end
|
|
261
|
+
values = keys.map { "#{_1}: args.#{_1}" }.join(", ")
|
|
262
|
+
[*todo, %( raise "pass #{keys.join(", ")} where the #{reader} reads them: #{file_of(name)}:#{LINE_MARKER}; args \#{JSON.generate({ #{values} })}")]
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
#: (String, String?, String?, String?, Array[Field], Array[Field]) -> Array[String]
|
|
266
|
+
def self.command_lines(name, entry, cwd, argv, captured, chained)
|
|
267
|
+
argv_words = captured.reject { _1.key == cwd }.map do |field|
|
|
268
|
+
next "*args.#{field.key}.split" if field.key == argv
|
|
269
|
+
|
|
270
|
+
field.kind == "string" ? "args.#{field.key}" : "args.#{field.key}.to_s"
|
|
271
|
+
end
|
|
272
|
+
unplaced = chained.reject { _1.key == cwd }
|
|
273
|
+
guard = entry ? [] : [%( raise #{ruby("fill in the entry point: #{file_of(name)}:4")} if CLI.include?(#{ruby(ENTRY_POINT_PLACEHOLDER)}))]
|
|
274
|
+
[
|
|
275
|
+
" returns do",
|
|
276
|
+
" number :exitCode, null: true",
|
|
277
|
+
" string :stdout",
|
|
278
|
+
" string :stderr",
|
|
279
|
+
" end",
|
|
280
|
+
" # Set this to false when the program changes nothing outside a scratch directory.",
|
|
281
|
+
" mutates true",
|
|
282
|
+
"",
|
|
283
|
+
cwd ? " def run(args:)" : " def run(args:, scratch:)",
|
|
284
|
+
*guard,
|
|
285
|
+
*(unplaced.empty? ? [] : unplaced_guard(name, unplaced, "program")),
|
|
286
|
+
" Nukadoko.run_command(RbConfig.ruby, [#{["CLI", *argv_words].join(", ")}], cwd: #{cwd ? "args.#{cwd}" : "scratch.dir"})",
|
|
287
|
+
" end"
|
|
288
|
+
]
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# A text the request sends as it is; anything else as JSON.
|
|
292
|
+
#: (String, [String, Array[String | Symbol]], String?, Array[Field]) -> Array[String]
|
|
293
|
+
def self.request_lines(name, request, body, fields)
|
|
294
|
+
method, segments = request
|
|
295
|
+
placed = segments.grep(Symbol).map(&:to_s) + [body].compact
|
|
296
|
+
unsent = fields.reject { placed.include?(_1.key) }
|
|
297
|
+
sent = fields.find { _1.key == body }
|
|
298
|
+
option = sent ? ", #{sent.kind == "string" ? "body" : "data"}: args.#{body}" : ""
|
|
299
|
+
[
|
|
300
|
+
" returns do",
|
|
301
|
+
" number :status",
|
|
302
|
+
" string :body",
|
|
303
|
+
" end",
|
|
304
|
+
" # The method is no proof either way: set this to false when the request changes nothing on the server.",
|
|
305
|
+
" mutates true",
|
|
306
|
+
"",
|
|
307
|
+
" def run(args:, request:)",
|
|
308
|
+
*(unsent.empty? ? [] : unplaced_guard(name, unsent, "request")),
|
|
309
|
+
" response = request.#{method.downcase}(#{path_literal(segments)}#{option})",
|
|
310
|
+
" { status: response.status, body: response.body }",
|
|
311
|
+
" end"
|
|
312
|
+
]
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# A Then step's claims. One stated value and one chained value make a
|
|
316
|
+
# whole claim: the chained value is the actual, the stated one the
|
|
317
|
+
# expected. A line that states its value with a word compares the one
|
|
318
|
+
# chained value with that word's literal. Otherwise the first stated
|
|
319
|
+
# value is the expected one, and observe raises until someone writes
|
|
320
|
+
# the reading, so the step records as unobserved instead of passing.
|
|
321
|
+
#: (String, String?, Array[String], Array[Field], Array[Field]) -> Array[String]
|
|
322
|
+
def self.then_lines(name, pattern, keys, stated, chained)
|
|
323
|
+
found = stated.empty? && chained.size == 1 && word_literal(pattern, chained.first&.json)
|
|
324
|
+
if found
|
|
325
|
+
actual = chained.first
|
|
326
|
+
word, literal, compare = found
|
|
327
|
+
text = OUTPUT_STREAMS.include?(actual.key) && actual.kind == "string"
|
|
328
|
+
numbered = NUMBER_WORDS.include?(word.to_s.downcase) || TIMES_WORDS.key?(word.to_s.downcase)
|
|
329
|
+
return [
|
|
330
|
+
if numbered
|
|
331
|
+
%( # "#{word}" states #{JSON.generate(literal)}. A number word takes any compare: change equal to the one the line says ("at least three" is at-least).)
|
|
332
|
+
else
|
|
333
|
+
%( # "#{word}" states #{JSON.generate(literal)} for the type #{actual.key} holds. A word's literal is compared with #{compare} alone.)
|
|
334
|
+
end,
|
|
335
|
+
*(text ? [" # The program ends its output with a newline that the feature line does not show."] : []),
|
|
336
|
+
" claims [{ actual: #{ruby(actual.key)},#{" view: \"text\"," if text} compare: #{ruby(compare.to_s)}, expected: { literal: #{ruby_literal(literal)} } }]"
|
|
337
|
+
]
|
|
338
|
+
end
|
|
339
|
+
if stated.size == 1 && chained.size == 1
|
|
340
|
+
expected = stated.first
|
|
341
|
+
actual = chained.first
|
|
342
|
+
text = OUTPUT_STREAMS.include?(actual.key) && actual.kind == "string" && expected.kind == "string"
|
|
343
|
+
return [
|
|
344
|
+
*(text ? [" # The program ends its output with a newline that the feature line does not show."] : []),
|
|
345
|
+
" claims [{ actual: #{ruby(actual.key)},#{" view: \"text\"," if text} compare: \"equal\", expected: #{ruby(expected.key)} }]"
|
|
346
|
+
]
|
|
347
|
+
end
|
|
348
|
+
actual = "actual"
|
|
349
|
+
actual = actual.succ while keys.include?(actual)
|
|
350
|
+
[
|
|
351
|
+
" claims [{ actual: #{ruby(actual)}, compare: \"equal\", expected: #{ruby(keys.first)} }]",
|
|
352
|
+
"",
|
|
353
|
+
" # TODO: read the value this line checks and return it as { #{actual}: }, or take it",
|
|
354
|
+
" # from an earlier step's returns through `from` and remove observe.",
|
|
355
|
+
" def observe(args:)",
|
|
356
|
+
" raise #{ruby("not implemented: #{name}")}",
|
|
357
|
+
" end"
|
|
358
|
+
]
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
#: (untyped) -> String
|
|
362
|
+
def self.ruby_literal(value) = value.is_a?(String) ? ruby(value) : JSON.generate(value)
|
|
363
|
+
|
|
364
|
+
# The first word's literal of `json`, and the compare it takes (not-equal
|
|
365
|
+
# right after a fixed `not`); nil when no word fixes a value of it.
|
|
366
|
+
#: (String?, String?) -> [String, untyped, String]?
|
|
367
|
+
def self.word_literal(pattern, json)
|
|
368
|
+
return nil if json.nil?
|
|
369
|
+
|
|
370
|
+
fixed = words(pattern)
|
|
371
|
+
literal_words(pattern).each do |word, values|
|
|
372
|
+
literal = values.find { json_of(_1) == json }
|
|
373
|
+
next if literal.nil?
|
|
374
|
+
|
|
375
|
+
index = fixed.index(word)
|
|
376
|
+
negated = index&.positive? && fixed[index - 1].to_s.downcase == "not"
|
|
377
|
+
return [word, literal, negated ? "not-equal" : "equal"]
|
|
378
|
+
end
|
|
379
|
+
nil
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
#: (untyped) -> String?
|
|
383
|
+
def self.json_of(value)
|
|
384
|
+
case value
|
|
385
|
+
when Array then "array"
|
|
386
|
+
when String then "string"
|
|
387
|
+
when Numeric then "number"
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
# Whether `source` is a stub this command writes for `name`: some
|
|
392
|
+
# reading of its own lines draws it byte for byte. `type_of` gives the
|
|
393
|
+
# type a step, by class name, returns under a key, or nil.
|
|
394
|
+
#: (String, String, ?^(String, String) -> untyped) -> bool
|
|
395
|
+
def self.unedited?(source, name, type_of = ->(_step, _key) {})
|
|
396
|
+
pattern = literal(source, "pattern")
|
|
397
|
+
description = literal(source, "description")
|
|
398
|
+
return false if description.nil? || pattern == false
|
|
399
|
+
|
|
400
|
+
text = pattern.is_a?(String) ? pattern : nil
|
|
401
|
+
# A description that is no plain string draws the TODO, as nil does.
|
|
402
|
+
said = description.is_a?(String) ? description : nil
|
|
403
|
+
captures = text ? read_pattern(text) : []
|
|
404
|
+
return false if captures.is_a?(Refusal)
|
|
405
|
+
|
|
406
|
+
entry = literal(source, "CLI = File.expand_path", parenthesized: true)
|
|
407
|
+
return false if entry == false
|
|
408
|
+
|
|
409
|
+
request = sent_request(source)
|
|
410
|
+
chained = chained_in(source, type_of)
|
|
411
|
+
return false if request == false || chained.nil?
|
|
412
|
+
|
|
413
|
+
variant = entry ? "command" : (request ? "request" : nil)
|
|
414
|
+
shape = {
|
|
415
|
+
variant: variant, entry: entry == ENTRY_POINT_PLACEHOLDER ? nil : entry, cwd: source[/cwd: args\.(\w+)\)$/, 1],
|
|
416
|
+
argv: source[/\*args\.(\w+)\.split/, 1], request: request && request[0], body: request && request[1], chained: chained
|
|
417
|
+
}
|
|
418
|
+
[false, true].product([nil, "docstring", "table"]).any? do |then_step, attachment|
|
|
419
|
+
next false if then_step && (variant || !states_value?(text, captures, attachment))
|
|
420
|
+
|
|
421
|
+
template(name: name, pattern: text, captures: captures, then_step: then_step, attachment: attachment, description: said, **shape) == source
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# The string a ` <call> "..."` line passes, nil when there is no such
|
|
426
|
+
# line, false when the line holds something else.
|
|
427
|
+
#: (String, String, ?parenthesized: bool) -> (String | false | nil)
|
|
428
|
+
def self.literal(source, call, parenthesized: false)
|
|
429
|
+
line = (parenthesized ? source[/^ #{Regexp.escape(call)}\((".*")\)$/, 1] : source[/^ #{call} (".*")$/, 1]) or return nil
|
|
430
|
+
node = Prism.parse(line).value.statements.body.first
|
|
431
|
+
node.is_a?(Prism::StringNode) ? node.unescaped : false
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# The request a `--request` stub sends, as [request, body key], nil
|
|
435
|
+
# when it sends none, false when the line is not one scaffold writes.
|
|
436
|
+
#: (String) -> ([[String, Array[String | Symbol]], String?] | false | nil)
|
|
437
|
+
def self.sent_request(source)
|
|
438
|
+
call = source[/^ response = request\.(?:get|post|put|patch|delete)\(.*\)$/] or return nil
|
|
439
|
+
node = Prism.parse(call.strip).value.statements.body.first
|
|
440
|
+
sent = node.is_a?(Prism::LocalVariableWriteNode) ? node.value : nil
|
|
441
|
+
return false unless sent.is_a?(Prism::CallNode) && sent.arguments
|
|
442
|
+
|
|
443
|
+
listed = sent.arguments or return false
|
|
444
|
+
path, options = listed.arguments
|
|
445
|
+
segments = case path
|
|
446
|
+
when Prism::StringNode then [path.unescaped]
|
|
447
|
+
when Prism::InterpolatedStringNode
|
|
448
|
+
path.parts.map do |part|
|
|
449
|
+
next part.unescaped if part.is_a?(Prism::StringNode)
|
|
450
|
+
|
|
451
|
+
inner = part.is_a?(Prism::EmbeddedStatementsNode) && part.statements&.body&.first
|
|
452
|
+
receiver = inner.receiver if inner.is_a?(Prism::CallNode)
|
|
453
|
+
return false unless inner.is_a?(Prism::CallNode) && receiver.is_a?(Prism::CallNode) && receiver.name == :args
|
|
454
|
+
|
|
455
|
+
inner.name
|
|
456
|
+
end
|
|
457
|
+
else return false
|
|
458
|
+
end
|
|
459
|
+
body = options && source[/, (?:body|data): args\.(\w+)\)$/, 1]
|
|
460
|
+
[[sent.name.to_s.upcase, segments], body]
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
# The `from` a stub declares, each key's producers by class name and
|
|
464
|
+
# the type the first returns; nil when a producer's type is unknown.
|
|
465
|
+
#: (String, ^(String, String) -> untyped) -> Array[Chained]?
|
|
466
|
+
def self.chained_in(source, type_of)
|
|
467
|
+
line = source[/^ from (.*)$/, 1] or return []
|
|
468
|
+
found = [] #: Array[[String, String]]
|
|
469
|
+
line.scan(/(\w+): (\[\[.*?\]\]|\[\w+, :\w+\])/) { found << [Regexp.last_match(1).to_s, Regexp.last_match(2).to_s] }
|
|
470
|
+
found.map do |key, list|
|
|
471
|
+
producers = [] #: Array[String]
|
|
472
|
+
list.scan(/\[(\w+), :\w+\]/) { producers << Regexp.last_match(1).to_s }
|
|
473
|
+
type = type_of.(producers.first.to_s, key) or return nil
|
|
474
|
+
Chained.new(key, producers, type)
|
|
475
|
+
end
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
# --- Then lines and the words that state a value ---
|
|
479
|
+
|
|
480
|
+
#: (String?) -> Array[String]
|
|
481
|
+
def self.words(pattern)
|
|
482
|
+
return [] unless pattern
|
|
483
|
+
|
|
484
|
+
pattern.split(/\s+/).filter_map do |word|
|
|
485
|
+
next if word.match?(/(?<!\\)[{(\/]/)
|
|
486
|
+
|
|
487
|
+
text = word.gsub(/\\(.)/, '\1').gsub(/\A[.,;:!?"'`]+|[.,;:!?"'`]+\z/, "")
|
|
488
|
+
text unless text.empty?
|
|
489
|
+
end
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
# Each word of the pattern that states a literal, with its values.
|
|
493
|
+
#: (String?) -> Array[[String, Array[untyped]]]
|
|
494
|
+
def self.literal_words(pattern)
|
|
495
|
+
fixed = words(pattern)
|
|
496
|
+
fixed.each_with_index.filter_map do |word, index|
|
|
497
|
+
lower = word.downcase
|
|
498
|
+
number = NUMBER_WORDS.index(lower) || TIMES_WORDS[lower]
|
|
499
|
+
next [word, [number]] if number
|
|
500
|
+
next unless LITERAL_WORDS.key?(lower)
|
|
501
|
+
next if lower == "neither" && fixed.drop(index + 1).any? { _1.downcase == "nor" }
|
|
502
|
+
|
|
503
|
+
[word, LITERAL_WORDS[lower]]
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
#: (String?, Array[Capture], String?) -> bool
|
|
508
|
+
def self.states_value?(pattern, captures, attachment) = captures.any? || !attachment.nil? || literal_words(pattern).any?
|
|
509
|
+
|
|
510
|
+
# A word's values differ in JSON type (`no` fixes 0, "", or []), so the
|
|
511
|
+
# claim needs exactly one chained value whose type the word fixes.
|
|
512
|
+
#: (String?, Array[Capture], String?, ?Array[Field]) -> String?
|
|
513
|
+
def self.literal_refusal(pattern, captures, attachment, chained = [])
|
|
514
|
+
return nil if captures.any? || attachment
|
|
515
|
+
|
|
516
|
+
word, values = literal_words(pattern).first
|
|
517
|
+
return nil unless word
|
|
518
|
+
|
|
519
|
+
only = chained.size == 1 ? chained.first : nil
|
|
520
|
+
return nil if only && word_literal(pattern, only.json)
|
|
521
|
+
|
|
522
|
+
shown = values.map { JSON.generate(_1) }
|
|
523
|
+
listed = shown.size == 1 ? shown.first : "#{shown[0...-1].to_a.join(", ")} or #{shown.last}"
|
|
524
|
+
why = if only.nil?
|
|
525
|
+
chained.empty? ? "no --from names the value it is compared with, so that value's JSON type is unknown" : "#{chained.size} --from values fill this step, so which one it is compared with is unknown"
|
|
526
|
+
elsif only.json.nil?
|
|
527
|
+
"--from #{only.key} is not declared as a bare z.string(), z.number(), or z.array(), so its JSON type is unknown"
|
|
528
|
+
else
|
|
529
|
+
"--from #{only.key} holds #{with_article(only.json)}, a type no word of the line fixes a value of"
|
|
530
|
+
end
|
|
531
|
+
%(the line states its value only with the word "#{word}", which fixes #{listed}, and #{why}; a literal of another JSON type would never hold)
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
#: (String) -> String
|
|
535
|
+
def self.with_article(json) = json == "array" ? "an array" : "a #{json}"
|
|
536
|
+
|
|
537
|
+
TYPE_FIX = "Capture the value with a parameter type of the JSON type the chained value holds ({int} or {float} for a number, {string} for a text), " \
|
|
538
|
+
"chain a value of the type the line states, or, when a word of the line states the value, take the capture out of --pattern, and scaffold again."
|
|
539
|
+
|
|
540
|
+
# A claim across two JSON types never holds, so a line that states one
|
|
541
|
+
# value and chains one of another type is refused.
|
|
542
|
+
#: (String?, Array[Capture], Array[Field], Array[Field]) -> String?
|
|
543
|
+
def self.type_refusal(pattern, captures, stated, chained)
|
|
544
|
+
return nil unless stated.size == 1 && chained.size == 1
|
|
545
|
+
|
|
546
|
+
line = stated.first
|
|
547
|
+
only = chained.first
|
|
548
|
+
return nil if line.json.nil? || only.json.nil? || line.json == only.json
|
|
549
|
+
|
|
550
|
+
captured = captures.find { _1.key == line.key }
|
|
551
|
+
what = captured ? "{#{captured.key}:#{captured.type}}" : "its #{line.key}"
|
|
552
|
+
head = "the line states #{what}, #{with_article(line.json)}, and --from #{only.key} holds #{with_article(only.json)}, "
|
|
553
|
+
word = word_literal(pattern, only.json)
|
|
554
|
+
return "#{head}so a claim could compare #{only.key} only with the word \"#{word[0]}\", and no claim would read #{what}; a Then step with no observe checks every value its line states" if word
|
|
555
|
+
|
|
556
|
+
"#{head}and no word of the line fixes #{with_article(only.json)}; a claim across two JSON types would never hold"
|
|
557
|
+
end
|
|
558
|
+
|
|
559
|
+
# --- one stub ---
|
|
560
|
+
|
|
561
|
+
#: (String, Hash[String, Array[String]], Array[String], Array[singleton(Step)], Array[Scenario]) -> void
|
|
562
|
+
def initialize(project, options, rest, steps, scenarios)
|
|
563
|
+
@project = project
|
|
564
|
+
@options = options
|
|
565
|
+
@rest = rest
|
|
566
|
+
@steps = steps
|
|
567
|
+
@scenarios = scenarios
|
|
568
|
+
end
|
|
569
|
+
|
|
570
|
+
# The paths written, and the lines for stderr, or a Refusal.
|
|
571
|
+
#: () -> ([Array[String], Array[String]] | Refusal)
|
|
572
|
+
def call
|
|
573
|
+
return feature(@options["--feature"].last) if @options.key?("--feature")
|
|
574
|
+
|
|
575
|
+
single
|
|
576
|
+
end
|
|
577
|
+
|
|
578
|
+
#: () -> ([Array[String], Array[String]] | Refusal)
|
|
579
|
+
def single
|
|
580
|
+
name = @rest.first.to_s
|
|
581
|
+
return Scaffold.refusal(%(Invalid step name "#{name}": must match [a-z0-9-]+ (kebab-case; it becomes the step's file name))) unless name.match?(VALID_NAME)
|
|
582
|
+
|
|
583
|
+
why = Scaffold.class_refusal(name)
|
|
584
|
+
return Scaffold.refusal(%(Invalid step name "#{name}": the Ruby engine names the step's class after it, and #{why})) if why
|
|
585
|
+
|
|
586
|
+
then_step = @options.key?("--then")
|
|
587
|
+
command = @options.key?("--command")
|
|
588
|
+
entry = @options.fetch("--command", []).last.to_s
|
|
589
|
+
entry = nil if entry.empty?
|
|
590
|
+
cwd = @options.fetch("--cwd", []).last
|
|
591
|
+
argv = @options.fetch("--argv", []).last
|
|
592
|
+
body = @options.fetch("--body", []).last
|
|
593
|
+
if command && then_step
|
|
594
|
+
return Scaffold.refusal("Refusing to scaffold: --command and --then together; a step that runs a program acts, and a Then step judges through claims",
|
|
595
|
+
"invalid-arguments")
|
|
596
|
+
end
|
|
597
|
+
request = nil
|
|
598
|
+
if @options.key?("--request")
|
|
599
|
+
if command
|
|
600
|
+
return Scaffold.refusal("Refusing to scaffold: --request and --command together; one When step either sends a request or runs a program", "invalid-arguments")
|
|
601
|
+
end
|
|
602
|
+
if then_step
|
|
603
|
+
return Scaffold.refusal("Refusing to scaffold: --request and --then together; the step that sends the request is a When, and a Then step reads its status or body with --from",
|
|
604
|
+
"invalid-arguments")
|
|
605
|
+
end
|
|
606
|
+
text = @options["--request"].last.to_s
|
|
607
|
+
request = Scaffold.read_request(text)
|
|
608
|
+
unless request
|
|
609
|
+
return Scaffold.refusal(%(Refusing to scaffold: --request takes "<METHOD> <path>", an upper-case method and a path with no space, each {key} in it an args key, ) +
|
|
610
|
+
%(such as "GET /items/{id}", and got #{JSON.generate(text)}))
|
|
611
|
+
end
|
|
612
|
+
unless SENT_METHODS.include?(request[0])
|
|
613
|
+
return Scaffold.refusal("Refusing to scaffold: --request's method #{request[0]} is not one the Ruby engine's request fixture sends (#{SENT_METHODS.join(", ")})")
|
|
614
|
+
end
|
|
615
|
+
end
|
|
616
|
+
if body && request.nil?
|
|
617
|
+
return Scaffold.refusal("Refusing to scaffold: --body names the args key a --request step sends, and only a --request step sends one", "invalid-arguments")
|
|
618
|
+
end
|
|
619
|
+
if cwd && !command
|
|
620
|
+
return Scaffold.refusal("Refusing to scaffold: --cwd names the directory the program runs in, and only a --command step runs one", "invalid-arguments")
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
description = @options["--description"].last
|
|
624
|
+
if description == ""
|
|
625
|
+
return Scaffold.refusal("Refusing to scaffold: --description is empty; say what the step does, or leave the flag out for a TODO that nuka check reports")
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
if entry && !File.file?(File.join(@project, entry))
|
|
629
|
+
looked = Pathname.new(File.expand_path(entry, @project)).relative_path_from(Pathname.new(File.expand_path(@project))).to_s
|
|
630
|
+
order = @command_before_name && entry.match?(VALID_NAME) ? "; #{Scaffold.command_took_the_name(entry)}" : ""
|
|
631
|
+
return Scaffold.refusal("Refusing to scaffold: entry point #{JSON.generate(entry)} is not a file (looked at #{looked}); pass the path of the program the step runs, " \
|
|
632
|
+
"relative to the directory nuka runs in, or give --command no value to leave a TODO#{order}")
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
pattern = @options["--pattern"].last
|
|
636
|
+
captures = pattern ? Scaffold.read_pattern(pattern) : []
|
|
637
|
+
return captures if captures.is_a?(Refusal)
|
|
638
|
+
|
|
639
|
+
links = from_links(captures)
|
|
640
|
+
return links if links.is_a?(Refusal)
|
|
641
|
+
|
|
642
|
+
keys = captures.map(&:key) + links.map(&:last).uniq
|
|
643
|
+
has = keys.empty? ? "it has none" : "its keys are #{keys.join(", ")}"
|
|
644
|
+
if cwd && !keys.include?(cwd)
|
|
645
|
+
return Scaffold.refusal("Refusing to scaffold: --cwd #{cwd} names no args key of this step (#{has}); pass the key a --from or a capture fills")
|
|
646
|
+
end
|
|
647
|
+
unknown = request ? request[1].grep(Symbol).map(&:to_s).reject { keys.include?(_1) } : []
|
|
648
|
+
if unknown.any?
|
|
649
|
+
return Scaffold.refusal("Refusing to scaffold: --request's path names #{unknown.map { "{#{_1}}" }.join(", ")}, which no capture or --from fills (#{has}); " \
|
|
650
|
+
"a {key} in the path is put in by that args key's value")
|
|
651
|
+
end
|
|
652
|
+
if argv
|
|
653
|
+
refused = argv_refusal(argv, command, cwd, captures)
|
|
654
|
+
return refused if refused
|
|
655
|
+
end
|
|
656
|
+
|
|
657
|
+
relative = Scaffold.file_of(name)
|
|
658
|
+
path = File.join(@project, relative)
|
|
659
|
+
if File.exist?(path)
|
|
660
|
+
unedited = Scaffold.unedited?(File.read(path), name, type_of)
|
|
661
|
+
remove = { fix: "Delete #{relative} if it holds no step to keep, then run the same command again; or choose another step name." }
|
|
662
|
+
unless @options.key?("--replace")
|
|
663
|
+
return Scaffold.refusal("Refusing to scaffold: #{relative} already exists", "file-exists", **remove) unless unedited
|
|
664
|
+
|
|
665
|
+
return Scaffold.refusal("Refusing to scaffold: #{relative} already exists, and it still holds what scaffold wrote", "file-exists",
|
|
666
|
+
run: [*%w[nuka scaffold], *@argv_words, "--replace"].join(" "))
|
|
667
|
+
end
|
|
668
|
+
unless unedited
|
|
669
|
+
return Scaffold.refusal("Refusing to scaffold: #{relative} is not what scaffold writes (someone edited it, or scaffold did not write it), so --replace leaves it",
|
|
670
|
+
"file-edited", **remove)
|
|
671
|
+
end
|
|
672
|
+
end
|
|
673
|
+
|
|
674
|
+
chained = chain(links)
|
|
675
|
+
return chained if chained.is_a?(Refusal)
|
|
676
|
+
|
|
677
|
+
from_fields = chained.map { Scaffold.chained_field(_1) }
|
|
678
|
+
cwd_field = (captures.map { Scaffold.capture_field(_1) } + from_fields).find { _1.key == cwd }
|
|
679
|
+
if cwd_field && cwd_field.kind != "string"
|
|
680
|
+
return Scaffold.refusal("Refusing to scaffold: --cwd #{cwd} may hold a value that is not a string, and run_command takes cwd as a string; " \
|
|
681
|
+
"chain a key whose schema is a bare string, or capture it as {#{cwd}:string}")
|
|
682
|
+
end
|
|
683
|
+
|
|
684
|
+
# A command's docstring has no place in argv, so it stays the author's call.
|
|
685
|
+
attachment = pattern && !command ? attachment_in_features(pattern) : nil
|
|
686
|
+
if body
|
|
687
|
+
sendable = keys + [attachment].compact
|
|
688
|
+
unless sendable.include?(body)
|
|
689
|
+
attached = %w[docstring table].include?(body) ? "; no feature line this pattern matches carries a #{body} (write the line first)" : ""
|
|
690
|
+
return Scaffold.refusal("Refusing to scaffold: --body #{body} names no args key of this step (#{sendable.empty? ? "it has none" : "its keys are #{sendable.join(", ")}"})#{attached}")
|
|
691
|
+
end
|
|
692
|
+
end
|
|
693
|
+
|
|
694
|
+
if then_step && !Scaffold.states_value?(pattern, captures, attachment)
|
|
695
|
+
return Scaffold.refusal("Refusing to scaffold: --then: #{STATES_NO_VALUE}",
|
|
696
|
+
fix: 'Capture the value the line checks in --pattern, such as "the count is {count:int}", or write a feature line with a docstring or a table, and scaffold again.')
|
|
697
|
+
end
|
|
698
|
+
literal = then_step && Scaffold.literal_refusal(pattern, captures, attachment, from_fields)
|
|
699
|
+
return Scaffold.refusal("Refusing to scaffold: --then: #{literal}", fix: LITERAL_FIX) if literal
|
|
700
|
+
|
|
701
|
+
stated = captures.map { Scaffold.capture_field(_1) } + [attachment].compact.map { Scaffold.attachment_field(_1) }
|
|
702
|
+
typed = then_step && Scaffold.type_refusal(pattern, captures, stated, from_fields)
|
|
703
|
+
return Scaffold.refusal("Refusing to scaffold: --then: #{typed}", fix: TYPE_FIX) if typed
|
|
704
|
+
|
|
705
|
+
variant = command ? "command" : (request ? "request" : nil)
|
|
706
|
+
write(relative, Scaffold.template(name: name, pattern: pattern, captures: captures, then_step: then_step, attachment: attachment, description: description,
|
|
707
|
+
variant: variant, entry: entry, cwd: cwd, argv: argv, request: request, body: body, chained: chained))
|
|
708
|
+
[[relative], []]
|
|
709
|
+
end
|
|
710
|
+
|
|
711
|
+
#: (String) -> String
|
|
712
|
+
def self.command_took_the_name(value)
|
|
713
|
+
"--command before the name takes the next word as its value (#{JSON.generate(value)} here); write the name first, or give --command a value"
|
|
714
|
+
end
|
|
715
|
+
|
|
716
|
+
# The command line after `nuka scaffold`, as typed, for a refusal's do.
|
|
717
|
+
attr_writer :argv_words #: Array[String]
|
|
718
|
+
# Whether `--command <value>` came before the name, so a name typed
|
|
719
|
+
# right after the flag was read as its value.
|
|
720
|
+
attr_writer :command_before_name #: bool
|
|
721
|
+
|
|
722
|
+
private
|
|
723
|
+
|
|
724
|
+
# Each `--from` as [step, key], or the reason one cannot be.
|
|
725
|
+
#: (Array[Capture]) -> (Array[[String, String]] | Refusal)
|
|
726
|
+
def from_links(captures)
|
|
727
|
+
keys = captures.map(&:key)
|
|
728
|
+
links = [] #: Array[[String, String]]
|
|
729
|
+
@options.fetch("--from", []).each do |text|
|
|
730
|
+
parts = /\A([a-z0-9-]+)\.([A-Za-z_]\w*)\z/.match(text)
|
|
731
|
+
return Scaffold.refusal("Refusing to scaffold: --from takes <step>.<key>, a step name and one identifier, and got #{JSON.generate(text)}") unless parts
|
|
732
|
+
|
|
733
|
+
step, key = parts[1].to_s, parts[2].to_s
|
|
734
|
+
if keys.include?(key) && links.none? { _2 == key }
|
|
735
|
+
return Scaffold.refusal(%(Refusing to scaffold: args key "#{key}" is filled by a capture and by --from, and one args field holds one value))
|
|
736
|
+
end
|
|
737
|
+
return Scaffold.refusal("Refusing to scaffold: --from #{text} is given twice") if links.include?([step, key])
|
|
738
|
+
|
|
739
|
+
links << [step, key]
|
|
740
|
+
end
|
|
741
|
+
links
|
|
742
|
+
end
|
|
743
|
+
|
|
744
|
+
# The type a step, by class name, declares for a returns key.
|
|
745
|
+
#: () -> ^(String, String) -> untyped
|
|
746
|
+
def type_of
|
|
747
|
+
lambda do |klass, key|
|
|
748
|
+
step = Check.new(@steps, []).usable.find { _1.name == klass }
|
|
749
|
+
step&.returns&.[](key)&.type
|
|
750
|
+
end
|
|
751
|
+
end
|
|
752
|
+
|
|
753
|
+
# Each `--from` resolved against the project's steps, grouped by key,
|
|
754
|
+
# or the first producer that cannot give its key.
|
|
755
|
+
#: (Array[[String, String]]) -> (Array[Chained] | Refusal)
|
|
756
|
+
def chain(links)
|
|
757
|
+
usable = Check.new(@steps, []).usable
|
|
758
|
+
broken = @steps.size - usable.size
|
|
759
|
+
by_key = {} #: Hash[String, Chained]
|
|
760
|
+
links.each do |step_name, key|
|
|
761
|
+
producer = usable.find { _1.step_name == step_name }
|
|
762
|
+
unless producer
|
|
763
|
+
unread = broken.zero? ? "" : "; #{broken} step file(s) did not import, and `nuka check` names them"
|
|
764
|
+
return Scaffold.refusal(%(Refusing to scaffold: no typed step is named "#{step_name}" (`nuka steps` lists the vocabulary)#{unread}), "step-inputs-invalid")
|
|
765
|
+
end
|
|
766
|
+
file = Pathname.new(producer.file).relative_path_from(Pathname.new(File.expand_path(@project))).to_s
|
|
767
|
+
return_it = "return #{key} from its run()"
|
|
768
|
+
again = { run: ["nuka", "scaffold", *@argv_words].join(" ") }
|
|
769
|
+
returns = producer.returns
|
|
770
|
+
unless returns
|
|
771
|
+
return Scaffold.refusal(%(Refusing to scaffold: #{step_name} declares no returns, so it has no key to chain; declare returns with "#{key}" in #{file}),
|
|
772
|
+
"step-inputs-invalid", fix: "Add a returns block with #{key} to #{file} and #{return_it}.", **again)
|
|
773
|
+
end
|
|
774
|
+
field = returns[key]
|
|
775
|
+
unless field
|
|
776
|
+
named = returns.fields.empty? ? "it returns no keys" : "its keys are #{returns.fields.map(&:name).join(", ")}"
|
|
777
|
+
return Scaffold.refusal(%(Refusing to scaffold: #{step_name} does not return "#{key}"; #{named}), "step-inputs-invalid",
|
|
778
|
+
fix: %(Add "#{key}" to returns in #{file} and #{return_it}.), **again)
|
|
779
|
+
end
|
|
780
|
+
earlier = by_key[key]
|
|
781
|
+
unless earlier
|
|
782
|
+
by_key[key] = Chained.new(key, [producer.name.to_s], field.type)
|
|
783
|
+
next
|
|
784
|
+
end
|
|
785
|
+
first = Schema.json_type(earlier.type)
|
|
786
|
+
this = Schema.json_type(field.type)
|
|
787
|
+
if first != this
|
|
788
|
+
earlier_name = usable.find { _1.name == earlier.producers.first }&.step_name
|
|
789
|
+
return Scaffold.refusal("Refusing to scaffold: --from #{earlier_name}.#{key} and --from #{step_name}.#{key} fill one args field, which holds one type, " +
|
|
790
|
+
%(and their schemas differ (#{JSON.generate(first)} and #{JSON.generate(this)}); give both producers the same schema for "#{key}", or chain them into different keys))
|
|
791
|
+
end
|
|
792
|
+
by_key[key] = earlier.with(producers: [*earlier.producers, producer.name.to_s])
|
|
793
|
+
end
|
|
794
|
+
by_key.values
|
|
795
|
+
end
|
|
796
|
+
|
|
797
|
+
# Why `--argv <key>` cannot split that key, or nil when it is a string
|
|
798
|
+
# capture that stays in argv.
|
|
799
|
+
#: (String, bool, String?, Array[Capture]) -> Refusal?
|
|
800
|
+
def argv_refusal(key, command, cwd, captures)
|
|
801
|
+
unless command
|
|
802
|
+
return Scaffold.refusal("Refusing to scaffold: --argv names the capture a --command step splits into argv, and only a --command step runs a program", "invalid-arguments")
|
|
803
|
+
end
|
|
804
|
+
if key == cwd
|
|
805
|
+
return Scaffold.refusal("Refusing to scaffold: --argv #{key} and --cwd #{key} name one key, and --cwd takes its key out of argv", "invalid-arguments")
|
|
806
|
+
end
|
|
807
|
+
|
|
808
|
+
captured = captures.find { _1.key == key }
|
|
809
|
+
return nil if captured && CAPTURED[captured.type] == :string
|
|
810
|
+
|
|
811
|
+
strings = captures.select { CAPTURED[_1.type] == :string }.map(&:key)
|
|
812
|
+
why = captured ? "--argv #{key} names a {#{key}:#{captured.type}} capture, which does not hand the step a string" : "--argv #{key} names no capture of the pattern"
|
|
813
|
+
if strings.empty?
|
|
814
|
+
return Scaffold.refusal("Refusing to scaffold: #{why}, and the pattern captures no string", "invalid-arguments",
|
|
815
|
+
fix: "Put an {args:string} capture where the arguments go in --pattern, and pass --argv args.")
|
|
816
|
+
end
|
|
817
|
+
|
|
818
|
+
words = @argv_words.each_with_index.map do |word, index|
|
|
819
|
+
next "--argv=#{strings.first}" if word.start_with?("--argv=")
|
|
820
|
+
|
|
821
|
+
@argv_words[index - 1] == "--argv" && index.positive? ? strings.first.to_s : word
|
|
822
|
+
end
|
|
823
|
+
Scaffold.refusal("Refusing to scaffold: #{why}; its string captures are #{strings.join(", ")}", "invalid-arguments", run: ["nuka", "scaffold", *words].join(" "))
|
|
824
|
+
end
|
|
825
|
+
|
|
826
|
+
public
|
|
827
|
+
|
|
828
|
+
# --- --feature ---
|
|
829
|
+
|
|
830
|
+
#: (String) -> ([Array[String], Array[String]] | Refusal)
|
|
831
|
+
def feature(target)
|
|
832
|
+
paths = target_paths(target)
|
|
833
|
+
return paths if paths.is_a?(Refusal)
|
|
834
|
+
|
|
835
|
+
broken = @steps.size - Check.new(@steps, []).usable.size
|
|
836
|
+
if broken.positive?
|
|
837
|
+
return Scaffold.refusal("Refusing to scaffold: #{broken} step file(s) did not import, so which lines are undefined is not known (a line may belong to a step in one of them)",
|
|
838
|
+
"step-file-import-failed", run: "nuka check")
|
|
839
|
+
end
|
|
840
|
+
|
|
841
|
+
usable = Check.new(@steps, []).usable
|
|
842
|
+
selected = Dir.chdir(@project) { Engine.load_features(paths) }
|
|
843
|
+
undefined = selected.flat_map { |scenario| scenario.steps.map { [scenario, _1] } }
|
|
844
|
+
.select { |_, line| Engine.matches(usable, line.text).empty? }
|
|
845
|
+
return [[], ["nothing to scaffold: every line in #{target} matches a step"]] if undefined.empty?
|
|
846
|
+
|
|
847
|
+
project_lines = (@scenarios + selected).flat_map { |scenario| scenario.steps.map { ["#{scenario.uri}:#{_1.line}", _1.text] } }
|
|
848
|
+
reading = undefined.map { _2.text }.uniq.to_h { [_1, Scaffold.infer(_1)] }
|
|
849
|
+
notes = []
|
|
850
|
+
planned = {}
|
|
851
|
+
undefined.group_by { |scenario, line| "#{scenario.uri}:#{line.line}" }.each do |where, group|
|
|
852
|
+
texts = group.map { _2.text }.uniq
|
|
853
|
+
patterns = texts.map { reading[_1][:pattern] }.uniq
|
|
854
|
+
if patterns.size > 1
|
|
855
|
+
notes << "#{where}: its Examples rows read as different patterns (#{patterns.map { JSON.generate(_1) }.join(", ")}), so no stub was written; " \
|
|
856
|
+
'scaffold it with nuka scaffold <name> --pattern "<line with {key:type} captures>"'
|
|
857
|
+
next
|
|
858
|
+
end
|
|
859
|
+
read = reading[texts.first]
|
|
860
|
+
pattern = read[:pattern]
|
|
861
|
+
if read[:readings]
|
|
862
|
+
notes << %(#{where}: "#{texts.first}" reads as #{read[:readings].map { JSON.generate(_1) }.join(" or ")}, so the stub's pattern is the line as written, ) +
|
|
863
|
+
"with no capture; put a {key:type} capture in it by hand"
|
|
864
|
+
elsif pattern.match?(NAMED)
|
|
865
|
+
other = project_lines.find { |_, text| reading.dig(text, :pattern) != pattern && Pattern.new(pattern).match(text) }
|
|
866
|
+
if other
|
|
867
|
+
if texts.size > 1
|
|
868
|
+
notes << "#{where}: its pattern #{JSON.generate(pattern)} would also match \"#{other[1]}\" (#{other[0]}), and its Examples rows have no single line " \
|
|
869
|
+
"to write instead, so no stub was written"
|
|
870
|
+
next
|
|
871
|
+
end
|
|
872
|
+
pattern = Scaffold.literal_pattern(texts.first)
|
|
873
|
+
notes << "#{where}: #{JSON.generate(read[:pattern])} would also match \"#{other[1]}\" (#{other[0]}), so the stub's pattern is the line as written, with no capture"
|
|
874
|
+
end
|
|
875
|
+
end
|
|
876
|
+
(planned[pattern] ||= []).concat(group)
|
|
877
|
+
end
|
|
878
|
+
|
|
879
|
+
taken = usable.map(&:step_name)
|
|
880
|
+
written = []
|
|
881
|
+
redos = []
|
|
882
|
+
planned.each do |pattern, lines|
|
|
883
|
+
name = distinct(Scaffold.name_for(pattern)) { |candidate| taken.include?(candidate) || File.exist?(File.join(@project, Scaffold.file_of(candidate))) || Scaffold.class_refusal(candidate) }
|
|
884
|
+
where = "#{lines.first[0].uri}:#{lines.first[1].line}"
|
|
885
|
+
then_step = lines.any? { _2.kind.to_s == "Outcome" }
|
|
886
|
+
kinds = lines.map { attachment_of(_2) }.uniq
|
|
887
|
+
attachment = kinds.size == 1 ? kinds.first : nil
|
|
888
|
+
captures = Scaffold.read_pattern(pattern)
|
|
889
|
+
# Every pattern here was read from a line or escaped from one, so
|
|
890
|
+
# a refusal is a fault of this command, not of the feature file.
|
|
891
|
+
raise captures if captures.is_a?(Refusal)
|
|
892
|
+
|
|
893
|
+
if then_step && !Scaffold.states_value?(pattern, captures, attachment)
|
|
894
|
+
notes << "#{where}: #{STATES_NO_VALUE}, so no stub was written; state the value this line checks in the line, such as a number or a quoted text, and scaffold again"
|
|
895
|
+
next
|
|
896
|
+
end
|
|
897
|
+
literal = then_step && Scaffold.literal_refusal(pattern, captures, attachment)
|
|
898
|
+
if literal
|
|
899
|
+
notes << "#{where}: #{literal}, so no stub was written; scaffold it alone with --then --from <step>.<key>, naming a value its step's returns " \
|
|
900
|
+
"declares as z.string(), z.number(), or z.array()"
|
|
901
|
+
next
|
|
902
|
+
end
|
|
903
|
+
taken << name
|
|
904
|
+
relative = Scaffold.file_of(name)
|
|
905
|
+
source = Scaffold.template(name: name, pattern: pattern, captures: captures, then_step: then_step, attachment: attachment, description: nil)
|
|
906
|
+
write(relative, source)
|
|
907
|
+
written << relative
|
|
908
|
+
again = Scaffold.template(name: name, pattern: pattern, captures: captures, then_step: then_step, attachment: attachment_in_features(pattern), description: nil)
|
|
909
|
+
redos << ["nuka", "scaffold", name, "--pattern", pattern, *("--then" if then_step), "--replace"].map { Selection.shell_word(_1) }.join(" ") if again == source
|
|
910
|
+
end
|
|
911
|
+
err = notes.map { "note: #{_1}" }
|
|
912
|
+
err += [REDO_HEADER, *redos.map { " #{_1}" }] if redos.any?
|
|
913
|
+
[written, err]
|
|
914
|
+
end
|
|
915
|
+
|
|
916
|
+
# How cucumber-expressions reads a line: with captures when every
|
|
917
|
+
# reading hands the step the same kind of value, otherwise as the line
|
|
918
|
+
# itself, with the readings it could not choose between.
|
|
919
|
+
#: (String) -> Hash[Symbol, untyped]
|
|
920
|
+
def self.infer(text)
|
|
921
|
+
generated = generate(text, Pattern::REGISTRY)
|
|
922
|
+
first = generated.first
|
|
923
|
+
same = generated.all? do |expression|
|
|
924
|
+
expression.parameter_types.size == first.parameter_types.size &&
|
|
925
|
+
expression.parameter_types.zip(first.parameter_types).all? { |a, b| a.name == b.name || (CAPTURED[a.name] && CAPTURED[a.name] == CAPTURED[b.name]) }
|
|
926
|
+
end
|
|
927
|
+
pattern = name_captures(first.source)
|
|
928
|
+
matched = begin
|
|
929
|
+
Pattern.new(pattern).match(text)
|
|
930
|
+
rescue StandardError
|
|
931
|
+
nil
|
|
932
|
+
end
|
|
933
|
+
return { pattern: pattern } if same && matched
|
|
934
|
+
|
|
935
|
+
{ pattern: literal_pattern(text), readings: generated.map(&:source) }
|
|
936
|
+
end
|
|
937
|
+
|
|
938
|
+
# The generator escapes `(`, `{`, and `/` but leaves a backslash bare,
|
|
939
|
+
# which the parser then refuses, so each backslash is doubled first.
|
|
940
|
+
#: (String, untyped) -> Array[untyped]
|
|
941
|
+
def self.generate(text, registry)
|
|
942
|
+
Cucumber::CucumberExpressions::CucumberExpressionGenerator.new(registry).generate_expressions(text.gsub("\\", "\\\\\\\\"))
|
|
943
|
+
end
|
|
944
|
+
|
|
945
|
+
# The line as a pattern that matches it and nothing else.
|
|
946
|
+
#: (String) -> String
|
|
947
|
+
def self.literal_pattern(text) = text.gsub("\\", "\\\\\\\\").gsub(/([({\/])/) { "\\#{Regexp.last_match(1)}" }
|
|
948
|
+
|
|
949
|
+
#: (String) -> String
|
|
950
|
+
def self.name_captures(source)
|
|
951
|
+
index = 0
|
|
952
|
+
source.gsub(/\\.|\{([^}]*)\}/) do |match|
|
|
953
|
+
next match if match.start_with?("\\")
|
|
954
|
+
|
|
955
|
+
key = index.zero? ? "value" : "value#{index + 1}"
|
|
956
|
+
index += 1
|
|
957
|
+
"{#{key}:#{Regexp.last_match(1)}}"
|
|
958
|
+
end
|
|
959
|
+
end
|
|
960
|
+
|
|
961
|
+
#: (String) -> String
|
|
962
|
+
def self.name_for(pattern)
|
|
963
|
+
literal = pattern.gsub(/\\(.)|\{[^}]*\}/) { Regexp.last_match(1) || " " }
|
|
964
|
+
words = literal.downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "")
|
|
965
|
+
return "step" if words.empty?
|
|
966
|
+
return words if words.size <= NAME_LIMIT
|
|
967
|
+
|
|
968
|
+
cut = words[0, NAME_LIMIT + 1].to_s
|
|
969
|
+
dash = cut.rindex("-")
|
|
970
|
+
(dash&.positive? ? cut[0, dash] : cut[0, NAME_LIMIT]).to_s.sub(/-+\z/, "")
|
|
971
|
+
end
|
|
972
|
+
|
|
973
|
+
private
|
|
974
|
+
|
|
975
|
+
#: (String) { (String) -> boolish } -> String
|
|
976
|
+
def distinct(base)
|
|
977
|
+
return base unless yield(base)
|
|
978
|
+
|
|
979
|
+
suffix = 2
|
|
980
|
+
suffix += 1 while yield("#{base}-#{suffix}")
|
|
981
|
+
"#{base}-#{suffix}"
|
|
982
|
+
end
|
|
983
|
+
|
|
984
|
+
#: (PickleStep) -> String?
|
|
985
|
+
def attachment_of(line) = line.doc_string ? "docstring" : (line.table ? "table" : nil)
|
|
986
|
+
|
|
987
|
+
# What every feature line the pattern matches carries, or nil when no
|
|
988
|
+
# line matches or the lines disagree.
|
|
989
|
+
#: (String) -> String?
|
|
990
|
+
def attachment_in_features(pattern)
|
|
991
|
+
expression = Pattern.new(pattern)
|
|
992
|
+
kinds = @scenarios.flat_map(&:steps).select { expression.match(_1.text) }.map { attachment_of(_1) }.uniq
|
|
993
|
+
kinds.size == 1 ? kinds.first : nil
|
|
994
|
+
rescue StandardError
|
|
995
|
+
nil
|
|
996
|
+
end
|
|
997
|
+
|
|
998
|
+
#: (String) -> (Array[String] | Refusal)
|
|
999
|
+
def target_paths(target)
|
|
1000
|
+
absolute = File.join(@project, target)
|
|
1001
|
+
return Dir.glob("**/*.feature", base: absolute).sort.map { File.join(target, _1) } if File.directory?(absolute)
|
|
1002
|
+
return [target] if File.file?(absolute)
|
|
1003
|
+
|
|
1004
|
+
Scaffold.refusal("nuka scaffold --feature: feature file not found: #{target}", "target-not-found")
|
|
1005
|
+
end
|
|
1006
|
+
|
|
1007
|
+
#: (String, String) -> void
|
|
1008
|
+
def write(relative, source)
|
|
1009
|
+
path = File.join(@project, relative)
|
|
1010
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
1011
|
+
File.write(path, source)
|
|
1012
|
+
end
|
|
1013
|
+
end
|
|
1014
|
+
end
|
|
1015
|
+
end
|