|
| 1 | +############################################################################## |
| 2 | +# (c) Crown copyright Met Office. All rights reserved. |
| 3 | +# For further details please refer to the file COPYRIGHT |
| 4 | +# which you should have received as part of this distribution |
| 5 | +############################################################################## |
| 6 | +""" |
| 7 | +Tests 'pfunit' tool. |
| 8 | +""" |
| 9 | + |
| 10 | +import logging |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +from pytest_subprocess.fake_process import FakeProcess |
| 14 | + |
| 15 | + |
| 16 | +from fab.tools.category import Category |
| 17 | +from fab.tools.pfunit import PfUnit |
| 18 | + |
| 19 | +from tests.conftest import ExtendedRecorder, call_list |
| 20 | + |
| 21 | + |
| 22 | +def test_pfunit_constructor_no_env(monkeypatch, caplog) -> None: |
| 23 | + """ |
| 24 | + Tests constructor when $PFUNIT is not defined |
| 25 | + """ |
| 26 | + # Make sure the environment variable PFUNIT is not defined: |
| 27 | + monkeypatch.delenv("PFUNIT", raising=False) |
| 28 | + |
| 29 | + with caplog.at_level(logging.ERROR): |
| 30 | + pfunit = PfUnit() |
| 31 | + assert ("$PFUNIT not defined in environment, pFUnit will likely " |
| 32 | + "not work." in caplog.text) |
| 33 | + assert len(caplog.records) == 1 |
| 34 | + assert caplog.records[0].levelname == "ERROR" |
| 35 | + |
| 36 | + assert pfunit.category == Category.PFUNIT |
| 37 | + assert pfunit.name == "funitproc" |
| 38 | + assert pfunit.exec_name == "funitproc" |
| 39 | + |
| 40 | + |
| 41 | +def test_pfunit_constructor_with_env(monkeypatch, caplog) -> None: |
| 42 | + """ |
| 43 | + Tests constructor when $PFUNIT is defined |
| 44 | + """ |
| 45 | + |
| 46 | + # Make sure the environment variable PFUNIT is defined: |
| 47 | + monkeypatch.setenv("PFUNIT", "/tmp") |
| 48 | + |
| 49 | + with caplog.at_level(logging.ERROR): |
| 50 | + pfunit = PfUnit() |
| 51 | + assert len(caplog.records) == 0 |
| 52 | + assert pfunit.category == Category.PFUNIT |
| 53 | + assert pfunit.name == "funitproc" |
| 54 | + assert pfunit.exec_name == "funitproc" |
| 55 | + assert pfunit.get_root_path() == Path("/tmp") |
| 56 | + |
| 57 | + |
| 58 | +def test_pfunit_paths(monkeypatch) -> None: |
| 59 | + """ |
| 60 | + Tests root and include paths. |
| 61 | + """ |
| 62 | + |
| 63 | + # Make sure the environment variable PFUNIT is defined: |
| 64 | + monkeypatch.setenv("PFUNIT", "/tmp") |
| 65 | + |
| 66 | + pfunit = PfUnit() |
| 67 | + assert pfunit.get_root_path() == Path("/tmp") |
| 68 | + assert pfunit.get_include_path() == Path("/tmp/include") |
| 69 | + |
| 70 | + |
| 71 | +def test_pfunit_driver(monkeypatch, tmp_path: Path) -> None: |
| 72 | + """ |
| 73 | + Tests that pfunit reads the driver.F90 file: |
| 74 | + """ |
| 75 | + |
| 76 | + # Make sure the environment variable PFUNIT is defined: |
| 77 | + monkeypatch.setenv("PFUNIT", str(tmp_path)) |
| 78 | + include_path = tmp_path / "include" |
| 79 | + include_path.mkdir() |
| 80 | + (include_path / "driver.F90").write_text("DRIVER\n") |
| 81 | + |
| 82 | + pfunit = PfUnit() |
| 83 | + assert pfunit.get_driver_f90() == "DRIVER\n" |
| 84 | + |
| 85 | + |
| 86 | +def test_pfunit_check_available(monkeypatch, |
| 87 | + subproc_record: ExtendedRecorder) -> None: |
| 88 | + """ |
| 89 | + Tests availability functionality. |
| 90 | + """ |
| 91 | + monkeypatch.setenv("PFUNIT", "/tmp") |
| 92 | + pfunit = PfUnit() |
| 93 | + assert pfunit.check_available() |
| 94 | + assert subproc_record.invocations() == [["/tmp/bin/funitproc", "-v"]] |
| 95 | + assert subproc_record.extras() == [{'cwd': None, |
| 96 | + 'env': None, |
| 97 | + 'stdout': None, |
| 98 | + 'stderr': None}] |
| 99 | + |
| 100 | + |
| 101 | +def test_pfunit_check_unavailable(monkeypatch, |
| 102 | + fake_process: FakeProcess) -> None: |
| 103 | + """ |
| 104 | + Tests availability failure. |
| 105 | + """ |
| 106 | + monkeypatch.setenv("PFUNIT", "/tmp") |
| 107 | + fake_process.register(['/tmp/bin/funitproc', '-v'], |
| 108 | + returncode=1, |
| 109 | + stderr="Something went wrong.") |
| 110 | + pfunit = PfUnit() |
| 111 | + assert not pfunit.check_available() |
| 112 | + assert call_list(fake_process) == [["/tmp/bin/funitproc", "-v"]] |
| 113 | + |
| 114 | + |
| 115 | +def test_pfunit_process(monkeypatch, |
| 116 | + tmp_path: Path, |
| 117 | + subproc_record: ExtendedRecorder) -> None: |
| 118 | + """ |
| 119 | + Tests processing a file |
| 120 | + """ |
| 121 | + monkeypatch.setenv("PFUNIT", str(tmp_path)) |
| 122 | + pfunit = PfUnit() |
| 123 | + pfunit.process(pf_path=tmp_path / "file.pf", |
| 124 | + f90_out_path=tmp_path / "file.f90") |
| 125 | + |
| 126 | + assert subproc_record.invocations() \ |
| 127 | + == [[str(tmp_path / "bin" / "funitproc"), |
| 128 | + str(tmp_path / "file.pf"), |
| 129 | + str(tmp_path / "file.f90")]] |
| 130 | + assert subproc_record.extras() == [{'cwd': None, |
| 131 | + 'env': None, |
| 132 | + 'stderr': None, |
| 133 | + 'stdout': None}] |
0 commit comments