|
1 | 1 | import csv |
2 | 2 | import json |
| 3 | +import os |
3 | 4 | import pathlib |
4 | 5 | import types |
5 | 6 | from collections import namedtuple |
@@ -629,22 +630,103 @@ def test_simulate_refuses_a_results_file_it_cannot_write( |
629 | 630 | assert results.read_bytes() == before |
630 | 631 |
|
631 | 632 |
|
| 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 | + |
632 | 640 | def test_simulation_log_check_names_the_file_that_is_wrong(tmp_path): |
633 | 641 | """The message says which of the three paths has to change.""" |
634 | | - good = str(tmp_path / "run.txt") |
| 642 | + good = _three_logs(tmp_path) |
635 | 643 |
|
636 | | - _refuse_logs_this_run_cannot_write(good, good, good) # canonical, no raise |
| 644 | + _refuse_logs_this_run_cannot_write(*good) # canonical, no raise |
637 | 645 |
|
638 | 646 | 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"))), |
642 | 650 | ): |
643 | 651 | with pytest.raises(ValueError, match=label): |
644 | 652 | _refuse_logs_this_run_cannot_write(*args) |
645 | 653 |
|
646 | 654 |
|
647 | 655 | def test_simulation_log_check_accepts_an_uppercase_suffix(tmp_path): |
648 | 656 | """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