|
| 1 | +############################################################################### |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2026 Advanced Micro Devices, Inc. |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | +# |
| 25 | +############################################################################### |
| 26 | + |
| 27 | +import importlib |
| 28 | +import re |
| 29 | +from pathlib import Path |
| 30 | +from unittest.mock import patch |
| 31 | + |
| 32 | +import pytest |
| 33 | + |
| 34 | + |
| 35 | +def _get_project_name_from_pyproject() -> str: |
| 36 | + repo_root = Path(__file__).resolve().parent.parent.parent |
| 37 | + pyproject = repo_root / "pyproject.toml" |
| 38 | + content = pyproject.read_text() |
| 39 | + in_project = False |
| 40 | + for line in content.splitlines(): |
| 41 | + if line.strip() == "[project]": |
| 42 | + in_project = True |
| 43 | + continue |
| 44 | + if in_project and line.strip().startswith("["): |
| 45 | + break |
| 46 | + if in_project: |
| 47 | + match = re.match(r'^\s*name\s*=\s*["\']([^"\']+)["\']', line) |
| 48 | + if match: |
| 49 | + return match.group(1) |
| 50 | + pytest.fail("Could not find name in [project] section of pyproject.toml") |
| 51 | + |
| 52 | + |
| 53 | +def test_version_resolved_using_package_name_from_pyproject(): |
| 54 | + expected_name = _get_project_name_from_pyproject() |
| 55 | + with patch("importlib.metadata.version", return_value="1.2.3") as mock_version: |
| 56 | + import nodescraper |
| 57 | + |
| 58 | + importlib.reload(nodescraper) |
| 59 | + assert nodescraper.__version__ == "1.2.3" |
| 60 | + mock_version.assert_called_once_with( |
| 61 | + expected_name |
| 62 | + ), "nodescraper.__init__ must call version() with the same name as pyproject.toml [project] name" |
| 63 | + |
| 64 | + |
| 65 | +def test_version_unknown_when_package_not_found(): |
| 66 | + from importlib.metadata import PackageNotFoundError |
| 67 | + |
| 68 | + expected_name = _get_project_name_from_pyproject() |
| 69 | + with patch("importlib.metadata.version", side_effect=PackageNotFoundError(expected_name)): |
| 70 | + import nodescraper |
| 71 | + |
| 72 | + importlib.reload(nodescraper) |
| 73 | + assert nodescraper.__version__ == "unknown" |
0 commit comments