~bzr-pqm/bzr/bzr.dev

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright (C) 2005, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""UI tests for the test framework."""

import bzrlib.transport
from bzrlib import (
    benchmarks,
    tests,
    )
from bzrlib.errors import ParamikoNotPresent
from bzrlib.tests import (
                          features,
                          TestCase,
                          TestCaseInTempDir,
                          TestSkipped,
                          )


class SelfTestPatch:

    def get_params_passed_to_core(self, cmdline):
        params = []
        def selftest(*args, **kwargs):
            """Capture the arguments selftest was run with."""
            params.append((args, kwargs))
            return True
        # Yes this prevents using threads to run the test suite in parallel,
        # however we don't have a clean dependency injector for commands, 
        # and even if we did - we'd still be testing that the glue is wired
        # up correctly. XXX: TODO: Solve this testing problem.
        original_selftest = tests.selftest
        tests.selftest = selftest
        try:
            self.run_bzr(cmdline)
            return params[0]
        finally:
            tests.selftest = original_selftest


class TestOptionsWritingToDisk(TestCaseInTempDir, SelfTestPatch):

    def test_benchmark_runs_benchmark_tests(self):
        """selftest --benchmark should change the suite factory."""
        params = self.get_params_passed_to_core('selftest --benchmark')
        self.assertEqual(benchmarks.test_suite,
            params[1]['test_suite_factory'])
        self.assertNotEqual(None, params[1]['bench_history'])
        benchfile = open(".perf_history", "rt")
        try:
            lines = benchfile.readlines()
        finally:
            benchfile.close()
        # Because we don't run the actual test code no output is made to the
        # file.
        self.assertEqual(0, len(lines))


class TestOptions(TestCase, SelfTestPatch):

    def test_load_list(self):
        params = self.get_params_passed_to_core('selftest --load-list foo')
        self.assertEqual('foo', params[1]['load_list'])

    def test_transport_set_to_sftp(self):
        # Test that we can pass a transport to the selftest core - sftp
        # version.
        self.requireFeature(features.paramiko)
        params = self.get_params_passed_to_core('selftest --transport=sftp')
        self.assertEqual(bzrlib.transport.sftp.SFTPAbsoluteServer,
            params[1]["transport"])

    def test_transport_set_to_memory(self):
        # Test that we can pass a transport to the selftest core - memory
        # version.
        import bzrlib.transport.memory
        params = self.get_params_passed_to_core('selftest --transport=memory')
        self.assertEqual(bzrlib.transport.memory.MemoryServer,
            params[1]["transport"])

    def test_parameters_passed_to_core(self):
        params = self.get_params_passed_to_core('selftest --list-only')
        self.assertTrue("list_only" in params[1])
        params = self.get_params_passed_to_core('selftest --list-only selftest')
        self.assertTrue("list_only" in params[1])
        params = self.get_params_passed_to_core(['selftest', '--list-only',
            '--exclude', 'selftest'])
        self.assertTrue("list_only" in params[1])
        params = self.get_params_passed_to_core(['selftest', '--list-only',
            'selftest', '--randomize', 'now'])
        self.assertSubset(["list_only", "random_seed"], params[1])

    def test_starting_with(self):
        params = self.get_params_passed_to_core('selftest --starting-with foo')
        self.assertEqual(['foo'], params[1]['starting_with'])

    def test_starting_with_multiple_argument(self):
        params = self.get_params_passed_to_core(
            'selftest --starting-with foo --starting-with bar')
        self.assertEqual(['foo', 'bar'], params[1]['starting_with'])

    def test_subunit(self):
        self.requireFeature(features.subunit)
        params = self.get_params_passed_to_core('selftest --subunit')
        self.assertEqual(tests.SubUnitBzrRunner, params[1]['runner_class'])

    def _parse_test_list(self, lines, newlines_in_header=0):
        "Parse a list of lines into a tuple of 3 lists (header,body,footer)."
        in_header = newlines_in_header != 0
        in_footer = False
        header = []
        body = []
        footer = []
        header_newlines_found = 0
        for line in lines:
            if in_header:
                if line == '':
                    header_newlines_found += 1
                    if header_newlines_found >= newlines_in_header:
                        in_header = False
                        continue
                header.append(line)
            elif not in_footer:
                if line.startswith('-------'):
                    in_footer = True
                else:
                    body.append(line)
            else:
                footer.append(line)
        # If the last body line is blank, drop it off the list
        if len(body) > 0 and body[-1] == '':
            body.pop()
        return (header,body,footer)

    def test_list_only(self):
        # check that bzr selftest --list-only outputs no ui noise
        def selftest(*args, **kwargs):
            """Capture the arguments selftest was run with."""
            return True
        def outputs_nothing(cmdline):
            out,err = self.run_bzr(cmdline)
            (header,body,footer) = self._parse_test_list(out.splitlines())
            num_tests = len(body)
            self.assertLength(0, header)
            self.assertLength(0, footer)
            self.assertEqual('', err)
        # Yes this prevents using threads to run the test suite in parallel,
        # however we don't have a clean dependency injector for commands, 
        # and even if we did - we'd still be testing that the glue is wired
        # up correctly. XXX: TODO: Solve this testing problem.
        original_selftest = tests.selftest
        tests.selftest = selftest
        try:
            outputs_nothing('selftest --list-only')
            outputs_nothing('selftest --list-only selftest')
            outputs_nothing(['selftest', '--list-only', '--exclude', 'selftest'])
        finally:
            tests.selftest = original_selftest

    def test_lsprof_tests(self):
        params = self.get_params_passed_to_core('selftest --lsprof-tests')
        self.assertEqual(True, params[1]["lsprof_tests"])