Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/test/test_uncompress.py
4052 views
1
# -*- coding: utf-8 -*-
2
"""
3
Test sage extraction of tarball / zip 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
# http://www.gnu.org/licenses/
14
# ****************************************************************************
15
16
from __future__ import print_function, absolute_import
17
18
import os
19
import unittest
20
import shutil
21
import tempfile
22
import subprocess
23
24
from sage_bootstrap.uncompress.zip_file import SageZipFile
25
from sage_bootstrap.uncompress.tar_file import SageTarFile
26
from sage_bootstrap.uncompress.action import (
27
open_archive, unpack_archive
28
)
29
30
31
class UncompressTarFileTestCase(unittest.TestCase):
32
33
def setUp(self):
34
self.tmp = tempfile.mkdtemp()
35
self.filename = os.path.join(self.tmp, 'test.tar.gz')
36
self.make_tarfile()
37
38
def tearDown(self):
39
shutil.rmtree(self.tmp)
40
41
def make_tarfile(self):
42
src = os.path.join(self.tmp, 'src')
43
os.mkdir(src)
44
os.mkdir(os.path.join(src, 'foo'))
45
with open(os.path.join(src, 'content'), 'w+') as f:
46
f.write('root file test')
47
with open(os.path.join(src, 'foo', 'subcontent'), 'w+') as f:
48
f.write('subdirectory file test')
49
subprocess.check_call([
50
'tar', 'czf', self.filename, 'content', 'foo'
51
], cwd=src)
52
53
def test_can_read(self):
54
self.assertTrue(SageTarFile.can_read(self.filename))
55
self.assertFalse(SageZipFile.can_read(self.filename))
56
57
def test_tarball(self):
58
archive = open_archive(self.filename)
59
content = archive.extractbytes('content')
60
self.assertEqual(content, b'root file test')
61
dst = os.path.join(self.tmp, 'dst')
62
unpack_archive(archive, dst)
63
subprocess.check_call([
64
'diff', '-r', 'src', 'dst'
65
], cwd=self.tmp)
66
67
68
class UncompressZipFileTestCase(unittest.TestCase):
69
70
def setUp(self):
71
self.tmp = tempfile.mkdtemp()
72
self.filename = os.path.join(self.tmp, 'test.zip')
73
self.make_zipfile()
74
75
def tearDown(self):
76
shutil.rmtree(self.tmp)
77
78
def make_zipfile(self):
79
src = os.path.join(self.tmp, 'src')
80
os.mkdir(src)
81
os.mkdir(os.path.join(src, 'foo'))
82
with open(os.path.join(src, 'content'), 'w+') as f:
83
f.write('root file test')
84
with open(os.path.join(src, 'foo', 'subcontent'), 'w+') as f:
85
f.write('subdirectory file test')
86
subprocess.check_call([
87
'zip', '-q', '-r', self.filename, 'content', 'foo'
88
], cwd=src)
89
90
def test_can_read(self):
91
self.assertTrue(SageZipFile.can_read(self.filename))
92
self.assertFalse(SageTarFile.can_read(self.filename))
93
94
def test_zipfile(self):
95
archive = open_archive(self.filename)
96
content = archive.extractbytes('content')
97
self.assertEqual(content, b'root file test')
98
dst = os.path.join(self.tmp, 'dst')
99
unpack_archive(archive, dst)
100
subprocess.check_call([
101
'diff', '-r', 'src', 'dst'
102
], cwd=self.tmp)
103
104