Skip to content

Commit 81c755d

Browse files
kannon92mr-c
authored andcommitted
feat: add tests and copy logs to output if specified
1 parent 57a4a87 commit 81c755d

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

cwltool/job.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,17 @@ def _execute(
314314
raise WorkflowException(f"{self.stdin} missing from pathmapper")
315315
else:
316316
stdin_path = rmap[1]
317-
318-
stderr_path = None
319-
if self.stderr is not None:
320-
abserr = os.path.join(self.base_path_logs, self.stderr)
321-
dnerr = os.path.dirname(abserr)
322-
if dnerr and not os.path.exists(dnerr):
323-
os.makedirs(dnerr)
324-
stderr_path = abserr
325-
326-
stdout_path = None
327-
if self.stdout is not None:
328-
absout = os.path.join(self.base_path_logs, self.stdout)
329-
dnout = os.path.dirname(absout)
330-
if dnout and not os.path.exists(dnout):
331-
os.makedirs(dnout)
332-
stdout_path = absout
317+
def stderr_stdout_log_path(base_path_logs, stderr_or_stdout) -> Optional[str]:
318+
if stderr_or_stdout is not None:
319+
abserr = os.path.join(base_path_logs, stderr_or_stdout)
320+
dnerr = os.path.dirname(abserr)
321+
if dnerr and not os.path.exists(dnerr):
322+
os.makedirs(dnerr)
323+
return abserr
324+
return None
325+
326+
stderr_path = stderr_stdout_log_path(self.base_path_logs, self.stderr)
327+
stdout_path = stderr_stdout_log_path(self.base_path_logs, self.stdout)
333328
commands = [str(x) for x in runtime + self.command_line]
334329
if runtimeContext.secret_store is not None:
335330
commands = cast(
@@ -395,6 +390,15 @@ def _execute(
395390
"'listing' in self.generatefiles but no "
396391
"generatemapper was setup."
397392
)
393+
# Move logs from log location to final output
394+
if self.outdir != self.base_path_logs:
395+
if stdout_path:
396+
new_stdout_path = stdout_path.replace(self.base_path_logs, self.outdir)
397+
shutil.copy2(stdout_path, new_stdout_path)
398+
if stderr_path:
399+
new_stderr_path = stderr_path.replace(self.base_path_logs, self.outdir)
400+
shutil.copy2(stderr_path, new_stderr_path)
401+
398402

399403
outputs = self.collect_outputs(self.outdir, rcode)
400404
outputs = bytes2str_in_dicts(outputs) # type: ignore

tests/test_stdout_stderr_log_dir.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
import json
3+
from pathlib import Path
4+
5+
from cwltool.main import main
6+
7+
from .util import get_main_output, needs_docker
8+
9+
def test_log_dir_echo_output() -> None:
10+
_, stdout, stderr = get_main_output(
11+
[
12+
"--log-dir",
13+
"logs",
14+
"tests/echo.cwl",
15+
"--inp", "hello"
16+
]
17+
)
18+
assert "completed success" in stderr, stderr
19+
assert json.loads(stdout)['out'].strip('\n') == 'hello'

0 commit comments

Comments
 (0)