Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for issue with attr: #3189

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 58 additions & 27 deletions setuptools/tests/config/test_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest

from distutils.errors import DistutilsOptionError
from setuptools.command.sdist import sdist
from setuptools.config import expand
from setuptools.discovery import find_package_path

Expand Down Expand Up @@ -56,34 +55,66 @@ def test_read_files(tmp_path, monkeypatch):
expand.read_files(["../a.txt"], tmp_path)


def test_read_attr(tmp_path, monkeypatch):
files = {
"pkg/__init__.py": "",
"pkg/sub/__init__.py": "VERSION = '0.1.1'",
"pkg/sub/mod.py": (
"VALUES = {'a': 0, 'b': {42}, 'c': (0, 1, 1)}\n"
"raise SystemExit(1)"
),
}
write_files(files, tmp_path)

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'
values = expand.read_attr('lib.mod.VALUES', {'lib': 'pkg/sub'})

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

# Make sure the same APIs work outside cwd
assert expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path) == '0.1.1'
values = expand.read_attr('lib.mod.VALUES', {'lib': 'pkg/sub'}, tmp_path)
assert values['c'] == (0, 1, 1)
class TestReadAttr:
def test_read_attr(self, tmp_path, monkeypatch):
files = {
"pkg/__init__.py": "",
"pkg/sub/__init__.py": "VERSION = '0.1.1'",
"pkg/sub/mod.py": (
"VALUES = {'a': 0, 'b': {42}, 'c': (0, 1, 1)}\n"
"raise SystemExit(1)"
),
}
write_files(files, tmp_path)

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'
values = expand.read_attr('lib.mod.VALUES', {'lib': 'pkg/sub'})

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

# Make sure the same APIs work outside cwd
assert expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path) == '0.1.1'
values = expand.read_attr('lib.mod.VALUES', {'lib': 'pkg/sub'}, tmp_path)
assert values['c'] == (0, 1, 1)

def test_import_order(self, tmp_path):
"""
Sometimes the import machinery will import the parent package of a nested
module, which triggers side-effects and might create problems (see issue #3176)

``read_attr`` should bypass these limitations by resolving modules statically
(via ast.literal_eval).
"""
files = {
"src/pkg/__init__.py": "from .main import func\nfrom .about import version",
"src/pkg/main.py": "import super_complicated_dep\ndef func(): return 42",
"src/pkg/about.py": "version = '42'",
}
write_files(files, tmp_path)
attr_desc = "pkg.about.version"
package_dir = {"": "src"}
# `import super_complicated_dep` should not run, otherwise the build fails
assert expand.read_attr(attr_desc, package_dir, tmp_path) == "42"


def test_resolve_class():
assert expand.resolve_class("setuptools.command.sdist.sdist") == sdist
@pytest.mark.parametrize(
'package_dir, file, module, return_value',
[
({"": "src"}, "src/pkg/main.py", "pkg.main", 42),
({"pkg": "lib"}, "lib/main.py", "pkg.main", 13),
({}, "single_module.py", "single_module", 70),
({}, "flat_layout/pkg.py", "flat_layout.pkg", 836),
]
)
def test_resolve_class(tmp_path, package_dir, file, module, return_value):
files = {file: f"class Custom:\n def testing(self): return {return_value}"}
write_files(files, tmp_path)
cls = expand.resolve_class(f"{module}.Custom", package_dir, tmp_path)
assert cls().testing() == return_value


@pytest.mark.parametrize(
Expand Down