Skip to content

Commit 8b4c8a3

Browse files
committed
Add tests for static 'attr' directive
1 parent cf576e2 commit 8b4c8a3

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

setuptools/tests/config/test_expand.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from setuptools._static import is_static
78
from setuptools.config import expand
89
from setuptools.discovery import find_package_path
910

@@ -93,11 +94,15 @@ def test_read_attr(self, tmp_path, monkeypatch):
9394
with monkeypatch.context() as m:
9495
m.chdir(tmp_path)
9596
# Make sure it can read the attr statically without evaluating the module
96-
assert expand.read_attr('pkg.sub.VERSION') == '0.1.1'
97+
version = expand.read_attr('pkg.sub.VERSION')
9798
values = expand.read_attr('lib.mod.VALUES', {'lib': 'pkg/sub'})
9899

100+
assert version == '0.1.1'
101+
assert is_static(values)
102+
99103
assert values['a'] == 0
100104
assert values['b'] == {42}
105+
assert is_static(values)
101106

102107
# Make sure the same APIs work outside cwd
103108
assert expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path) == '0.1.1'
@@ -118,7 +123,28 @@ def test_read_annotated_attr(self, tmp_path, example):
118123
}
119124
write_files(files, tmp_path)
120125
# Make sure this attribute can be read statically
121-
assert expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path) == '0.1.1'
126+
version = expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path)
127+
assert version == '0.1.1'
128+
assert is_static(version)
129+
130+
@pytest.mark.parametrize(
131+
"example",
132+
[
133+
"VERSION = (lambda: '0.1.1')()\n",
134+
"def fn(): return '0.1.1'\nVERSION = fn()\n",
135+
"VERSION: str = (lambda: '0.1.1')()\n",
136+
],
137+
)
138+
def test_read_dynamic_attr(self, tmp_path, monkeypatch, example):
139+
files = {
140+
"pkg/__init__.py": "",
141+
"pkg/sub/__init__.py": example,
142+
}
143+
write_files(files, tmp_path)
144+
monkeypatch.chdir(tmp_path)
145+
version = expand.read_attr('pkg.sub.VERSION')
146+
assert version == '0.1.1'
147+
assert not is_static(version)
122148

123149
def test_import_order(self, tmp_path):
124150
"""

0 commit comments

Comments
 (0)