~thedac/codetree/bug-hunt

« back to all changes in this revision

Viewing changes to tests/test_handlers.py

  • Committer: Matthew Wedgwood
  • Date: 2013-09-14 18:28:43 UTC
  • Revision ID: git-v1:edbff0125c04d47b52e6e038d50fba5b83cb2019
Increase test coverage. Add boolean return value to handlers.

* Additional test coverage addeded for the HttpFileHandler
* In order to diffentiate between success and failure of downloads, a
  boolean return value has been added to the handler get() methods

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from unittest import TestCase
3
3
from urlparse import urlparse
4
4
from tempfile import mkdtemp
 
5
from urllib2 import URLError
5
6
import subprocess
6
7
import shutil
7
8
try:
123
124
 
124
125
        # overwrite (delete) existing when asked
125
126
        options = {"overwrite": True}
126
 
        bh.get(dest, options)
 
127
        self.assertTrue(bh.get(dest, options))
127
128
        _rmtree.assert_called_with(dest)
128
129
 
129
130
        # don't overwrite if not asked
130
131
        options = {"overwrite": False}
131
 
        bh.get(dest, options)
 
132
        self.assertFalse(bh.get(dest, options))
132
133
        _rmtree.assert_not_called()
133
134
 
134
135
        # don't overwrite if source = parent
135
136
        options = {"overwrite": True}
136
137
        bh.is_same_branch = MagicMock(return_value=True)
137
 
        bh.get(dest, options)
 
138
        self.assertTrue(bh.get(dest, options))
138
139
        _rmtree.assert_not_called()
139
140
 
140
141
    @patch("codetree.handlers.check_output")
143
144
        source = BzrURLs[0]
144
145
        dest = "foo"
145
146
        bh = BzrSourceHandler(source)
146
 
        bh.get(dest)
147
 
        assert(was_called_with_cmd(_call, ('bzr', 'branch', source, dest)))
 
147
        self.assertTrue(bh.get(dest))
 
148
        self.assertTrue(was_called_with_cmd(_call, ('bzr', 'branch', source, dest)))
148
149
 
149
150
    @patch("codetree.handlers.check_output")
150
151
    @patch("codetree.handlers.os.makedirs")
152
153
        source = BzrURLs[0]
153
154
        dest = "foo/bar/baz"
154
155
        bh = BzrSourceHandler(source)
155
 
        bh.get(dest)
 
156
        self.assertTrue(bh.get(dest))
156
157
        _makedirs.assert_called_with(os.path.dirname(dest))
157
158
 
158
159
    @patch("codetree.handlers.check_output")
162
163
        dest = "foo"
163
164
        bh = BzrSourceHandler(source)
164
165
        bh.is_same_branch = MagicMock(return_value=True)
165
 
        bh.get(dest)
 
166
        self.assertTrue(bh.get(dest))
166
167
        assert(was_called_with_cmd(_call, ('bzr', 'pull', '-d', dest)))
167
168
 
168
169
    @patch("codetree.handlers.check_output")
174
175
 
175
176
        revno = "1"
176
177
        options = {"revno": "1"}
177
 
        bh.get(dest, options)
 
178
        self.assertTrue(bh.get(dest, options))
178
179
        assert(was_called_with_cmd(_call, ('bzr', 'update', dest, '-r', revno)))
179
180
 
180
181
    def test_same_branch(self):
210
211
    @patch("codetree.handlers.fileutils.mkdir")
211
212
    def creates_directory(self, _mkdir):
212
213
        lh = LocalHandler("@")
213
 
        lh.get("foo")
 
214
        self.assertTrue(lh.get("foo"))
214
215
        _mkdir.assert_called_with('foo', overwrite=False)
215
216
 
216
217
 
223
224
    @patch('codetree.handlers.os.path.exists')
224
225
    @patch('codetree.handlers.urllib2.urlopen')
225
226
    def test_gets_file(self, _urlopen, _exists, _unlink):
226
 
        _urlopen.return_value = StringIO("words words")
227
 
        hh = HttpFileHandler(HttpURLs[0])
228
 
        _open = mock_open()
229
 
        with patch('codetree.handlers.open', _open, create=True):
230
 
            hh.get('foo')
231
 
        _open.assert_called_with("foo", "w")
232
 
        _urlopen.assert_called_with(HttpURLs[0])
 
227
        destfile = "foo"
 
228
 
 
229
        _urlopen.return_value = StringIO("words words")
 
230
        hh = HttpFileHandler(HttpURLs[0])
 
231
 
 
232
        # New file
 
233
        _open = mock_open()
 
234
        _exists.return_value = False
 
235
        with patch('codetree.handlers.open', _open, create=True):
 
236
            self.assertTrue(hh.get(destfile))
 
237
        self.assertFalse(_unlink.called)
 
238
        _open.assert_called_with(destfile, "w")
 
239
        _urlopen.assert_called_with(HttpURLs[0])
 
240
 
 
241
    @patch('codetree.handlers.os.unlink')
 
242
    @patch('codetree.handlers.os.path.exists')
 
243
    @patch('codetree.handlers.urllib2.urlopen')
 
244
    def test_gets_file_no_overwrite(self, _urlopen, _exists, _unlink):
 
245
        destfile = "foo"
 
246
 
 
247
        _urlopen.return_value = StringIO("words words")
 
248
        hh = HttpFileHandler(HttpURLs[0])
 
249
 
 
250
        # Existing file
 
251
        _open = mock_open()
 
252
        _exists.return_value = True
 
253
        with patch('codetree.handlers.open', _open, create=True):
 
254
            self.assertFalse(hh.get(destfile))
 
255
        self.assertFalse(_unlink.called)
 
256
        self.assertFalse(_open.called)
 
257
        self.assertFalse(_urlopen.called)
 
258
 
 
259
    @patch('codetree.handlers.os.unlink')
 
260
    @patch('codetree.handlers.os.path.exists')
 
261
    @patch('codetree.handlers.urllib2.urlopen')
 
262
    def test_gets_file_with_overwite(self, _urlopen, _exists, _unlink):
 
263
        destfile = "foo"
 
264
 
 
265
        _urlopen.return_value = StringIO("words words")
 
266
        hh = HttpFileHandler(HttpURLs[0])
 
267
 
 
268
        # Overwrite existing file
 
269
        _open = mock_open()
 
270
        _exists.return_value = True
 
271
        with patch('codetree.handlers.open', _open, create=True):
 
272
            self.assertTrue(hh.get(destfile, options={"overwrite": True}))
 
273
        _unlink.assert_called_with(destfile)
 
274
        _open.assert_called_with(destfile, "w")
 
275
        _urlopen.assert_called_with(HttpURLs[0])
 
276
 
 
277
    @patch('codetree.handlers.os.unlink')
 
278
    @patch('codetree.handlers.os.path.exists')
 
279
    @patch('codetree.handlers.urllib2.urlopen')
 
280
    def test_gets_file_bad_url(self, _urlopen, _exists, _unlink):
 
281
        destfile = "foo"
 
282
 
 
283
        _urlopen.return_value = StringIO("words words")
 
284
        hh = HttpFileHandler(HttpURLs[0])
 
285
 
 
286
        # Broken source
 
287
        _open = mock_open()
 
288
        _exists.return_value = False
 
289
        _urlopen.side_effect = URLError('failed')
 
290
        with patch('codetree.handlers.open', _open, create=True):
 
291
            self.assertFalse(hh.get(destfile))