scout-rig 0.2.6 → 0.2.7
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/.vimproject +31 -13
- data/README.md +27 -4
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/doc/Improvements.md +396 -0
- data/doc/StartHere.md +95 -0
- data/doc/developer/Architecture.md +232 -0
- data/doc/developer/DataConversionInternals.md +407 -0
- data/doc/developer/DesignPrinciples.md +254 -0
- data/doc/developer/PythonTaskRunnerInternals.md +371 -0
- data/doc/developer/PythonWorkflowInternals.md +539 -0
- data/doc/developer/ScoutPythonInternals.md +504 -0
- data/doc/user/Cookbook.md +337 -0
- data/doc/user/DefiningPythonTasks.md +708 -0
- data/doc/user/DrivingWorkflowsFromPython.md +418 -0
- data/doc/user/IteratingPythonData.md +142 -0
- data/doc/user/PassingDataToPython.md +223 -0
- data/doc/user/PythonEnvironments.md +197 -0
- data/doc/user/RunningPythonFromRuby.md +267 -0
- data/doc/user/ScriptingPython.md +281 -0
- data/doc/user/TestingAndPackaging.md +487 -0
- data/lib/scout/python/script.rb +11 -2
- data/lib/scout/workflow/python/inputs.rb +33 -23
- data/lib/scout/workflow/python/task.rb +219 -22
- data/lib/scout/workflow/python.rb +236 -9
- data/lib/scout-rig/testing/availability.rb +608 -0
- data/python/README.md +51 -0
- data/python/scout/__init__.py +2 -0
- data/python/scout/runner.py +193 -29
- data/python/scout/task_errors.py +6 -0
- data/research/00_scope_and_themes.md +131 -0
- data/research/data-conversion-and-scripting-analysis.md +209 -0
- data/research/design-philosophy-analysis.md +246 -0
- data/research/python-helper-package-analysis.md +272 -0
- data/research/pythonworkflow-analysis.md +199 -0
- data/research/scoutpython-bridge-analysis.md +185 -0
- data/research/synthesis-report.md +109 -0
- data/research/testing-availability-baseline.md +1625 -0
- data/research/testing-availability-final-report.md +500 -0
- data/research/validation-report.md +149 -0
- data/scout-rig.gemspec +35 -5
- data/test/scout/python/test_util.rb +13 -0
- data/test/scout/test_python.rb +10 -0
- data/test/scout/workflow/python/test_task.rb +1119 -1
- data/test/scout-rig/testing/test_availability.rb +495 -0
- data/test/scout-rig/testing/test_test_support.rb +166 -0
- data/test/test_helper.rb +96 -19
- metadata +47 -4
- data/doc/Python.md +0 -482
- data/doc/PythonWorkflow.md +0 -163
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
|
|
2
2
|
require File.expand_path(__FILE__).sub(%r(.*/test/), '').sub(/test_(.*)\.rb/,'\1')
|
|
3
|
+
require 'scout/workflow/python'
|
|
4
|
+
require 'shellwords'
|
|
5
|
+
require 'tmpdir'
|
|
6
|
+
require 'fileutils'
|
|
3
7
|
|
|
4
8
|
class TestPythonWorkflowTask < Test::Unit::TestCase
|
|
5
9
|
def test_load_metadata
|
|
@@ -33,5 +37,1119 @@ scout.task(hello)
|
|
|
33
37
|
assert_equal 'hello', res.first['name']
|
|
34
38
|
end
|
|
35
39
|
end
|
|
36
|
-
end
|
|
37
40
|
|
|
41
|
+
def test_load_directory_batch_matches_default_metadata_and_runs_one_process
|
|
42
|
+
with_python_task_directory do |directory, files|
|
|
43
|
+
calls = capture_python_runs do
|
|
44
|
+
@batch_workflow = PythonWorkflow.load_directory(directory, 'BatchMetadataTest', batch: true)
|
|
45
|
+
end
|
|
46
|
+
batch_metadata = workflow_task_metadata(@batch_workflow)
|
|
47
|
+
alpha_value_description = batch_metadata.find { |metadata| metadata['name'] == 'alpha' }
|
|
48
|
+
.fetch('inputs').find { |input| input['name'] == 'value' }
|
|
49
|
+
.fetch('description')
|
|
50
|
+
assert_equal 'The text to return unchanged.', alpha_value_description
|
|
51
|
+
|
|
52
|
+
assert_equal 1, calls.length, 'batch loader should start Python once for all files'
|
|
53
|
+
assert_equal '--scout-metadata-files', Shellwords.split(calls.first.last).first
|
|
54
|
+
|
|
55
|
+
calls = capture_python_runs do
|
|
56
|
+
default_workflow = PythonWorkflow.load_directory(directory, 'DefaultMetadataTest')
|
|
57
|
+
assert_equal batch_metadata, workflow_task_metadata(default_workflow)
|
|
58
|
+
end
|
|
59
|
+
assert_equal 1, calls.length, 'default loader should use the available batch metadata collection'
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_annotation_mapping_contract
|
|
64
|
+
cases = {
|
|
65
|
+
'str' => 'string',
|
|
66
|
+
'bytes' => 'binary',
|
|
67
|
+
'int' => 'integer',
|
|
68
|
+
'float' => 'float',
|
|
69
|
+
'bool' => 'boolean',
|
|
70
|
+
'pathlib.Path' => 'path',
|
|
71
|
+
'typing.Any' => 'string',
|
|
72
|
+
'typing.Optional[int]' => 'integer',
|
|
73
|
+
'int | None' => 'integer',
|
|
74
|
+
'pathlib.Path | None' => 'path',
|
|
75
|
+
'typing.Optional[pathlib.Path]' => 'path',
|
|
76
|
+
'typing.Annotated[pathlib.Path, "f"]' => 'path',
|
|
77
|
+
'list[str]' => 'list[string]',
|
|
78
|
+
'typing.List[pathlib.Path]' => 'list[path]',
|
|
79
|
+
'typing.Annotated[list[str], "a"]' => 'list[string]',
|
|
80
|
+
'typing.Optional[list[int]]' => 'list[integer]'
|
|
81
|
+
}
|
|
82
|
+
cases.each do |annotation, expected|
|
|
83
|
+
source = "import scout, typing, pathlib\ndef probe(param: #{annotation} = None) -> str:\n return 'ok'\nscout.task(probe)\n"
|
|
84
|
+
TmpFile.with_file source do |task_file|
|
|
85
|
+
param = PythonWorkflow.read_python_metadata(task_file).first['params'].first
|
|
86
|
+
assert_equal expected, param['type'], "annotation #{annotation}"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Optional/union Path annotations must land on :file on the Scout side,
|
|
91
|
+
# with required-ness driven by the Python default.
|
|
92
|
+
['pathlib.Path | None', 'typing.Optional[pathlib.Path]'].each do |annotation|
|
|
93
|
+
source = "import scout, typing, pathlib\ndef probe(f: #{annotation}) -> str:\n return 'ok'\nscout.task(probe)\n"
|
|
94
|
+
TmpFile.with_file source do |task_file|
|
|
95
|
+
param = PythonWorkflow.read_python_metadata(task_file).first['params'].first
|
|
96
|
+
assert_equal :file, PythonWorkflow.map_param(param)[:type], "annotation #{annotation}"
|
|
97
|
+
assert_true PythonWorkflow.map_param(param)[:options][:required], "annotation #{annotation} required without default"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
source_with_default = "import scout, typing, pathlib\ndef probe(f: #{annotation} = None) -> str:\n return 'ok'\nscout.task(probe)\n"
|
|
101
|
+
TmpFile.with_file source_with_default do |task_file|
|
|
102
|
+
param = PythonWorkflow.read_python_metadata(task_file).first['params'].first
|
|
103
|
+
mapped = PythonWorkflow.map_param(param)
|
|
104
|
+
assert_false mapped[:options].key?(:required) && mapped[:options][:required], "annotation #{annotation} not required with None default"
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_unsupported_annotations_fail_loudly
|
|
110
|
+
['dict', 'list', 'typing.List', 'int | str'].each do |annotation|
|
|
111
|
+
source = "import scout, typing\ndef probe(param: #{annotation} = None) -> str:\n return 'ok'\nscout.task(probe)\n"
|
|
112
|
+
TmpFile.with_file source do |task_file|
|
|
113
|
+
error = assert_raise(ProcessFailed) { PythonWorkflow.read_python_metadata(task_file) }
|
|
114
|
+
assert_match(/Unsupported parameter annotation for 'param'/, error.message)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def test_input_contract_reports_python_and_scout_sides
|
|
120
|
+
source = "import scout, pathlib\ndef probe(word: str, items: list[pathlib.Path] = None, n: int = 3) -> str:\n return 'ok'\nscout.task(probe)\n"
|
|
121
|
+
with_runtime_task(source) do |_directory, workflow, file|
|
|
122
|
+
contract = workflow.python_input_contracts['probe']
|
|
123
|
+
assert_equal 'probe', contract['task']
|
|
124
|
+
inputs = contract['inputs']
|
|
125
|
+
word = inputs.find { |i| i['name'] == 'word' }
|
|
126
|
+
assert_equal 'string', word['python_type']
|
|
127
|
+
assert_equal :string, word['scout_type']
|
|
128
|
+
assert_true word['required']
|
|
129
|
+
items = inputs.find { |i| i['name'] == 'items' }
|
|
130
|
+
assert_equal 'list[path]', items['python_type']
|
|
131
|
+
assert_equal :file_array, items['scout_type']
|
|
132
|
+
assert_equal '+', items['flags']['nargs']
|
|
133
|
+
assert_equal 'path', items['flags']['elem_type']
|
|
134
|
+
assert_false items['required']
|
|
135
|
+
assert_equal 3, inputs.find { |i| i['name'] == 'n' }['default']
|
|
136
|
+
|
|
137
|
+
assert_equal 'list[path]', workflow.python_input_contract('probe', 'items')['python_type']
|
|
138
|
+
assert_nil workflow.python_input_contract('probe', 'missing')
|
|
139
|
+
assert_nil workflow.python_input_contract('missing', 'items')
|
|
140
|
+
|
|
141
|
+
# Behavior column reflects the registered semantics per scout type.
|
|
142
|
+
assert_equal 'text or loaded file contents', word['behavior']
|
|
143
|
+
assert_equal 'list of filenames', items['behavior']
|
|
144
|
+
assert_false word['flags'].key?('noload')
|
|
145
|
+
|
|
146
|
+
workflow.python_literal_strings = true
|
|
147
|
+
workflow.python_task :probe, file: file
|
|
148
|
+
literal = workflow.python_input_contracts['probe']['inputs'].find { |i| i['name'] == 'word' }
|
|
149
|
+
assert_equal 'literal text', literal['behavior']
|
|
150
|
+
assert_true literal['flags']['noload']
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def test_build_python_argv_list_and_empty_string_semantics
|
|
155
|
+
params = [
|
|
156
|
+
{ 'name' => 'items', 'type' => 'list[string]' },
|
|
157
|
+
{ 'name' => 'word', 'type' => 'string' },
|
|
158
|
+
{ 'name' => 'other', 'type' => 'string', 'default' => 'dflt' }
|
|
159
|
+
]
|
|
160
|
+
assert_equal %w[--items a b], PythonWorkflow.build_python_argv(params, { 'items' => %w[a b] })
|
|
161
|
+
assert_equal %w[--items a b], PythonWorkflow.build_python_argv(params, { 'items' => 'a, b' })
|
|
162
|
+
TmpFile.with_file "line1\nline2\n" do |list_file|
|
|
163
|
+
# Contract: a String list value stays path text; filenames are never
|
|
164
|
+
# reinterpreted as contents (the Array form is exercised below).
|
|
165
|
+
assert_equal ['--items', list_file], PythonWorkflow.build_python_argv(params, { 'items' => list_file })
|
|
166
|
+
end
|
|
167
|
+
assert_equal %w[--word --x], PythonWorkflow.build_python_argv(params, { 'word' => '--x' })
|
|
168
|
+
assert_equal ['--word', ''], PythonWorkflow.build_python_argv(params, { 'word' => '' })
|
|
169
|
+
assert_equal ['--other', ''], PythonWorkflow.build_python_argv(params, { 'other' => '' })
|
|
170
|
+
assert_equal [], PythonWorkflow.build_python_argv(params, { 'word' => nil })
|
|
171
|
+
assert_equal %w[--other dflt], PythonWorkflow.build_python_argv(params, { 'other' => 'dflt' })
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def test_literal_strings_flag_keeps_string_inputs_literal
|
|
176
|
+
source = "import scout\ndef probe(word: str = None) -> str:\n return word\nscout.task(probe)\n"
|
|
177
|
+
with_runtime_task(source) do |directory, workflow, file|
|
|
178
|
+
x_file = File.join(directory, 'x.txt')
|
|
179
|
+
File.write(x_file, 'file-contents')
|
|
180
|
+
|
|
181
|
+
# Default (false): Scout's input layer loads a String naming an
|
|
182
|
+
# existing file into its contents before Python runs.
|
|
183
|
+
assert_equal 'file-contents', workflow.job(:probe, 'word' => x_file).run
|
|
184
|
+
|
|
185
|
+
# Opt-in: the input is registered with :noload, the literal path
|
|
186
|
+
# string reaches Python. Re-registering replaces the task, so drop the
|
|
187
|
+
# memoized job (Persist.memory key includes the task object's id).
|
|
188
|
+
workflow.python_literal_strings = true
|
|
189
|
+
workflow.python_task :probe, file: file
|
|
190
|
+
Workflow.job_cache.clear
|
|
191
|
+
assert_equal x_file, workflow.job(:probe, 'word' => x_file).run
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def test_path_params_reach_python_as_pathlib_objects
|
|
196
|
+
source = "import pathlib\ndef probe_type(f: pathlib.Path = None) -> str:\n return type(f).__name__\n"
|
|
197
|
+
with_runtime_task("import scout, pathlib\n#{source}scout.task(probe_type)\n") do |_directory, workflow, _file|
|
|
198
|
+
# pathlib.Path is PosixPath on this platform; the caster must yield the
|
|
199
|
+
# real object, not a string.
|
|
200
|
+
assert_equal 'PosixPath', workflow.job(:probe_type, 'f' => __FILE__).run
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
source = "def probe_read(f: pathlib.Path = None) -> str:\n return f.read_text().strip()\n"
|
|
204
|
+
with_runtime_task("import scout, pathlib\n#{source}scout.task(probe_read)\n") do |directory, workflow, _file|
|
|
205
|
+
path_file = File.join(directory, 'readable.txt')
|
|
206
|
+
File.write(path_file, 'inside-file')
|
|
207
|
+
assert_equal 'inside-file', workflow.job(:probe_read, 'f' => path_file).run
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def test_consumer_reads_producer_output_path
|
|
212
|
+
Dir.mktmpdir('python-chain') do |directory|
|
|
213
|
+
producer = File.join(directory, 'producer.py')
|
|
214
|
+
File.write(producer, <<~PYTHON)
|
|
215
|
+
import pathlib
|
|
216
|
+
|
|
217
|
+
def producer() -> pathlib.Path:
|
|
218
|
+
out = pathlib.Path('#{directory}') / 'produced.txt'
|
|
219
|
+
out.write_text('chain-contents')
|
|
220
|
+
return out
|
|
221
|
+
|
|
222
|
+
import scout
|
|
223
|
+
scout.task(producer)
|
|
224
|
+
PYTHON
|
|
225
|
+
consumer = File.join(directory, 'consumer.py')
|
|
226
|
+
File.write(consumer, <<~PYTHON)
|
|
227
|
+
import pathlib
|
|
228
|
+
|
|
229
|
+
def consumer(p: pathlib.Path) -> str:
|
|
230
|
+
return p.read_text().strip()
|
|
231
|
+
|
|
232
|
+
import scout
|
|
233
|
+
scout.task(consumer)
|
|
234
|
+
PYTHON
|
|
235
|
+
workflow = PythonWorkflow.load_directory(Path.setup(directory), "PythonChain#{object_id}_#{rand(1_000_000)}")
|
|
236
|
+
begin
|
|
237
|
+
produced = workflow.job(:producer).run
|
|
238
|
+
assert_equal "#{directory}/produced.txt", File.expand_path(produced)
|
|
239
|
+
assert_equal 'chain-contents', File.read(produced)
|
|
240
|
+
|
|
241
|
+
assert_equal 'chain-contents', workflow.job(:consumer, 'p' => produced).run
|
|
242
|
+
ensure
|
|
243
|
+
Object.send(:remove_const, workflow.name.to_sym) if workflow.name && Object.const_defined?(workflow.name.to_sym, false)
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def test_list_params_keep_file_meaning_across_modes
|
|
249
|
+
list_source = "import scout\ndef probe(items: list[str] = None) -> str:\n return ','.join(items or [])\nscout.task(probe)\n"
|
|
250
|
+
with_runtime_task(list_source) do |directory, workflow, file|
|
|
251
|
+
a = File.join(directory, 'a.txt'); File.write(a, 'A')
|
|
252
|
+
b = File.join(directory, 'b.txt'); File.write(b, 'B')
|
|
253
|
+
|
|
254
|
+
# list[str] maps to :array, Scout's :array layer passes each element
|
|
255
|
+
# through unchanged (path text): the bridge serializes an Array
|
|
256
|
+
# literally and never reinterprets a filename as file contents.
|
|
257
|
+
assert_equal "#{a},#{b}", workflow.job(:probe, 'items' => [a, b]).run
|
|
258
|
+
assert_equal 'A,B', workflow.job(:probe, 'items' => ['A', 'B']).run
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
path_source = "import scout, pathlib\ndef probe(items: list[pathlib.Path] = None) -> str:\n return ','.join(type(e).__name__ for e in (items or []))\nscout.task(probe)\n"
|
|
262
|
+
with_runtime_task(path_source) do |directory, workflow, file|
|
|
263
|
+
a = File.join(directory, 'a.txt'); File.write(a, 'A')
|
|
264
|
+
b = File.join(directory, 'b.txt'); File.write(b, 'B')
|
|
265
|
+
|
|
266
|
+
# list[Path]: elements are cast to Path objects; passing an Array
|
|
267
|
+
# keeps each element's meaning (a bare filename String would be
|
|
268
|
+
# expanded by Scout's :file_array layer instead).
|
|
269
|
+
assert_equal 'PosixPath,PosixPath', workflow.job(:probe, 'items' => [a, b]).run
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def test_refresh_python_tasks_reconciles_directory_changes
|
|
274
|
+
with_runtime_task("import scout\ndef alpha_task() -> str:\n return 'alpha'\nscout.task(alpha_task)\n") do |directory, workflow, file|
|
|
275
|
+
assert_equal({ added: [], changed: [], removed: [], unchanged: %w[alpha_task] }, workflow.refresh_python_tasks!)
|
|
276
|
+
|
|
277
|
+
File.write(file, "import scout\ndef alpha_task(extra: int = 1) -> str:\n return 'alpha'\nscout.task(alpha_task)\n")
|
|
278
|
+
assert_equal({ added: [], changed: %w[alpha_task], removed: [], unchanged: [] }, workflow.refresh_python_tasks!)
|
|
279
|
+
assert workflow.python_input_contracts['alpha_task']['inputs'].any? { |i| i['name'] == 'extra' }
|
|
280
|
+
|
|
281
|
+
beta = File.join(directory, 'beta_task.py')
|
|
282
|
+
File.write(beta, "import scout\ndef beta_task() -> str:\n return 'beta'\nscout.task(beta_task)\n")
|
|
283
|
+
assert_equal({ added: %w[beta_task], changed: [], removed: [], unchanged: %w[alpha_task] }, workflow.refresh_python_tasks!)
|
|
284
|
+
assert workflow.tasks.key?(:beta_task)
|
|
285
|
+
|
|
286
|
+
File.delete(file)
|
|
287
|
+
assert_equal({ added: [], changed: [], removed: %w[alpha_task], unchanged: %w[beta_task] }, workflow.refresh_python_tasks!)
|
|
288
|
+
assert_false workflow.tasks.key?(:alpha_task)
|
|
289
|
+
assert workflow.tasks.key?(:beta_task)
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def test_refresh_python_tasks_flips_registered_input_type_without_duplicates
|
|
294
|
+
with_runtime_task("import scout\ndef alpha_task(value: str = None) -> str:\n return str(value)\nscout.task(alpha_task)\n") do |_directory, workflow, file|
|
|
295
|
+
assert_equal :string, workflow.tasks[:alpha_task].inputs.assoc(:value)[1]
|
|
296
|
+
|
|
297
|
+
# Param type flip: str -> Path must flip :string -> :file on refresh.
|
|
298
|
+
File.write(file, "import scout, pathlib\ndef alpha_task(value: pathlib.Path = None) -> str:\n return str(value)\nscout.task(alpha_task)\n")
|
|
299
|
+
summary = workflow.refresh_python_tasks!
|
|
300
|
+
assert_equal %w[alpha_task], summary[:changed]
|
|
301
|
+
assert_equal :file, workflow.tasks[:alpha_task].inputs.assoc(:value)[1]
|
|
302
|
+
|
|
303
|
+
before = workflow_task_metadata(workflow)
|
|
304
|
+
|
|
305
|
+
# Re-registration must not accumulate inputs or duplicate task names.
|
|
306
|
+
input_names = workflow.tasks[:alpha_task].inputs.map(&:first)
|
|
307
|
+
assert_equal input_names.uniq, input_names
|
|
308
|
+
assert_equal 1, input_names.length
|
|
309
|
+
assert workflow_task_metadata(workflow).map { |m| m['name'] }.tally.values.all? { |n| n == 1 }
|
|
310
|
+
|
|
311
|
+
# Idempotency: a second refresh with unchanged sources reports all
|
|
312
|
+
# unchanged and keeps the task object identity stable.
|
|
313
|
+
unchanged_task = workflow.tasks[:alpha_task]
|
|
314
|
+
snapshot = workflow_task_metadata(workflow)
|
|
315
|
+
summary2 = workflow.refresh_python_tasks!
|
|
316
|
+
assert_equal({ added: [], changed: [], removed: [], unchanged: %w[alpha_task] }, summary2)
|
|
317
|
+
assert workflow.tasks[:alpha_task].equal?(unchanged_task)
|
|
318
|
+
assert_equal before, workflow_task_metadata(workflow)
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def test_batch_runs_conditional_main_registration
|
|
323
|
+
with_python_task_directory(conditional_main: true) do |directory, _files|
|
|
324
|
+
batch_calls = capture_python_runs do
|
|
325
|
+
@conditional_batch = PythonWorkflow.load_directory(directory, 'ConditionalBatchTest', batch: true)
|
|
326
|
+
end
|
|
327
|
+
default_calls = capture_python_runs do
|
|
328
|
+
@conditional_default = PythonWorkflow.load_directory(directory, 'ConditionalDefaultTest')
|
|
329
|
+
end
|
|
330
|
+
batch = @conditional_batch
|
|
331
|
+
default = @conditional_default
|
|
332
|
+
|
|
333
|
+
assert_equal workflow_task_metadata(default), workflow_task_metadata(batch)
|
|
334
|
+
assert_equal %w[alpha beta], workflow_task_metadata(batch).map { |metadata| metadata['name'] }.sort
|
|
335
|
+
assert_equal 1, batch_calls.length
|
|
336
|
+
assert_equal '--scout-metadata-files', Shellwords.split(batch_calls.first.last).first
|
|
337
|
+
assert_equal 1, default_calls.length
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def test_workflow_extension_then_directory_assignment_loads_tasks_in_batch
|
|
342
|
+
with_python_task_directory do |directory, _files|
|
|
343
|
+
workflow_name = "PythonWorkflowLifecycleTest#{object_id}"
|
|
344
|
+
workflow = Module.new
|
|
345
|
+
Object.const_set(workflow_name, workflow)
|
|
346
|
+
workflow.extend Workflow
|
|
347
|
+
|
|
348
|
+
calls = capture_python_runs do
|
|
349
|
+
workflow.extend PythonWorkflow
|
|
350
|
+
end
|
|
351
|
+
assert_equal [], calls, 'extending PythonWorkflow must not preload the default task directory'
|
|
352
|
+
assert_empty workflow.tasks, 'extension should not register default-directory tasks'
|
|
353
|
+
|
|
354
|
+
calls = capture_python_runs { workflow.python_task_dir = directory }
|
|
355
|
+
|
|
356
|
+
assert_equal 1, calls.length, 'directory assignment should use one batch metadata invocation'
|
|
357
|
+
assert_equal '--scout-metadata-files', Shellwords.split(calls.first.last).first
|
|
358
|
+
metadata = workflow_task_metadata(workflow)
|
|
359
|
+
assert_equal %w[alpha beta], metadata.map { |task| task['name'] }.sort
|
|
360
|
+
alpha = metadata.find { |task| task['name'] == 'alpha' }
|
|
361
|
+
assert_equal 'The text to return unchanged.', alpha.fetch('inputs').find { |input| input['name'] == 'value' }.fetch('description')
|
|
362
|
+
ensure
|
|
363
|
+
Object.send(:remove_const, workflow_name) if workflow_name && Object.const_defined?(workflow_name, false)
|
|
364
|
+
end
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def test_batch_does_not_reexecute_shared_imported_module_between_files
|
|
368
|
+
with_python_task_directory(shared_import: true) do |_directory, files|
|
|
369
|
+
metadata = PythonWorkflow.read_python_metadata_files(files)
|
|
370
|
+
first = metadata.fetch(File.expand_path(files[0])).map { |meta| meta['name'] }
|
|
371
|
+
second = metadata.fetch(File.expand_path(files[1])).map { |meta| meta['name'] }
|
|
372
|
+
|
|
373
|
+
assert_equal %w[alpha shared_task], first.sort
|
|
374
|
+
assert_equal ['beta'], second
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
def test_register_python_task_metadata_rejects_empty_and_malformed_metadata
|
|
379
|
+
with_runtime_task("import scout\ndef probe() -> str:\n return 'ok'\nscout.task(probe)\n") do |_directory, workflow, file|
|
|
380
|
+
registered = workflow.tasks[:probe]
|
|
381
|
+
error = assert_raise(RuntimeError) do
|
|
382
|
+
workflow.send(:register_python_task_metadata, file, [])
|
|
383
|
+
end
|
|
384
|
+
assert_match(/No tasks exported by/, error.message)
|
|
385
|
+
|
|
386
|
+
error = assert_raise(RuntimeError) do
|
|
387
|
+
workflow.send(:register_python_task_metadata, file, ['not-a-hash'])
|
|
388
|
+
end
|
|
389
|
+
assert_match(/expected a Hash/, error.message)
|
|
390
|
+
|
|
391
|
+
error = assert_raise(RuntimeError) do
|
|
392
|
+
workflow.send(:register_python_task_metadata, file, [{ 'params' => [] }])
|
|
393
|
+
end
|
|
394
|
+
assert_match(/missing 'name'/, error.message)
|
|
395
|
+
|
|
396
|
+
error = assert_raise(RuntimeError) do
|
|
397
|
+
workflow.send(:register_python_task_metadata, file, [{ 'name' => 'probe' }])
|
|
398
|
+
end
|
|
399
|
+
assert_match(/missing 'params'/, error.message)
|
|
400
|
+
|
|
401
|
+
error = assert_raise(RuntimeError) do
|
|
402
|
+
workflow.send(:register_python_task_metadata, file, [{ 'name' => 'probe', 'params' => 'nope' }])
|
|
403
|
+
end
|
|
404
|
+
assert_match(/'params' must be a list/, error.message)
|
|
405
|
+
|
|
406
|
+
# No DSL side effects happened for any rejected metadata.
|
|
407
|
+
assert workflow.tasks[:probe].equal?(registered)
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def test_register_python_task_metadata_rejects_duplicate_exported_names
|
|
412
|
+
with_runtime_task("import scout\ndef probe() -> str:\n return 'ok'\nscout.task(probe)\n") do |_directory, workflow, file|
|
|
413
|
+
duplicated = [
|
|
414
|
+
{ 'name' => 'probe', 'params' => [], 'returns' => 'str' },
|
|
415
|
+
{ 'name' => 'probe', 'params' => [], 'returns' => 'str' }
|
|
416
|
+
]
|
|
417
|
+
error = assert_raise(RuntimeError) do
|
|
418
|
+
workflow.send(:register_python_task_metadata, file, duplicated)
|
|
419
|
+
end
|
|
420
|
+
assert_match(/Duplicate exported task name probe/, error.message)
|
|
421
|
+
|
|
422
|
+
other_file = File.expand_path('other_exported.py')
|
|
423
|
+
conflict = [{ 'name' => 'probe', 'params' => [], 'returns' => 'str' }]
|
|
424
|
+
workflow.send(:register_python_task_metadata, file, conflict)
|
|
425
|
+
assert workflow.tasks[:probe]
|
|
426
|
+
|
|
427
|
+
error = assert_raise(RuntimeError) do
|
|
428
|
+
workflow.send(:register_python_task_metadata, other_file, conflict)
|
|
429
|
+
end
|
|
430
|
+
assert_match(/already registered by/, error.message)
|
|
431
|
+
|
|
432
|
+
# Re-registering the same file replaces its entry without error.
|
|
433
|
+
replaced = [{ 'name' => 'probe', 'params' => [{ 'name' => 'word', 'type' => 'str', 'annotation' => 'str', 'required' => true }], 'returns' => 'str' }]
|
|
434
|
+
workflow.send(:register_python_task_metadata, file, replaced)
|
|
435
|
+
assert workflow.tasks[:probe]
|
|
436
|
+
names = workflow.tasks.keys.map(&:to_s)
|
|
437
|
+
assert_equal 1, names.count('probe')
|
|
438
|
+
end
|
|
439
|
+
end
|
|
440
|
+
def test_docstring_extension_is_extracted_and_applies_only_to_its_function
|
|
441
|
+
source = <<~PYTHON
|
|
442
|
+
import scout
|
|
443
|
+
|
|
444
|
+
def tailored(value: str) -> str:
|
|
445
|
+
"""An individual description.
|
|
446
|
+
|
|
447
|
+
Extension: json
|
|
448
|
+
|
|
449
|
+
Args:
|
|
450
|
+
value: The parameter help must survive extraction.
|
|
451
|
+
"""
|
|
452
|
+
return value
|
|
453
|
+
|
|
454
|
+
def sibling() -> str:
|
|
455
|
+
"""A sibling description."""
|
|
456
|
+
return 'text'
|
|
457
|
+
|
|
458
|
+
scout.task(tailored)
|
|
459
|
+
scout.task(sibling)
|
|
460
|
+
PYTHON
|
|
461
|
+
|
|
462
|
+
with_runtime_task(source) do |_directory, workflow, file|
|
|
463
|
+
metadata = PythonWorkflow.read_python_metadata(file).to_h { |meta| [meta['name'], meta] }
|
|
464
|
+
assert_equal 'json', metadata.fetch('tailored').fetch('extension')
|
|
465
|
+
assert_false metadata.fetch('sibling').key?('extension')
|
|
466
|
+
assert_equal 'An individual description.', metadata.fetch('tailored').fetch('description')
|
|
467
|
+
assert_equal 'The parameter help must survive extraction.', metadata.fetch('tailored').fetch('params').first.fetch('help')
|
|
468
|
+
|
|
469
|
+
assert_equal 'json', workflow.tasks[:tailored].extension
|
|
470
|
+
assert_not_equal 'json', workflow.tasks[:sibling].extension
|
|
471
|
+
assert_match(/\.json\z/, workflow.job(:tailored, value: 'sample').path.to_s)
|
|
472
|
+
end
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
def test_docstring_extension_overrides_file_level_fallback_only_for_that_function
|
|
476
|
+
source = <<~PYTHON
|
|
477
|
+
import scout
|
|
478
|
+
|
|
479
|
+
def custom() -> str:
|
|
480
|
+
"""Custom result.
|
|
481
|
+
|
|
482
|
+
Extension: json
|
|
483
|
+
"""
|
|
484
|
+
return 'custom'
|
|
485
|
+
|
|
486
|
+
def fallback() -> str:
|
|
487
|
+
"""Fallback result."""
|
|
488
|
+
return 'fallback'
|
|
489
|
+
|
|
490
|
+
scout.task(custom)
|
|
491
|
+
scout.task(fallback)
|
|
492
|
+
PYTHON
|
|
493
|
+
|
|
494
|
+
with_runtime_task(source) do |_directory, _loaded, file|
|
|
495
|
+
workflow = Module.new
|
|
496
|
+
workflow.extend Workflow
|
|
497
|
+
workflow.extend PythonWorkflow
|
|
498
|
+
workflow.python_task :fixture, file: file, extension: 'csv'
|
|
499
|
+
|
|
500
|
+
assert_equal 'json', workflow.tasks[:custom].extension
|
|
501
|
+
assert_equal 'csv', workflow.tasks[:fallback].extension
|
|
502
|
+
assert_match(/\.json\z/, workflow.job(:custom).path.to_s)
|
|
503
|
+
assert_match(/\.csv\z/, workflow.job(:fallback).path.to_s)
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
def test_docstring_extension_is_available_in_batch_and_per_file_metadata_discovery
|
|
508
|
+
with_extension_task_directory do |directory|
|
|
509
|
+
custom_file = File.join(directory, 'custom.py')
|
|
510
|
+
fallback_file = File.join(directory, 'fallback.py')
|
|
511
|
+
|
|
512
|
+
# Exercise the batch metadata collector directly, and verify registration
|
|
513
|
+
# on the normal batch directory-loading route.
|
|
514
|
+
batch_metadata = PythonWorkflow.read_python_metadata_files([custom_file, fallback_file])
|
|
515
|
+
assert_equal 'json', batch_metadata.fetch(File.expand_path(custom_file)).first.fetch('extension')
|
|
516
|
+
assert_false batch_metadata.fetch(File.expand_path(fallback_file)).first.key?('extension')
|
|
517
|
+
|
|
518
|
+
workflow_name = "ExtensionDiscovery#{object_id}"
|
|
519
|
+
begin
|
|
520
|
+
workflow = PythonWorkflow.load_directory(Path.setup(directory), workflow_name, batch: true)
|
|
521
|
+
assert_equal 'json', workflow.tasks[:custom].extension
|
|
522
|
+
assert_not_equal 'json', workflow.tasks[:fallback].extension
|
|
523
|
+
ensure
|
|
524
|
+
Object.send(:remove_const, workflow_name) if Object.const_defined?(workflow_name, false)
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
# The installed Open API lacks readpath, which the per-file directory
|
|
528
|
+
# loader uses. Exercise per-file runner metadata extraction directly.
|
|
529
|
+
[custom_file, fallback_file].each do |file|
|
|
530
|
+
metadata = PythonWorkflow.read_python_metadata(file).first
|
|
531
|
+
expected_extension = File.basename(file) == 'custom.py' ? 'json' : nil
|
|
532
|
+
assert_equal expected_extension, metadata['extension']
|
|
533
|
+
end
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
def test_runtime_task_receives_its_step_files_directory
|
|
538
|
+
source = <<~PYTHON
|
|
539
|
+
import os
|
|
540
|
+
from pathlib import Path
|
|
541
|
+
import scout
|
|
542
|
+
|
|
543
|
+
def sidecar_path() -> str:
|
|
544
|
+
directory = Path(os.environ["SCOUT_JOB_FILES_DIR"])
|
|
545
|
+
directory.mkdir(parents=True, exist_ok=True)
|
|
546
|
+
(directory / "auxiliary.txt").write_text("task output", encoding="utf-8")
|
|
547
|
+
return str(directory)
|
|
548
|
+
|
|
549
|
+
scout.task(sidecar_path)
|
|
550
|
+
PYTHON
|
|
551
|
+
|
|
552
|
+
with_runtime_task(source) do |_directory, workflow, _file|
|
|
553
|
+
step = workflow.job(:sidecar_path)
|
|
554
|
+
returned_directory = step.run
|
|
555
|
+
assert_equal File.expand_path(step.files_dir), File.expand_path(returned_directory)
|
|
556
|
+
assert_equal 'task output', File.read(File.join(step.files_dir, 'auxiliary.txt'))
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
def test_docstring_extension_rejects_empty_multi_token_and_duplicate_fields
|
|
561
|
+
["Extension:", "Extension: json csv", "Extension: json\nExtension: csv"].each do |fields|
|
|
562
|
+
source = <<~PYTHON
|
|
563
|
+
import scout
|
|
564
|
+
|
|
565
|
+
def invalid() -> str:
|
|
566
|
+
"""Invalid extension.
|
|
567
|
+
#{fields.gsub("\n", "\n ")}
|
|
568
|
+
"""
|
|
569
|
+
return 'x'
|
|
570
|
+
|
|
571
|
+
scout.task(invalid)
|
|
572
|
+
PYTHON
|
|
573
|
+
TmpFile.with_file(source) do |file|
|
|
574
|
+
error = assert_raise(ProcessFailed) { PythonWorkflow.read_python_metadata(file) }
|
|
575
|
+
assert_match(/Extension:/, error.message)
|
|
576
|
+
end
|
|
577
|
+
end
|
|
578
|
+
end
|
|
579
|
+
|
|
580
|
+
def test_run_file_does_not_inherit_stale_files_directory
|
|
581
|
+
TmpFile.with_file("import os, json
|
|
582
|
+
print(json.dumps(os.environ.get('SCOUT_JOB_FILES_DIR')))
|
|
583
|
+
") do |file|
|
|
584
|
+
previous = ENV['SCOUT_JOB_FILES_DIR']
|
|
585
|
+
ENV['SCOUT_JOB_FILES_DIR'] = '/stale/parent/sidecar'
|
|
586
|
+
output = ScoutPython.run_file(file, '')
|
|
587
|
+
assert_nil JSON.parse(output.read)
|
|
588
|
+
assert_equal '/stale/parent/sidecar', ENV['SCOUT_JOB_FILES_DIR'], 'run_file must not mutate Ruby ENV'
|
|
589
|
+
ensure
|
|
590
|
+
previous.nil? ? ENV.delete('SCOUT_JOB_FILES_DIR') : ENV['SCOUT_JOB_FILES_DIR'] = previous
|
|
591
|
+
end
|
|
592
|
+
end
|
|
593
|
+
|
|
594
|
+
def test_run_file_explicit_environment_overrides_stale_parent_value
|
|
595
|
+
TmpFile.with_file("import os, json
|
|
596
|
+
print(json.dumps(os.environ.get('SCOUT_JOB_FILES_DIR')))
|
|
597
|
+
") do |file|
|
|
598
|
+
previous = ENV['SCOUT_JOB_FILES_DIR']
|
|
599
|
+
ENV['SCOUT_JOB_FILES_DIR'] = '/stale/parent/sidecar'
|
|
600
|
+
output = ScoutPython.run_file(file, '', env: { 'SCOUT_JOB_FILES_DIR' => '/explicit/child/sidecar' })
|
|
601
|
+
assert_equal '/explicit/child/sidecar', JSON.parse(output.read)
|
|
602
|
+
assert_equal '/stale/parent/sidecar', ENV['SCOUT_JOB_FILES_DIR'], 'run_file must not mutate Ruby ENV'
|
|
603
|
+
ensure
|
|
604
|
+
previous.nil? ? ENV.delete('SCOUT_JOB_FILES_DIR') : ENV['SCOUT_JOB_FILES_DIR'] = previous
|
|
605
|
+
end
|
|
606
|
+
end
|
|
607
|
+
|
|
608
|
+
def test_run_file_unrelated_environment_does_not_inherit_stale_files_directory
|
|
609
|
+
TmpFile.with_file("import os, json\nprint(json.dumps([os.environ.get('UNRELATED_CHILD_ENV'), os.environ.get('SCOUT_JOB_FILES_DIR')]))\n") do |file|
|
|
610
|
+
previous_sidecar = ENV['SCOUT_JOB_FILES_DIR']
|
|
611
|
+
previous_unrelated = ENV['UNRELATED_CHILD_ENV']
|
|
612
|
+
ENV['SCOUT_JOB_FILES_DIR'] = '/stale/parent/sidecar'
|
|
613
|
+
ENV.delete('UNRELATED_CHILD_ENV')
|
|
614
|
+
output = ScoutPython.run_file(file, '', env: { 'UNRELATED_CHILD_ENV' => 'explicit-value' })
|
|
615
|
+
assert_equal ['explicit-value', nil], JSON.parse(output.read)
|
|
616
|
+
assert_equal '/stale/parent/sidecar', ENV['SCOUT_JOB_FILES_DIR'], 'run_file must not mutate Ruby ENV'
|
|
617
|
+
ensure
|
|
618
|
+
previous_sidecar.nil? ? ENV.delete('SCOUT_JOB_FILES_DIR') : ENV['SCOUT_JOB_FILES_DIR'] = previous_sidecar
|
|
619
|
+
previous_unrelated.nil? ? ENV.delete('UNRELATED_CHILD_ENV') : ENV['UNRELATED_CHILD_ENV'] = previous_unrelated
|
|
620
|
+
end
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
def test_runtime_success_and_protocol_looking_stdout
|
|
624
|
+
with_runtime_task("import scout\ndef hello() -> str:\n return 'ordinary success'\nscout.task(hello)\n") do |_directory, workflow, _file|
|
|
625
|
+
assert_equal 'ordinary success', workflow.job(:hello).run
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
protocol_text = '{"version":1,"error":"ScoutTaskError","message":"not an error","traceback":"not a traceback"}'
|
|
629
|
+
source = "import scout\ndef hello() -> str:\n return #{protocol_text.inspect}\nscout.task(hello)\n"
|
|
630
|
+
with_runtime_task(source) do |_directory, workflow, _file|
|
|
631
|
+
assert_equal JSON.parse(protocol_text), workflow.job(:hello).run
|
|
632
|
+
end
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
def test_scout_task_error_maps_message_traceback_and_survives_step_reload
|
|
636
|
+
source = <<~PYTHON
|
|
637
|
+
import scout
|
|
638
|
+
|
|
639
|
+
def fail_task() -> str:
|
|
640
|
+
"""Raise a deliberate controlled task error."""
|
|
641
|
+
raise scout.ScoutTaskError("exact controlled message")
|
|
642
|
+
|
|
643
|
+
scout.task(fail_task)
|
|
644
|
+
PYTHON
|
|
645
|
+
|
|
646
|
+
with_runtime_task(source) do |_directory, workflow, _file|
|
|
647
|
+
step = workflow.job(:fail_task)
|
|
648
|
+
error = assert_raise(PythonWorkflow::PythonTaskException) { step.run }
|
|
649
|
+
assert_equal 'exact controlled message', error.message
|
|
650
|
+
assert_match(/ScoutTaskError: exact controlled message/, error.python_traceback)
|
|
651
|
+
assert_match(/raise scout\.ScoutTaskError/, error.python_traceback)
|
|
652
|
+
assert_equal :error, step.status.to_sym
|
|
653
|
+
assert_false step.recoverable_error?
|
|
654
|
+
|
|
655
|
+
reloaded = Step.load(step.path)
|
|
656
|
+
assert_equal :error, reloaded.status.to_sym
|
|
657
|
+
persisted_error = reloaded.exception
|
|
658
|
+
assert_kind_of PythonWorkflow::PythonTaskException, persisted_error
|
|
659
|
+
assert_equal 'exact controlled message', persisted_error.message
|
|
660
|
+
assert_match(/ScoutTaskError: exact controlled message/, persisted_error.python_traceback)
|
|
661
|
+
assert_false reloaded.recoverable_error?
|
|
662
|
+
end
|
|
663
|
+
end
|
|
664
|
+
|
|
665
|
+
def test_arbitrary_task_exception_remains_process_failed_with_python_traceback
|
|
666
|
+
source = <<~PYTHON
|
|
667
|
+
import scout
|
|
668
|
+
|
|
669
|
+
def fail_task() -> str:
|
|
670
|
+
raise ValueError("unexpected failure")
|
|
671
|
+
|
|
672
|
+
scout.task(fail_task)
|
|
673
|
+
PYTHON
|
|
674
|
+
|
|
675
|
+
with_runtime_task(source) do |_directory, workflow, _file|
|
|
676
|
+
step = workflow.job(:fail_task)
|
|
677
|
+
error = assert_raise(ProcessFailed) { step.run }
|
|
678
|
+
assert_match(/ValueError: unexpected failure/, error.message)
|
|
679
|
+
assert_match(/Traceback \(most recent call last\)/, error.message)
|
|
680
|
+
assert_match(/raise ValueError/, error.message)
|
|
681
|
+
assert_equal :error, step.status.to_sym
|
|
682
|
+
assert_true step.recoverable_error?
|
|
683
|
+
end
|
|
684
|
+
end
|
|
685
|
+
|
|
686
|
+
def test_runtime_import_failure_remains_process_failed_with_traceback
|
|
687
|
+
source = "import scout\ndef hello() -> str:\n return 'ok'\nscout.task(hello)\n"
|
|
688
|
+
with_runtime_task(source) do |_directory, workflow, file|
|
|
689
|
+
File.write(file, "import module_that_does_not_exist_for_scout_task\n")
|
|
690
|
+
step = workflow.job(:hello)
|
|
691
|
+
error = assert_raise(ProcessFailed) { step.run }
|
|
692
|
+
assert_match(/ModuleNotFoundError/, error.message)
|
|
693
|
+
assert_match(/module_that_does_not_exist_for_scout_task/, error.message)
|
|
694
|
+
assert_equal :error, step.status.to_sym
|
|
695
|
+
assert_true step.recoverable_error?
|
|
696
|
+
end
|
|
697
|
+
end
|
|
698
|
+
|
|
699
|
+
def test_missing_or_malformed_protocol_does_not_become_controlled_error
|
|
700
|
+
source = "import scout\ndef hello() -> str:\n return 'ok'\nscout.task(hello)\n"
|
|
701
|
+
with_runtime_task(source) do |_directory, workflow, _file|
|
|
702
|
+
[nil, '{not json', '{"version":2,"error":"ScoutTaskError","message":"x","traceback":"y"}',
|
|
703
|
+
'{"version":1,"error":"ArbitraryClass","message":"x","traceback":"y"}',
|
|
704
|
+
'{"version":1,"error":"ScoutTaskError","message":4,"traceback":"y"}'].each do |protocol|
|
|
705
|
+
step = workflow.job(:hello, "case-#{protocol.inspect}")
|
|
706
|
+
original = ScoutPython.method(:run_file)
|
|
707
|
+
ScoutPython.define_singleton_method(:run_file) do |_file, args, **_options|
|
|
708
|
+
path = Shellwords.split(args).each_cons(2).find { |key, _value| key == '--scout-error-file' }&.last
|
|
709
|
+
File.write(path, protocol) if path && protocol
|
|
710
|
+
raise ProcessFailed.new(nil, 'synthetic runner failure')
|
|
711
|
+
end
|
|
712
|
+
error = assert_raise(ProcessFailed) { step.run }
|
|
713
|
+
assert_match(/synthetic runner failure/, error.message)
|
|
714
|
+
assert_equal :error, step.status.to_sym
|
|
715
|
+
assert_true step.recoverable_error?
|
|
716
|
+
ensure
|
|
717
|
+
ScoutPython.define_singleton_method(:run_file, original) if original
|
|
718
|
+
end
|
|
719
|
+
end
|
|
720
|
+
end
|
|
721
|
+
|
|
722
|
+
def test_process_start_failure_is_not_mapped_to_controlled_error
|
|
723
|
+
source = "import scout\ndef hello() -> str:\n return 'ok'\nscout.task(hello)\n"
|
|
724
|
+
with_runtime_task(source) do |_directory, workflow, _file|
|
|
725
|
+
step = workflow.job(:hello)
|
|
726
|
+
original = ScoutPython.method(:run_file)
|
|
727
|
+
ScoutPython.define_singleton_method(:run_file) do |_file, _args, **_options|
|
|
728
|
+
raise ProcessFailed.new(nil, 'synthetic executable could not be started')
|
|
729
|
+
end
|
|
730
|
+
error = assert_raise(ProcessFailed) { step.run }
|
|
731
|
+
assert_match(/Failed to run synthetic executable could not be started/, error.message)
|
|
732
|
+
assert_true step.recoverable_error?
|
|
733
|
+
ensure
|
|
734
|
+
ScoutPython.define_singleton_method(:run_file, original) if original
|
|
735
|
+
end
|
|
736
|
+
end
|
|
737
|
+
|
|
738
|
+
def test_refresh_python_tasks_applies_literal_strings_flip_without_source_change
|
|
739
|
+
source = "import scout\ndef probe(word: str = None) -> str:\n return word\nscout.task(probe)\n"
|
|
740
|
+
with_runtime_task(source) do |directory, workflow, file|
|
|
741
|
+
x_file = File.join(directory, 'x.txt')
|
|
742
|
+
File.write(x_file, 'file-contents')
|
|
743
|
+
|
|
744
|
+
# Registered with the default (false): the file is loaded.
|
|
745
|
+
assert_equal 'file-contents', workflow.job(:probe, 'word' => x_file).run
|
|
746
|
+
assert_false workflow.tasks[:probe].inputs.assoc(:word)[4].key?(:noload)
|
|
747
|
+
|
|
748
|
+
# Flipping the config re-registers on refresh even though the
|
|
749
|
+
# Python metadata (and the source file) are byte-identical.
|
|
750
|
+
workflow.python_literal_strings = true
|
|
751
|
+
summary = workflow.refresh_python_tasks!
|
|
752
|
+
assert_equal %w[probe], summary[:changed], 'config-only change must be visible in the refresh summary'
|
|
753
|
+
assert_true workflow.tasks[:probe].inputs.assoc(:word)[4][:noload], 'noload must be applied to the live registration'
|
|
754
|
+
|
|
755
|
+
# Real behaviour follows the new registration, and the earlier job is
|
|
756
|
+
# not replayed after the re-registration.
|
|
757
|
+
Workflow.job_cache.clear
|
|
758
|
+
assert_equal x_file, workflow.job(:probe, 'word' => x_file).run
|
|
759
|
+
|
|
760
|
+
# No input duplication from the re-registration.
|
|
761
|
+
input_names = workflow.tasks[:probe].inputs.map(&:first)
|
|
762
|
+
assert_equal input_names.uniq, input_names
|
|
763
|
+
|
|
764
|
+
# Idempotency: a second refresh (same sources, same config) is a no-op
|
|
765
|
+
# with stable task identity.
|
|
766
|
+
task_before = workflow.tasks[:probe]
|
|
767
|
+
summary2 = workflow.refresh_python_tasks!
|
|
768
|
+
assert_equal({ added: [], changed: [], removed: [], unchanged: %w[probe] }, summary2)
|
|
769
|
+
assert workflow.tasks[:probe].equal?(task_before)
|
|
770
|
+
end
|
|
771
|
+
end
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
def test_load_directory_accepts_literal_strings_before_loading
|
|
775
|
+
source = "import scout\ndef probe(word: str = None) -> str:\n return word\nscout.task(probe)\n"
|
|
776
|
+
Dir.mktmpdir('python-literal-init') do |directory|
|
|
777
|
+
File.write(File.join(directory, 'runtime_task.py'), source)
|
|
778
|
+
workflow_name = "PythonLiteralInitTest#{object_id}_#{rand(1_000_000)}"
|
|
779
|
+
workflow = PythonWorkflow.load_directory(Path.setup(directory), workflow_name, literal_strings: true)
|
|
780
|
+
begin
|
|
781
|
+
options = workflow.tasks[:probe].inputs.assoc(:word)[4]
|
|
782
|
+
assert_true options[:noload], 'literal_strings: true must apply before task loading'
|
|
783
|
+
contract = workflow.python_input_contracts['probe']
|
|
784
|
+
assert_false contract.key?('stale')
|
|
785
|
+
assert_equal 'literal text', contract['inputs'].first['behavior']
|
|
786
|
+
ensure
|
|
787
|
+
Object.send(:remove_const, workflow_name) if workflow_name && Object.const_defined?(workflow_name, false)
|
|
788
|
+
end
|
|
789
|
+
end
|
|
790
|
+
end
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
def test_refresh_rejects_python_task_name_taken_by_native_task
|
|
794
|
+
Dir.mktmpdir('python-native-conflict') do |directory|
|
|
795
|
+
File.write(File.join(directory, 'alpha.py'), "import scout\ndef alpha() -> str:\n return 'alpha'\nscout.task(alpha)\n")
|
|
796
|
+
File.write(File.join(directory, 'beta.py'), "import scout\ndef beta() -> str:\n return 'beta'\nscout.task(beta)\n")
|
|
797
|
+
workflow_name = "PythonNativeConflictTest#{object_id}_#{rand(1_000_000)}"
|
|
798
|
+
workflow = PythonWorkflow.load_directory(Path.setup(directory), workflow_name)
|
|
799
|
+
begin
|
|
800
|
+
# Native task takes the beta name AFTER the Python registration.
|
|
801
|
+
workflow.task beta: :string do 'native beta' end
|
|
802
|
+
|
|
803
|
+
# alpha.py still refreshes fine on its own: a Python file whose
|
|
804
|
+
# task is not shadowed is unaffected.
|
|
805
|
+
File.write(File.join(directory, 'alpha.py'), "import scout\ndef alpha(extra: int = 1) -> str:\n return 'alpha'\nscout.task(alpha)\n")
|
|
806
|
+
summary = workflow.refresh_python_tasks!
|
|
807
|
+
assert_equal %w[alpha], summary[:changed]
|
|
808
|
+
|
|
809
|
+
# The native name must be rejected explicitly, before any mutation:
|
|
810
|
+
# refreshing the shadowed file raises and leaves beta native.
|
|
811
|
+
File.write(File.join(directory, 'beta.py'), "import scout\ndef beta(n: int = 1) -> str:\n return str(n)\nscout.task(beta)\n")
|
|
812
|
+
error = assert_raise(RuntimeError) { workflow.refresh_python_tasks! }
|
|
813
|
+
assert_match(/conflicts with a native \(Ruby-defined\) task/, error.message)
|
|
814
|
+
assert_equal 'native beta', workflow.job(:beta).run, 'native task must survive the failed refresh'
|
|
815
|
+
assert_equal 'alpha', workflow.job(:alpha).run
|
|
816
|
+
|
|
817
|
+
# Removing the native task makes the same refresh succeed — the
|
|
818
|
+
# rejected conflict was the only blocker. The native run above left a
|
|
819
|
+
# persisted job under the same name; drop both replay layers so the
|
|
820
|
+
# re-registered task actually executes.
|
|
821
|
+
persisted = workflow.job(:beta)
|
|
822
|
+
FileUtils.rm_rf(persisted.path)
|
|
823
|
+
FileUtils.rm_rf("#{persisted.path}.info")
|
|
824
|
+
workflow.tasks.delete(:beta)
|
|
825
|
+
Workflow.job_cache.clear
|
|
826
|
+
summary = workflow.refresh_python_tasks!
|
|
827
|
+
assert_equal %w[beta], summary[:changed]
|
|
828
|
+
assert_equal 1, workflow.job(:beta, 'n' => 1).run
|
|
829
|
+
ensure
|
|
830
|
+
Object.send(:remove_const, workflow_name) if workflow_name && Object.const_defined?(workflow_name, false)
|
|
831
|
+
end
|
|
832
|
+
end
|
|
833
|
+
end
|
|
834
|
+
|
|
835
|
+
def test_refresh_failure_leaves_registry_and_tracking_coherent
|
|
836
|
+
Dir.mktmpdir('python-refresh-failure') do |directory|
|
|
837
|
+
File.write(File.join(directory, 'alpha.py'), "import scout\ndef alpha() -> str:\n return 'alpha'\nscout.task(alpha)\n")
|
|
838
|
+
File.write(File.join(directory, 'beta.py'), "import scout\ndef beta() -> str:\n return 'beta'\nscout.task(beta)\n")
|
|
839
|
+
workflow_name = "PythonRefreshFailureTest#{object_id}_#{rand(1_000_000)}"
|
|
840
|
+
workflow = PythonWorkflow.load_directory(Path.setup(directory), workflow_name)
|
|
841
|
+
begin
|
|
842
|
+
assert_equal %w[alpha beta], workflow_task_metadata(workflow).map { |m| m['name'] }.sort
|
|
843
|
+
|
|
844
|
+
# Introduce a cross-file name collision: gamma redefines alpha. The
|
|
845
|
+
# refresh must fail before mutating anything.
|
|
846
|
+
File.write(File.join(directory, 'gamma.py'), "import scout\ndef alpha(x: int = 1) -> str:\n return str(x)\nscout.task(alpha)\n")
|
|
847
|
+
assert_raise(RuntimeError) { workflow.refresh_python_tasks! }
|
|
848
|
+
|
|
849
|
+
# Previously working registry remains usable.
|
|
850
|
+
assert_equal 'alpha', workflow.job(:alpha).run
|
|
851
|
+
assert_equal 'beta', workflow.job(:beta).run
|
|
852
|
+
|
|
853
|
+
# A subsequent refresh without the invalid file behaves coherently:
|
|
854
|
+
# no stale-tracking duplicates or ghosts.
|
|
855
|
+
File.delete(File.join(directory, 'gamma.py'))
|
|
856
|
+
File.write(File.join(directory, 'alpha.py'), "import scout\ndef alpha(extra: int = 1) -> str:\n return 'alpha'\nscout.task(alpha)\n")
|
|
857
|
+
summary = workflow.refresh_python_tasks!
|
|
858
|
+
assert_equal %w[alpha], summary[:changed]
|
|
859
|
+
assert_equal %w[beta], summary[:unchanged]
|
|
860
|
+
assert_equal %w[alpha beta], workflow.tasks.keys.map(&:to_s).sort
|
|
861
|
+
names = workflow_task_metadata(workflow).map { |m| m['name'] }
|
|
862
|
+
assert_equal names.uniq, names
|
|
863
|
+
ensure
|
|
864
|
+
Object.send(:remove_const, workflow_name) if workflow_name && Object.const_defined?(workflow_name, false)
|
|
865
|
+
end
|
|
866
|
+
end
|
|
867
|
+
end
|
|
868
|
+
|
|
869
|
+
def test_body_only_edit_reports_unchanged_and_keeps_cached_result
|
|
870
|
+
source = "import scout\ndef probe() -> str:\n return 'one'\nscout.task(probe)\n"
|
|
871
|
+
with_runtime_task(source) do |_directory, workflow, file|
|
|
872
|
+
job = workflow.job(:probe, 'case-a')
|
|
873
|
+
assert_equal 'one', job.run
|
|
874
|
+
|
|
875
|
+
# Body-only edit: metadata (params/annotations/docstring shape) is
|
|
876
|
+
# unchanged, only the function body differs.
|
|
877
|
+
File.write(file, "import scout\ndef probe() -> str:\n return 'two'\nscout.task(probe)\n")
|
|
878
|
+
|
|
879
|
+
task_before = workflow.tasks[:probe]
|
|
880
|
+
summary = workflow.refresh_python_tasks!
|
|
881
|
+
assert_equal({ added: [], changed: [], removed: [], unchanged: %w[probe] }, summary)
|
|
882
|
+
assert workflow.tasks[:probe].equal?(task_before), 'body-only edit must not redefine the task'
|
|
883
|
+
|
|
884
|
+
# Definition refresh and result invalidation are DISTINCT operations
|
|
885
|
+
# and there are two independent replay layers:
|
|
886
|
+
# (i) the in-process Workflow.job_cache keyed on
|
|
887
|
+
# workflow/task/id/provided-inputs only, and (ii) the on-disk Step
|
|
888
|
+
# job directory, which run honors when a job is done?. Neither key
|
|
889
|
+
# includes the Python source body. Both layers replay the OLD
|
|
890
|
+
# value after a body-only edit.
|
|
891
|
+
assert_equal 'one', workflow.job(:probe, 'case-a').run
|
|
892
|
+
|
|
893
|
+
Workflow.job_cache.clear
|
|
894
|
+
assert_equal 'one', workflow.job(:probe, 'case-a').run, 'on-disk job replays the old value without the cache'
|
|
895
|
+
|
|
896
|
+
Workflow.job_cache.clear
|
|
897
|
+
step = workflow.job(:probe, 'case-a')
|
|
898
|
+
FileUtils.rm_rf(step.path)
|
|
899
|
+
FileUtils.rm_rf("#{step.path}.info")
|
|
900
|
+
assert_equal 'two', workflow.job(:probe, 'case-a').run, 'removing both replay layers re-executes the edited body'
|
|
901
|
+
end
|
|
902
|
+
end
|
|
903
|
+
|
|
904
|
+
# A compact view of the registration
|
|
905
|
+
# tracking record, so assertions can check registry AND tracking coherence.
|
|
906
|
+
def python_tracking_snapshot(workflow)
|
|
907
|
+
workflow.instance_variable_get(:@python_task_registrations).to_a.map do |file, registration|
|
|
908
|
+
[File.basename(file), registration['metas'].map { |m| m['name'] }.sort]
|
|
909
|
+
end.sort
|
|
910
|
+
end
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
# One .py file exporting several tasks. register_python_task_metadata
|
|
914
|
+
# stores one record per FILE, but refresh re-registers per TASK with a
|
|
915
|
+
# single-element metadata array, so the file's tracking entry is
|
|
916
|
+
# overwritten to hold only the last applied task. The untracked tasks then
|
|
917
|
+
# look like ghost :added entries, trip the native-conflict check against
|
|
918
|
+
# their own names, and disappear from python_input_contracts.
|
|
919
|
+
def test_refresh_multi_task_file_keeps_registry_and_tracking_in_step
|
|
920
|
+
multi = "import scout\ndef build(out: str = None) -> str:\n return 'build'\nscout.task(build)\ndef edit(src: str = None) -> str:\n return 'edit'\nscout.task(edit)\ndef inspect(result: str = None) -> str:\n return 'inspect'\nscout.task(inspect)\n"
|
|
921
|
+
Dir.mktmpdir('python-multi-task') do |directory|
|
|
922
|
+
file = File.join(directory, 'pipeline.py')
|
|
923
|
+
File.write(file, multi)
|
|
924
|
+
workflow_name = "PythonMultiTaskFileTest#{object_id}_#{rand(1_000_000)}"
|
|
925
|
+
workflow = PythonWorkflow.load_directory(Path.setup(directory), workflow_name)
|
|
926
|
+
begin
|
|
927
|
+
assert_equal %w[build edit inspect], workflow.tasks.keys.map(&:to_s).sort
|
|
928
|
+
assert_equal [['pipeline.py', %w[build edit inspect]]], python_tracking_snapshot(workflow)
|
|
929
|
+
assert_equal %w[build edit inspect], workflow.python_input_contracts.keys.sort
|
|
930
|
+
|
|
931
|
+
# Change ONE task's signature: refresh must re-register only build
|
|
932
|
+
# and keep edit/inspect tracked by the same file record.
|
|
933
|
+
File.write(file, multi.sub('def build(out: str = None)', 'def build(out: str = None, extra: int = 1)'))
|
|
934
|
+
summary = workflow.refresh_python_tasks!
|
|
935
|
+
assert_equal %w[build], summary[:changed]
|
|
936
|
+
assert_equal %w[edit inspect], summary[:unchanged].sort
|
|
937
|
+
assert_equal %w[build edit inspect], workflow.tasks.keys.map(&:to_s).sort
|
|
938
|
+
assert_equal [['pipeline.py', %w[build edit inspect]]], python_tracking_snapshot(workflow), 'file record must still track every task it exports'
|
|
939
|
+
assert_equal %w[build edit inspect], workflow.python_input_contracts.keys.sort, 'every exported task must stay reportable'
|
|
940
|
+
|
|
941
|
+
# Second refresh with unchanged sources is a pure no-op.
|
|
942
|
+
second = workflow.refresh_python_tasks!
|
|
943
|
+
assert_equal({ added: [], changed: [], removed: [], unchanged: %w[build edit inspect] }, second)
|
|
944
|
+
assert_equal %w[build edit inspect], workflow.python_input_contracts.keys.sort
|
|
945
|
+
|
|
946
|
+
# Removing ONE exported task prunes registry and tracking together.
|
|
947
|
+
File.write(file, multi.sub('def build(out: str = None)', 'def build(out: str = None, extra: int = 1)').sub("scout.task(edit)\n", ''))
|
|
948
|
+
third = workflow.refresh_python_tasks!
|
|
949
|
+
assert_equal %w[edit], third[:removed]
|
|
950
|
+
assert_equal %w[build inspect], workflow.tasks.keys.map(&:to_s).sort
|
|
951
|
+
assert_equal [['pipeline.py', %w[build inspect]]], python_tracking_snapshot(workflow)
|
|
952
|
+
assert_equal %w[build inspect], workflow.python_input_contracts.keys.sort
|
|
953
|
+
|
|
954
|
+
fourth = workflow.refresh_python_tasks!
|
|
955
|
+
assert_equal({ added: [], changed: [], removed: [], unchanged: %w[build inspect] }, fourth)
|
|
956
|
+
ensure
|
|
957
|
+
Object.send(:remove_const, workflow_name) if workflow_name && Object.const_defined?(workflow_name, false)
|
|
958
|
+
end
|
|
959
|
+
end
|
|
960
|
+
end
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
# A removed task is never pruned from @python_task_registrations, so
|
|
964
|
+
# every later refresh re-reports it as removed. The second refresh over
|
|
965
|
+
# unchanged sources must be a pure no-op.
|
|
966
|
+
def test_refresh_removed_task_is_not_rereported
|
|
967
|
+
with_runtime_task("import scout\ndef alpha_task() -> str:\n return 'alpha'\nscout.task(alpha_task)\n") do |_directory, workflow, file|
|
|
968
|
+
File.delete(file)
|
|
969
|
+
assert_equal({ added: [], changed: [], removed: %w[alpha_task], unchanged: [] }, workflow.refresh_python_tasks!)
|
|
970
|
+
assert_false workflow.tasks.key?(:alpha_task)
|
|
971
|
+
|
|
972
|
+
# Idempotency: the same sources refreshed again must change nothing.
|
|
973
|
+
assert_equal({ added: [], changed: [], removed: [], unchanged: [] }, workflow.refresh_python_tasks!)
|
|
974
|
+
end
|
|
975
|
+
end
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
# eval_str=True stays the resolution mechanism.
|
|
979
|
+
# With `from __future__ import annotations`, str/Path/list[Path]/Path|None/
|
|
980
|
+
# Annotated[Path, ...] resolve exactly like plain annotations, while a
|
|
981
|
+
# quoted forward reference that cannot be evaluated and an unresolvable
|
|
982
|
+
# name are rejected loudly (never a silent :string mapping).
|
|
983
|
+
def test_postponed_annotations_resolve_to_scout_types
|
|
984
|
+
source = "from __future__ import annotations\nimport scout\nfrom pathlib import Path\nfrom typing import Annotated\ndef probe(a: str, b: Path = None, c: list[Path] = None, d: Path | None = None, e: Annotated[Path, 'extra'] = None) -> str:\n return 'ok'\nscout.task(probe)\n"
|
|
985
|
+
with_runtime_task(source) do |_directory, workflow, _file|
|
|
986
|
+
contract = workflow.python_input_contracts['probe']
|
|
987
|
+
assert_false contract.key?('stale')
|
|
988
|
+
types = contract['inputs'].to_h { |i| [i['name'], i['scout_type']] }
|
|
989
|
+
assert_equal :string, types['a']
|
|
990
|
+
assert_equal :file, types['b']
|
|
991
|
+
assert_equal :file_array, types['c']
|
|
992
|
+
assert_equal :file, types['d']
|
|
993
|
+
assert_equal :file, types['e'], 'Annotated[Path, ...] must map through to :file'
|
|
994
|
+
assert_equal "typing.Annotated[pathlib._local.Path, 'extra']", contract['inputs'].find { |i| i['name'] == 'e' }['python_annotation'], 'the complete Annotated form is preserved in the annotation field'
|
|
995
|
+
end
|
|
996
|
+
end
|
|
997
|
+
|
|
998
|
+
# The postponed-annotation surface runs end to end, not just at
|
|
999
|
+
# registration. One task under `from __future__ import annotations` is
|
|
1000
|
+
# executed as a real Scout job and its output contract holds.
|
|
1001
|
+
def test_postponed_annotations_run_end_to_end
|
|
1002
|
+
source = "from __future__ import annotations\nimport scout\nfrom pathlib import Path\nfrom typing import Annotated\ndef probe(word: str, items: list[Path] = None, alt: Path | None = None, tagged: Annotated[Path, 'extra'] = None) -> str:\n return word + ':' + ','.join(e.name for e in (items or []))\nscout.task(probe)\n"
|
|
1003
|
+
with_runtime_task(source) do |directory, workflow, _file|
|
|
1004
|
+
contract = workflow.python_input_contracts['probe']
|
|
1005
|
+
assert_false contract.key?('stale')
|
|
1006
|
+
types = contract['inputs'].to_h { |i| [i['name'], i['scout_type']] }
|
|
1007
|
+
assert_equal :string, types['word']
|
|
1008
|
+
assert_equal :file_array, types['items']
|
|
1009
|
+
assert_equal :file, types['alt']
|
|
1010
|
+
assert_equal :file, types['tagged']
|
|
1011
|
+
assert_equal "typing.Annotated[pathlib._local.Path, 'extra']", contract['inputs'].find { |i| i['name'] == 'tagged' }['python_annotation']
|
|
1012
|
+
|
|
1013
|
+
a = File.join(directory, 'one.txt'); File.write(a, 'x')
|
|
1014
|
+
b = File.join(directory, 'two.txt'); File.write(b, 'y')
|
|
1015
|
+
assert_equal "hi:#{File.basename(a)},#{File.basename(b)}", workflow.job(:probe, 'word' => 'hi', 'items' => [a, b]).run
|
|
1016
|
+
end
|
|
1017
|
+
end
|
|
1018
|
+
|
|
1019
|
+
# Both unresolvable shapes — a quoted forward reference under
|
|
1020
|
+
# postponement and a genuinely undefined name — must fail loudly, and the
|
|
1021
|
+
# message must name the parameter and the annotation so the two cases are
|
|
1022
|
+
# distinguishable from a genuinely unsupported type.
|
|
1023
|
+
def test_postponed_unresolvable_annotations_fail_loudly_with_guidance
|
|
1024
|
+
# Quoted forward reference under postponement: eval_str cannot resolve
|
|
1025
|
+
# it, the annotation stays a string, and our error names the parameter,
|
|
1026
|
+
# the annotation, and the reason it stayed a string.
|
|
1027
|
+
quoted = "from __future__ import annotations\nimport scout\ndef probe(d: \"Path | None\" = None) -> str:\n return 'ok'\nscout.task(probe)\n"
|
|
1028
|
+
TmpFile.with_file quoted do |task_file|
|
|
1029
|
+
error = assert_raise(ProcessFailed) { PythonWorkflow.read_python_metadata(task_file) }
|
|
1030
|
+
assert_match(/Unsupported parameter annotation for 'd'/, error.message)
|
|
1031
|
+
assert_match(/'Path \| None'/, error.message)
|
|
1032
|
+
assert_match(/stayed the string/, error.message)
|
|
1033
|
+
end
|
|
1034
|
+
|
|
1035
|
+
# Genuinely undefined name (with or without postponement): the module
|
|
1036
|
+
# cannot be imported at all — still loud, never a silent :string.
|
|
1037
|
+
undefined = "import scout\ndef probe(f: SomeUndefinedClass = None) -> str:\n return 'ok'\nscout.task(probe)\n"
|
|
1038
|
+
[undefined, undefined.sub('import scout', "from __future__ import annotations\nimport scout")].each do |source|
|
|
1039
|
+
TmpFile.with_file source do |task_file|
|
|
1040
|
+
error = assert_raise(ProcessFailed) { PythonWorkflow.read_python_metadata(task_file) }
|
|
1041
|
+
assert_match(/NameError: name 'SomeUndefinedClass' is not defined/, error.message)
|
|
1042
|
+
end
|
|
1043
|
+
end
|
|
1044
|
+
end
|
|
1045
|
+
|
|
1046
|
+
private
|
|
1047
|
+
|
|
1048
|
+
def with_extension_task_directory
|
|
1049
|
+
Dir.mktmpdir('python-workflow-extension-test') do |directory|
|
|
1050
|
+
File.write(File.join(directory, 'custom.py'), <<~PYTHON)
|
|
1051
|
+
import scout
|
|
1052
|
+
|
|
1053
|
+
def custom() -> str:
|
|
1054
|
+
"""Custom extension.
|
|
1055
|
+
|
|
1056
|
+
Extension: json
|
|
1057
|
+
"""
|
|
1058
|
+
return 'custom'
|
|
1059
|
+
|
|
1060
|
+
scout.task(custom)
|
|
1061
|
+
PYTHON
|
|
1062
|
+
File.write(File.join(directory, 'fallback.py'), <<~PYTHON)
|
|
1063
|
+
import scout
|
|
1064
|
+
|
|
1065
|
+
def fallback() -> str:
|
|
1066
|
+
"""No custom extension."""
|
|
1067
|
+
return 'fallback'
|
|
1068
|
+
|
|
1069
|
+
scout.task(fallback)
|
|
1070
|
+
PYTHON
|
|
1071
|
+
yield directory
|
|
1072
|
+
end
|
|
1073
|
+
end
|
|
1074
|
+
|
|
1075
|
+
def with_runtime_task(source)
|
|
1076
|
+
Dir.mktmpdir('python-task-runtime') do |directory|
|
|
1077
|
+
file = File.join(directory, 'runtime_task.py')
|
|
1078
|
+
File.write(file, source)
|
|
1079
|
+
workflow_name = "PythonTaskRuntimeTest#{object_id}_#{rand(1_000_000)}"
|
|
1080
|
+
workflow = PythonWorkflow.load_directory(Path.setup(directory), workflow_name)
|
|
1081
|
+
yield directory, workflow, file
|
|
1082
|
+
ensure
|
|
1083
|
+
Object.send(:remove_const, workflow_name) if workflow_name && Object.const_defined?(workflow_name, false)
|
|
1084
|
+
end
|
|
1085
|
+
end
|
|
1086
|
+
|
|
1087
|
+
def with_python_task_directory(conditional_main: false, shared_import: false)
|
|
1088
|
+
Dir.mktmpdir('python-workflow-test') do |dir|
|
|
1089
|
+
if shared_import
|
|
1090
|
+
File.write(File.join(dir, 'shared.py'), "import scout\ndef shared_task() -> str:\n return 'shared'\nscout.task(shared_task)\n")
|
|
1091
|
+
alpha_registration = "import shared\nscout.task(alpha)"
|
|
1092
|
+
beta_registration = "import shared\nscout.task(beta)"
|
|
1093
|
+
elsif conditional_main
|
|
1094
|
+
alpha_registration = "if __name__ == '__main__':\n scout.task(alpha)"
|
|
1095
|
+
beta_registration = "if __name__ == '__main__':\n scout.task(beta)"
|
|
1096
|
+
else
|
|
1097
|
+
alpha_registration = 'scout.task(alpha)'
|
|
1098
|
+
beta_registration = 'scout.task(beta)'
|
|
1099
|
+
end
|
|
1100
|
+
File.write(File.join(dir, 'alpha.py'), <<~PYTHON)
|
|
1101
|
+
import scout
|
|
1102
|
+
def alpha(value: str, excited: bool = False) -> str:
|
|
1103
|
+
"""Greet the user.
|
|
1104
|
+
|
|
1105
|
+
Args:
|
|
1106
|
+
value: The text to return unchanged.
|
|
1107
|
+
"""
|
|
1108
|
+
return value
|
|
1109
|
+
#{alpha_registration}
|
|
1110
|
+
PYTHON
|
|
1111
|
+
File.write(File.join(dir, 'beta.py'), <<~PYTHON)
|
|
1112
|
+
import scout
|
|
1113
|
+
def beta(value: int) -> int:
|
|
1114
|
+
"""Return the numeric value."""
|
|
1115
|
+
return value
|
|
1116
|
+
#{beta_registration}
|
|
1117
|
+
PYTHON
|
|
1118
|
+
yield Path.setup(dir), [File.join(dir, 'alpha.py'), File.join(dir, 'beta.py')]
|
|
1119
|
+
end
|
|
1120
|
+
end
|
|
1121
|
+
|
|
1122
|
+
def capture_python_runs
|
|
1123
|
+
original = ScoutPython.method(:run_file)
|
|
1124
|
+
calls = []
|
|
1125
|
+
ScoutPython.define_singleton_method(:run_file) do |file, args, **options|
|
|
1126
|
+
calls << [file, args]
|
|
1127
|
+
original.call(file, args, **options)
|
|
1128
|
+
end
|
|
1129
|
+
yield
|
|
1130
|
+
calls
|
|
1131
|
+
ensure
|
|
1132
|
+
ScoutPython.define_singleton_method(:run_file, original) if original
|
|
1133
|
+
end
|
|
1134
|
+
|
|
1135
|
+
def workflow_task_metadata(workflow)
|
|
1136
|
+
workflow.tasks.values.map do |task|
|
|
1137
|
+
{
|
|
1138
|
+
'name' => task.name.to_s,
|
|
1139
|
+
'type' => task.type.to_s,
|
|
1140
|
+
'description' => task.description,
|
|
1141
|
+
'returns' => task.returns,
|
|
1142
|
+
'extension' => task.extension,
|
|
1143
|
+
'inputs' => task.inputs.map do |name, type, description, default, options|
|
|
1144
|
+
{
|
|
1145
|
+
'name' => name.to_s,
|
|
1146
|
+
'type' => type.to_s,
|
|
1147
|
+
'description' => description,
|
|
1148
|
+
'default' => default,
|
|
1149
|
+
'options' => (options || {}).transform_keys(&:to_s)
|
|
1150
|
+
}
|
|
1151
|
+
end
|
|
1152
|
+
}
|
|
1153
|
+
end.sort_by { |metadata| metadata['name'] }
|
|
1154
|
+
end
|
|
1155
|
+
end
|