|
| 1 | +import unittest |
| 2 | +import tempfile |
| 3 | +import pathlib |
| 4 | +from pyperformance import _pyproject_toml |
| 5 | + |
| 6 | + |
| 7 | +class TestPyProjectToml(unittest.TestCase): |
| 8 | + def test_parse_project_with_readme_string(self): |
| 9 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 10 | + tmp_path = pathlib.Path(tmpdir) |
| 11 | + toml_content = """ |
| 12 | + [project] |
| 13 | + name = "my-test-bench" |
| 14 | + version = "1.0" |
| 15 | + readme = "README.md" |
| 16 | + """ |
| 17 | + (tmp_path / "README.md").touch() |
| 18 | + |
| 19 | + data = _pyproject_toml.parse_pyproject_toml( |
| 20 | + toml_content, rootdir=str(tmp_path), requirefiles=True |
| 21 | + ) |
| 22 | + self.assertEqual(data["project"]["readme"]["file"], "README.md") |
| 23 | + |
| 24 | + def test_parse_full_valid_project_section(self): |
| 25 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 26 | + toml_content = """ |
| 27 | + [project] |
| 28 | + name = "my-full-bench" |
| 29 | + version = "2.1.3" |
| 30 | + dependencies = [ |
| 31 | + "pyperf", |
| 32 | + "six", |
| 33 | + ] |
| 34 | + requires-python = ">=3.12" |
| 35 | + """ |
| 36 | + data = _pyproject_toml.parse_pyproject_toml( |
| 37 | + toml_content, rootdir=str(tmpdir) |
| 38 | + ) |
| 39 | + project = data["project"] |
| 40 | + self.assertEqual(project["name"], "my-full-bench") |
| 41 | + self.assertEqual(project["version"], "2.1.3") |
| 42 | + self.assertEqual(project["dependencies"], ["pyperf", "six"]) |
| 43 | + self.assertEqual(project["requires-python"], ">=3.12") |
| 44 | + |
| 45 | + def test_parse_fails_on_missing_name(self): |
| 46 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 47 | + toml_content = """ |
| 48 | + [project] |
| 49 | + version = "1.0" |
| 50 | + """ |
| 51 | + |
| 52 | + with self.assertRaisesRegex(ValueError, r'missing required "name" field'): |
| 53 | + _pyproject_toml.parse_pyproject_toml(toml_content, rootdir=str(tmpdir)) |
| 54 | + |
| 55 | + def test_parse_fails_on_unsupported_section(self): |
| 56 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 57 | + toml_content = """ |
| 58 | + [project] |
| 59 | + name = "my-test-bench" |
| 60 | + version = "1.0" |
| 61 | +
|
| 62 | + [foobar] |
| 63 | + key = "value" |
| 64 | + """ |
| 65 | + |
| 66 | + with self.assertRaisesRegex(ValueError, "unsupported sections"): |
| 67 | + _pyproject_toml.parse_pyproject_toml(toml_content, rootdir=str(tmpdir)) |
| 68 | + |
| 69 | + def test_parse_readme_file_missing_with_requirefiles_true(self): |
| 70 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 71 | + toml_content = """ |
| 72 | + [project] |
| 73 | + name = "my-test-bench" |
| 74 | + version = "1.0" |
| 75 | + readme = "MISSING_README.md" |
| 76 | + """ |
| 77 | + |
| 78 | + with self.assertRaisesRegex(ValueError, "does not exist"): |
| 79 | + _pyproject_toml.parse_pyproject_toml( |
| 80 | + toml_content, rootdir=str(tmpdir), requirefiles=True |
| 81 | + ) |
| 82 | + |
| 83 | + def test_parse_readme_file_missing_with_requirefiles_false(self): |
| 84 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 85 | + toml_content = """ |
| 86 | + [project] |
| 87 | + name = "my-test-bench" |
| 88 | + version = "1.0" |
| 89 | + readme = "MISSING_README.md" |
| 90 | + """ |
| 91 | + |
| 92 | + data = _pyproject_toml.parse_pyproject_toml( |
| 93 | + toml_content, rootdir=str(tmpdir), requirefiles=False |
| 94 | + ) |
| 95 | + self.assertEqual(data["project"]["readme"]["file"], "MISSING_README.md") |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + unittest.main() |
0 commit comments