-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(readme): add test for the example in the readme
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,45 @@ | ||
# | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
# Copyright (c) 2024, QUEENS contributors. | ||
# | ||
# This file is part of QUEENS. | ||
# | ||
# QUEENS is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
# Lesser General Public License as published by the Free Software Foundation, either version 3 of | ||
# the License, or (at your option) any later version. QUEENS is distributed in the hope that it will | ||
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You | ||
# should have received a copy of the GNU Lesser General Public License along with QUEENS. If not, | ||
# see <https://www.gnu.org/licenses/>. | ||
# | ||
"""Extract QUEENS example from the readme.""" | ||
|
||
from pathlib import Path | ||
|
||
EXAMPLE_MARKER = "<!---example marker, do not remove this comment-->" | ||
|
||
|
||
def get_queens_example_from_readme(output_dir): | ||
"""Extract the example from the readme. | ||
Args: | ||
output_dir (str): Output directory for the QUEENS run. | ||
""" | ||
readme_path = Path(__file__).parents[1] / "README.md" | ||
|
||
# Split the example in the readme using the marker | ||
text = readme_path.read_text().split(EXAMPLE_MARKER) | ||
|
||
# Only one example should appear | ||
if len(text) != 3: | ||
raise ValueError("Could not extract the example from the readme!") | ||
|
||
# Extract the source | ||
example_source = ( | ||
text[1] | ||
.replace("```python", "") | ||
.replace("```", "") | ||
.replace('output_dir="."', f'output_dir="{output_dir}"') | ||
) | ||
|
||
return example_source |
This file contains 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,32 @@ | ||
# | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
# Copyright (c) 2024, QUEENS contributors. | ||
# | ||
# This file is part of QUEENS. | ||
# | ||
# QUEENS is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
# Lesser General Public License as published by the Free Software Foundation, either version 3 of | ||
# the License, or (at your option) any later version. QUEENS is distributed in the hope that it will | ||
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You | ||
# should have received a copy of the GNU Lesser General Public License along with QUEENS. If not, | ||
# see <https://www.gnu.org/licenses/>. | ||
# | ||
"""Test the readme QUEENS example.""" | ||
|
||
from queens.utils.run_subprocess import run_subprocess | ||
from test_utils.get_example_in_read_me import get_queens_example_from_readme | ||
|
||
|
||
def test_queens_readme_example(tmp_path): | ||
"""Test if the example in the readme runs.""" | ||
# Get the source of the example | ||
example_source = get_queens_example_from_readme(tmp_path) | ||
|
||
# Run the script | ||
process_returncode, _, _, _ = run_subprocess( | ||
f"python -c '{example_source}'", raise_error_on_subprocess_failure=False | ||
) | ||
|
||
# Check for an exit code | ||
assert not process_returncode |