|
8 | 8 | 0, str(Path(__file__).resolve().parents[1] / "workflow" / "scripts") |
9 | 9 | ) |
10 | 10 |
|
11 | | -from run_multi import slurm_jobname_prefix # noqa: E402 |
| 11 | +import pytest # noqa: E402 |
| 12 | +import yaml # noqa: E402 |
| 13 | + |
| 14 | +from run_multi import ( # noqa: E402 |
| 15 | + _CONVERT_KEYS, |
| 16 | + _snakemake_cmd, |
| 17 | + _validate_configs, |
| 18 | + slurm_jobname_prefix, |
| 19 | +) |
12 | 20 |
|
13 | 21 | # The SLURM executor's own rule (snakemake_executor_plugin_slurm): it raises a |
14 | 22 | # WorkflowError and aborts the whole run if the prefix does not match. |
@@ -36,3 +44,80 @@ def test_jobname_prefix_keeps_the_label_readable(): |
36 | 44 | """The label must lead, since that is what a queue listing truncates to.""" |
37 | 45 | assert slurm_jobname_prefix("nuclei_labels") == "pw-nuclei_labels" |
38 | 46 | assert slurm_jobname_prefix("convert") == "pw-convert" |
| 47 | + |
| 48 | + |
| 49 | +def test_common_configfile_is_merged_under_the_per_config_one(): |
| 50 | + """Snakemake merges --configfile values in order, later winning. |
| 51 | +
|
| 52 | + That ordering is the whole mechanism: shared settings come from common.yaml |
| 53 | + and the per-config file overrides only what differs. Swap the two and every |
| 54 | + config would silently get the shared defaults instead of its own channel. |
| 55 | + """ |
| 56 | + cmd = _snakemake_cmd( |
| 57 | + Path("config/config_nuclei.yaml"), |
| 58 | + workflow_dir=Path("workflow"), |
| 59 | + profile=None, |
| 60 | + cores=8, |
| 61 | + dry_run=False, |
| 62 | + common=Path("config/common.yaml"), |
| 63 | + ) |
| 64 | + i = cmd.index("--configfile") |
| 65 | + assert cmd[i + 1].endswith("common.yaml") |
| 66 | + assert cmd[i + 2].endswith("config_nuclei.yaml") |
| 67 | + |
| 68 | + # Without a common file the invocation is unchanged: one configfile, so |
| 69 | + # a self-contained config keeps working exactly as before. |
| 70 | + plain = _snakemake_cmd( |
| 71 | + Path("config/config_nuclei.yaml"), |
| 72 | + workflow_dir=Path("workflow"), |
| 73 | + profile=None, |
| 74 | + cores=8, |
| 75 | + dry_run=False, |
| 76 | + ) |
| 77 | + j = plain.index("--configfile") |
| 78 | + assert plain[j + 1].endswith("config_nuclei.yaml") |
| 79 | + assert not plain[j + 2].endswith(".yaml") |
| 80 | + |
| 81 | + |
| 82 | +def test_convert_keys_must_agree_across_configs(): |
| 83 | + """`convert` runs once from the first config, so a later one is ignored. |
| 84 | +
|
| 85 | + Setting shard on the second config and watching a million files appear |
| 86 | + anyway is invisible without this check -- there is no log line saying the |
| 87 | + value was dropped, because nothing ever read it. |
| 88 | + """ |
| 89 | + paths = [Path("a.yaml"), Path("b.yaml")] |
| 90 | + base = {"work_dir": "/w", "tile_shape": [16, 512, 512], "level": 0} |
| 91 | + good = [ |
| 92 | + {**base, "label_name": "a", "shard": True}, |
| 93 | + {**base, "label_name": "b", "shard": True}, |
| 94 | + ] |
| 95 | + assert _validate_configs(paths, good) == "/w" |
| 96 | + |
| 97 | + bad = [ |
| 98 | + {**base, "label_name": "a", "shard": True}, |
| 99 | + {**base, "label_name": "b", "shard": False}, |
| 100 | + ] |
| 101 | + # It reports every problem and exits, rather than raising, so that a |
| 102 | + # mistake costs one readable message instead of a traceback. |
| 103 | + with pytest.raises(SystemExit): |
| 104 | + _validate_configs(paths, bad) |
| 105 | + |
| 106 | + |
| 107 | +def test_shipped_multi_configs_are_consistent(): |
| 108 | + """The shipped example must satisfy its own validator. |
| 109 | +
|
| 110 | + It is the thing users copy, so a config set that run_multi would refuse to |
| 111 | + start is worse than no example at all. |
| 112 | + """ |
| 113 | + cfg_dir = Path(__file__).resolve().parents[1] / "workflow" / "config" |
| 114 | + multi = yaml.safe_load((cfg_dir / "multi.yaml").read_text()) |
| 115 | + common = yaml.safe_load((cfg_dir.parent / multi["common"]).read_text()) |
| 116 | + paths = [cfg_dir.parent / p for p in multi["segmentations"]] |
| 117 | + cfgs = [{**common, **yaml.safe_load(p.read_text())} for p in paths] |
| 118 | + |
| 119 | + assert _validate_configs(paths, cfgs) == common["work_dir"] |
| 120 | + # Every key convert reads comes from the shared file, not a per-config one. |
| 121 | + for path in paths: |
| 122 | + own = yaml.safe_load(path.read_text()) |
| 123 | + assert not set(own) & set(_CONVERT_KEYS), path.name |
0 commit comments