summaryrefslogtreecommitdiff
path: root/test/libtest.py
diff options
context:
space:
mode:
authorjvoisin2016-03-27 19:08:48 +0200
committerjvoisin2016-03-27 19:08:48 +0200
commit368474eaaa6cce9d219ff1963ff31e521eca62e9 (patch)
treeaffd71b4f51861178545c44892c061c0e4e4b56f /test/libtest.py
parentcae9853318964813b60db05e71d431eb49366d34 (diff)
Move some testfiles around
The long-term goal is to use nose2 for testing
Diffstat (limited to 'test/libtest.py')
-rw-r--r--test/libtest.py203
1 files changed, 0 insertions, 203 deletions
diff --git a/test/libtest.py b/test/libtest.py
deleted file mode 100644
index 98cb4ea..0000000
--- a/test/libtest.py
+++ /dev/null
@@ -1,203 +0,0 @@
1#!/usr/bin/env python
2# -*- coding: utf-8 -*
3
4"""
5 Unit test for the library
6"""
7
8import os
9import sys
10import stat
11import shutil
12import tarfile
13import tempfile
14import unittest
15
16import test
17import libmat
18
19
20class TestRemovelib(test.MATTest):
21 """ test the remove_all() method
22 """
23
24 def test_remove(self):
25 """make sure that the lib remove all compromizing meta"""
26 for _, dirty in self.file_list:
27 current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
28 current_file.remove_all()
29 current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
30 self.assertTrue(current_file.is_clean())
31
32 def test_remove_fileformat_specific_options(self):
33 """ test metadata removal with fileformat-specific options """
34 for _, dirty in self.file_list: # can't be faster than that :/
35 if dirty.endswith('pdf'):
36 current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
37 current_file.remove_all()
38 current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
39 self.assertTrue(current_file.is_clean())
40
41 def test_remove_empty(self):
42 """Test removal with clean files"""
43 for clean, _ in self.file_list:
44 current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
45 current_file.remove_all()
46 current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
47 self.assertTrue(current_file.is_clean())
48
49
50class TestListlib(test.MATTest):
51 """ test the get_meta() method
52 """
53
54 def test_list(self):
55 """check if get_meta returns metadata"""
56 for _, dirty in self.file_list:
57 current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
58 self.assertIsNotNone(current_file.get_meta())
59
60 def testlist_list_empty(self):
61 """check that a listing of a clean file returns an empty dict"""
62 for clean, _ in self.file_list:
63 current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
64 self.assertEqual(current_file.get_meta(), dict())
65
66
67class TestisCleanlib(test.MATTest):
68 """ Test the is_clean() method
69 """
70
71 def test_dirty(self):
72 """test is_clean on dirty files"""
73 for _, dirty in self.file_list:
74 current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
75 self.assertFalse(current_file.is_clean())
76
77 def test_clean(self):
78 """test is_clean on clean files"""
79 for clean, _ in self.file_list:
80 current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
81 self.assertTrue(current_file.is_clean())
82
83
84class TestFileAttributes(unittest.TestCase):
85 """
86 test various stuffs about files (readable, writable, exist, ...)
87 """
88
89 def test_not_exist(self):
90 """ test MAT's behaviour on non-existent file"""
91 self.assertFalse(libmat.mat.create_class_file('non_existent_file', False, add2archive=True))
92
93 def test_empty(self):
94 """ test MAT's behaviour on empty file"""
95 open('empty_file', 'a').close()
96 self.assertFalse(libmat.mat.create_class_file('empty_file', False, add2archive=True))
97 os.remove('empty_file')
98
99 def test_not_writtable(self):
100 """ test MAT's behaviour on non-writable file"""
101 self.assertFalse(libmat.mat.create_class_file('not_writtable', False, add2archive=True))
102
103 def test_not_readable(self):
104 """ test MAT's behaviour on non-readable file"""
105 open('non_readable', 'a').close()
106 os.chmod('non_readable', 0 | stat.S_IWRITE)
107 self.assertFalse(libmat.mat.create_class_file('non_readable', False, add2archive=True))
108 os.remove('non_readable')
109
110
111class TestSecureRemove(unittest.TestCase):
112 """ Test the secure_remove function
113 """
114
115 def test_remove_existing(self):
116 """ test the secure removal of an existing file
117 """
118 _, file_to_remove = tempfile.mkstemp()
119 self.assertTrue(libmat.mat.secure_remove(file_to_remove))
120
121 def test_remove_fail(self):
122 """ test the secure removal of an non-removable file
123 """
124 self.assertRaises(libmat.exceptions.UnableToWriteFile, libmat.mat.secure_remove, '/NOTREMOVABLE')
125
126
127class TestArchiveProcessing(test.MATTest):
128 """ Test archives processing
129 """
130
131 def test_remove_bz2(self):
132 """ Test MAT's ability to process .tar.bz2
133 """
134 tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
135 tar = tarfile.open(tarpath, "w:bz2")
136 for clean, dirty in self.file_list:
137 tar.add(dirty)
138 tar.add(clean)
139 tar.close()
140 current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
141 current_file.remove_all()
142 current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
143 self.assertTrue(current_file.is_clean())
144
145 def test_remove_tar(self):
146 """ Test MAT on tar files
147 """
148 tarpath = os.path.join(self.tmpdir, "test.tar")
149 tar = tarfile.open(tarpath, "w")
150 for clean, dirty in self.file_list:
151 tar.add(dirty)
152 tar.add(clean)
153 tar.close()
154 current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
155 current_file.remove_all()
156 current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
157 self.assertTrue(current_file.is_clean())
158
159 def test_remove_gz(self):
160 """ Test MAT on tar.gz files
161 """
162 tarpath = os.path.join(self.tmpdir, "test.tar.gz")
163 tar = tarfile.open(tarpath, "w")
164 for clean, dirty in self.file_list:
165 tar.add(dirty)
166 tar.add(clean)
167 tar.close()
168 current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
169 current_file.remove_all()
170 current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
171 self.assertTrue(current_file.is_clean())
172
173 def test_get_unsupported(self):
174 """ Test the get_unsupported feature, used by the GUI
175 """
176 tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
177 tar = tarfile.open(tarpath, "w")
178 for f in ('libtest.py', 'test.py', 'clitest.py'):
179 tar.add(f, f)
180 tar.close()
181 current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
182 unsupported_files = set(current_file.is_clean(list_unsupported=True))
183 self.assertEqual(unsupported_files, {'libtest.py', 'test.py', 'clitest.py'})
184
185 def test_archive_unwritable_content(self):
186 path = os.path.join(self.tmpdir, './unwritable_content.zip')
187 shutil.copy2('./unwritable_content.zip', self.tmpdir)
188 current_file = libmat.mat.create_class_file(path, False, add2archive=False)
189 current_file.remove_all()
190 current_file = libmat.mat.create_class_file(path, False, add2archive=False)
191 self.assertTrue(current_file.is_clean())
192
193
194def get_tests():
195 """ Returns every libtests"""
196 suite = unittest.TestSuite()
197 suite.addTest(unittest.makeSuite(TestRemovelib))
198 suite.addTest(unittest.makeSuite(TestListlib))
199 suite.addTest(unittest.makeSuite(TestisCleanlib))
200 suite.addTest(unittest.makeSuite(TestFileAttributes))
201 suite.addTest(unittest.makeSuite(TestSecureRemove))
202 suite.addTest(unittest.makeSuite(TestArchiveProcessing))
203 return suite