|
| 1 | +""" |
| 2 | +Test methods in stager. |
| 3 | +""" |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from pytest import fixture |
| 8 | + |
| 9 | +from uwtools.api.config import get_yaml_config |
| 10 | +from uwtools.drivers.stager import FileStager |
| 11 | + |
| 12 | +B_FILE = Path("./b.foo").resolve() |
| 13 | + |
| 14 | + |
| 15 | +class NullDriver(FileStager): |
| 16 | + def __init__(self, config_file, rundir): |
| 17 | + config = get_yaml_config(config_file) |
| 18 | + config.dereference() |
| 19 | + self.config = config |
| 20 | + self.rundir = rundir |
| 21 | + |
| 22 | + def taskname(self, s): |
| 23 | + return f"Null {s}" |
| 24 | + |
| 25 | + |
| 26 | +def setup_module(module): # noqa: ARG001 |
| 27 | + B_FILE.touch() |
| 28 | + |
| 29 | + |
| 30 | +def teardown_module(module): # noqa: ARG001 |
| 31 | + B_FILE.unlink() |
| 32 | + |
| 33 | + |
| 34 | +@fixture |
| 35 | +def driver(tmp_path): |
| 36 | + i = tmp_path / "input" |
| 37 | + i.mkdir(parents=True, exist_ok=True) |
| 38 | + for fn in ["a.foo", "b.foo"]: |
| 39 | + (i / fn).touch() |
| 40 | + config = """ |
| 41 | + tmp: %s |
| 42 | + files_to_copy: |
| 43 | + a.foo: '{{ tmp }}/input/a.foo' |
| 44 | + b.foo: %s |
| 45 | + files_to_link: |
| 46 | + c.foo: '{{ tmp }}/input/a.foo' |
| 47 | + d.foo: '{{ tmp }}/input/a.foo' |
| 48 | + output/<files>: !glob '{{ tmp }}/input/*.foo' |
| 49 | + files_to_hardlink: |
| 50 | + e.foo: %s |
| 51 | + f.foo: '{{ tmp }}/input/a.foo' |
| 52 | + """ % (tmp_path, B_FILE, B_FILE) |
| 53 | + cfg = tmp_path / "config.yaml" |
| 54 | + cfg.write_text(config) |
| 55 | + return NullDriver(config_file=cfg, rundir=tmp_path) |
| 56 | + |
| 57 | + |
| 58 | +def test_files_to_copy(driver, tmp_path): |
| 59 | + driver.files_copied() |
| 60 | + assert (tmp_path / "a.foo").is_file() |
| 61 | + assert (tmp_path / "b.foo").is_file() |
| 62 | + |
| 63 | + |
| 64 | +def test_files_to_hardlink(driver, tmp_path): |
| 65 | + driver.files_hardlinked() |
| 66 | + a = tmp_path / "input" / "a.foo" |
| 67 | + e = tmp_path / "e.foo" |
| 68 | + f = tmp_path / "f.foo" |
| 69 | + assert e.is_file() |
| 70 | + assert not e.samefile(B_FILE) |
| 71 | + assert a.samefile(f) |
| 72 | + |
| 73 | + |
| 74 | +def test_files_to_link(driver, tmp_path): |
| 75 | + driver.files_linked() |
| 76 | + for fn in ["c", "d", "output/a", "output/b"]: |
| 77 | + fp = Path(tmp_path / f"{fn}.foo") |
| 78 | + assert fp.is_symlink() |
0 commit comments