Skip to content

Commit

Permalink
Ensure os.stat returns consistent result in mocked unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coloursofnoise committed Aug 6, 2023
1 parent 3af631c commit 4540ca1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mons/commands/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def determine_configuration(srcdir: fs.Directory):
# Artifact still has to be shared between all projects
newest_artifact, _ = max(
(
(output, os.stat(fs.joindir(srcdir, proj, "bin", output)))
(output, os.stat(fs.joindir(srcdir, proj, "bin", output)).st_mtime_ns)
for proj in artifacts.keys()
for output in common_outputs
),
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,14 @@ def mock_filesystem(request, tmp_path):
yield root
return

paths = list()

def create_fs(root, mockup):
if isinstance(mockup, dict):
for dir, contents in mockup.items():
path = os.path.join(root, dir)
os.mkdir(path)
paths.append(path)
create_fs(path, contents)
elif isinstance(mockup, (list, tuple)):
for node in mockup:
Expand All @@ -179,6 +182,12 @@ def create_fs(root, mockup):
else:
with open(path, "x"):
pass
paths.append(path)

create_fs(root, mockup)

# set consistent last modified/accessed times for created paths
for i, path in enumerate(paths):
os.utime(path, (i, i))

yield os.walk(root) if walk else root
1 change: 0 additions & 1 deletion tests/integration/test_main_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def test_determine_configuration_no_common(caplog, mock_filesystem):
)
def test_determine_configuration_most_recent(caplog, mock_filesystem):
with caplog.at_level(logging.DEBUG, main.__name__):
# result may be inconsistent because files are created so close together.
assert main.determine_configuration(mock_filesystem) == "RELEASE/net452"
assert "Most recent" in caplog.text

Expand Down

0 comments on commit 4540ca1

Please sign in to comment.