Debian Codegraph @

extension_helpers/tests/test_utils.py

raw
1 import os
2 import time
3
4 import pytest
5
6 from .._utils import (
7 abi_to_versions,
8 get_limited_api_option,
9 import_file,
10 write_if_different,
11 )
12
13
14 @pytest.mark.parametrize("path_type", ("str", "path"))
15 def test_import_file(tmp_path, path_type):
16 filepath = tmp_path / "spam.py"
17 if path_type == "str":
18 filepath = str(filepath)
19 with open(filepath, "w") as f:
20 f.write("magic = 12345")
21 module = import_file(filepath)
22 assert module.magic == 12345
23
24
25 @pytest.mark.parametrize("path_type", ("str", "path"))
26 def test_write_if_different(tmp_path, path_type):
27 filepath = tmp_path / "test.txt"
28 if path_type == "str":
29 filepath = str(filepath)
30 write_if_different(filepath, b"abc")
31 time1 = os.path.getmtime(filepath)
32 time.sleep(0.01)
33 write_if_different(filepath, b"abc")
34 time2 = os.path.getmtime(filepath)
35 assert time2 == time1
36 time.sleep(0.01)
37 write_if_different(filepath, b"abcd")
38 time3 = os.path.getmtime(filepath)
39 assert time3 > time1
40
41
42 class TestGetLimitedAPIOption:
43
44 def test_nofiles(self, tmp_path):
45 assert get_limited_api_option(tmp_path) is None
46
47 def test_empty_setup_cfg(self, tmp_path):
48 (tmp_path / "setup.cfg").write_text("")
49 assert get_limited_api_option(tmp_path) is None
50
51 def test_empty_pyproject_toml(self, tmp_path):
52 (tmp_path / "pyproject.toml").write_text("")
53 assert get_limited_api_option(tmp_path) is None
54
55 def test_setup_cfg(self, tmp_path):
56
57 (tmp_path / "setup.cfg").write_text("[bdist_wheel]\npy_limited_api=cp311")
58 assert get_limited_api_option(tmp_path) == "cp311"
59
60 # Make sure things still work even if an empty pyproject.toml file is present
61
62 (tmp_path / "pyproject.toml").write_text("")
63 assert get_limited_api_option(tmp_path) == "cp311"
64
65 # And if the pyproject.toml has the right section but not the right option
66
67 (tmp_path / "setup.cfg.toml").write_text("[tool.distutils.bdist_wheel]\nspam=1\n")
68 assert get_limited_api_option(tmp_path) == "cp311"
69
70 def test_pyproject(self, tmp_path):
71
72 (tmp_path / "pyproject.toml").write_text(
73 '[tool.distutils.bdist_wheel]\npy-limited-api="cp312"\n'
74 )
75 assert get_limited_api_option(tmp_path) == "cp312"
76
77 # Make sure things still work even if an empty setup.cfg file is present
78
79 (tmp_path / "setup.cfg.toml").write_text("\n")
80 assert get_limited_api_option(tmp_path) == "cp312"
81
82 # And if the setup.cfg has the right section but not the right option
83
84 (tmp_path / "setup.cfg.toml").write_text("[bdist_wheel]\nspam=1\n")
85 assert get_limited_api_option(tmp_path) == "cp312"
86
87
88 def test_abi_to_versions_invalid():
89 assert abi_to_versions("spam") == (None, None)
90
91
92 def test_abi_to_versions_valid():
93 assert abi_to_versions("cp39") == ((3, 9), "0x03090000")
94 assert abi_to_versions("cp310") == ((3, 10), "0x030A0000")
95 assert abi_to_versions("cp311") == ((3, 11), "0x030B0000")
96 assert abi_to_versions("cp312") == ((3, 12), "0x030C0000")
97 assert abi_to_versions("cp313") == ((3, 13), "0x030D0000")