Skip to content

Commit 175662f

Browse files
Ryan CaliRyan Cali
authored andcommitted
Added file test, atlocation test, hash test
1 parent a8c3a37 commit 175662f

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

pydra/engine/audit.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
import attr
66
from ..utils.messenger import send_message, make_message, gen_uuid, now, AuditFlag
7-
from .helpers import ensure_list, gather_runtime_info
7+
from .helpers import ensure_list, gather_runtime_info, hash_file
88

99

1010
class Audit:
@@ -183,8 +183,10 @@ def audit_task(self, task):
183183

184184
if hasattr(task.inputs, "in_file"):
185185
input_file = task.inputs.in_file
186+
file_hash = hash_file(input_file)
186187
at_location = os.path.abspath(input_file)
187188
else:
189+
file_hash = None
188190
at_location = None
189191
input_file = None
190192

@@ -221,7 +223,7 @@ def audit_task(self, task):
221223
"AtLocation": at_location,
222224
"GeneratedBy": "test", # if not part of workflow, this will be none
223225
"@type": "input",
224-
"digest": "checksum", # hash value under helpers.py
226+
"digest": file_hash, # hash value under helpers.py
225227
}
226228

227229
# new code to be added here for i/o tracking - WIP

pydra/engine/tests/test_task.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
from ..core import Workflow
1212
from ..task import AuditFlag, ShellCommandTask, DockerTask, SingularityTask
1313
from ...utils.messenger import FileMessenger, PrintMessenger, collect_messages
14-
from .utils import gen_basic_wf, use_validator
15-
from ..specs import MultiInputObj, MultiOutputObj, SpecInfo, FunctionSpec, BaseSpec
14+
from .utils import gen_basic_wf, use_validator, Submitter
15+
from ..specs import MultiInputObj, MultiOutputObj, SpecInfo, FunctionSpec, BaseSpec, ShellSpec, File
16+
from ..helpers import hash_file
1617

1718
no_win = pytest.mark.skipif(
1819
sys.platform.startswith("win"),
@@ -1021,6 +1022,7 @@ def testfunc(a: int, b: float = 0.1) -> ty.NamedTuple("Output", [("out", float)]
10211022
if "AssociatedWith" in data:
10221023
assert None == data["AssociatedWith"]
10231024

1025+
10241026
# assert any(json_content)
10251027

10261028

@@ -1062,6 +1064,50 @@ def test_audit_shellcommandtask(tmpdir):
10621064
assert any(command_content)
10631065

10641066

1067+
def test_audit_shellcommandtask_file(tmpdir):
1068+
# create test.txt file with "This is a test" in it in the tmpdir
1069+
with open(tmpdir / "test.txt", "w") as f:
1070+
f.write("This is a test.")
1071+
1072+
cmd = "cat"
1073+
file_in = tmpdir / "test.txt"
1074+
test_file_hash = hash_file(file_in)
1075+
my_input_spec = SpecInfo(
1076+
name='Input',
1077+
fields=[
1078+
(
1079+
'in_file',
1080+
attr.ib(
1081+
type=File,
1082+
metadata={
1083+
'position': 1,
1084+
'argstr': '',
1085+
'help_string': 'text',
1086+
'mandatory': True,
1087+
},
1088+
),
1089+
)
1090+
],
1091+
bases=(ShellSpec,),
1092+
)
1093+
shelly = ShellCommandTask(
1094+
name='shelly', in_file=file_in, input_spec=my_input_spec, executable=cmd, audit_flags=AuditFlag.PROV, messengers=PrintMessenger()
1095+
)
1096+
shelly.cache_dir = tmpdir
1097+
shelly()
1098+
message_path = tmpdir / shelly.checksum / "messages"
1099+
for file in glob.glob(str(message_path) + "/*.jsonld"):
1100+
with open(file, "r") as f:
1101+
data = json.load(f)
1102+
print(file_in)
1103+
if "AtLocation" in data:
1104+
assert data["AtLocation"] == str(file_in)
1105+
if "digest" in data:
1106+
assert test_file_hash == data["digest"]
1107+
1108+
1109+
1110+
10651111
def test_audit_shellcommandtask_version(tmpdir):
10661112
import subprocess as sp
10671113

0 commit comments

Comments
 (0)