-
Notifications
You must be signed in to change notification settings - Fork 7
#552 Added pfunit tool and tests. #562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Joerg Henrichs (hiker)
wants to merge
6
commits into
main
Choose a base branch
from
552_add_pfunit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0620cbd
#552 Added pfunit tool and tests.
hiker b461780
#552 Add pfunit to ToolRepository.
hiker cee3c29
Merge remote-tracking branch 'origin/main' into 552_add_pfunit
hiker c20120b
Merge remote-tracking branch 'origin/main' into 552_add_pfunit
hiker 876541f
Merge remote-tracking branch 'origin/main' into 552_add_pfunit
hiker 5a49560
Merge remote-tracking branch 'origin/main' into 552_add_pfunit
hiker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| ############################################################################## | ||
| # (c) Crown copyright Met Office. All rights reserved. | ||
| # For further details please refer to the file COPYRIGHT | ||
| # which you should have received as part of this distribution | ||
| ############################################################################## | ||
|
|
||
| """This file contains the Rsync class for synchronising file trees. | ||
| """ | ||
|
|
||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from fab.tools.tool import Tool | ||
| from fab.tools.category import Category | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class PfUnit(Tool): | ||
| """ | ||
| This is a class to encapsulate pFUnit. It relies on the environment | ||
| variable $PFUNIT to indicate the location of the source code. | ||
| This is required since besides .mod files and executable, it also | ||
| contains the source code for a Fortran driver program . | ||
| It assumes that pFUnit's preprocessor `funitproc` is in $PFUNIT/bin. | ||
| """ | ||
| Category.add("PFUNIT") | ||
|
|
||
| def __init__(self): | ||
| pfunit_home = os.environ.get("PFUNIT", "") | ||
|
MatthewHambley marked this conversation as resolved.
|
||
| if not pfunit_home: | ||
| logger.error("$PFUNIT not defined in environment, pFUnit will " | ||
| "likely not work.") | ||
| self._pfunit_home = Path(pfunit_home) | ||
|
|
||
| exec_name = self._pfunit_home / "bin" / "funitproc" | ||
| super().__init__("funitproc", exec_name=exec_name, | ||
| category=Category.PFUNIT, | ||
| availability_option="-v") | ||
|
|
||
| def get_root_path(self) -> Path: | ||
| """ | ||
| :returns: the root path of pFUnit. | ||
| """ | ||
| return self._pfunit_home | ||
|
|
||
| def get_include_path(self) -> Path: | ||
| """ | ||
| :returns: the include directory for PFUnit. | ||
| """ | ||
| return self._pfunit_home / "include" | ||
|
|
||
| def get_driver_f90(self) -> str: | ||
| """ | ||
| :returns: the content of pFUnit's driver.F90 file. | ||
| """ | ||
| driver_path = self._pfunit_home / "include" / "driver.F90" | ||
| with driver_path.open("r", encoding='utf-8') as f: | ||
| driver_f90 = f.read() | ||
| return driver_f90 | ||
|
|
||
| def process(self, pf_path: Path, | ||
| f90_out_path: Path): | ||
| """ | ||
| Processes the .pf file to create an output f90 file. | ||
|
|
||
| :param pf_path: the input path. | ||
| :param f90_out_path: destination path. | ||
| """ | ||
| return self.run(additional_parameters=[pf_path, f90_out_path]) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| ############################################################################## | ||
| # (c) Crown copyright Met Office. All rights reserved. | ||
| # For further details please refer to the file COPYRIGHT | ||
| # which you should have received as part of this distribution | ||
| ############################################################################## | ||
| """ | ||
| Tests 'pfunit' tool. | ||
| """ | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| from pytest_subprocess.fake_process import FakeProcess | ||
|
|
||
|
|
||
| from fab.tools.category import Category | ||
| from fab.tools.pfunit import PfUnit | ||
|
|
||
| from tests.conftest import ExtendedRecorder, call_list | ||
|
|
||
|
|
||
| def test_pfunit_constructor_no_env(monkeypatch, caplog) -> None: | ||
| """ | ||
| Tests constructor when $PFUNIT is not defined | ||
| """ | ||
| # Make sure the environment variable PFUNIT is not defined: | ||
| monkeypatch.delenv("PFUNIT", raising=False) | ||
|
|
||
| with caplog.at_level(logging.ERROR): | ||
| pfunit = PfUnit() | ||
| assert ("$PFUNIT not defined in environment, pFUnit will likely " | ||
| "not work." in caplog.text) | ||
| assert len(caplog.records) == 1 | ||
| assert caplog.records[0].levelname == "ERROR" | ||
|
|
||
| assert pfunit.category == Category.PFUNIT | ||
| assert pfunit.name == "funitproc" | ||
| assert pfunit.exec_name == "funitproc" | ||
| assert pfunit.get_flags() == [] | ||
|
|
||
|
|
||
| def test_pfunit_constructor_with_env(monkeypatch, caplog) -> None: | ||
| """ | ||
| Tests constructor when $PFUNIT is defined | ||
| """ | ||
|
|
||
| # Make sure the environment variable PFUNIT is defined: | ||
| monkeypatch.setenv("PFUNIT", "/tmp") | ||
|
|
||
| with caplog.at_level(logging.ERROR): | ||
| pfunit = PfUnit() | ||
| assert len(caplog.records) == 0 | ||
| assert pfunit.category == Category.PFUNIT | ||
| assert pfunit.name == "funitproc" | ||
| assert pfunit.exec_name == "funitproc" | ||
| assert pfunit.get_flags() == [] | ||
| assert pfunit.get_root_path() == Path("/tmp") | ||
|
|
||
|
|
||
| def test_pfunit_paths(monkeypatch) -> None: | ||
| """ | ||
| Tests root and include paths. | ||
| """ | ||
|
|
||
| # Make sure the environment variable PFUNIT is defined: | ||
| monkeypatch.setenv("PFUNIT", "/tmp") | ||
|
|
||
| pfunit = PfUnit() | ||
| assert pfunit.get_root_path() == Path("/tmp") | ||
| assert pfunit.get_include_path() == Path("/tmp/include") | ||
|
|
||
|
|
||
| def test_pfunit_driver(monkeypatch, tmp_path: Path) -> None: | ||
| """ | ||
| Tests that pfunit reads the driver.F90 file: | ||
| """ | ||
|
|
||
| # Make sure the environment variable PFUNIT is defined: | ||
| monkeypatch.setenv("PFUNIT", str(tmp_path)) | ||
| include_path = tmp_path / "include" | ||
| include_path.mkdir() | ||
| (include_path / "driver.F90").write_text("DRIVER\n") | ||
|
|
||
| pfunit = PfUnit() | ||
| assert pfunit.get_driver_f90() == "DRIVER\n" | ||
|
|
||
|
|
||
| def test_pfunit_check_available(monkeypatch, | ||
| subproc_record: ExtendedRecorder) -> None: | ||
| """ | ||
| Tests availability functionality. | ||
| """ | ||
| monkeypatch.setenv("PFUNIT", "/tmp") | ||
| pfunit = PfUnit() | ||
| assert pfunit.check_available() | ||
| assert subproc_record.invocations() == [["/tmp/bin/funitproc", "-v"]] | ||
| assert subproc_record.extras() == [{'cwd': None, | ||
| 'env': None, | ||
| 'stdout': None, | ||
| 'stderr': None}] | ||
|
|
||
|
|
||
| def test_pfunit_check_unavailable(monkeypatch, | ||
| fake_process: FakeProcess) -> None: | ||
| """ | ||
| Tests availability failure. | ||
| """ | ||
| monkeypatch.setenv("PFUNIT", "/tmp") | ||
| fake_process.register(['/tmp/bin/funitproc', '-v'], | ||
| returncode=1, | ||
| stderr="Something went wrong.") | ||
| pfunit = PfUnit() | ||
| assert not pfunit.check_available() | ||
| assert call_list(fake_process) == [["/tmp/bin/funitproc", "-v"]] | ||
|
|
||
|
|
||
| def test_pfunit_process(monkeypatch, | ||
| tmp_path: Path, | ||
| subproc_record: ExtendedRecorder) -> None: | ||
| """ | ||
| Tests processing a file | ||
| """ | ||
| monkeypatch.setenv("PFUNIT", str(tmp_path)) | ||
| pfunit = PfUnit() | ||
| pfunit.process(pf_path=tmp_path / "file.pf", | ||
| f90_out_path=tmp_path / "file.f90") | ||
|
|
||
| assert subproc_record.invocations() \ | ||
| == [[str(tmp_path / "bin" / "funitproc"), | ||
| str(tmp_path / "file.pf"), | ||
| str(tmp_path / "file.f90")]] | ||
| assert subproc_record.extras() == [{'cwd': None, | ||
| 'env': None, | ||
| 'stderr': None, | ||
| 'stdout': None}] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In response to this documentation I say "no it doesn't." Looks like a copy-and-paste mistake.