Skip to content

Commit

Permalink
Add tests for static 'attr' directive
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Jan 8, 2025
1 parent cf576e2 commit 8b4c8a3
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions setuptools/tests/config/test_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

from setuptools._static import is_static
from setuptools.config import expand
from setuptools.discovery import find_package_path

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

assert version == '0.1.1'
assert is_static(values)

assert values['a'] == 0
assert values['b'] == {42}
assert is_static(values)

# Make sure the same APIs work outside cwd
assert expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path) == '0.1.1'
Expand All @@ -118,7 +123,28 @@ def test_read_annotated_attr(self, tmp_path, example):
}
write_files(files, tmp_path)
# Make sure this attribute can be read statically
assert expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path) == '0.1.1'
version = expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path)
assert version == '0.1.1'
assert is_static(version)

@pytest.mark.parametrize(
"example",
[
"VERSION = (lambda: '0.1.1')()\n",
"def fn(): return '0.1.1'\nVERSION = fn()\n",
"VERSION: str = (lambda: '0.1.1')()\n",
],
)
def test_read_dynamic_attr(self, tmp_path, monkeypatch, example):
files = {
"pkg/__init__.py": "",
"pkg/sub/__init__.py": example,
}
write_files(files, tmp_path)
monkeypatch.chdir(tmp_path)
version = expand.read_attr('pkg.sub.VERSION')
assert version == '0.1.1'
assert not is_static(version)

def test_import_order(self, tmp_path):
"""
Expand Down

0 comments on commit 8b4c8a3

Please sign in to comment.