Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/test/test_download.py
4052 views
1
# -*- coding: utf-8 -*-
2
"""
3
Test downloading files
4
"""
5
6
# ****************************************************************************
7
# Copyright (C) 2015 Volker Braun <[email protected]>
8
#
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 2 of the License, or
12
# (at your option) any later version.
13
# https://www.gnu.org/licenses/
14
# ****************************************************************************
15
from __future__ import print_function, absolute_import
16
17
import unittest
18
import tempfile
19
20
21
from .capture import CapturedLog
22
from sage_bootstrap.download import Download, MirrorList
23
from sage_bootstrap.compat import StringIO
24
25
26
class DownloadTestCase(unittest.TestCase):
27
28
def test_download_mirror_list(self):
29
tmp = tempfile.NamedTemporaryFile()
30
tmp.close()
31
progress = StringIO()
32
Download(MirrorList.URL, tmp.name, progress=progress).run()
33
self.assertEqual(progress.getvalue(),
34
'[......................................................................]\n')
35
with open(tmp.name, 'r') as f:
36
content = f.read()
37
self.assertTrue(content.startswith('# Sage Mirror List'))
38
39
def test_error(self):
40
URL = 'http://files.sagemath.org/sage_bootstrap/this_url_does_not_exist'
41
progress = StringIO()
42
download = Download(URL, progress=progress)
43
log = CapturedLog()
44
45
def action():
46
with log:
47
download.run()
48
self.assertRaises(IOError, action)
49
self.assertIsNotFoundError(log.messages())
50
self.assertEqual(progress.getvalue(),
51
'[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]\n')
52
53
def test_ignore_errors(self):
54
URL = 'http://files.sagemath.org/sage_bootstrap/this_url_does_not_exist'
55
with CapturedLog() as log:
56
Download(URL, progress=False, ignore_errors=True).run()
57
self.assertIsNotFoundError(log.messages())
58
59
def assertIsNotFoundError(self, messages):
60
self.assertEqual(len(messages), 1)
61
self.assertEqual(messages[0][0], 'ERROR')
62
self.assertTrue(messages[0][1].startswith('[Errno'))
63
self.assertTrue(messages[0][1].endswith(
64
"[Errno 404] Not Found: '//files.sagemath.org/sage_bootstrap/this_url_does_not_exist'"))
65
66