aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_contrib_buildbot_cvs_mail.py
blob: 30aaad074d9d2b997836016e0c5e8b5d507c0728 (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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# This file is part of Buildbot.  Buildbot 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, version 2.
#
# 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.
#
# Copyright Buildbot Team Members

import sys
import re
import os

from twisted.python import log
from twisted.trial import unittest
from twisted.internet import protocol, defer, utils, reactor

test = '''
Update of /cvsroot/test
In directory example:/tmp/cvs-serv21085

Modified Files:
        README hello.c
Log Message:
two files checkin

'''
golden_1_11_regex=[
    '^From:',
    '^To: buildbot@example.com$',
    '^Reply-To: noreply@example.com$',
    '^Subject: cvs update for project test$',
    '^Date:',
    '^X-Mailer: Python buildbot-cvs-mail',
    '^$',
    '^Cvsmode: 1.11$',
    '^Category: None',
    '^CVSROOT: \"ext:example:/cvsroot\"',
    '^Files: test README 1.1,1.2 hello.c 2.2,2.3$',
    '^Project: test$',
    '^$',
    '^Update of /cvsroot/test$',
    '^In directory example:/tmp/cvs-serv21085$',
    '^$',
    '^Modified Files:$',
    'README hello.c$',
    'Log Message:$',
    '^two files checkin',
    '^$',
    '^$']
    
golden_1_12_regex=[
    '^From: ',
    '^To: buildbot@example.com$',
    '^Reply-To: noreply@example.com$',
    '^Subject: cvs update for project test$',
    '^Date: ',
    '^X-Mailer: Python buildbot-cvs-mail',
    '^$',
    '^Cvsmode: 1.12$',
    '^Category: None$',
    '^CVSROOT: \"ext:example.com:/cvsroot\"$',
    '^Files: README 1.1 1.2 hello.c 2.2 2.3$',
    '^Path: test$',
    '^Project: test$',
    '^$',
    '^Update of /cvsroot/test$',
    '^In directory example:/tmp/cvs-serv21085$',
    '^$',
    '^Modified Files:$',
    'README hello.c$',
    '^Log Message:$',
    'two files checkin',
    '^$',
    '^$' ]

class _SubprocessProtocol(protocol.ProcessProtocol):
    def __init__(self, input, deferred):
        self.input = input
        self.deferred = deferred
        self.output = ''

    def outReceived(self, s):
        self.output += s
    errReceived = outReceived

    def connectionMade(self):
        # push the input and send EOF
        self.transport.write(self.input)
        self.transport.closeStdin()

    def processEnded(self, reason):
        self.deferred.callback((self.output, reason.value.exitCode))

def getProcessOutputAndValueWithInput(executable, args, input):
    "similar to getProcessOutputAndValue, but also allows injection of input on stdin"
    d = defer.Deferred()
    p = _SubprocessProtocol(input, d)
    reactor.spawnProcess(p, executable, (executable,) + tuple(args))
    return d

class TestBuildbotCvsMail(unittest.TestCase):
    buildbot_cvs_mail_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../contrib/buildbot_cvs_mail.py'))
    if not os.path.exists(buildbot_cvs_mail_path):
        skip = ("'%s' does not exist (normal unless run from git)"
                % buildbot_cvs_mail_path)

    def assertOutputOk(self, (output, code), regexList):
        "assert that the output from getProcessOutputAndValueWithInput matches expectations"
        try:
            self.failUnlessEqual(code, 0, "subprocess exited uncleanly")
            lines = output.splitlines()
            self.failUnlessEqual(len(lines), len(regexList),
                        "got wrong number of lines of output")

            misses = []
            for line, regex in zip(lines, regexList):
                m = re.search(regex, line)
                if not m:
                    misses.append((regex,line))
            self.assertEqual(misses, [], "got non-matching lines")
        except:
            log.msg("got output:\n" + output)
            raise

    def test_buildbot_cvs_mail_from_cvs1_11(self):
        # Simulate CVS 1.11 
        d = getProcessOutputAndValueWithInput(sys.executable,
                [ self.buildbot_cvs_mail_path, '--cvsroot=\"ext:example:/cvsroot\"',
                  '--email=buildbot@example.com', '-P', 'test', '-R', 'noreply@example.com', '-t',
                  'test', 'README', '1.1,1.2', 'hello.c', '2.2,2.3' ],
                input=test)
        d.addCallback(self.assertOutputOk, golden_1_11_regex)
        return d

    def test_buildbot_cvs_mail_from_cvs1_12(self):
        # Simulate CVS 1.12, with --path option
        d = getProcessOutputAndValueWithInput(sys.executable,
                [ self.buildbot_cvs_mail_path, '--cvsroot=\"ext:example.com:/cvsroot\"',
                  '--email=buildbot@example.com', '-P', 'test', '--path', 'test',
                  '-R', 'noreply@example.com', '-t', 
                  'README', '1.1', '1.2', 'hello.c', '2.2', '2.3' ],
                input=test)
        d.addCallback(self.assertOutputOk, golden_1_12_regex)
        return d

    def test_buildbot_cvs_mail_no_args_exits_with_error(self):
        d = utils.getProcessOutputAndValue(sys.executable, [ self.buildbot_cvs_mail_path ])
        def check((stdout, stderr, code)):
            self.assertEqual(code, 2)
        d.addCallback(check)
        return d
        
    def test_buildbot_cvs_mail_without_email_opt_exits_with_error(self):
        d = utils.getProcessOutputAndValue(sys.executable, [ self.buildbot_cvs_mail_path,
                                '--cvsroot=\"ext:example.com:/cvsroot\"',
                                '-P', 'test', '--path', 'test',
                                '-R', 'noreply@example.com', '-t', 
                                'README', '1.1', '1.2', 'hello.c', '2.2', '2.3'])
        def check((stdout, stderr, code)):
            self.assertEqual(code, 2)
        d.addCallback(check)
        return d

    def test_buildbot_cvs_mail_without_cvsroot_opt_exits_with_error(self):
        d = utils.getProcessOutputAndValue(sys.executable, [ self.buildbot_cvs_mail_path,
                                '--complete-garbage-opt=gomi',
                                '--cvsroot=\"ext:example.com:/cvsroot\"',
                                '--email=buildbot@example.com','-P', 'test', '--path',
                                'test', '-R', 'noreply@example.com', '-t', 
                                'README', '1.1', '1.2', 'hello.c', '2.2', '2.3'])
        def check((stdout, stderr, code)):
            self.assertEqual(code, 2)
        d.addCallback(check)
        return d

    def test_buildbot_cvs_mail_with_unknown_opt_exits_with_error(self):
        d = utils.getProcessOutputAndValue(sys.executable, [ self.buildbot_cvs_mail_path,
                                '--email=buildbot@example.com','-P', 'test', '--path',
                                'test', '-R', 'noreply@example.com', '-t', 
                                'README', '1.1', '1.2', 'hello.c', '2.2', '2.3'])
        def check((stdout, stderr, code)):
            self.assertEqual(code, 2)
        d.addCallback(check)
        return d