scout-rig 0.2.5 → 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/python/util.rb +9 -0
- data/lib/scout/workflow/python/inputs.rb +33 -23
- data/lib/scout/workflow/python/task.rb +219 -18
- 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
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
# Focused behavior tests for ScoutRig::Testing::Availability (step 3 of the
|
|
2
|
+
# testing-availability plan; see research/testing-availability-baseline.md).
|
|
3
|
+
#
|
|
4
|
+
# The suite runs on FAKE interpreters alone, so it is green on a machine with
|
|
5
|
+
# no python and no R at all. Only the handful of tests that genuinely need a
|
|
6
|
+
# real interpreter (a real stdlib import, a real R runtime) are omit-gated.
|
|
7
|
+
|
|
8
|
+
require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
|
|
9
|
+
require 'scout-rig/testing/availability'
|
|
10
|
+
|
|
11
|
+
require 'fileutils'
|
|
12
|
+
require 'tmpdir'
|
|
13
|
+
require 'open3'
|
|
14
|
+
|
|
15
|
+
class TestScoutRigTestingAvailability < Test::Unit::TestCase
|
|
16
|
+
AVAIL = ScoutRig::Testing::Availability
|
|
17
|
+
|
|
18
|
+
ENV_KEYS = %w[PATH PYTHON SCOUT_TEST_PYTHON SCOUT_TEST_RSCRIPT PYTHONHOME PYTHONPATH].freeze
|
|
19
|
+
|
|
20
|
+
def setup
|
|
21
|
+
@env_snapshot = ENV_KEYS.each_with_object({}) { |k, h| h[k] = ENV[k] }
|
|
22
|
+
AVAIL.reset!
|
|
23
|
+
# NOTE: scratch lives under Dir.tmpdir, NOT Test::Unit::TestCase.tmpdir:
|
|
24
|
+
# the base test_helper setup/teardown callbacks `Open.rm_rf` that tree,
|
|
25
|
+
# which would delete the fakes mid-test (observed as Errno::ENOENT).
|
|
26
|
+
@scratch = File.join(Dir.tmpdir, "scout-rig-availability-#{$$}-#{rand(1e9).to_i}")
|
|
27
|
+
FileUtils.mkdir_p(@scratch)
|
|
28
|
+
@fakes = []
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def teardown
|
|
32
|
+
ENV_KEYS.each { |k| v = @env_snapshot[k]; v.nil? ? ENV.delete(k) : ENV[k] = v }
|
|
33
|
+
AVAIL.reset!
|
|
34
|
+
FileUtils.rm_rf(@scratch) if File.directory?(@scratch)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# --- fake interpreter helpers ----------------------------------------
|
|
38
|
+
|
|
39
|
+
# Writes a #!/bin/sh fake executable named `name` into the scratch dir and
|
|
40
|
+
# returns its absolute path.
|
|
41
|
+
def fake_executable(name, body)
|
|
42
|
+
path = File.join(@scratch, name)
|
|
43
|
+
File.write(path, "#!/bin/sh\n#{body}\n")
|
|
44
|
+
File.chmod(0o755, path)
|
|
45
|
+
@fakes << path
|
|
46
|
+
path
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def counter_value(counter)
|
|
50
|
+
File.exist?(counter) ? File.read(counter).to_i : 0
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# A fake python that echoes every probe argument after '-c <source>' to
|
|
54
|
+
# stderr and exits 1: it lets each test observe exactly what the probe
|
|
55
|
+
# passed as module names.
|
|
56
|
+
ARG_ECHO_BODY = 'shift 2; for a in "$@"; do printf "%s\n" "$a" >&2; done; exit 1'.freeze
|
|
57
|
+
|
|
58
|
+
# --- Python: runnable present / missing / non-runnable -----------------
|
|
59
|
+
|
|
60
|
+
def test_python_runnable_present_when_fake_exits_zero
|
|
61
|
+
exe = fake_executable('python_ok', 'exit 0')
|
|
62
|
+
assert AVAIL.python?(executable: exe)
|
|
63
|
+
assert_nil AVAIL.python_modules_reason(executable: exe)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_python_runnable_output_is_ignored_when_exit_zero
|
|
67
|
+
# Junk on stdout (and stderr) must not turn a success into a failure:
|
|
68
|
+
# the helpers decide on exit status only.
|
|
69
|
+
exe = fake_executable('python_noisy_ok', 'echo "junk on stdout"; echo "junk on stderr" >&2; exit 0')
|
|
70
|
+
assert AVAIL.python?(executable: exe)
|
|
71
|
+
assert_nil AVAIL.python_modules_reason(executable: exe)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def test_python_non_runnable_when_fake_exits_one_even_silent
|
|
75
|
+
# The exit-1 fake may print nothing: still false.
|
|
76
|
+
exe = fake_executable('python_silent_fail', 'exit 1')
|
|
77
|
+
refute AVAIL.python?(executable: exe)
|
|
78
|
+
reason = AVAIL.python_modules_reason('sys', executable: exe)
|
|
79
|
+
assert_not_nil reason, 'a module probe against a non-runnable interpreter must explain itself'
|
|
80
|
+
assert_includes reason, exe
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def test_python_stderr_marker_lands_in_reason
|
|
84
|
+
exe = fake_executable('python_fail_marker', 'echo "deliberate failure marker" >&2; exit 1')
|
|
85
|
+
refute AVAIL.python?(executable: exe)
|
|
86
|
+
reason = AVAIL.python_modules_reason('sys', executable: exe)
|
|
87
|
+
assert_includes reason, 'deliberate failure marker'
|
|
88
|
+
assert_includes reason, exe
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def test_python_missing_executable_returns_false_never_raises
|
|
92
|
+
exe = File.join(@scratch, 'python_does_not_exist_zz')
|
|
93
|
+
refute File.exist?(exe)
|
|
94
|
+
refute AVAIL.python?(executable: exe)
|
|
95
|
+
reason = AVAIL.python_modules_reason('sys', executable: exe)
|
|
96
|
+
assert_not_nil reason
|
|
97
|
+
assert_match(/could not start|No such file|ENOENT/i, reason)
|
|
98
|
+
assert_includes reason, exe
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# --- Python modules -----------------------------------------------------
|
|
102
|
+
|
|
103
|
+
def test_python_modules_missing_module_reason_names_module_and_executable
|
|
104
|
+
exe = fake_executable('python_import_fail', 'echo "definitely_not_a_module_xyz: No module named" >&2; exit 1')
|
|
105
|
+
refute AVAIL.python_modules?('definitely_not_a_module_xyz', executable: exe)
|
|
106
|
+
reason = AVAIL.python_modules_reason('definitely_not_a_module_xyz', executable: exe)
|
|
107
|
+
assert_includes reason, 'definitely_not_a_module_xyz'
|
|
108
|
+
assert_includes reason, exe
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def test_python_modules_real_stdlib_gated
|
|
112
|
+
omit 'no python interpreter available' unless AVAIL.python?
|
|
113
|
+
assert AVAIL.python_modules?('sys', 'os')
|
|
114
|
+
assert_nil AVAIL.python_modules_reason('sys', 'os')
|
|
115
|
+
refute AVAIL.python_modules?('definitely_not_a_module_xyz')
|
|
116
|
+
reason = AVAIL.python_modules_reason('definitely_not_a_module_xyz')
|
|
117
|
+
assert_includes reason, 'definitely_not_a_module_xyz'
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# --- injection safety ---------------------------------------------------
|
|
121
|
+
|
|
122
|
+
def test_python_modules_multi_line_name_travels_as_one_name
|
|
123
|
+
# The whole string (embedded newline and all) must travel as ONE argv
|
|
124
|
+
# element: nothing on the second line may execute.
|
|
125
|
+
exe = fake_executable('python_arg_echo', ARG_ECHO_BODY)
|
|
126
|
+
refute AVAIL.python_modules?("sys\nimport os", executable: exe)
|
|
127
|
+
reason = AVAIL.python_modules_reason("sys\nimport os", executable: exe)
|
|
128
|
+
# The reason must report the WHOLE string as one missing module name, not
|
|
129
|
+
# only "sys" as if the newline had split or re-parsed it.
|
|
130
|
+
assert_includes reason, "sys\nimport os"
|
|
131
|
+
assert_includes reason, exe
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def test_python_modules_quote_and_flag_like_names_are_inert
|
|
135
|
+
exe = fake_executable('python_arg_echo2', ARG_ECHO_BODY)
|
|
136
|
+
["'; import os", '-c', '--version'].each do |weird|
|
|
137
|
+
refute AVAIL.python_modules?(weird, executable: exe), weird.inspect
|
|
138
|
+
reason = AVAIL.python_modules_reason(weird, executable: exe)
|
|
139
|
+
assert_includes reason, weird, weird.inspect
|
|
140
|
+
assert_includes reason, exe, weird.inspect
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# --- R runtime ----------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
def test_rscript_runnable_present_when_fake_exits_zero
|
|
147
|
+
exe = fake_executable('Rscript_ok', 'exit 0')
|
|
148
|
+
assert AVAIL.rscript?(executable: exe)
|
|
149
|
+
assert_nil AVAIL.r_packages_reason('base', executable: exe)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def test_rscript_non_runnable_reports_failure_with_stderr
|
|
153
|
+
exe = fake_executable('Rscript_fail', 'echo "R deliberate failure marker" >&2; exit 1')
|
|
154
|
+
refute AVAIL.rscript?(executable: exe)
|
|
155
|
+
reason = AVAIL.r_packages_reason('base', executable: exe)
|
|
156
|
+
assert_not_nil reason
|
|
157
|
+
assert_includes reason, 'R deliberate failure marker'
|
|
158
|
+
assert_includes reason, exe
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def test_rscript_missing_executable_returns_false_never_raises
|
|
162
|
+
exe = File.join(@scratch, 'Rscript_does_not_exist_zz')
|
|
163
|
+
refute File.exist?(exe)
|
|
164
|
+
refute AVAIL.rscript?(executable: exe)
|
|
165
|
+
reason = AVAIL.r_packages_reason('base', executable: exe)
|
|
166
|
+
assert_not_nil reason
|
|
167
|
+
assert_match(/could not start|No such file|ENOENT/i, reason)
|
|
168
|
+
assert_includes reason, exe
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# --- R packages: loadability not list presence --------------------------
|
|
172
|
+
|
|
173
|
+
def test_r_package_missing_reports_missing_name
|
|
174
|
+
# Fake Rscript that plays the probe's contract: prints the missing
|
|
175
|
+
# package name to stdout, exit 1.
|
|
176
|
+
exe = fake_executable('Rscript_missing_pkg', 'echo "nosuchpkgxyz123"; exit 1')
|
|
177
|
+
refute AVAIL.r_packages?('nosuchpkgxyz123', executable: exe)
|
|
178
|
+
reason = AVAIL.r_packages_reason('nosuchpkgxyz123', executable: exe)
|
|
179
|
+
assert_includes reason, 'nosuchpkgxyz123'
|
|
180
|
+
assert_includes reason, exe
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def test_r_package_installed_but_not_loadable_reports_unavailable
|
|
184
|
+
# "Installed but not loadable": the fake acknowledges the package exists
|
|
185
|
+
# (prints a list-presence line) but reports a load failure and exits 1.
|
|
186
|
+
# Availability must follow loadability, not list presence.
|
|
187
|
+
exe = fake_executable(
|
|
188
|
+
'Rscript_not_loadable',
|
|
189
|
+
'echo "error: package or namespace load failed" >&2; exit 1'
|
|
190
|
+
)
|
|
191
|
+
refute AVAIL.r_packages?('stats', executable: exe)
|
|
192
|
+
reason = AVAIL.r_packages_reason('stats', executable: exe)
|
|
193
|
+
assert_not_nil reason
|
|
194
|
+
assert_includes reason, 'error: package or namespace load failed'
|
|
195
|
+
assert_includes reason, exe
|
|
196
|
+
# Loadability, not list presence: the fake never printed a "missing
|
|
197
|
+
# packages" line to stdout, so the reason must not claim one.
|
|
198
|
+
refute_includes reason, 'missing R packages'
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def test_r_packages_real_runtime_gated
|
|
202
|
+
omit 'no Rscript available' unless AVAIL.rscript?
|
|
203
|
+
assert AVAIL.r_packages?('base')
|
|
204
|
+
assert_nil AVAIL.r_packages_reason('base')
|
|
205
|
+
refute AVAIL.r_packages?('nosuchpkgxyz123')
|
|
206
|
+
reason = AVAIL.r_packages_reason('nosuchpkgxyz123')
|
|
207
|
+
assert_includes reason, 'nosuchpkgxyz123'
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def test_r_injection_safety
|
|
211
|
+
# An R analog of the Python injection checks: the whole name (newline
|
|
212
|
+
# included) must be passed through as data, never re-parsed as R source.
|
|
213
|
+
exe = fake_executable('Rscript_arg_echo', ARG_ECHO_BODY)
|
|
214
|
+
weird = "a\nquit(status=0)"
|
|
215
|
+
refute AVAIL.r_packages?(weird, executable: exe)
|
|
216
|
+
reason = AVAIL.r_packages_reason(weird, executable: exe)
|
|
217
|
+
assert_includes reason, weird
|
|
218
|
+
assert_includes reason, exe
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# --- timeout ------------------------------------------------------------
|
|
222
|
+
|
|
223
|
+
def test_hanging_python_is_bounded_and_reports_timeout
|
|
224
|
+
exe = fake_executable('python_hang', 'sleep 30')
|
|
225
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
226
|
+
refute AVAIL.python?(executable: exe, timeout: 1)
|
|
227
|
+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
|
|
228
|
+
assert elapsed < 8, "probe took #{elapsed}s; the 1s timeout was not enforced"
|
|
229
|
+
|
|
230
|
+
reason = AVAIL.python_modules_reason('sys', executable: exe, timeout: 1)
|
|
231
|
+
assert_includes reason, 'timed out'
|
|
232
|
+
assert_includes reason, exe
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def test_timeout_is_not_memoized
|
|
236
|
+
counter = File.join(@scratch, 'hang_counter')
|
|
237
|
+
exe = fake_executable(
|
|
238
|
+
'python_hang_count',
|
|
239
|
+
"n=$(cat #{counter} 2>/dev/null || echo 0); echo $((n+1)) > #{counter}; sleep 30"
|
|
240
|
+
)
|
|
241
|
+
AVAIL.python_modules?('sys', executable: exe, timeout: 1)
|
|
242
|
+
AVAIL.python_modules?('sys', executable: exe, timeout: 1)
|
|
243
|
+
assert_equal 2, counter_value(counter), 'a timeout must not be memoized; both calls must run'
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# --- launch error -------------------------------------------------------
|
|
247
|
+
|
|
248
|
+
def test_launch_error_clean_handling_no_raise
|
|
249
|
+
exe = File.join(@scratch, 'missing_dir_zz', 'python')
|
|
250
|
+
refute AVAIL.python?(executable: exe)
|
|
251
|
+
refute AVAIL.python_modules?('sys', executable: exe)
|
|
252
|
+
reason = AVAIL.r_packages_reason('base', executable: exe)
|
|
253
|
+
assert_not_nil reason
|
|
254
|
+
assert_includes reason, exe
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# --- selection precedence ------------------------------------------------
|
|
258
|
+
|
|
259
|
+
def test_embedded_route_honors_env_python_and_agreement_yields_nil_divergence
|
|
260
|
+
fake = fake_executable('python_env', 'exit 0')
|
|
261
|
+
bin = File.join(@scratch, 'bin_env')
|
|
262
|
+
FileUtils.mkdir_p(bin)
|
|
263
|
+
%w[python python3].each { |n| File.symlink(fake, File.join(bin, n)) }
|
|
264
|
+
ENV['PYTHON'] = fake
|
|
265
|
+
ENV['PATH'] = "#{bin}:#{ENV['PATH']}"
|
|
266
|
+
AVAIL.reset!
|
|
267
|
+
|
|
268
|
+
assert_equal fake, AVAIL.embedded_python
|
|
269
|
+
assert_equal 'python', AVAIL.subprocess_python
|
|
270
|
+
assert AVAIL.python?(route: :embedded)
|
|
271
|
+
assert_nil AVAIL.python_routes_divergence_reason
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def test_scout_test_python_overrides_route_default_and_executable_overrides_all
|
|
275
|
+
default_fake = fake_executable('python_default', 'exit 0')
|
|
276
|
+
pin_counter = File.join(@scratch, 'pin_counter')
|
|
277
|
+
# Sanity probe (no module args) exits 0; a module probe fails like an
|
|
278
|
+
# empty interpreter, so the reason distinguishes WHICH fake ran.
|
|
279
|
+
pin_counter_fake = fake_executable(
|
|
280
|
+
'python_pin',
|
|
281
|
+
"n=$(cat #{pin_counter} 2>/dev/null || echo 0); echo $((n+1)) > #{pin_counter}; " \
|
|
282
|
+
'shift 2; if [ "$#" -eq 0 ]; then exit 0; else echo "import failed" >&2; exit 1; fi'
|
|
283
|
+
)
|
|
284
|
+
explicit_fake = selective_python('python_explicit', 'sys')
|
|
285
|
+
|
|
286
|
+
bin = File.join(@scratch, 'bin_pin')
|
|
287
|
+
FileUtils.mkdir_p(bin)
|
|
288
|
+
File.symlink(default_fake, File.join(bin, 'python'))
|
|
289
|
+
ENV['PATH'] = "#{bin}:#{ENV['PATH']}"
|
|
290
|
+
AVAIL.reset!
|
|
291
|
+
assert AVAIL.python?, 'route default (PATH python) should work first'
|
|
292
|
+
|
|
293
|
+
# SCOUT_TEST_PYTHON beats the route default: the pinned fake is the one
|
|
294
|
+
# that runs. The counting pin proves WHICH fake executed; the reason for a
|
|
295
|
+
# failing module probe must name the pin, never the route default.
|
|
296
|
+
ENV['SCOUT_TEST_PYTHON'] = pin_counter_fake
|
|
297
|
+
AVAIL.reset!
|
|
298
|
+
assert AVAIL.python?
|
|
299
|
+
reason = AVAIL.python_modules_reason('nosuch_zz')
|
|
300
|
+
assert_not_nil reason
|
|
301
|
+
assert_includes reason, pin_counter_fake
|
|
302
|
+
refute_includes reason, default_fake
|
|
303
|
+
assert_equal 2, counter_value(pin_counter), 'python? + module probe = exactly two pin runs'
|
|
304
|
+
|
|
305
|
+
# explicit executable: beats the pin
|
|
306
|
+
ENV['SCOUT_TEST_PYTHON'] = pin_counter_fake
|
|
307
|
+
assert AVAIL.python?(executable: explicit_fake)
|
|
308
|
+
reason = AVAIL.python_modules_reason('nosuch_zz', executable: explicit_fake)
|
|
309
|
+
assert_not_nil reason
|
|
310
|
+
assert_includes reason, explicit_fake
|
|
311
|
+
refute_includes reason, pin_counter_fake
|
|
312
|
+
assert_equal 2, counter_value(pin_counter), 'an explicit executable must bypass the pin entirely'
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def test_divergence_reason_non_nil_when_routes_disagree
|
|
316
|
+
env_fake = fake_executable('python_div_env', 'exit 0')
|
|
317
|
+
path_fake = fake_executable('python_div_path', 'exit 0')
|
|
318
|
+
bin = File.join(@scratch, 'bin_div')
|
|
319
|
+
FileUtils.mkdir_p(bin)
|
|
320
|
+
%w[python python3].each { |n| File.symlink(path_fake, File.join(bin, n)) }
|
|
321
|
+
ENV['PYTHON'] = env_fake
|
|
322
|
+
ENV['PATH'] = "#{bin}:#{ENV['PATH']}"
|
|
323
|
+
AVAIL.reset!
|
|
324
|
+
|
|
325
|
+
assert_equal env_fake, AVAIL.embedded_python
|
|
326
|
+
assert_equal 'python', AVAIL.subprocess_python
|
|
327
|
+
reason = AVAIL.python_routes_divergence_reason
|
|
328
|
+
assert_not_nil reason
|
|
329
|
+
assert_includes reason, 'embedded'
|
|
330
|
+
assert_includes reason, 'subprocess'
|
|
331
|
+
assert_match(/ENV\[.PYTHON.\]|PATH/, reason)
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def test_resolution_helpers_never_run_the_interpreter
|
|
335
|
+
# Pure resolution: a python fake that would fail hard if executed must
|
|
336
|
+
# not be executed by the resolution helpers.
|
|
337
|
+
ENV['PYTHON'] = nil
|
|
338
|
+
fake = fake_executable('python_resolution', 'exit 42')
|
|
339
|
+
bin = File.join(@scratch, 'bin_res')
|
|
340
|
+
FileUtils.mkdir_p(bin)
|
|
341
|
+
File.symlink(fake, File.join(bin, 'python3'))
|
|
342
|
+
ENV['PATH'] = bin.to_s
|
|
343
|
+
AVAIL.reset!
|
|
344
|
+
|
|
345
|
+
assert_equal 'python3', AVAIL.embedded_python
|
|
346
|
+
# No `python` (only python3) in this PATH, so the subprocess route
|
|
347
|
+
# resolves to nil - the route cannot run anything at all.
|
|
348
|
+
assert_nil AVAIL.subprocess_python
|
|
349
|
+
# And nothing was executed: the fake would have exited 42, which nothing
|
|
350
|
+
# records (no counter, no marker), and no probe was run.
|
|
351
|
+
assert !File.exist?(File.join(@scratch, 'resolution_ran_marker')), 'resolution helpers must not execute the interpreter'
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def test_subprocess_python_resolves_the_literal_python_when_present
|
|
355
|
+
fake = fake_executable('python_res_sub', 'exit 42')
|
|
356
|
+
bin = File.join(@scratch, 'bin_res2')
|
|
357
|
+
FileUtils.mkdir_p(bin)
|
|
358
|
+
File.symlink(fake, File.join(bin, 'python'))
|
|
359
|
+
ENV['PATH'] = "#{bin}:#{ENV['PATH']}"
|
|
360
|
+
AVAIL.reset!
|
|
361
|
+
|
|
362
|
+
assert_equal 'python', AVAIL.subprocess_python
|
|
363
|
+
assert !File.exist?(File.join(@scratch, 'resolution_ran_marker')), 'resolution helpers must not execute the interpreter'
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
# --- memoization ---------------------------------------------------------
|
|
367
|
+
|
|
368
|
+
def test_memoization_same_key_does_not_rerun
|
|
369
|
+
counter = File.join(@scratch, 'memo_counter')
|
|
370
|
+
exe = counting_python('python_count', counter)
|
|
371
|
+
assert AVAIL.python_modules?('sys', executable: exe)
|
|
372
|
+
assert_equal 1, counter_value(counter)
|
|
373
|
+
assert AVAIL.python_modules?('sys', executable: exe)
|
|
374
|
+
assert_equal 1, counter_value(counter), 'identical call must be served from memo'
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def test_reset_re_runs_the_probe
|
|
378
|
+
counter = File.join(@scratch, 'reset_counter')
|
|
379
|
+
exe = counting_python('python_count_reset', counter)
|
|
380
|
+
assert AVAIL.python_modules?('sys', executable: exe)
|
|
381
|
+
assert_equal 1, counter_value(counter)
|
|
382
|
+
AVAIL.reset!
|
|
383
|
+
assert AVAIL.python_modules?('sys', executable: exe)
|
|
384
|
+
assert_equal 2, counter_value(counter), 'reset! must clear memoization'
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
def test_memo_keys_are_sensitive_to_names_timeout_and_executable
|
|
388
|
+
counter = File.join(@scratch, 'keys_counter')
|
|
389
|
+
exe = counting_python('python_count_keys', counter)
|
|
390
|
+
exe2 = counting_python('python_count_keys2', counter)
|
|
391
|
+
|
|
392
|
+
AVAIL.python_modules?('sys', executable: exe)
|
|
393
|
+
assert_equal 1, counter_value(counter)
|
|
394
|
+
AVAIL.python_modules?('os', executable: exe) # different module list -> new key
|
|
395
|
+
assert_equal 2, counter_value(counter)
|
|
396
|
+
AVAIL.python_modules?('sys', executable: exe, timeout: 60) # different timeout (not DEFAULT_TIMEOUT=30) -> new key
|
|
397
|
+
assert_equal 3, counter_value(counter)
|
|
398
|
+
AVAIL.python_modules?('sys', executable: exe2) # different executable -> new key
|
|
399
|
+
assert_equal 4, counter_value(counter)
|
|
400
|
+
AVAIL.python_modules?('sys', executable: exe) # same key again -> memo
|
|
401
|
+
assert_equal 4, counter_value(counter)
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
# --- unknown route -------------------------------------------------------
|
|
405
|
+
|
|
406
|
+
def test_unknown_route_raises_argument_error
|
|
407
|
+
assert_raise(ArgumentError) { AVAIL.python?(route: :bogus) }
|
|
408
|
+
assert_raise(ArgumentError) { AVAIL.python_modules?('sys', route: :bogus) }
|
|
409
|
+
assert_raise(ArgumentError) { AVAIL.python_modules_reason('sys', route: :bogus) }
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
# --- mixin ----------------------------------------------------------------
|
|
413
|
+
|
|
414
|
+
def test_mixin_exposes_helpers_as_instance_methods
|
|
415
|
+
object = Object.new
|
|
416
|
+
object.extend(ScoutRig::Testing::Helpers)
|
|
417
|
+
exe = selective_python('python_mixin', 'sys|os')
|
|
418
|
+
assert object.python?(executable: exe)
|
|
419
|
+
assert_nil object.python_modules_reason('sys', executable: exe)
|
|
420
|
+
refute object.python_modules?('nosuch_zz', executable: exe)
|
|
421
|
+
reason = object.python_modules_reason('nosuch_zz', executable: exe)
|
|
422
|
+
assert_includes reason, exe
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# --- isolation: requiring the library must not load production code -------
|
|
426
|
+
|
|
427
|
+
def test_require_does_not_load_pycall_or_production_entry
|
|
428
|
+
probe = <<~RUBY
|
|
429
|
+
require 'scout-rig/testing/availability'
|
|
430
|
+
require 'json'
|
|
431
|
+
require 'json'
|
|
432
|
+
puts({
|
|
433
|
+
pycall: (defined?(PyCall) || 'nil').to_s,
|
|
434
|
+
scout_rig: (defined?(ScoutRig) || 'nil').to_s,
|
|
435
|
+
scout_python: (defined?(ScoutPython) || 'nil').to_s,
|
|
436
|
+
production: $LOADED_FEATURES.grep(%r{scout/python\\.rb$|scout-rig\\.rb$}).inspect
|
|
437
|
+
}.to_json)
|
|
438
|
+
RUBY
|
|
439
|
+
stdout, stderr, status = run_isolated_ruby_full(probe)
|
|
440
|
+
require 'json'
|
|
441
|
+
assert status.success?, "isolated probe failed: #{stderr}"
|
|
442
|
+
assert !stdout.strip.empty?, "isolated probe printed nothing: #{stderr}"
|
|
443
|
+
loaded = JSON.parse(stdout)
|
|
444
|
+
assert_equal 'nil', loaded['pycall'], 'PyCall must not load'
|
|
445
|
+
%w[scout_rig].each do |key|
|
|
446
|
+
next if loaded[key] == 'expression' # Ruby 3.5+ reports autoload-triggered modules differently
|
|
447
|
+
assert_equal 'constant', loaded[key], "#{key} must load"
|
|
448
|
+
end
|
|
449
|
+
assert_equal 'nil', loaded['scout_python'], 'ScoutPython must not load'
|
|
450
|
+
assert_equal '[]', loaded['production'], 'no production entry may be loaded'
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
private
|
|
454
|
+
|
|
455
|
+
# A fake python whose "stdlib" is exactly `ok` (a shell case pattern or
|
|
456
|
+
# alternation like 'sys|os'): the sanity probe (no module args) succeeds,
|
|
457
|
+
# any module outside the allowlist fails to import. This is the minimal
|
|
458
|
+
# faithful stand-in for a real interpreter.
|
|
459
|
+
def selective_python(name, ok)
|
|
460
|
+
fake_executable(
|
|
461
|
+
name,
|
|
462
|
+
'shift 2; for a in "$@"; do case "$a" in ' \
|
|
463
|
+
"#{ok}) ;; *) echo \"No module named '$a'\" >&2; exit 1;; esac; done; exit 0"
|
|
464
|
+
)
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
def counting_python(name, counter)
|
|
468
|
+
fake_executable(
|
|
469
|
+
name,
|
|
470
|
+
"n=$(cat #{counter} 2>/dev/null || echo 0); echo $((n+1)) > #{counter}; exit 0"
|
|
471
|
+
)
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
# Runs a snippet in a FRESH ruby process with the repo's lib on the path,
|
|
475
|
+
# so memoization and load state cannot leak from this process.
|
|
476
|
+
def run_isolated_ruby_full(code)
|
|
477
|
+
# repo/lib: test/scout-rig/testing/test_availability.rb is three levels
|
|
478
|
+
# below test/, and lib/ sits beside test/.
|
|
479
|
+
paths = [File.expand_path('../../../lib', __dir__)]
|
|
480
|
+
# scout-essentials lib, taken from where THIS process actually loaded
|
|
481
|
+
# scout/testing/availability from (the released gem predates the file, so
|
|
482
|
+
# the load path entry cannot be assumed).
|
|
483
|
+
loaded = $LOADED_FEATURES.grep(%r{scout/testing/availability\.rb$}).first
|
|
484
|
+
paths << File.dirname(File.dirname(File.dirname(loaded))) if loaded
|
|
485
|
+
# The child runs WITHOUT this file's -Itest flag and without the ambient
|
|
486
|
+
# RUBYOPT load paths, so only these two libs (plus default gems) can be
|
|
487
|
+
# required: that is what makes the no-production-load assertion honest.
|
|
488
|
+
args = paths.flat_map { |p| ['-I', p] }
|
|
489
|
+
Open3.capture3({ 'RUBYOPT' => nil }, RbConfig.ruby, *args, '-e', code)
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
def run_isolated_ruby(code)
|
|
493
|
+
run_isolated_ruby_full(code).first
|
|
494
|
+
end
|
|
495
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
|
|
2
|
+
|
|
3
|
+
class TestScoutRigTestSupport < Test::Unit::TestCase
|
|
4
|
+
def test_shared_assertion_and_state_bootstrap_are_active
|
|
5
|
+
assert_equal_path File.expand_path('.'), File.expand_path('tmp/..')
|
|
6
|
+
assert_respond_to Scout::Testing, :with_tmpdir
|
|
7
|
+
assert_respond_to Scout::Testing, :with_isolated_state
|
|
8
|
+
|
|
9
|
+
root = tmpdir.to_s
|
|
10
|
+
assert_equal_path File.join(root, 'tmpfiles'), TmpFile.tmpdir.to_s
|
|
11
|
+
assert_equal_path File.join(root, 'var', 'cache'), Persist.cache_dir.find.to_s
|
|
12
|
+
assert_equal_path File.join(root, 'persistence-locks'), Persist.lock_dir.to_s
|
|
13
|
+
assert_equal_path File.join(root, 'var', 'cache'), Open.remote_cache_dir.to_s
|
|
14
|
+
assert_equal_path File.join(root, 'sensible-write'), Open.sensible_write_dir.to_s
|
|
15
|
+
assert_equal_path File.join(root, 'sensible-write-locks'), Open.sensible_write_lock_dir.to_s
|
|
16
|
+
assert_equal_path File.join(root, 'var', 'jobs'), Workflow.directory.find.to_s
|
|
17
|
+
assert_equal_path File.join(root, 'entity_properties'), Entity.entity_property_cache.to_s
|
|
18
|
+
assert_equal_path File.join(root, 'batch'), SchedulerJob.batch_base_dir
|
|
19
|
+
assert_equal_path Persist.cache_dir.find.to_s, Open.remote_cache_dir.to_s
|
|
20
|
+
assert_equal 0, Log::ProgressBar.default_severity
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_binding_paths_restore_previous_class_accessors
|
|
24
|
+
owners = [[Persist, :cache_dir, 'var/cache'],
|
|
25
|
+
[Open, :remote_cache_dir, 'var/cache'],
|
|
26
|
+
[Entity, :entity_property_cache, 'entity_properties'],
|
|
27
|
+
[SchedulerJob, :batch_base_dir, 'batch']]
|
|
28
|
+
prior = owners.map do |owner, accessor, _path|
|
|
29
|
+
ivar = "@#{accessor}".to_sym
|
|
30
|
+
[owner, accessor, owner.instance_variable_defined?(ivar), owner.instance_variable_get(ivar)]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
state = Scout::Testing::State.new(File.join(tmpdir.to_s, 'extra-bound-state'),
|
|
34
|
+
bindings: owners.map { |owner, accessor, path| [owner, accessor, path] })
|
|
35
|
+
state.activate
|
|
36
|
+
begin
|
|
37
|
+
assert_equal_path File.join(tmpdir.to_s, 'extra-bound-state', 'entity_properties'), Entity.entity_property_cache.to_s
|
|
38
|
+
assert_equal_path File.join(tmpdir.to_s, 'extra-bound-state', 'batch'), SchedulerJob.batch_base_dir
|
|
39
|
+
assert_equal_path File.join(tmpdir.to_s, 'extra-bound-state', 'var', 'cache'), Persist.cache_dir.find.to_s
|
|
40
|
+
assert_equal_path File.join(tmpdir.to_s, 'extra-bound-state', 'var', 'cache'), Open.remote_cache_dir.to_s
|
|
41
|
+
assert File.directory?(Entity.entity_property_cache.to_s)
|
|
42
|
+
assert File.directory?(SchedulerJob.batch_base_dir)
|
|
43
|
+
ensure
|
|
44
|
+
state.restore
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
prior.each do |owner, accessor, was_defined, value|
|
|
48
|
+
ivar = "@#{accessor}".to_sym
|
|
49
|
+
assert_equal was_defined, owner.instance_variable_defined?(ivar)
|
|
50
|
+
assert_equal value, owner.instance_variable_get(ivar) if was_defined
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
class TestScoutRigScopedStateRestore < Test::Unit::TestCase
|
|
56
|
+
def test_shared_state_restores_globals_and_memory_cache_even_after_error
|
|
57
|
+
globals_before = state_globals
|
|
58
|
+
Persist::MEMORY_CACHE[:scout_rig_test_support_sentinel] = :preserve
|
|
59
|
+
prior = Persist::MEMORY_CACHE.dup
|
|
60
|
+
root = File.join(tmpdir.to_s, 'nested-isolation')
|
|
61
|
+
|
|
62
|
+
assert_raise(RuntimeError) do
|
|
63
|
+
Scout::Testing.with_isolated_state(root) do
|
|
64
|
+
assert_empty Persist::MEMORY_CACHE
|
|
65
|
+
Persist::MEMORY_CACHE[:temporary] = :discard
|
|
66
|
+
assert_equal_path File.join(root, 'tmpfiles'), TmpFile.tmpdir
|
|
67
|
+
raise 'expected test exception'
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
assert_equal prior, Persist::MEMORY_CACHE
|
|
72
|
+
assert_equal globals_before, state_globals
|
|
73
|
+
ensure
|
|
74
|
+
Persist::MEMORY_CACHE.delete(:scout_rig_test_support_sentinel)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
def state_globals
|
|
80
|
+
[[TmpFile, :@tmpdir], [Persist, :@cache_dir], [Persist, :@lock_dir],
|
|
81
|
+
[Open, :@remote_cache_dir], [Open, :@sensible_write_dir],
|
|
82
|
+
[Open, :@sensible_write_lock_dir]].to_h do |owner, ivar|
|
|
83
|
+
[[owner.name, ivar], [owner.instance_variable_defined?(ivar), owner.instance_variable_get(ivar)]]
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
class TestScoutRigLocalStateRestore < Test::Unit::TestCase
|
|
89
|
+
def test_scalar_derived_and_cache_state_are_restored_without_clearing_old_cache
|
|
90
|
+
workflow = Module.new
|
|
91
|
+
workflow.extend Workflow
|
|
92
|
+
workflow.define_singleton_method(:name) { 'scout-rig-state-test' }
|
|
93
|
+
task = Class.new { attr_accessor :directory }.new
|
|
94
|
+
task.directory = 'original-task-directory'
|
|
95
|
+
workflow.instance_variable_set(:@tasks, { probe: task })
|
|
96
|
+
|
|
97
|
+
workflow_state = [workflow.instance_variable_defined?(:@directory), workflow.instance_variable_get(:@directory)]
|
|
98
|
+
task_state = [task.instance_variable_defined?(:@directory), task.instance_variable_get(:@directory)]
|
|
99
|
+
cache_state = [Workflow.instance_variable_defined?(:@job_cache), Workflow.instance_variable_get(:@job_cache)]
|
|
100
|
+
severity_state = [Log::ProgressBar.instance_variable_defined?(:@default_severity), Log::ProgressBar.instance_variable_get(:@default_severity)]
|
|
101
|
+
entity_cache_state = [Entity.instance_variable_defined?(:@entity_property_cache), Entity.instance_variable_get(:@entity_property_cache)]
|
|
102
|
+
old_cache = { preserved: :entry }
|
|
103
|
+
|
|
104
|
+
begin
|
|
105
|
+
Workflow.workflows << workflow
|
|
106
|
+
Workflow.directory = Path.setup(File.join(tmpdir.to_s, 'rig-jobs'))
|
|
107
|
+
Workflow.job_cache = old_cache
|
|
108
|
+
Log::ProgressBar.default_severity = 17
|
|
109
|
+
Entity.entity_property_cache = Path.setup(File.join(tmpdir.to_s, 'rig-entity-cache'))
|
|
110
|
+
@scout_rig_test_state = capture_rig_test_state
|
|
111
|
+
|
|
112
|
+
Workflow.directory = tmpdir.var.jobs
|
|
113
|
+
workflow.directory = Workflow.directory[workflow.name]
|
|
114
|
+
Workflow.job_cache = {}
|
|
115
|
+
Log::ProgressBar.default_severity = 0
|
|
116
|
+
Entity.entity_property_cache = tmpdir.entity_properties
|
|
117
|
+
|
|
118
|
+
assert_not_same old_cache, Workflow.job_cache
|
|
119
|
+
assert_empty Workflow.job_cache
|
|
120
|
+
assert_equal 0, Log::ProgressBar.default_severity
|
|
121
|
+
assert_equal_path File.join(tmpdir.to_s, 'entity_properties'), Entity.entity_property_cache.to_s
|
|
122
|
+
assert_equal_path File.join(tmpdir.to_s, 'var', 'jobs', workflow.name), workflow.directory.find.to_s
|
|
123
|
+
assert_equal_path File.join(tmpdir.to_s, 'var', 'jobs', workflow.name, 'probe'), task.directory.to_s
|
|
124
|
+
|
|
125
|
+
restore_rig_test_state
|
|
126
|
+
assert_same old_cache, Workflow.job_cache
|
|
127
|
+
assert_equal({ preserved: :entry }, Workflow.job_cache)
|
|
128
|
+
assert_equal 17, Log::ProgressBar.default_severity
|
|
129
|
+
assert_equal_path File.join(tmpdir.to_s, 'rig-entity-cache'), Entity.entity_property_cache.to_s
|
|
130
|
+
refute workflow.instance_variable_defined?(:@directory)
|
|
131
|
+
assert_equal 'original-task-directory', task.directory
|
|
132
|
+
ensure
|
|
133
|
+
Workflow.workflows.delete(workflow)
|
|
134
|
+
restore_rig_test_state
|
|
135
|
+
workflow.instance_variable_set(:@directory, workflow_state[1]) if workflow_state[0]
|
|
136
|
+
task.instance_variable_set(:@directory, task_state[1]) if task_state[0]
|
|
137
|
+
restore_test_ivar(Workflow, :@job_cache, cache_state)
|
|
138
|
+
restore_test_ivar(Log::ProgressBar, :@default_severity, severity_state)
|
|
139
|
+
restore_test_ivar(Entity, :@entity_property_cache, entity_cache_state)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
private
|
|
144
|
+
|
|
145
|
+
def restore_test_ivar(owner, name, state)
|
|
146
|
+
was_defined, value = state
|
|
147
|
+
if was_defined
|
|
148
|
+
owner.instance_variable_set(name, value)
|
|
149
|
+
elsif owner.instance_variable_defined?(name)
|
|
150
|
+
owner.remove_instance_variable(name)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
class TestScoutRigTemporaryDirectoryHelpers < Test::Unit::TestCase
|
|
156
|
+
def test_with_tmpdir_yields_string_path_and_removes_it
|
|
157
|
+
path = nil
|
|
158
|
+
Scout::Testing.with_tmpdir(parent: tmpdir.to_s, prefix: 'scout-rig-helper-') do |directory|
|
|
159
|
+
path = directory
|
|
160
|
+
assert_kind_of String, directory
|
|
161
|
+
assert File.directory?(directory)
|
|
162
|
+
assert_match(/\Ascout-rig-helper-/, File.basename(directory))
|
|
163
|
+
end
|
|
164
|
+
refute File.exist?(path)
|
|
165
|
+
end
|
|
166
|
+
end
|