Commits

Dan Buch committed f325a4e

Filling in more actual testing in the smoke test, touching up setup script along the way

Comments (0)

Files changed (3)

-.*\.pyc
+.*\.(pyc|log)
 ^build$
 ^dist$
 ^MANIFEST.*$

devscripts/smoketest.py

 import logging
 import os
 import sys
+import time
 import unittest
 
-from os.path import dirname, abspath, join as pathjoin
-from subprocess import check_call, PIPE, CalledProcessError
+from os import mkdir
+from os.path import dirname, abspath, isdir, join as pathjoin
+from shutil import rmtree, copytree
+from subprocess import check_call, Popen, PIPE, CalledProcessError
+from tempfile import mkdtemp
 
 
 TOP = dirname(dirname(abspath(__file__)))
-sys.path.insert(0, TOP)
-os.environ['PYTHONPATH'] = \
-    os.environ.get('PYTHONPATH', '') + ':{0}'.format(TOP)
-
-REINDENT_SCRIPT = pathjoin(TOP, 'scripts', 'reindent')
-INPUTS_DIR = pathjoin(TOP, 'devscripts', 'inputs')
+ORIG_SYSPATH = sys.path[:]
+ORIG_PYTHONPATH = os.environ.get('PYTHONPATH', '')
+ORIG_PATH = os.environ.get('PATH', '')
+ORIG_CWD = os.getcwd()
 _LOGSETUP = {'done': False}
 
 
     if _LOGSETUP['done']:
         return
 
-    logging.basicConfig(file='./smoketest.log')
+    logging.basicConfig(filename=pathjoin(ORIG_CWD, 'reindent-smoketest.log'),
+                        level=logging.DEBUG)
     _LOGSETUP['done'] = True
 
 
 class TestReindentSystemVersion(unittest.TestCase):
     _version = ''
+    _tempdir = ''
 
     def setUp(self):
         if not self._has_version():
             raise Exception('Cannot test version {0}'.format(self._version))
-        self.log = logging.getLogger('smoketest')
+        self.log = logging.getLogger()
+
+        self.log.info('Setting up for `{0}`'.format(self._exe))
+        self._set_up_tempdir()
+        os.chdir(self._code_root)
+        self._install_in_tempdir()
+        os.chdir(self._tempdir)
+
+        sys.path.insert(0, self._install_lib)
+        os.environ['PYTHONPATH'] = \
+            ORIG_PYTHONPATH + ':{0}'.format(self._install_lib)
+        os.environ['PATH'] = ORIG_PATH + ':{0}'.format(self._install_scripts)
 
     def tearDown(self):
+        self.log.info('Tearing down up for `{0}`'.format(self._exe))
         self._clean_up_tempdir()
 
+        sys.path[:] = ORIG_SYSPATH
+        os.environ['PYTHONPATH'] = ORIG_PYTHONPATH
+        os.environ['PATH'] = ORIG_PATH
+
+    def _set_up_tempdir(self):
+        self._tempdir = mkdtemp(
+            prefix='reindent-smoketest-{0}-{1}-'.format(
+                self._version, time.time()
+            )
+        )
+        if not isdir(self._code_root):
+            copytree(TOP, self._code_root)
+        if not isdir(self._install_root):
+            mkdir(self._install_root)
+            mkdir(self._install_lib)
+            mkdir(self._install_scripts)
+
+    def _install_in_tempdir(self):
+        try:
+            check_call([
+                    self._exe, self._setup_script, 'install',
+                    '--install-lib', self._install_lib,
+                    '--install-scripts', self._install_scripts,
+                ], stdout=PIPE, stderr=PIPE
+            )
+        except CalledProcessError:
+            self.log.exception('Failed to install')
+            self.assertTrue(False, 'Can install')
+
     def _clean_up_tempdir(self):
-        pass
+        rmtree(self._tempdir)
 
     @property
     def _exe(self):
         return 'python{0}'.format(self._version)
 
+    @property
+    def _setup_script(self):
+        return pathjoin(self._code_root, 'setup.py')
+
+    @property
+    def _reindent_script(self):
+        return pathjoin(self._install_scripts, 'reindent')
+
+    @property
+    def _inputs_dir(self):
+        return pathjoin(self._code_root, 'devscripts', 'inputs')
+
+    @property
+    def _code_root(self):
+        return pathjoin(self._tempdir, 'code-root')
+
+    @property
+    def _install_root(self):
+        return pathjoin(self._tempdir, 'install-root')
+
+    @property
+    def _install_lib(self):
+        return pathjoin(self._install_root, 'lib')
+
+    @property
+    def _install_scripts(self):
+        return pathjoin(self._install_root, 'bin')
+
     def _has_version(self):
         try:
             check_call([self._exe, '-c', 'import sys'],
 
     def test_version_can_execute_script(self):
         try:
-            check_call([self._exe, REINDENT_SCRIPT, '-h'],
-                       stdout=PIPE, stderr=PIPE)
+            command = [self._exe, self._reindent_script, '-h']
+            self.log.info('Executing %s', ' '.join(command))
+            check_call(command, stdout=PIPE, stderr=PIPE)
         except CalledProcessError:
             self.log.exception('Failed to execute script')
             self.assertTrue(False, 'Can execute script')
 
-    def test_version_can_reindent_twospaces(self):
-        pass
+    def test_is_importing_correct_library(self):
+        try:
+            job = Popen([self._exe, '-c',
+                'import reindent, sys;' +
+                'sys.stdout.write(reindent.__file__.replace(".pyc", ".py"))'],
+                stdout=PIPE, stderr=PIPE
+            )
+            expected_lib = pathjoin(self._install_lib, 'reindent', '__init__.py')
+            self.log.debug('Expected library path is %s', expected_lib)
+            out = job.communicate()[0]
+            self.assertEqual(expected_lib, out.strip(), 'Is importing correct library')
+        except CalledProcessError:
+            self.log.exception('Failed to import library')
+            self.assertTrue(False, 'Can import library')
 
-    def test_version_can_reindent_tabs(self):
-        pass
+    #def test_version_can_reindent_twospaces(self):
+        #pass
 
-    def test_version_can_reindent_non_python(self):
-        pass
+    #def test_version_can_reindent_tabs(self):
+        #pass
 
-    def test_version_can_reindent_hot_mess(self):
-        pass
+    #def test_version_can_reindent_non_python(self):
+        #pass
+
+    #def test_version_can_reindent_hot_mess(self):
+        #pass
 
 
 class TestReindentVersion23(TestReindentSystemVersion):
 from distutils.core import setup
 
 
+SETUP_ARGS = dict(
+    name='Reindent',
+    version='0.2.0',
+    author='Tim Peters',
+    author_email='nottimsemail@notadomain.foo',
+    scripts=['./scripts/reindent'],
+    packages=[
+        'reindent',
+        'reindent.v23',
+        'reindent.v24',
+        'reindent.v25',
+        'reindent.v26',
+        'reindent.v27',
+        'reindent.v30',
+        'reindent.v31',
+        'reindent.v32',
+    ],
+    maintainer='Dan Buch',
+    maintainer_email='dan@meatballhat.com',
+    description='reindent script by Tim Peters',
+    keywords=['reindent', 'pep8', 'syntax', 'lint', 'tab', 'space'],
+    classifiers=[
+        'Development Status :: 6 - Mature',
+        'Environment :: Console',
+        'Intended Audience :: Developers',
+        'License :: Public Domain',
+        'Natural Language :: English',
+        'Programming Language :: Python',
+        'Topic :: Software Development :: Quality Assurance',
+    ],
+    long_description='<replaced-at-runtime>',
+    platforms=['any'],
+    license='Public Domain'
+)
+
+
 def main():
-    setup(
-        name='Reindent',
-        version='0.2.0',
-        author="Tim Peters",
-        author_email='nottimsemail@notadomain.foo',
-        scripts=['./scripts/reindent'],
-        packages=['reindent'],
-        maintainer="Dan Buch",
-        maintainer_email="dan@meatballhat.com",
-        description='reindent script by Tim Peters',
-        keywords=['reindent', 'pep8', 'syntax', 'lint', 'tab', 'space'],
-        classifiers=[
-            "Development Status :: 6 - Mature",
-            "Environment :: Console",
-            "Intended Audience :: Developers",
-            "License :: Public Domain",
-            "Natural Language :: English",
-            "Programming Language :: Python",
-            "Topic :: Software Development :: Quality Assurance",
-        ],
-        long_description=open('README.txt').read(),
-        platforms=['any'],
-        license="Public Domain"
-    )
-
+    SETUP_ARGS['long_description'] = open('README.txt').read()
+    setup(**SETUP_ARGS)
     return 0