summaryrefslogtreecommitdiff
path: root/src/distutils2/tests/test_test.py
blob: da60d92c461e598bcb293dccbac57b4bcdfb1d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os, sys, shutil, re
from copy import copy
from os.path import join
from StringIO import StringIO
from distutils2.tests.support import unittest, TempdirManager
from distutils2.command.test import test
from distutils2.dist import Distribution
import subprocess

try:
    any
except NameError:
    from distutils2._backport import any

EXPECTED_OUTPUT_RE = r'''FAIL: test_blah \(myowntestmodule.SomeTest\)
----------------------------------------------------------------------
Traceback \(most recent call last\):
  File ".+/myowntestmodule.py", line \d+, in test_blah
    self.fail\("horribly"\)
AssertionError: horribly
'''

here = os.path.dirname(os.path.abspath(__file__))


class TestTest(TempdirManager, unittest.TestCase):

    def setUp(self):
        super(TestTest, self).setUp()

        distutils2path = os.path.dirname(os.path.dirname(here))
        self.old_pythonpath = os.environ.get('PYTHONPATH', '')
        os.environ['PYTHONPATH'] = distutils2path + os.pathsep + self.old_pythonpath

    def tearDown(self):
        os.environ['PYTHONPATH'] = self.old_pythonpath
        super(TestTest, self).tearDown()

    def assert_re_match(self, pattern, string):
        def quote(s):
            lines = ['## ' + line for line in s.split('\n')]
            sep = ["#" * 60]
            return [''] + sep + lines + sep
        msg = quote(pattern) + ["didn't match"] + quote(string)
        msg = "\n".join(msg)
        if not re.search(pattern, string):
            self.fail(msg)

    def run_with_dist_cwd(self, pkg_dir):
        cwd = os.getcwd()
        command = [sys.executable, "setup.py", "test"]
        try:
            os.chdir(pkg_dir)
            test_proc = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
            _, errors = test_proc.communicate()
            return errors
        finally:
            os.chdir(cwd)

    def prepare_dist(self, dist_name):
        pkg_dir = join(os.path.dirname(__file__), "dists", dist_name)
        temp_pkg_dir = join(self.mkdtemp(), dist_name)
        shutil.copytree(pkg_dir, temp_pkg_dir)
        return temp_pkg_dir
        
    def test_runs_simple_tests(self):
        self.pkg_dir = self.prepare_dist('simple_test')
        output = self.run_with_dist_cwd(self.pkg_dir)
        self.assert_re_match(EXPECTED_OUTPUT_RE, output)
        self.assertFalse(os.path.exists(join(self.pkg_dir, 'build')))

    def test_builds_extensions(self):
        self.pkg_dir = self.prepare_dist("extensions_test")
        output = self.run_with_dist_cwd(self.pkg_dir)
        self.assert_re_match(EXPECTED_OUTPUT_RE, output)
        self.assertTrue(os.path.exists(join(self.pkg_dir, 'build')))
        self.assertTrue(any(x.startswith('lib') for x in os.listdir(join(self.pkg_dir, 'build'))))

    def test_custom_test_loader(self):
        self.pkg_dir = self.prepare_dist("custom_loader")
        output = self.run_with_dist_cwd(self.pkg_dir)
        self.assert_re_match(EXPECTED_OUTPUT_RE, output)

    def _test_works_with_2to3(self):
        pass

    @unittest.skipUnless(sys.version > '2.6', 'Need >= 2.6')
    def test_checks_requires(self):
        from distutils2.tests.with_support import examine_warnings
        dist = Distribution()
        dist.tests_require = ['ohno_ohno-impossible_1234-name_stop-that!']
        cmd = test(dist)
        def examinator(ws):
            cmd.ensure_finalized()
            self.assertEqual(1, len(ws))
            warning = ws[0]
            self.assertIs(warning.category, RuntimeWarning)

        examine_warnings(examinator)

def test_suite():
    return unittest.makeSuite(TestTest)