summaryrefslogtreecommitdiff
path: root/tests/test_prdiff.py
blob: 13e156a28cdfeaf8de67d914385ee6747f32fa6e (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import osc.commandline
import osc.core
import osc.oscerr
import os
import re
import sys
from common import GET, POST, OscTestCase, addExpectedRequest, EXPECTED_REQUESTS


#FIXTURES_DIR = os.path.join(os.getcwd(), 'prdiff_fixtures')
FIXTURES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'prdiff_fixtures')

API_URL  = 'http://localhost/'
UPSTREAM = 'some:project'
BRANCH   = 'home:user:branches:' + UPSTREAM

def rdiff_url(pkg, oldprj, newprj):
    return API_URL + 'source/%s/%s?unified=1&opackage=%s&oproject=%s&cmd=diff&expand=1&tarlimit=0&filelimit=0' % \
        (newprj, pkg, pkg, oldprj.replace(':', '%3A'))

def request_url(prj):
    return API_URL + 'search/request?match=%%28state%%2F%%40name%%3D%%27new%%27+or+state%%2F%%40name%%3D%%27review%%27%%29+and+%%28action%%2Ftarget%%2F%%40project%%3D%%27%s%%27+or+submit%%2Ftarget%%2F%%40project%%3D%%27%s%%27+or+action%%2Fsource%%2F%%40project%%3D%%27%s%%27+or+submit%%2Fsource%%2F%%40project%%3D%%27%s%%27%%29' % \
        tuple([prj.replace(':', '%3A')] * 4)

def GET_PROJECT_PACKAGES(*projects):
    def decorator(test_method):
        def wrapped_test_method(*args):
            for project in projects:
                addExpectedRequest('GET', API_URL + 'source/' + project,
                                   file='%s/directory' % project)
            test_method(*args)
        # "rename" method otherwise we cannot specify a TestCaseClass.testName
        # cmdline arg when using unittest.main()
        wrapped_test_method.__name__ = test_method.__name__
        return wrapped_test_method
    return decorator

def POST_RDIFF(oldprj, newprj):
    def decorator(test_method):
        def wrapped_test_method(*args):
            addExpectedRequest('POST', rdiff_url('common-one',   oldprj, newprj), exp='', text='')
            addExpectedRequest('POST', rdiff_url('common-two',   oldprj, newprj), exp='', file='common-two-diff')
            addExpectedRequest('POST', rdiff_url('common-three', oldprj, newprj), exp='', text='')
            test_method(*args)
        # "rename" method otherwise we cannot specify a TestCaseClass.testName
        # cmdline arg when using unittest.main()
        wrapped_test_method.__name__ = test_method.__name__
        return wrapped_test_method
    return decorator

def suite():
    import unittest
    return unittest.makeSuite(TestProjectDiff)

class TestProjectDiff(OscTestCase):
    diff_hdr = 'Index: %s\n==================================================================='
    def _get_fixtures_dir(self):
        return FIXTURES_DIR

    def _change_to_tmpdir(self, *args):
        os.chdir(os.path.join(self.tmpdir, *args))

    def _run_prdiff(self, *args):
        """Runs osc prdiff, returning captured STDOUT as a string."""
        cli = osc.commandline.Osc()
        argv = ['osc', '--no-keyring', '--no-gnome-keyring', 'prdiff']
        argv.extend(args)
        cli.main(argv=argv)
        return sys.stdout.getvalue()


    def testPrdiffTooManyArgs(self):
        def runner():
            self._run_prdiff('one', 'two', 'superfluous-arg')
        self.assertRaises(osc.oscerr.WrongArgs, runner)


    @GET_PROJECT_PACKAGES(UPSTREAM, BRANCH)
    @POST_RDIFF(UPSTREAM, BRANCH)
    @POST(rdiff_url('only-in-new', UPSTREAM, BRANCH), exp='', text='')
    def testPrdiffZeroArgs(self):
        exp = """identical: common-one
differs:   common-two
identical: common-three
identical: only-in-new
"""
        def runner():
            self._run_prdiff()

        os.chdir('/tmp')
        self.assertRaises(osc.oscerr.WrongArgs, runner)

        self._change_to_tmpdir(FIXTURES_DIR, UPSTREAM)
        self.assertRaises(osc.oscerr.WrongArgs, runner)

        self._change_to_tmpdir(FIXTURES_DIR, BRANCH)
        out = self._run_prdiff()
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES(UPSTREAM, BRANCH)
    @POST_RDIFF(UPSTREAM, BRANCH)
    @POST(rdiff_url('only-in-new', UPSTREAM, BRANCH), exp='', text='')
    def testPrdiffOneArg(self):
        self._change_to_tmpdir()
        exp = """identical: common-one
differs:   common-two
identical: common-three
identical: only-in-new
"""
        out = self._run_prdiff('home:user:branches:some:project')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES('old:prj', 'new:prj')
    @POST_RDIFF('old:prj', 'new:prj')
    def testPrdiffTwoArgs(self):
        self._change_to_tmpdir()
        exp = """identical: common-one
differs:   common-two
identical: common-three
"""
        out = self._run_prdiff('old:prj', 'new:prj')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES('old:prj', 'new:prj')
    @POST_RDIFF('old:prj', 'new:prj')
    def testPrdiffOldOnly(self):
        self._change_to_tmpdir()
        exp = """identical: common-one
differs:   common-two
identical: common-three
old only:  only-in-old
"""
        out = self._run_prdiff('--show-not-in-new', 'old:prj', 'new:prj')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES('old:prj', 'new:prj')
    @POST_RDIFF('old:prj', 'new:prj')
    def testPrdiffNewOnly(self):
        self._change_to_tmpdir()
        exp = """identical: common-one
differs:   common-two
identical: common-three
new only:  only-in-new
"""
        out = self._run_prdiff('--show-not-in-old', 'old:prj', 'new:prj')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES('old:prj', 'new:prj')
    @POST_RDIFF('old:prj', 'new:prj')
    def testPrdiffDiffstat(self):
        self._change_to_tmpdir()
        exp = """identical: common-one
differs:   common-two

 common-two |    1 +
 1 file changed, 1 insertion(+)

identical: common-three
"""
        out = self._run_prdiff('--diffstat', 'old:prj', 'new:prj')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES('old:prj', 'new:prj')
    @POST_RDIFF('old:prj', 'new:prj')
    def testPrdiffUnified(self):
        self._change_to_tmpdir()
        exp = """identical: common-one
differs:   common-two

Index: common-two
===================================================================
--- common-two\t2013-01-18 19:18:38.225983117 +0000
+++ common-two\t2013-01-18 19:19:27.882082325 +0000
@@ -1,4 +1,5 @@
 line one
 line two
 line three
+an extra line
 last line

identical: common-three
"""
        out = self._run_prdiff('--unified', 'old:prj', 'new:prj')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES('old:prj', 'new:prj')
    @POST(rdiff_url('common-two',   'old:prj', 'new:prj'), exp='', file='common-two-diff')
    @POST(rdiff_url('common-three', 'old:prj', 'new:prj'), exp='', text='')
    def testPrdiffInclude(self):
        self._change_to_tmpdir()
        exp = """differs:   common-two
identical: common-three
"""
        out = self._run_prdiff('--include', 'common-t',
                               'old:prj', 'new:prj')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES('old:prj', 'new:prj')
    @POST(rdiff_url('common-two',   'old:prj', 'new:prj'), exp='', file='common-two-diff')
    @POST(rdiff_url('common-three', 'old:prj', 'new:prj'), exp='', text='')
    def testPrdiffExclude(self):
        self._change_to_tmpdir()
        exp = """differs:   common-two
identical: common-three
"""
        out = self._run_prdiff('--exclude', 'one', 'old:prj', 'new:prj')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES('old:prj', 'new:prj')
    @POST(rdiff_url('common-two',   'old:prj', 'new:prj'), exp='', file='common-two-diff')
    def testPrdiffIncludeExclude(self):
        self._change_to_tmpdir()
        exp = """differs:   common-two
"""
        out = self._run_prdiff('--include', 'common-t',
                               '--exclude', 'three',
                               'old:prj', 'new:prj')
        self.assertEqualMultiline(out, exp)


    @GET_PROJECT_PACKAGES(UPSTREAM, BRANCH)
    @GET(request_url(UPSTREAM), exp='', file='request')
    @POST(rdiff_url('common-one',   UPSTREAM, BRANCH), exp='', text='')
    @POST(rdiff_url('common-two',   UPSTREAM, BRANCH), exp='', file='common-two-diff')
    @POST(rdiff_url('common-three', UPSTREAM, BRANCH), exp='', file='common-two-diff')
    @POST(rdiff_url('only-in-new',  UPSTREAM, BRANCH), exp='', text='')
    def testPrdiffRequestsMatching(self):
        self._change_to_tmpdir()
        exp = """identical: common-one
differs:   common-two

148023  State:new        By:user         When:2013-01-11T11:04:14
        submit:          home:user:branches:some:project/common-two@7 ->    some:project
        Descr: - Fix it to work - Improve support for something

differs:   common-three
identical: only-in-new
"""
        out = self._run_prdiff('--requests', UPSTREAM, BRANCH)
        self.assertEqualMultiline(out, exp)


    # Reverse the direction of the diff.
    @GET_PROJECT_PACKAGES(BRANCH, UPSTREAM)
    @GET(request_url(BRANCH), exp='', file='no-requests')
    @POST(rdiff_url('common-one',   BRANCH, UPSTREAM), exp='', text='')
    @POST(rdiff_url('common-two',   BRANCH, UPSTREAM), exp='', file='common-two-diff')
    @POST(rdiff_url('common-three', BRANCH, UPSTREAM), exp='', file='common-two-diff')
    @POST(rdiff_url('only-in-new',  BRANCH, UPSTREAM), exp='', text='')
    def testPrdiffRequestsSwitched(self):
        self._change_to_tmpdir()
        exp = """identical: common-one
differs:   common-two
differs:   common-three
identical: only-in-new
"""
        out = self._run_prdiff('--requests', BRANCH, UPSTREAM)
        self.assertEqualMultiline(out, exp)



if __name__ == '__main__':
    import unittest
    unittest.main()