Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/test/test_download_cmdline.py
4052 views
1
# -*- coding: utf-8 -*-
2
"""
3
Test sage-download-file commandline utility
4
"""
5
6
# ****************************************************************************
7
# Copyright (C) 2016 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
16
import os
17
import unittest
18
import subprocess
19
import logging
20
21
from sage_bootstrap.download.mirror_list import MIRRORLIST_FILENAME
22
from sage_bootstrap.util import is_url
23
24
25
log = logging.getLogger()
26
27
28
EXECUTABLE = os.path.join(
29
os.path.dirname(os.path.dirname(__file__)),
30
'bin',
31
'sage-download-file',
32
)
33
34
35
class SageDownloadFileTestCase(unittest.TestCase):
36
37
maxDiff = None
38
39
def test_print_mirror_list_no_network(self):
40
"""
41
Subsequent runs of sage-download-file
42
"""
43
try:
44
os.remove(MIRRORLIST_FILENAME)
45
except OSError:
46
pass
47
env = dict(os.environ)
48
env['http_proxy'] = 'http://192.0.2.0:5187/'
49
env['https_proxy'] = 'http://192.0.2.0:5187/'
50
env['ftp_proxy'] = 'http://192.0.2.0:5187/'
51
env['rsync_proxy'] = 'http://192.0.2.0:5187/'
52
proc = subprocess.Popen(
53
[EXECUTABLE, '--print-fastest-mirror', '--timeout=0.001'],
54
stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
55
env=env,
56
)
57
stdout, stderr = proc.communicate()
58
stdout = stdout.decode('utf-8')
59
stderr = stderr.decode('utf-8')
60
rc = proc.returncode
61
# returns successfully
62
self.assertEqual(rc, 0)
63
# Prints single url (the default) to stdout
64
self.assertTrue(is_url(stdout))
65
# Prints error to stderr
66
self.assertTrue(stderr.find('Downloading the mirror list failed') >= 0)
67
68
def test_print_mirror_list_timing(self):
69
"""
70
The first run of sage-download-file
71
"""
72
try:
73
os.remove(MIRRORLIST_FILENAME)
74
except OSError:
75
pass
76
proc = subprocess.Popen(
77
[EXECUTABLE, '--print-fastest-mirror'],
78
stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
79
)
80
stdout, stderr = proc.communicate()
81
stdout = stdout.decode('utf-8')
82
stderr = stderr.decode('utf-8')
83
rc = proc.returncode
84
# returns successfully
85
self.assertEqual(rc, 0)
86
# Prints single url to stdout
87
self.assertTrue(is_url(stdout))
88
# Prints mirrors to stderr
89
self.assertTrue(len(stderr.strip().splitlines()) > 3)
90
91
def test_print_mirror_list_cached(self):
92
"""
93
Subsequent runs of sage-download-file
94
"""
95
proc = subprocess.Popen(
96
[EXECUTABLE, '--print-fastest-mirror'],
97
stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
98
)
99
stdout, stderr = proc.communicate()
100
stdout = stdout.decode('utf-8')
101
stderr = stderr.decode('utf-8')
102
rc = proc.returncode
103
# returns successfully
104
self.assertEqual(rc, 0)
105
# Prints single url to stdout
106
self.assertTrue(is_url(stdout))
107
# May or may not print to stderr depending on whether cache was saved
108
109