Skip to content

Commit

Permalink
Test: Fix namespace tests to avoid modifying source files.
Browse files Browse the repository at this point in the history
Update to use pytest framework and a higher-level subprocess API.

This should fix random Windows test failures, due to parallel execution of these tests in which the same files were modified by multiple parallel tests.
  • Loading branch information
bjones1 committed Aug 19, 2018
1 parent f3628fa commit e4ab403
Showing 1 changed file with 57 additions and 84 deletions.
141 changes: 57 additions & 84 deletions tests/unit/test_modulegraph/test_setuptools_nspkg.py
Expand Up @@ -4,48 +4,43 @@
flavour used by pip
"""
import os
import shutil
import sys
import subprocess
import unittest
import textwrap
import shutil

import pytest

from PyInstaller.lib.modulegraph import modulegraph

gRootDir = os.path.dirname(os.path.abspath(__file__))
gSrcDir = os.path.join(gRootDir, 'testpkg-setuptools-namespace')

def install_testpkg(test_dir):
p = subprocess.Popen([
sys.executable, 'setup.py', 'install',
'--install-lib', test_dir,
'--single-version-externally-managed',
'--record', os.path.join(test_dir, 'record.lst'),
], cwd=gSrcDir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

data = p.communicate()[0]
@pytest.fixture
def install_testpkg(tmpdir):
# Copy the package to ``dest_dir``, so that the build won't modify anything in ``gSrcDir``.
dest_dir = str(tmpdir / 'data')
shutil.copytree(gSrcDir, dest_dir)

exit = p.wait()
return exit
# A directory to place the resulting built library in.
libdir = str(tmpdir / 'test')

# Perform the build.
subprocess.check_call([
sys.executable, 'setup.py', 'install',
'--install-lib', libdir,
'--single-version-externally-managed',
'--record', os.path.join(libdir, 'record.lst'),
], cwd=dest_dir)

class TestPythonBehaviour (unittest.TestCase):
def setUp(self):
test_dir = os.path.join(gRootDir, 'test.dir')
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
return libdir

os.mkdir(test_dir)
exit = install_testpkg(test_dir)
self.assertEqual(exit, 0)

def tearDown(self):
test_dir = os.path.join(gRootDir, 'test.dir')
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
class TestPythonBehaviour(object):

def importModule(self, name):
test_dir = os.path.join(gRootDir, 'test.dir')
def importModule(self, name, libdir):
if '.' in name:
script = textwrap.dedent("""\
import site
Expand All @@ -55,92 +50,70 @@ def importModule(self, name):
except ImportError:
import %s
print (%s.__name__)
""") %(test_dir, name, name.rsplit('.', 1)[0], name)
""") % (str(libdir), name, name.rsplit('.', 1)[0], name)
else:
script = textwrap.dedent("""\
import site
site.addsitedir(%r)
import %s
print (%s.__name__)
""") %(test_dir, name, name)

p = subprocess.Popen([sys.executable, '-c', script],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'testpkg-relimport'),
""") % (str(libdir), name, name)

data = subprocess.check_output(
[sys.executable, '-c', script],
stderr=subprocess.STDOUT,
cwd=os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'testpkg-relimport'),
)
data = p.communicate()[0]
if sys.version_info[0] != 2:
data = data.decode('UTF-8')
data = data.strip()
if data.endswith(' refs]'):
data = data.rsplit('\n', 1)[0].strip()

sts = p.wait()

if sts != 0:
print (data)
self.fail("import of %r failed"%(name,))

return data

def testToplevel(self):
m = self.importModule('nspkg.module')
self.assertEqual(m, 'nspkg.module')

def testSub(self):
m = self.importModule('nspkg.nssubpkg.sub')
self.assertEqual(m, 'nspkg.nssubpkg.sub')

class TestModuleGraphImport (unittest.TestCase):
if not hasattr(unittest.TestCase, 'assertIsInstance'):
def assertIsInstance(self, value, types):
if not isinstance(value, types):
self.fail("%r is not an instance of %r", value, types)
def testToplevel(self, install_testpkg):
m = self.importModule('nspkg.module', install_testpkg)
assert m == 'nspkg.module'

def setUp(self):
test_dir = os.path.join(gRootDir, 'test.dir')
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
def testSub(self, install_testpkg):
m = self.importModule('nspkg.nssubpkg.sub', install_testpkg)
assert m == 'nspkg.nssubpkg.sub'

os.mkdir(test_dir)
exit = install_testpkg(test_dir)
self.assertEqual(exit, 0)

self.mf = modulegraph.ModuleGraph(path=[ test_dir ] + sys.path)
@pytest.fixture
def install_testpkg_modulegraph(install_testpkg):
return modulegraph.ModuleGraph(path=[str(install_testpkg)] + sys.path)

def tearDown(self):
test_dir = os.path.join(gRootDir, 'test.dir')
if os.path.exists(test_dir):
shutil.rmtree(test_dir)

def testRootPkg(self):
self.mf.import_hook('nspkg')
class TestModuleGraphImport(object):

node = self.mf.findNode('nspkg')
self.assertIsInstance(node, modulegraph.NamespacePackage)
self.assertEqual(node.identifier, 'nspkg')
self.assertEqual(node.filename, '-')
def testRootPkg(self, install_testpkg_modulegraph):
install_testpkg_modulegraph.import_hook('nspkg')

def testRootPkgModule(self):
self.mf.import_hook('nspkg.module')
node = install_testpkg_modulegraph.findNode('nspkg')
assert isinstance(node, modulegraph.NamespacePackage)
assert node.identifier == 'nspkg'
assert node.filename == '-'

node = self.mf.findNode('nspkg.module')
self.assertIsInstance(node, modulegraph.SourceModule)
self.assertEqual(node.identifier, 'nspkg.module')
def testRootPkgModule(self, install_testpkg_modulegraph):
install_testpkg_modulegraph.import_hook('nspkg.module')

def testSubRootPkgModule(self):
self.mf.import_hook('nspkg.nssubpkg.sub')
node = install_testpkg_modulegraph.findNode('nspkg.module')
assert isinstance(node, modulegraph.SourceModule)
assert node.identifier == 'nspkg.module'

node = self.mf.findNode('nspkg.nssubpkg.sub')
self.assertIsInstance(node, modulegraph.SourceModule)
self.assertEqual(node.identifier, 'nspkg.nssubpkg.sub')
def testSubRootPkgModule(self, install_testpkg_modulegraph):
install_testpkg_modulegraph.import_hook('nspkg.nssubpkg.sub')

node = install_testpkg_modulegraph.findNode('nspkg.nssubpkg.sub')
assert isinstance(node, modulegraph.SourceModule)
assert node.identifier == 'nspkg.nssubpkg.sub'

node = self.mf.findNode('nspkg')
self.assertIsInstance(node, modulegraph.NamespacePackage)
node = install_testpkg_modulegraph.findNode('nspkg')
assert isinstance(node, modulegraph.NamespacePackage)


if __name__ == "__main__":
Expand Down

0 comments on commit e4ab403

Please sign in to comment.