Skip to content

Commit

Permalink
Revert "mock mtime writing instead of adding sleeps to ensure mtimes …
Browse files Browse the repository at this point in the history
…are different"

This reverts commit 0895965.
  • Loading branch information
tclose committed Sep 26, 2023
1 parent 2f5cb34 commit fbf78f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
7 changes: 3 additions & 4 deletions pydra/engine/tests/test_node_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,10 @@ def test_task_init_7(tmp_path):
output_dir1 = nn1.output_dir

# changing the content of the file
time.sleep(2) # need the mtime to be different
file2 = tmp_path / "file2.txt"
with mock.patch("time.time") as t:
t.return_value = time.time() + 10 # mock mtime to ensure different
with open(file2, "w") as f:
f.write("from pydra")
with open(file2, "w") as f:
f.write("from pydra")

nn2 = fun_file_list(name="NA", filename_list=[file1, file2])
output_dir2 = nn2.output_dir
Expand Down
37 changes: 15 additions & 22 deletions pydra/engine/tests/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import attrs
from copy import deepcopy
from unittest import mock
import time

from ..specs import (
Expand Down Expand Up @@ -196,11 +195,10 @@ def test_input_file_hash_2(tmp_path):
assert hash1 == hash2

# checking if different content (the same name) affects the hash
time.sleep(2) # ensure mtime is different
file_diffcontent = tmp_path / "in_file_1.txt"
with mock.patch("time.time") as t:
t.return_value = time.time() + 10 # mock mtime to ensure different
with open(file_diffcontent, "w") as f:
f.write("hi")
with open(file_diffcontent, "w") as f:
f.write("hi")
hash3 = inputs(in_file=file_diffcontent).hash
assert hash1 != hash3

Expand Down Expand Up @@ -228,12 +226,10 @@ def test_input_file_hash_2a(tmp_path):
assert hash1 == hash2

# checking if different content (the same name) affects the hash
time.sleep(2) # ensure mtime is different
file_diffcontent = tmp_path / "in_file_1.txt"
# Ensure that the mtime will be incremented by mocking time.time
with mock.patch("time.time") as t:
t.return_value = time.time() + 10
with open(file_diffcontent, "w") as f:
f.write("hi")
with open(file_diffcontent, "w") as f:
f.write("hi")
hash3 = inputs(in_file=file_diffcontent).hash
assert hash1 != hash3

Expand Down Expand Up @@ -272,10 +268,9 @@ def test_input_file_hash_3(tmp_path):
# assert id(files_hash1["in_file"][filename]) == id(files_hash2["in_file"][filename])

# recreating the file
with mock.patch("time.time") as t:
t.return_value = time.time() + 10 # mock mtime to ensure different
with open(file, "w") as f:
f.write("hello")
time.sleep(2) # ensure mtime is different
with open(file, "w") as f:
f.write("hello")

hash3 = my_inp.hash
# files_hash3 = deepcopy(my_inp.files_hash)
Expand Down Expand Up @@ -328,11 +323,10 @@ def test_input_file_hash_4(tmp_path):
assert hash1 == hash2

# checking if different content (the same name) affects the hash
time.sleep(2) # need the mtime to be different
file_diffcontent = tmp_path / "in_file_1.txt"
with mock.patch("time.time") as t:
t.return_value = time.time() + 10 # mock mtime to ensure different
with open(file_diffcontent, "w") as f:
f.write("hi")
with open(file_diffcontent, "w") as f:
f.write("hi")
hash3 = inputs(in_file=[[file_diffcontent, 3]]).hash
assert hash1 != hash3

Expand Down Expand Up @@ -366,11 +360,10 @@ def test_input_file_hash_5(tmp_path):
assert hash1 == hash2

# checking if different content (the same name) affects the hash
time.sleep(2) # ensure mtime is different
file_diffcontent = tmp_path / "in_file_1.txt"
with mock.patch("time.time") as t:
t.return_value = time.time() + 10 # mock mtime to ensure different
with open(file_diffcontent, "w") as f:
f.write("hi")
with open(file_diffcontent, "w") as f:
f.write("hi")
hash3 = inputs(in_file=[{"file": file_diffcontent, "int": 3}]).hash
assert hash1 != hash3

Expand Down
10 changes: 4 additions & 6 deletions pydra/utils/tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from hashlib import blake2b
from pathlib import Path
import time
from unittest import mock

import attrs
import pytest
import typing as ty
Expand Down Expand Up @@ -335,11 +335,9 @@ def test_persistent_hash_cache(cache_path, text_file):
assert hash_object(text_file, persistent_cache=cache_path) == modified_hash

# Test that changes to the text file result in new hash
text_file.fspath.write_text("bar")
# Ensure that the mtime will be incremented by mocking time.time
with mock.patch("time.time") as t:
t.return_value = time.time() + 10
assert hash_object(text_file, persistent_cache=cache_path) != modified_hash
time.sleep(2) # Need to ensure that the mtimes will be different
text_file_path.write_text("bar")
assert hash_object(text_file, persistent_cache=cache_path) != modified_hash
assert len(list(cache_path.iterdir())) == 2


Expand Down

0 comments on commit fbf78f3

Please sign in to comment.