~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
#! /usr/bin/python

# Copyright (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


"""External black-box test for bzr.

This always runs bzr as an external process to try to catch bugs
related to argument processing, startup, etc.

This replaces the previous test.sh which was not very portable."""

import sys, os, traceback

try:
    import shutil
    from subprocess import call, Popen
except ImportError, e:
    sys.stderr.write("testbzr: sorry, this test suite requires modules from python2.4\n"
                     + '    ' + str(e))
    sys.exit(1)


class CommandFailed(Exception):
    pass


def runcmd(cmd, retcode=0):
    """run one command and check the output.

    If a single string is based, it is split into words.
    For commands that are not simple space-separated words, please
    pass a list instead."""
    
    if isinstance(cmd, basestring):
        cmd = cmd.split()
    log_linenumber()
    logfile.write('$ %r\n' % cmd)
    actual_retcode = call(cmd, stdout=logfile, stderr=logfile)
    if retcode != actual_retcode:
        raise CommandFailed("test failed: %r returned %d, expected %d"
                            % (cmd, actual_retcode, retcode))


def progress(msg):
    print '* ' + msg
    logfile.write('* '+ msg + '\n')
    log_linenumber()


def log_linenumber():
    """Log the stack frame location two things up."""
    stack = traceback.extract_stack()[-3]
    logfile.write('   at %s:%d\n' % stack[:2])


TESTDIR = "testbzr.tmp"

# prepare an empty scratch directory
if os.path.exists(TESTDIR):
    shutil.rmtree(TESTDIR)


logfile = open('testbzr.log', 'wt', buffering=1)


try: 
    os.mkdir(TESTDIR)
    os.chdir(TESTDIR)


    progress("testing introductory commands")
    runcmd("bzr version")
    runcmd("bzr help")
    runcmd("bzr --help")

    progress("testing user identity")
    # this should always identify something, if only "john@localhost"
    runcmd("bzr whoami")
    runcmd("bzr whoami --email")

    progress("testing invalid commands")
    runcmd("bzr pants", retcode=1)
    runcmd("bzr --pants off", retcode=1)

    progress("all tests passed!")
except Exception, e:
    sys.stderr.write('*' * 50 + '\n'
                     + 'testbzr: tests failed\n'
                     + 'see bzr-test.log for more information\n'
                     + '*' * 50 + '\n')
    logfile.write('tests failed!\n')
    traceback.print_exc(None, logfile)
    sys.exit(1)