Skip to content

Commit d14b432

Browse files
committed
Use assert_completed when possible and also change assert_completed
to not complain of a null spec when checking an attached job.
1 parent 6217874 commit d14b432

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

docs/user_guide.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ following example shows:
311311
.. literalinclude:: ../tests/user_guide/test_prelaunch.py
312312
:language: python
313313
:dedent: 4
314-
:lines: 10-16
314+
:lines: 12-18
315315

316316
where the contents of ``pre_launch.sh`` is
317317

tests/_test_tools.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ def _read_file(path: Optional[Path]) -> str:
2828
return f.read()
2929

3030

31-
def assert_completed(job: Job, status: Optional[JobStatus]) -> None:
31+
def assert_completed(job: Job, status: Optional[JobStatus], attached=False) -> None:
3232
assert status is not None
3333
if status.state != JobState.COMPLETED:
34-
assert job.spec is not None
35-
stdout = _read_file(job.spec.stdout_path)
36-
stderr = _read_file(job.spec.stderr_path)
37-
raise AssertionError('Job not completed. Exit code: %s, Status message: %s, '
38-
'stdout: %s, stderr: %s'
39-
% (status.exit_code, status.message, stdout, stderr))
40-
34+
if not attached:
35+
assert job.spec is not None
36+
stdout = _read_file(job.spec.stdout_path)
37+
stderr = _read_file(job.spec.stderr_path)
38+
raise AssertionError('Job not completed. Exit code: %s, Status message: %s, '
39+
'stdout: %s, stderr: %s'
40+
% (status.exit_code, status.message, stdout, stderr))
41+
else:
42+
raise AssertionError('Job not completed. Exit code: %s, Status message: %s'
43+
% (status.exit_code, status.message))
4144

4245
def _get_executor_instance(ep: ExecutorTestParams, job: Optional[Job] = None) -> JobExecutor:
4346
if job is not None:

tests/test_executor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_attach(execparams: ExecutorTestParams) -> None:
4747
job2 = Job()
4848
ex.attach(job2, native_id)
4949
status2 = job2.wait(timeout=_get_timeout(execparams))
50-
assert_completed(job2, status2)
50+
assert_completed(job2, status2, attached=True)
5151
status1 = job1.wait(timeout=_get_timeout(execparams))
5252
assert_completed(job1, status1)
5353

@@ -64,7 +64,7 @@ def test_attach2(execparams: ExecutorTestParams) -> None:
6464
ex2 = _get_executor_instance(execparams)
6565
ex2.attach(job2, native_id)
6666
status = job2.wait(timeout=_get_timeout(execparams))
67-
assert_completed(job2, status)
67+
assert_completed(job2, status, attached=True)
6868

6969

7070
def test_cancel(execparams: ExecutorTestParams) -> None:

tests/user_guide/test_prelaunch.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import os
22
from pathlib import Path
33

4+
from _test_tools import assert_completed
45
from psij import Job, JobSpec, JobExecutor, JobState
56

67

78
def test_user_guide_pre_launch() -> None:
89
script_dir = os.path.dirname(os.path.realpath(__file__))
910

11+
# START
1012
ex = JobExecutor.get_instance('local')
1113
spec = JobSpec('/bin/bash', ['-c', 'module is-loaded test'])
1214
spec.pre_launch = Path(script_dir) / 'pre_launch.sh'
1315

1416
job = Job(spec)
1517
ex.submit(job)
1618
status = job.wait()
17-
assert status is not None
18-
assert status.state == JobState.COMPLETED
19+
# END
20+
assert_completed(job, status)

0 commit comments

Comments
 (0)