Skip to content

Commit 952df09

Browse files
committed
BUG: refuse working logs that are one file, and options that split a row
import_results points input_file, output_file and error_file at one path, and a run then appends input rows and output rows into it. Compared by inode once the files exist, so a symlink, a hard link, a/../run.txt and a case-insensitive filesystem are all the same file rather than three names. json.dumps kwargs reach the writer, so indent=2 wrote records across several lines while every reader here takes one line at a time. The run finished and the completeness check then called the file it had just written damaged. indent of 0 and "" do the same, as does a newline inside separators. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 0a85005 commit 952df09

2 files changed

Lines changed: 158 additions & 9 deletions

File tree

‎rocketpy/simulation/monte_carlo.py‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
_SIMULATION_LOG_SUFFIX = ".txt"
4545

4646

47-
def _refuse_logs_this_run_cannot_write(input_file, output_file, error_file):
47+
def _refuse_logs_this_run_cannot_write(
48+
input_file, output_file, error_file, export_config=None
49+
):
4850
"""Reject a log file ``simulate`` would damage rather than extend.
4951
5052
A ``.csv`` or ``.json`` is importable for analysis, but this run would
@@ -65,6 +67,71 @@ def _refuse_logs_this_run_cannot_write(input_file, output_file, error_file):
6567
f"{label} at a {_SIMULATION_LOG_SUFFIX} file to run."
6668
)
6769

70+
_refuse_logs_that_are_one_file(
71+
(
72+
("input_file", input_file),
73+
("output_file", output_file),
74+
("error_file", error_file),
75+
)
76+
)
77+
_refuse_export_options_that_break_a_line(export_config or {})
78+
79+
80+
def _points_at_the_same_file(one, other):
81+
"""Whether two names reach one file, by inode when both already exist.
82+
83+
``samefile`` settles symlinks, hard links and a case-insensitive filesystem,
84+
none of which text comparison sees. It needs both to exist, so a run that has
85+
not created them yet falls back to the resolved paths, which still normalises
86+
``a/../run.txt`` and any symlinked parent.
87+
"""
88+
one, other = Path(one), Path(other)
89+
try:
90+
return one.samefile(other)
91+
except OSError:
92+
return one.resolve() == other.resolve()
93+
94+
95+
def _refuse_logs_that_are_one_file(labelled_paths):
96+
"""Each log has to be its own file, however the three were named.
97+
98+
``import_results`` points all three at one path, and the run then appends
99+
input rows and output rows into it. The completeness check reports the mess
100+
afterwards, by which time the file it was given is already gone.
101+
"""
102+
for index, (label, path) in enumerate(labelled_paths):
103+
for other_label, other in labelled_paths[index + 1 :]:
104+
if _points_at_the_same_file(path, other):
105+
raise ValueError(
106+
f"{label} and {other_label} are the same file ('{path}' and "
107+
f"'{other}'). A run appends input rows and output rows "
108+
f"separately, so sharing one log writes both into it and "
109+
f"leaves neither readable. Give each its own file."
110+
)
111+
112+
113+
def _refuse_export_options_that_break_a_line(export_config):
114+
"""Reject export options that would split one record over several lines.
115+
116+
The logs hold one JSON object per line and every reader here assumes it, so
117+
``indent`` of any kind, ``0`` and ``""`` included, leaves a file that the
118+
completeness check calls damaged once the run it just finished is over.
119+
"""
120+
if export_config.get("indent") is not None:
121+
raise ValueError(
122+
f"indent={export_config['indent']!r} cannot be used with a Monte "
123+
f"Carlo run: the logs hold one JSON object per line, and an "
124+
f"indented record spans several. Export the results with indent "
125+
f"after the run instead."
126+
)
127+
separators = export_config.get("separators")
128+
if separators and any("\n" in str(part) for part in separators):
129+
raise ValueError(
130+
f"separators={separators!r} cannot be used with a Monte Carlo run: "
131+
f"a newline inside a record splits it across lines, and the logs "
132+
f"hold one JSON object per line."
133+
)
134+
68135

69136
class MonteCarlo: # pylint: disable=too-many-public-methods
70137
"""Class to run a Monte Carlo simulation of a rocket flight.
@@ -253,7 +320,7 @@ def simulate(
253320

254321
# Before anything is opened: __setup_files truncates for append=False.
255322
_refuse_logs_this_run_cannot_write(
256-
self.input_file, self.output_file, self.error_file
323+
self.input_file, self.output_file, self.error_file, kwargs
257324
)
258325

259326
print("Starting Monte Carlo analysis")

‎tests/unit/simulation/test_monte_carlo.py‎

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csv
22
import json
3+
import os
34
import pathlib
45
import types
56
from collections import namedtuple
@@ -629,22 +630,103 @@ def test_simulate_refuses_a_results_file_it_cannot_write(
629630
assert results.read_bytes() == before
630631

631632

633+
def _three_logs(tmp_path):
634+
"""Three distinct, acceptable working logs."""
635+
return [
636+
str(tmp_path / f"run.{part}.txt") for part in ("inputs", "outputs", "errors")
637+
]
638+
639+
632640
def test_simulation_log_check_names_the_file_that_is_wrong(tmp_path):
633641
"""The message says which of the three paths has to change."""
634-
good = str(tmp_path / "run.txt")
642+
good = _three_logs(tmp_path)
635643

636-
_refuse_logs_this_run_cannot_write(good, good, good) # canonical, no raise
644+
_refuse_logs_this_run_cannot_write(*good) # canonical, no raise
637645

638646
for label, args in (
639-
("input_file", (str(tmp_path / "a.csv"), good, good)),
640-
("output_file", (good, str(tmp_path / "b.json"), good)),
641-
("error_file", (good, good, str(tmp_path / "c.csv"))),
647+
("input_file", (str(tmp_path / "a.csv"), good[1], good[2])),
648+
("output_file", (good[0], str(tmp_path / "b.json"), good[2])),
649+
("error_file", (good[0], good[1], str(tmp_path / "c.csv"))),
642650
):
643651
with pytest.raises(ValueError, match=label):
644652
_refuse_logs_this_run_cannot_write(*args)
645653

646654

647655
def test_simulation_log_check_accepts_an_uppercase_suffix(tmp_path):
648656
"""A .TXT log is the same file to the filesystem, so it is accepted."""
649-
upper = str(tmp_path / "run.TXT")
650-
_refuse_logs_this_run_cannot_write(upper, upper, upper)
657+
upper = [str(tmp_path / f"run.{part}.TXT") for part in ("in", "out", "err")]
658+
_refuse_logs_this_run_cannot_write(*upper)
659+
660+
661+
def _three_logs(tmp_path):
662+
"""Three distinct, acceptable working logs."""
663+
return [
664+
str(tmp_path / f"run.{part}.txt") for part in ("inputs", "outputs", "errors")
665+
]
666+
667+
668+
def test_working_logs_must_be_three_different_files(tmp_path):
669+
"""``import_results`` points all three at one path, which cannot work.
670+
671+
A run appends input rows and output rows separately, so one shared log ends
672+
up holding both and neither reader can make sense of it.
673+
"""
674+
shared = str(tmp_path / "result.txt")
675+
676+
with pytest.raises(ValueError, match="same file"):
677+
_refuse_logs_this_run_cannot_write(shared, shared, shared)
678+
679+
680+
@pytest.mark.parametrize("alias", ["dotdot", "symlink", "hardlink"])
681+
def test_a_log_named_two_ways_is_still_one_file(tmp_path, alias):
682+
"""Text comparison misses every way one file answers to two names."""
683+
inputs, _, errors = _three_logs(tmp_path)
684+
pathlib.Path(inputs).write_text("", encoding="utf-8")
685+
(tmp_path / "sub").mkdir()
686+
687+
if alias == "dotdot":
688+
other = str(tmp_path / "sub" / ".." / "run.inputs.txt")
689+
else:
690+
other = str(tmp_path / f"run.{alias}.txt")
691+
try:
692+
if alias == "symlink":
693+
pathlib.Path(other).symlink_to(inputs)
694+
else:
695+
os.link(inputs, other)
696+
except (OSError, NotImplementedError):
697+
pytest.skip(f"{alias} not available on this filesystem")
698+
699+
with pytest.raises(ValueError, match="same file"):
700+
_refuse_logs_this_run_cannot_write(inputs, other, errors)
701+
702+
703+
def test_three_separate_logs_are_accepted(tmp_path):
704+
"""The control: distinct .txt paths raise nothing."""
705+
_refuse_logs_this_run_cannot_write(*_three_logs(tmp_path))
706+
707+
708+
@pytest.mark.parametrize("indent", [2, 0, ""])
709+
def test_an_indented_record_is_refused_before_anything_is_written(tmp_path, indent):
710+
"""``indent`` splits a record over lines the readers take one at a time.
711+
712+
Without this the run finished, then the completeness check called the file
713+
it had just written damaged.
714+
"""
715+
with pytest.raises(ValueError, match="indent"):
716+
_refuse_logs_this_run_cannot_write(*_three_logs(tmp_path), {"indent": indent})
717+
718+
719+
def test_a_newline_in_the_separators_is_refused_too(tmp_path):
720+
"""The same hazard by another name."""
721+
with pytest.raises(ValueError, match="separators"):
722+
_refuse_logs_this_run_cannot_write(
723+
*_three_logs(tmp_path), {"separators": (",\n", ": ")}
724+
)
725+
726+
727+
@pytest.mark.parametrize(
728+
"harmless", [{"indent": None}, {"sort_keys": True}, {"ensure_ascii": False}]
729+
)
730+
def test_export_options_that_keep_one_line_are_left_alone(tmp_path, harmless):
731+
"""Only what puts a newline inside a record is refused."""
732+
_refuse_logs_this_run_cannot_write(*_three_logs(tmp_path), harmless)

0 commit comments

Comments
 (0)