[Distutils] test command for setup.py

Thomas Heller thomas.heller@ion-tof.com
Tue Sep 26 14:55:02 2000


> "Thomas Heller" <thomas.heller@ion-tof.com> writes:
>
> > I like the simplicity of your test command,
> > however I would like (and have partly implmented)
> > the following changes:
> > - don't use so many options, the build directories could
> > be retrieved from the 'build' command
>
> I would, if I could figure out how. )-:
This is what I currently have in one of my setup scripts:

class test (Command):
    # Original version of this class posted
    # by Berthold Höllmann to distutils-sig@python.org
    description = "test the distribution prior to install"

    user_options = [
        ('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.test_dir = 'test'
        self.test_prefix = 'test_'
        self.test_suffixes = None

    # initialize_options ()

    def finalize_options (self):
        import os
        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])

        build = self.get_finalized_command('build')
        self.build_purelib = build.build_purelib
        self.build_platlib = build.build_platlib

    # finalize_options ()


    def run (self):
        import sys
        # 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[:]

    # run ()

# class test

Thomas