-
-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathtest_download.py
64 lines (53 loc) · 2.41 KB
/
test_download.py
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
# -*- coding: utf-8 -*-
"""
Test downloading files
"""
# ****************************************************************************
# Copyright (C) 2015 Volker Braun <[email protected]>
#
# 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.
# https://www.gnu.org/licenses/
# ****************************************************************************
from __future__ import print_function, absolute_import
import unittest
import tempfile
from .capture import CapturedLog
from sage_bootstrap.download import Download, MirrorList
from sage_bootstrap.compat import StringIO
class DownloadTestCase(unittest.TestCase):
def test_download_mirror_list(self):
tmp = tempfile.NamedTemporaryFile()
tmp.close()
progress = StringIO()
Download(MirrorList.URL, tmp.name, progress=progress).run()
self.assertEqual(progress.getvalue(),
'[......................................................................]\n')
with open(tmp.name, 'r') as f:
content = f.read()
self.assertTrue(content.startswith('# Sage Mirror List'))
def test_error(self):
URL = 'http://files.sagemath.org/sage_bootstrap/this_url_does_not_exist'
progress = StringIO()
download = Download(URL, progress=progress)
log = CapturedLog()
def action():
with log:
download.run()
self.assertRaises(IOError, action)
self.assertIsNotFoundError(log.messages())
self.assertEqual(progress.getvalue(),
'[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]\n')
def test_ignore_errors(self):
URL = 'http://files.sagemath.org/sage_bootstrap/this_url_does_not_exist'
with CapturedLog() as log:
Download(URL, progress=False, ignore_errors=True).run()
self.assertIsNotFoundError(log.messages())
def assertIsNotFoundError(self, messages):
self.assertEqual(len(messages), 1)
self.assertEqual(messages[0][0], 'ERROR')
self.assertTrue(messages[0][1].startswith('[Errno'))
self.assertTrue(messages[0][1].endswith(
"[Errno 404] Not Found: '//files.sagemath.org/sage_bootstrap/this_url_does_not_exist'"))