Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/essreduce/src/ess/reduce/nexus/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ def write(self, output: Path | str) -> Path:
Write the merged file.

The file is written under a temporary name next to ``output`` and renamed
when complete, so ``output`` never holds a partial file.
when complete, so ``output`` never holds a partial file. A partial file
would look valid: it starts as a copy of the first run's file, so
reducing it would silently give wrong results.

Parameters
----------
Expand Down
15 changes: 9 additions & 6 deletions packages/essreduce/tests/nexus/merge_test.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the context of this test. Why does it use chmod to begin with? If it's about disabling access to an input file mid-merge, you can just delete the file. Or trigger a write failure in some other way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know either. Might be no particular reason.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, the comment didn't explain it. The failure must happen after write has created merged.nxs.partial, i.e. when opening the second input, so the cleanup is actually exercised. Deleting the file fails earlier: the size/mtime check at the start of write raises FileNotFoundError before .partial exists, and the test would pass even with the cleanup removed (checked). That's why chmod, which keeps size and mtime.

6e73d10 replaces chmod with overwriting the file with zeros and restoring its mtime, which should also work on Windows, so the skip is gone. The write docstring now also says why a partial output must never exist: it starts as a copy of the first run's file and looks valid.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2026 Scipp contributors (https://github.com/scipp)
import os
from collections.abc import Callable
from pathlib import Path

Expand Down Expand Up @@ -313,12 +314,14 @@ def test_write_leaves_no_file_if_writing_fails(
runs: list[Path], tmp_path: Path
) -> None:
plan = plan_merge(runs, RULES)
plan.files[1].chmod(0) # keeps the size and modification time
try:
with pytest.raises(OSError, match='nable to'):
plan.write(tmp_path / 'merged.nxs')
finally:
plan.files[1].chmod(0o644)
# Make opening the second file fail, after the partial file was created.
# Size and modification time must stay the same, or `write` refuses earlier.
path = plan.files[1]
stat = path.stat()
path.write_bytes(b'\0' * stat.st_size)
os.utime(path, ns=(stat.st_atime_ns, stat.st_mtime_ns))
with pytest.raises(OSError, match='file signature not found'):
plan.write(tmp_path / 'merged.nxs')
assert sorted(p.name for p in tmp_path.iterdir()) == ['a.nxs', 'b.nxs']


Expand Down
Loading