Skip to content

Commit 98fe24c

Browse files
committed
Mock the actual setup provider defined in setup.py
Currently `setup` is always mocked using `distutils.core` but this might cause issues with certain packages. Fix this behavior by parsing the `setup.py` file for the correct module to import. Closes: #116 Signed-off-by: Bennati, Stefano <stefano.bennati@here.com>
1 parent 39588f1 commit 98fe24c

3 files changed

Lines changed: 71 additions & 6 deletions

File tree

src/python_inspector/setup_py_live_eval.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import ConfigParser as configparser
2020

2121
import mock
22-
import setuptools
2322
from commoncode.command import pushd
2423
from packvers.requirements import Requirement
2524

@@ -54,11 +53,24 @@ def iter_requirements(level, extras, setup_file):
5453
setup_requires = {}
5554
# change directory to setup.py path
5655
with pushd(os.path.dirname(setup_file)):
57-
with mock.patch.object(setuptools, "setup") as mock_setup:
58-
sys.path.append(os.path.dirname(setup_file))
59-
g = {"__file__": setup_file, "__name__": "__main__"}
60-
with open(setup_file) as sf:
61-
exec(sf.read(), g)
56+
with open(setup_file) as sf:
57+
file_contents = sf.read()
58+
setup_provider = re.findall(r"from ([a-z._]+) import setup", file_contents)
59+
if len(setup_provider) == 1:
60+
setup_provider = setup_provider[0]
61+
else:
62+
setup_provider = ""
63+
if not ((setup_provider == "distutils.core") or (setup_provider == "setuptools")):
64+
print(
65+
f"Warning: unable to recognize 'import {setup_provider}' in {setup_file}: "
66+
"defaulting to 'distutils.core'."
67+
)
68+
setup_provider = "distutils.core"
69+
exec(f"import {setup_provider}")
70+
with mock.patch.object(eval(setup_provider), "setup") as mock_setup:
71+
sys.path.append(os.path.dirname(setup_file))
72+
g = {"__file__": setup_file, "__name__": "__main__"}
73+
exec(file_contents, g)
6274
sys.path.pop()
6375
# removing the assertion `assert g["setup"]`` since this is not true for all cases
6476
# for example when setuptools.setup() is called instead of setup()

tests/data/setup-distutils.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Copyright 2018 Matthew Aynalem
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
from distutils.core import setup
17+
from setuptools import find_packages
18+
19+
setup(
20+
name='packer.py',
21+
version='0.3.0',
22+
author='Matthew Aynalem',
23+
author_email='maynalem@gmail.com',
24+
packages=['packerpy'],
25+
url='https://github.com/mayn/packer.py',
26+
license='Apache License 2.0',
27+
description='packer.py - python library to run hashicorp packer CLI commands',
28+
keywords="hashicorp packer",
29+
install_requires=[
30+
],
31+
classifiers=[
32+
'License :: OSI Approved :: Apache Software License',
33+
'Programming Language :: Python :: 2',
34+
'Programming Language :: Python :: 2.7',
35+
'Programming Language :: Python :: 3',
36+
'Programming Language :: Python :: 3.4',
37+
'Programming Language :: Python :: 3.5',
38+
'Programming Language :: Python :: 3.6',
39+
],
40+
)

tests/test_setup_py_live_eval.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
REQ = abspath(join(dirname(__file__), "./data/requirements.devel.txt"))
1919
SETUP = abspath(join(dirname(__file__), "./data/setup.txt"))
20+
SETUP_DISTUTILS = abspath(join(dirname(__file__), "./data/setup-distutils.txt"))
2021

2122

2223
def test_iter_requirements_with_setup_py():
@@ -29,3 +30,15 @@ def test_iter_requirements_with_setup_py():
2930

3031
# Dev
3132
assert list(iter_requirements("dev", [], SETUP)) == ["click>=6.1.0", "mock>=1.3.0"]
33+
34+
35+
def test_iter_requirements_with_setup_py_distutils():
36+
"""Test against setup.py files which import distutils"""
37+
# Min
38+
assert list(iter_requirements("min", [], SETUP_DISTUTILS)) == []
39+
40+
# PyPI
41+
assert list(iter_requirements("pypi", [], SETUP_DISTUTILS)) == []
42+
43+
# Dev
44+
assert list(iter_requirements("dev", [], SETUP_DISTUTILS)) == []

0 commit comments

Comments
 (0)