Skip to content

Commit a380873

Browse files
committed
test hashing of lazy fields
1 parent 9a411db commit a380873

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

pydra/engine/lazy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LazyField(ty.Generic[T], metaclass=abc.ABCMeta):
3030

3131
def __bytes_repr__(self, cache):
3232
yield type(self).__name__.encode() + b"("
33-
yield from bytes(hash_single(self.source, cache))
33+
yield b"source=" + bytes(hash_single(self._source, cache))
3434
yield b"field=" + self._field.encode()
3535
yield b"type=" + bytes(hash_single(self._type, cache))
3636
yield b"cast_from=" + bytes(hash_single(self._cast_from, cache))

pydra/utils/tests/test_hash.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import random
2222
from fileformats.generic import Directory, File
2323
from pydra.utils.hash import hash_function
24+
from pydra.utils.tests.utils import Concatenate
2425

2526

2627
def test_hash_file(tmpdir):
@@ -558,3 +559,14 @@ def __repr__(self):
558559
),
559560
):
560561
hash_object(A())
562+
563+
564+
def test_hash_task(tmp_path):
565+
"""
566+
Test that the hash of a task is consistent across runs
567+
"""
568+
569+
concatenate1 = Concatenate()
570+
concatenate2 = Concatenate()
571+
572+
assert hash_function(concatenate1) == hash_function(concatenate2)

pydra/utils/tests/utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import typing as ty
2+
from pathlib import Path
13
from fileformats.generic import File, BinaryFile
24
from fileformats.core.mixin import WithSeparateHeader, WithMagicNumber
35
from pydra.compose import shell, python
@@ -94,3 +96,38 @@ class Outputs(shell.Outputs):
9496
)
9597

9698
executable = "echo"
99+
100+
101+
@python.define(outputs=["out_file"])
102+
def Concatenate(
103+
in_file1: File,
104+
in_file2: File,
105+
out_file: ty.Optional[Path] = None,
106+
duplicates: int = 1,
107+
) -> File:
108+
"""Concatenates the contents of two files and writes them to a third
109+
110+
Parameters
111+
----------
112+
in_file1 : Path
113+
A text file
114+
in_file2 : Path
115+
Another text file
116+
out_file : Path
117+
The path to write the output file to
118+
119+
Returns
120+
-------
121+
out_file: Path
122+
A text file made by concatenating the two inputs
123+
"""
124+
if out_file is None:
125+
out_file = Path("out_file.txt").absolute()
126+
contents = []
127+
for _ in range(duplicates):
128+
for fname in (in_file1, in_file2):
129+
with open(fname) as f:
130+
contents.append(f.read())
131+
with open(out_file, "w") as f:
132+
f.write("\n".join(contents))
133+
return out_file

0 commit comments

Comments
 (0)