[Distutils] test command for setup.py

Berthold Höllmann hoel@germanlloyd.org
Tue Sep 19 07:28:03 2000


--=-=-=
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

hoel@germanlloyd.org (Berthold H=F6llmann) writes:

> Hello,
>=20
> I'm thinking about providing a framework for pre-install testing for
> distutils enabled Python packages. I think about a new "test" command,
> which inserts the lib build path into PYTHONPATH and runs specified
> tests. Is anyone else working on something like this, and what are
> your opinions and requirements on this?

Attatched you find a first version of test.py. This copied into
../distutils/command allows

python setup.py test


First this command calls "build" to enshure a correctly build
disttribution.

It then modifies "sys.path" to point it to the build directory,
imports files from a subdirectory, and calls a function named "test"
in the imported module. The default name of the subdirectory is
"test". From this directory by default all modules named "test_*.py"
are imported.

Additional options are:

  - test_dir (test-dir): name of the directory containg the test files
                 (default: "test")

  - test_prefix (test-prefix): prefix for the test files. (default:
                 "test_")

  - test_suffixes (test-suffixes): suffixes used to generate filenames
                 for the tests (default: None, which means: look for
                 all files in test_dir named test_prefix<something>.py
                 and list all <somethings>s for test_suffixes).

I took build.py as a template for writing this.

Berthold

--=-=-=
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=test.py

"""distutils.command.test

Implements the Distutils 'test' command."""

# created 2000/09/18, Greg Ward

__revision__ = "$Id"

import sys, os
from distutils.core import Command
from distutils.util import get_platform


class test (Command):

    description = "test the distribution prior to install"

    user_options = [
        ('build-base=', 'b',
         "base directory for build library"),
        ('build-purelib=', None,
         "build directory for platform-neutral distributions"),
        ('build-platlib=', None,
         "build directory for platform-specific distributions"),
        ('build-lib=', None,
         "build directory for all distribution (defaults to either " +
         "build-purelib or build-platlib"),
        ('build-scripts=', None,
         "build directory for scripts"),
        ('test-dir=', None,
         "directory that contains the test definitions"),
        ('test-prefix=', None,
         "prefix to the testcase filename"),
        ('test-suffix=', None,
         "a list of suffixes used to generate names the of the testcases")
        ]

    def initialize_options (self):
        self.build_base = 'build'
        # these are decided only after 'build_base' has its final value
        # (unless overridden by the user or client)
        self.build_purelib = None
        self.build_platlib = None
        self.build_lib = None
        self.build_scripts = None
        self.test_dir = 'test'
        self.test_prefix = 'test_'
        self.test_suffixes = None
    def finalize_options (self):

        # Need this to name platform-specific directories, but sys.platform
        # is not enough -- it only names the OS and version, not the
        # hardware architecture!
        self.plat = get_platform ()

        # 'build_purelib' and 'build_platlib' just default to 'lib' and
        # 'lib.<plat>' under the base build directory.  We only use one of
        # them for a given distribution, though --
        if self.build_purelib is None:
            self.build_purelib = os.path.join (self.build_base, 'lib')
        if self.build_platlib is None:
            self.build_platlib = os.path.join (self.build_base,
                                               'lib.' + self.plat)

        # 'build_lib' is the actual directory that we will use for this
        # particular module distribution -- if user didn't supply it, pick
        # one of 'build_purelib' or 'build_platlib'.
        if self.build_lib is None:
            if self.distribution.ext_modules:
                self.build_lib = self.build_platlib
            else:
                self.build_lib = self.build_purelib

        if self.build_scripts is None:
            self.build_scripts = os.path.join (self.build_base, 'scripts')

        if self.test_suffixes is None:
            self.test_suffixes = []
            pref_len = len(self.test_prefix)
            for file in os.listdir(self.test_dir):
                if (file[-3:] == ".py" and
                    file[:pref_len]==self.test_prefix):
                    self.test_suffixes.append(file[pref_len:-3])
    # finalize_options ()


    def run (self):

        # Invoke the 'build' command to "build" pure Python modules
        # (ie. copy 'em into the build tree)
        self.run_command ('build')

        # remember old sys.path to restore it afterwards
        old_path = sys.path[:]

        # extend sys.path
        sys.path.insert(0, self.build_purelib)
        sys.path.insert(0, self.build_platlib)
        sys.path.insert(0, self.test_dir)

        # build include path for test
        for case in self.test_suffixes:
            TEST = __import__(self.test_prefix+case,
                              globals(), locals(),
                              [''])
            TEST.test()

        # restore sys.path
        sys.path = old_path[:]


# class test

--=-=-=


-- 
       email: hoel@GermanLloyd.org
   )   tel. : +49 (40) 3 61 49 - 73 74
  (
C[_]  These opinions might be mine, but never those of my employer.

--=-=-=--