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