- bump version to 0.130.1
[opensuse:osc.git] / tests / test_commit.py
1 import osc.core
2 import osc.oscerr
3 import os
4 import sys
5 from common import GET, PUT, POST, DELETE, OscTestCase
6 from xml.etree import cElementTree as ET
7 FIXTURES_DIR = os.path.join(os.getcwd(), 'commit_fixtures')
8
9 def suite():
10     import unittest
11     return unittest.makeSuite(TestCommit)
12
13 rev_dummy = '<revision rev="repository">\n  <srcmd5>empty</srcmd5>\n</revision>'
14
15 class TestCommit(OscTestCase):
16     def _get_fixtures_dir(self):
17         return FIXTURES_DIR
18
19     @GET('http://localhost/source/osctest/simple?rev=latest', file='testSimple_filesremote')
20     @POST('http://localhost/source/osctest/simple?comment=&cmd=commitfilelist&user=Admin',
21           file='testSimple_missingfilelist', expfile='testSimple_lfilelist')
22     @PUT('http://localhost/source/osctest/simple/nochange?rev=repository',
23          exp='This file didn\'t change but\nis modified.\n', text=rev_dummy)
24     @POST('http://localhost/source/osctest/simple?comment=&cmd=commitfilelist&user=Admin',
25           file='testSimple_cfilesremote', expfile='testSimple_lfilelist')
26     def test_simple(self):
27         """a simple commit (only one modified file)"""
28         self._change_to_pkg('simple')
29         p = osc.core.Package('.')
30         p.commit()
31         exp = 'Sending    nochange\nTransmitting file data .\nCommitted revision 2.\n'
32         self.assertEqual(sys.stdout.getvalue(), exp)
33         self._check_digests('testSimple_cfilesremote')
34         self.assertTrue(os.path.exists('nochange'))
35         self.assertEqual(open('nochange', 'r').read(), open(os.path.join('.osc', 'nochange'), 'r').read())
36         self._check_status(p, 'nochange', ' ')
37         self._check_status(p, 'foo', ' ')
38         self._check_status(p, 'merge', ' ')
39
40     @GET('http://localhost/source/osctest/add?rev=latest', file='testAddfile_filesremote')
41     @POST('http://localhost/source/osctest/add?comment=&cmd=commitfilelist&user=Admin',
42           file='testAddfile_missingfilelist', expfile='testAddfile_lfilelist')
43     @PUT('http://localhost/source/osctest/add/add?rev=repository',
44          exp='added file\n', text=rev_dummy)
45     @POST('http://localhost/source/osctest/add?comment=&cmd=commitfilelist&user=Admin',
46           file='testAddfile_cfilesremote', expfile='testAddfile_lfilelist')
47     def test_addfile(self):
48         """commit a new file"""
49         self._change_to_pkg('add')
50         p = osc.core.Package('.')
51         p.commit()
52         exp = 'Sending    add\nTransmitting file data .\nCommitted revision 2.\n'
53         self.assertEqual(sys.stdout.getvalue(), exp)
54         self._check_digests('testAddfile_cfilesremote')
55         self.assertTrue(os.path.exists('add'))
56         self.assertEqual(open('add', 'r').read(), open(os.path.join('.osc', 'add'), 'r').read())
57         self.assertFalse(os.path.exists(os.path.join('.osc', '_to_be_added')))
58         self._check_status(p, 'add', ' ')
59         self._check_status(p, 'foo', ' ')
60         self._check_status(p, 'merge', ' ')
61         self._check_status(p, 'nochange', ' ')
62
63     @GET('http://localhost/source/osctest/delete?rev=latest', file='testDeletefile_filesremote')
64     @POST('http://localhost/source/osctest/delete?comment=&cmd=commitfilelist&user=Admin',
65           file='testDeletefile_cfilesremote', expfile='testDeletefile_lfilelist')
66     def test_deletefile(self):
67         """delete a file"""
68         self._change_to_pkg('delete')
69         p = osc.core.Package('.')
70         p.commit()
71         exp = 'Deleting    nochange\nTransmitting file data \nCommitted revision 2.\n'
72         self.assertEqual(sys.stdout.getvalue(), exp)
73         self._check_digests('testDeletefile_cfilesremote')
74         self.assertFalse(os.path.exists('nochange'))
75         self.assertFalse(os.path.exists(os.path.join('.osc', 'nochange')))
76         self.assertFalse(os.path.exists(os.path.join('.osc', '_to_be_deleted')))
77         self._check_status(p, 'foo', ' ')
78         self._check_status(p, 'merge', ' ')
79
80     @GET('http://localhost/source/osctest/conflict?rev=latest', file='testConflictfile_filesremote')
81     def test_conflictfile(self):
82         """package has a file which is in conflict state"""
83         self._change_to_pkg('conflict')
84         ret = osc.core.Package('.').commit()
85         self.assertTrue(ret == 1)
86         exp = 'Please resolve all conflicts before committing using "osc resolved FILE"!\n'
87         self.assertEqual(sys.stdout.getvalue(), exp)
88         self._check_digests('testConflictfile_filesremote')
89         self._check_conflictlist('merge\n')
90
91     @GET('http://localhost/source/osctest/nochanges?rev=latest', file='testNoChanges_filesremote')
92     def test_nochanges(self):
93         """package has no changes (which can be committed)"""
94         self._change_to_pkg('nochanges')
95         p = osc.core.Package('.')
96         ret = p.commit()
97         self.assertTrue(ret == 1)
98         exp = 'nothing to do for package nochanges\n'
99         self.assertEqual(sys.stdout.getvalue(), exp)
100         self._check_status(p, 'foo', 'S')
101         self._check_status(p, 'merge', '!')
102         self._check_status(p, 'nochange', ' ')
103
104     @GET('http://localhost/source/osctest/multiple?rev=latest', file='testMultiple_filesremote')
105     @POST('http://localhost/source/osctest/multiple?comment=&cmd=commitfilelist&user=Admin',
106           file='testMultiple_missingfilelist', expfile='testMultiple_lfilelist')
107     @PUT('http://localhost/source/osctest/multiple/nochange?rev=repository', exp='This file did change.\n', text=rev_dummy)
108     @PUT('http://localhost/source/osctest/multiple/add?rev=repository', exp='added file\n', text=rev_dummy)
109     @PUT('http://localhost/source/osctest/multiple/add2?rev=repository', exp='add2\n', text=rev_dummy)
110     @POST('http://localhost/source/osctest/multiple?comment=&cmd=commitfilelist&user=Admin',
111           file='testMultiple_cfilesremote', expfile='testMultiple_lfilelist')
112     def test_multiple(self):
113         """a simple commit (only one modified file)"""
114         self._change_to_pkg('multiple')
115         p = osc.core.Package('.')
116         p.commit()
117         exp = 'Deleting    foo\nDeleting    merge\nSending    nochange\n' \
118             'Sending    add\nSending    add2\nTransmitting file data ...\nCommitted revision 2.\n'
119         self.assertEqual(sys.stdout.getvalue(), exp)
120         self._check_digests('testMultiple_cfilesremote')
121         self.assertFalse(os.path.exists(os.path.join('.osc', '_to_be_added')))
122         self.assertFalse(os.path.exists(os.path.join('.osc', '_to_be_deleted')))
123         self.assertFalse(os.path.exists(os.path.join('.osc', 'foo')))
124         self.assertFalse(os.path.exists(os.path.join('.osc', 'merge')))
125         self.assertRaises(osc.oscerr.OscIOError, p.status, 'foo')
126         self.assertRaises(osc.oscerr.OscIOError, p.status, 'merge')
127         self._check_status(p, 'add', ' ')
128         self._check_status(p, 'add2', ' ')
129         self._check_status(p, 'nochange', ' ')
130
131     @GET('http://localhost/source/osctest/multiple?rev=latest', file='testPartial_filesremote')
132     @POST('http://localhost/source/osctest/multiple?comment=&cmd=commitfilelist&user=Admin',
133           file='testPartial_missingfilelist', expfile='testPartial_lfilelist')
134     @PUT('http://localhost/source/osctest/multiple/add?rev=repository', exp='added file\n', text=rev_dummy)
135     @PUT('http://localhost/source/osctest/multiple/nochange?rev=repository', exp='This file did change.\n', text=rev_dummy)
136     @POST('http://localhost/source/osctest/multiple?comment=&cmd=commitfilelist&user=Admin',
137           file='testPartial_cfilesremote', expfile='testPartial_lfilelist')
138     def test_partial(self):
139         """commit only some files"""
140         self._change_to_pkg('multiple')
141         p = osc.core.Package('.')
142         p.todo = ['foo', 'add', 'nochange']
143         p.commit()
144         exp = 'Deleting    foo\nSending    nochange\n' \
145             'Sending    add\nTransmitting file data ..\nCommitted revision 2.\n'
146         self.assertEqual(sys.stdout.getvalue(), exp)
147         self._check_digests('testPartial_cfilesremote')
148         self._check_addlist('add2\n')
149         self._check_deletelist('merge\n')
150         self._check_status(p, 'add2', 'A')
151         self._check_status(p, 'merge', 'D')
152         self._check_status(p, 'add', ' ')
153         self._check_status(p, 'nochange', ' ')
154         self.assertRaises(osc.oscerr.OscIOError, p.status, 'foo')
155
156     @GET('http://localhost/source/osctest/simple?rev=latest', file='testSimple_filesremote')
157     @POST('http://localhost/source/osctest/simple?comment=&cmd=commitfilelist&user=Admin',
158           file='testSimple_missingfilelist', expfile='testSimple_lfilelist')
159     @PUT('http://localhost/source/osctest/simple/nochange?rev=repository', exp='This file didn\'t change but\nis modified.\n',
160         exception=IOError('test exception'), text=rev_dummy)
161     def test_interrupt(self):
162         """interrupt a commit"""
163         self._change_to_pkg('simple')
164         p = osc.core.Package('.')
165         self.assertRaises(IOError, p.commit)
166         exp = 'Sending    nochange\nTransmitting file data .'
167         self.assertTrue(sys.stdout.getvalue(), exp)
168         self._check_digests('testSimple_filesremote')
169         self.assertTrue(os.path.exists('nochange'))
170         self._check_status(p, 'nochange', 'M')
171
172     @GET('http://localhost/source/osctest/allstates?rev=latest', file='testPartial_filesremote')
173     @POST('http://localhost/source/osctest/allstates?comment=&cmd=commitfilelist&user=Admin',
174           file='testAllStates_missingfilelist', expfile='testAllStates_lfilelist')
175     @PUT('http://localhost/source/osctest/allstates/add?rev=repository', exp='added file\n', text=rev_dummy)
176     @PUT('http://localhost/source/osctest/allstates/missing?rev=repository', exp='replaced\n', text=rev_dummy)
177     @PUT('http://localhost/source/osctest/allstates/nochange?rev=repository', exp='This file did change.\n', text=rev_dummy)
178     @POST('http://localhost/source/osctest/allstates?comment=&cmd=commitfilelist&user=Admin',
179           file='testAllStates_cfilesremote', expfile='testAllStates_lfilelist')
180     def test_allstates(self):
181         """commit all files (all states are available except 'C')"""
182         self._change_to_pkg('allstates')
183         p = osc.core.Package('.')
184         p.commit()
185         exp = 'Deleting    foo\nSending    missing\nSending    nochange\n' \
186             'Sending    add\nTransmitting file data ...\nCommitted revision 2.\n'
187         self.assertEqual(sys.stdout.getvalue(), exp)
188         self._check_digests('testAllStates_expfiles', 'skipped')
189         self.assertFalse(os.path.exists(os.path.join('.osc', '_to_be_added')))
190         self.assertFalse(os.path.exists(os.path.join('.osc', '_to_be_deleted')))
191         self.assertFalse(os.path.exists('foo'))
192         self.assertFalse(os.path.exists(os.path.join('.osc', 'foo')))
193         self._check_status(p, 'add', ' ')
194         self._check_status(p, 'nochange', ' ')
195         self._check_status(p, 'merge', '!')
196         self._check_status(p, 'missing', ' ')
197         self._check_status(p, 'skipped', 'S')
198         self._check_status(p, 'test', ' ')
199
200     @GET('http://localhost/source/osctest/add?rev=latest', file='testAddfile_filesremote')
201     @POST('http://localhost/source/osctest/add?comment=&cmd=commitfilelist&user=Admin',
202           file='testAddfile_cfilesremote', expfile='testAddfile_lfilelist')
203     def test_remoteexists(self):
204         """file 'add' should be committed but already exists on the server"""
205         self._change_to_pkg('add')
206         p = osc.core.Package('.')
207         p.commit()
208         exp = 'Sending    add\nTransmitting file data \nCommitted revision 2.\n'
209         self.assertEqual(sys.stdout.getvalue(), exp)
210         self._check_digests('testAddfile_cfilesremote')
211         self.assertTrue(os.path.exists('add'))
212         self.assertEqual(open('add', 'r').read(), open(os.path.join('.osc', 'add'), 'r').read())
213         self.assertFalse(os.path.exists(os.path.join('.osc', '_to_be_added')))
214         self._check_status(p, 'add', ' ')
215         self._check_status(p, 'foo', ' ')
216         self._check_status(p, 'merge', ' ')
217         self._check_status(p, 'nochange', ' ')
218
219     @GET('http://localhost/source/osctest/branch?rev=latest', file='testExpand_filesremote')
220     @POST('http://localhost/source/osctest/branch?comment=&cmd=commitfilelist&user=Admin&keeplink=1',
221           file='testExpand_missingfilelist', expfile='testExpand_lfilelist')
222     @PUT('http://localhost/source/osctest/branch/simple?rev=repository', exp='simple modified file.\n', text=rev_dummy)
223     @POST('http://localhost/source/osctest/branch?comment=&cmd=commitfilelist&user=Admin&keeplink=1',
224           file='testExpand_cfilesremote', expfile='testExpand_lfilelist')
225     @GET('http://localhost/source/osctest/branch?rev=87ea02aede261b0267aabaa97c756e7a', file='testExpand_expandedfilesremote')
226     def test_expand(self):
227         """commit an expanded package"""
228         self._change_to_pkg('branch')
229         p = osc.core.Package('.')
230         p.commit()
231         exp = 'Sending    simple\nTransmitting file data .\nCommitted revision 7.\n'
232         self.assertEqual(sys.stdout.getvalue(), exp)
233         self._check_digests('testExpand_expandedfilesremote')
234         self._check_status(p, 'simple', ' ')
235
236     @GET('http://localhost/source/osctest/added_missing?rev=latest', file='testAddedMissing_filesremote')
237     def test_added_missing(self):
238         """commit an added file which is missing"""
239         self._change_to_pkg('added_missing')
240         p = osc.core.Package('.')
241         ret = p.commit()
242         self.assertTrue(ret == 1)
243         exp = 'file \'add\' is marked as \'A\' but does not exist\n'
244         self.assertEqual(sys.stdout.getvalue(), exp)
245         self._check_status(p, 'add', '!')
246
247     @GET('http://localhost/source/osctest/added_missing?rev=latest', file='testAddedMissing_filesremote')
248     @POST('http://localhost/source/osctest/added_missing?comment=&cmd=commitfilelist&user=Admin',
249           file='testAddedMissing_missingfilelist', expfile='testAddedMissing_lfilelist')
250     @PUT('http://localhost/source/osctest/added_missing/bar?rev=repository', exp='foobar\n', text=rev_dummy)
251     @POST('http://localhost/source/osctest/added_missing?comment=&cmd=commitfilelist&user=Admin',
252           file='testAddedMissing_cfilesremote', expfile='testAddedMissing_lfilelist')
253     def test_added_missing2(self):
254         """commit an added file, another added file missing (but it's not part of the commit)"""
255         self._change_to_pkg('added_missing')
256         p = osc.core.Package('.')
257         p.todo = ['bar']
258         p.commit()
259         exp = 'Sending    bar\nTransmitting file data .\nCommitted revision 2.\n'
260         self.assertEqual(sys.stdout.getvalue(), exp)
261         self._check_status(p, 'add', '!')
262         self._check_status(p, 'bar', ' ')
263
264 if __name__ == '__main__':
265     import unittest
266     unittest.main()