Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/test/test_package_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 tempfile
19
import subprocess
20
import shutil
21
import logging
22
23
from sage_bootstrap.env import SAGE_DISTFILES
24
from sage_bootstrap.download.mirror_list import MIRRORLIST_FILENAME
25
from sage_bootstrap.package import Package
26
from test.capture import CapturedLog
27
28
29
log = logging.getLogger()
30
31
32
PATH = os.path.join(
33
os.path.dirname(os.path.dirname(__file__)),
34
'bin'
35
)
36
37
38
EXECUTABLE = os.path.join(PATH, 'sage-package')
39
40
41
class SagePackageTestCase(unittest.TestCase):
42
43
def run_command(self, *args, **kwds):
44
env = dict(os.environ)
45
env.update(kwds.get('env', {}))
46
env['PATH'] = PATH + os.pathsep + env['PATH']
47
kwds.update(
48
stdin=None,
49
stdout=subprocess.PIPE,
50
stderr=subprocess.PIPE,
51
env=env,
52
)
53
log.debug('running {}: {}'.format(args, kwds))
54
proc = subprocess.Popen(args, **kwds)
55
stdout, stderr = proc.communicate()
56
stdout = stdout.decode('utf-8')
57
stderr = stderr.decode('utf-8')
58
log.debug(u'stdout="{}", stderr="{}"'.format(stdout, stderr))
59
rc = proc.returncode
60
return (rc, stdout, stderr)
61
62
def test_config(self):
63
rc, stdout, stderr = self.run_command(EXECUTABLE, 'config')
64
# Prints nothing to stderr
65
self.assertEqual(stderr, '')
66
# returns successfully
67
self.assertEqual(rc, 0)
68
# Prints to stdout
69
self.assertTrue(stdout.startswith('Configuration:\n'))
70
71
def test_list(self):
72
rc, stdout, stderr = self.run_command(EXECUTABLE, 'list')
73
# Prints nothing to stderr
74
self.assertEqual(stderr, '')
75
# returns successfully
76
self.assertEqual(rc, 0)
77
# Prints to stdout
78
self.assertTrue('configure' in stdout.splitlines())
79
80
def test_name(self):
81
pkg = Package('configure')
82
rc, stdout, stderr = self.run_command(EXECUTABLE, 'name', pkg.tarball_filename)
83
# Prints nothing to stderr
84
self.assertEqual(stderr, '')
85
# returns successfully
86
self.assertEqual(rc, 0)
87
# Prints to stdout
88
self.assertEqual(stdout.rstrip(), 'configure')
89
90
def test_tarball(self):
91
pkg = Package('configure')
92
rc, stdout, stderr = self.run_command(EXECUTABLE, 'tarball', pkg.name)
93
# Prints nothing to stderr
94
self.assertEqual(stderr, '')
95
# returns successfully
96
self.assertEqual(rc, 0)
97
# Prints to stdout
98
self.assertEqual(stdout.rstrip(), pkg.tarball_filename)
99
100
def test_apropos(self):
101
rc, stdout, stderr = self.run_command(EXECUTABLE, 'apropos', 'python')
102
# Prints nothing to stderr
103
self.assertEqual(stderr, '')
104
# returns successfully
105
self.assertEqual(rc, 0)
106
# Prints to stdout
107
self.assertTrue(stdout.startswith('Did you mean:'))
108
109
def test_download(self):
110
pkg = Package('configure')
111
with CapturedLog() as _:
112
pkg.tarball.download()
113
rc, stdout, stderr = self.run_command(EXECUTABLE, 'download', pkg.name)
114
# Prints info to stderr
115
self.assertTrue(stderr.startswith('Using cached file'))
116
# returns successfully
117
self.assertEqual(rc, 0)
118
# Prints filename to stdout
119
self.assertEqual(stdout.rstrip(), pkg.tarball.upstream_fqn)
120
121
def test_update(self):
122
pkg = Package('configure')
123
# The confball never has a patchlevel since we are upstream...
124
self.assertEqual(pkg.patchlevel, -1)
125
rc, stdout, stderr = self.run_command(EXECUTABLE, 'update', pkg.name, pkg.version)
126
# Prints nothing to stderr
127
self.assertEqual(stderr, '')
128
# returns successfully
129
self.assertEqual(rc, 0)
130
# Prints nothing to stdout
131
self.assertEqual(stdout, '')
132
133
def test_fix_checksum(self):
134
pkg = Package('configure')
135
rc, stdout, stderr = self.run_command(EXECUTABLE, 'fix-checksum', 'configure')
136
# Prints nothing to stderr
137
self.assertEqual(stderr, '')
138
# returns successfully
139
self.assertEqual(rc, 0)
140
# Prints to stdout
141
self.assertEqual(
142
stdout.rstrip(),
143
'Checksum of {0} (tarball {1}) unchanged'.format(pkg.name, pkg.tarball_filename))
144
145
def test_create(self):
146
tmp = tempfile.mkdtemp()
147
with open(os.path.join(tmp, 'configure.ac'), 'w+') as f:
148
f.write('test')
149
os.mkdir(os.path.join(tmp, 'build'))
150
os.mkdir(os.path.join(tmp, 'build', 'pkgs'))
151
os.mkdir(os.path.join(tmp, 'upstream'))
152
with open(os.path.join(tmp, 'upstream', 'Foo-13.5.tgz'), 'w+') as f:
153
f.write('tarball content')
154
rc, stdout, stderr = self.run_command(
155
EXECUTABLE,
156
'create', 'foo',
157
'--version', '13.5',
158
'--tarball', 'Foo-VERSION.tgz',
159
'--type', 'standard',
160
env=dict(SAGE_ROOT=tmp)
161
)
162
self.assertEqual(rc, 0)
163
with open(os.path.join(tmp, 'build', 'pkgs', 'foo', 'package-version.txt')) as f:
164
self.assertEqual(f.read(), '13.5\n')
165
with open(os.path.join(tmp, 'build', 'pkgs', 'foo', 'type')) as f:
166
self.assertEqual(f.read(), 'standard\n')
167
with open(os.path.join(tmp, 'build', 'pkgs', 'foo', 'checksums.ini')) as f:
168
self.assertEqual(
169
f.read(),
170
'tarball=Foo-VERSION.tgz\n' +
171
'sha1=15d0e36e27c69bc758231f8e9add837f40a40cd0\n' +
172
'md5=bc62fed5e35f31aeea2af95c00473d4d\n' +
173
'cksum=1436769867\n'
174
)
175
shutil.rmtree(tmp)
176
177