diff options
| author | jvoisin | 2014-02-01 04:10:09 +0000 |
|---|---|---|
| committer | jvoisin | 2014-02-01 04:10:09 +0000 |
| commit | 36c963ef60babe18c5bbd0feb624f2d5615a23df (patch) | |
| tree | 7c08971388f54ba2b7f56ba3a783872540c6b53e | |
| parent | 173449009769ce86493a179acb9c66c87125dce3 (diff) | |
Fix https://labs.riseup.net/code/issues/6416
This commit fix the "Does not inform the user when files where lost
while cleaning an archive" (for the CLI) bug, and adds a test for it.
| -rwxr-xr-x | mat | 30 | ||||
| -rw-r--r-- | test/clitest.py | 26 |
2 files changed, 41 insertions, 15 deletions
| @@ -12,6 +12,7 @@ import hachoir_core | |||
| 12 | 12 | ||
| 13 | from MAT import mat | 13 | from MAT import mat |
| 14 | from MAT import strippers | 14 | from MAT import strippers |
| 15 | from MAT import archive | ||
| 15 | 16 | ||
| 16 | 17 | ||
| 17 | def parse(): | 18 | def parse(): |
| @@ -59,7 +60,7 @@ def display_version(*_): | |||
| 59 | sys.exit(0) | 60 | sys.exit(0) |
| 60 | 61 | ||
| 61 | 62 | ||
| 62 | def list_meta(class_file, filename, force): | 63 | def list_meta(class_file, filename, force, add2archive): |
| 63 | ''' | 64 | ''' |
| 64 | Print all the metadata of 'filename' on stdout | 65 | Print all the metadata of 'filename' on stdout |
| 65 | ''' | 66 | ''' |
| @@ -74,7 +75,7 @@ def list_meta(class_file, filename, force): | |||
| 74 | print('\t' + key + ' : ' + str(value)) | 75 | print('\t' + key + ' : ' + str(value)) |
| 75 | 76 | ||
| 76 | 77 | ||
| 77 | def is_clean(class_file, filename, force): | 78 | def is_clean(class_file, filename, force, add2archive): |
| 78 | ''' | 79 | ''' |
| 79 | Say if 'filename' is clean or not | 80 | Say if 'filename' is clean or not |
| 80 | ''' | 81 | ''' |
| @@ -84,21 +85,30 @@ def is_clean(class_file, filename, force): | |||
| 84 | print('[+] %s is not clean' % filename) | 85 | print('[+] %s is not clean' % filename) |
| 85 | 86 | ||
| 86 | 87 | ||
| 87 | def clean_meta(class_file, filename, force): | 88 | def clean_meta(class_file, filename, force, add2archive): |
| 88 | ''' | 89 | ''' |
| 89 | Clean the file 'filename' | 90 | Clean the file 'filename' |
| 90 | ''' | 91 | ''' |
| 91 | if not class_file.is_writable: | 92 | if not class_file.is_writable: |
| 92 | print('[-] %s is not writable' % filename) | 93 | print('[-] %s is not writable' % filename) |
| 93 | return | 94 | return |
| 94 | print('[+] Cleaning %s' % filename) | 95 | print('[*] Cleaning %s' % filename) |
| 95 | if not force and class_file.is_clean(): | 96 | if not force and class_file.is_clean(): |
| 96 | print('%s is already clean' % filename) | 97 | print('[+] %s is already clean' % filename) |
| 98 | elif not add2archive: | ||
| 99 | is_archive = isinstance(class_file, archive.GenericArchiveStripper) | ||
| 100 | is_terminal = isinstance(class_file, archive.TerminalZipStripper) | ||
| 101 | if is_archive and not is_terminal: | ||
| 102 | unsupported_list = class_file.list_unsupported() | ||
| 103 | if type(unsupported_list) == list and unsupported_list: | ||
| 104 | print('[-] Can not clean: %s.'\ | ||
| 105 | 'It contains unsupported filetypes:' % filename) | ||
| 106 | for i in unsupported_list: | ||
| 107 | print('- %s' % i) | ||
| 108 | elif class_file.remove_all(): | ||
| 109 | print('[+] %s cleaned !' % filename) | ||
| 97 | else: | 110 | else: |
| 98 | if class_file.remove_all(): | 111 | print('[-] Unable to clean %s', filename) |
| 99 | print('%s cleaned !' % filename) | ||
| 100 | else: | ||
| 101 | print('Unable to clean %s', filename) | ||
| 102 | 112 | ||
| 103 | 113 | ||
| 104 | def list_supported(): | 114 | def list_supported(): |
| @@ -142,7 +152,7 @@ def main(): | |||
| 142 | class_file = mat.create_class_file(filename, args.backup, | 152 | class_file = mat.create_class_file(filename, args.backup, |
| 143 | add2archive=args.add2archive, low_pdf_quality=args.low_pdf_quality) | 153 | add2archive=args.add2archive, low_pdf_quality=args.low_pdf_quality) |
| 144 | if class_file: | 154 | if class_file: |
| 145 | func(class_file, filename, args.force) | 155 | func(class_file, filename, args.force, args.add2archive) |
| 146 | else: | 156 | else: |
| 147 | print('Unable to process %s' % filename) | 157 | print('Unable to process %s' % filename) |
| 148 | 158 | ||
diff --git a/test/clitest.py b/test/clitest.py index 312822f..ad895f5 100644 --- a/test/clitest.py +++ b/test/clitest.py | |||
| @@ -5,9 +5,11 @@ | |||
| 5 | Unit test for the CLI interface | 5 | Unit test for the CLI interface |
| 6 | ''' | 6 | ''' |
| 7 | 7 | ||
| 8 | import os | ||
| 8 | import unittest | 9 | import unittest |
| 9 | import subprocess | 10 | import subprocess |
| 10 | import sys | 11 | import sys |
| 12 | import tarfile | ||
| 11 | 13 | ||
| 12 | sys.path.append('..') | 14 | sys.path.append('..') |
| 13 | from MAT import mat | 15 | from MAT import mat |
| @@ -21,14 +23,14 @@ class TestRemovecli(test.MATTest): | |||
| 21 | def test_remove(self): | 23 | def test_remove(self): |
| 22 | '''make sure that the cli remove all compromizing meta''' | 24 | '''make sure that the cli remove all compromizing meta''' |
| 23 | for _, dirty in self.file_list: | 25 | for _, dirty in self.file_list: |
| 24 | subprocess.call(['../mat', dirty]) | 26 | subprocess.call(['../mat', '--add2archive', dirty]) |
| 25 | current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) | 27 | current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) |
| 26 | self.assertTrue(current_file.is_clean()) | 28 | self.assertTrue(current_file.is_clean()) |
| 27 | 29 | ||
| 28 | def test_remove_empty(self): | 30 | def test_remove_empty(self): |
| 29 | '''Test removal with clean files''' | 31 | '''Test removal with clean files\n''' |
| 30 | for clean, _ in self.file_list: | 32 | for clean, _ in self.file_list: |
| 31 | subprocess.call(['../mat', clean]) | 33 | subprocess.call(['../mat', '--add2archive', clean]) |
| 32 | current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True) | 34 | current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True) |
| 33 | self.assertTrue(current_file.is_clean()) | 35 | self.assertTrue(current_file.is_clean()) |
| 34 | 36 | ||
| @@ -97,11 +99,24 @@ class TestFileAttributes(unittest.TestCase): | |||
| 97 | 99 | ||
| 98 | def test_empty(self): | 100 | def test_empty(self): |
| 99 | ''' test MAT's behaviour on empty file''' | 101 | ''' test MAT's behaviour on empty file''' |
| 100 | proc = subprocess.Popen(['../mat', 'empty_file'], | 102 | proc = subprocess.Popen(['../mat', 'empty_file'], stdout=subprocess.PIPE) |
| 101 | stdout=subprocess.PIPE) | ||
| 102 | stdout, _ = proc.communicate() | 103 | stdout, _ = proc.communicate() |
| 103 | self.assertEqual(str(stdout).strip('\n'), 'Unable to process %s' % 'ilikecookies') | 104 | self.assertEqual(str(stdout).strip('\n'), 'Unable to process %s' % 'ilikecookies') |
| 104 | 105 | ||
| 106 | class TestUnsupported(test.MATTest): | ||
| 107 | def test_abort_unsupported(self): | ||
| 108 | ''' test if the cli aborts on unsupported files | ||
| 109 | ''' | ||
| 110 | tarpath = os.path.join(self.tmpdir, "test.tar.bz2") | ||
| 111 | tar = tarfile.open(tarpath, "w") | ||
| 112 | for f in ('../mat.desktop', '../README.security', '../setup.py'): | ||
| 113 | tar.add(f, f[3:]) # trim '../' | ||
| 114 | tar.close() | ||
| 115 | proc = subprocess.Popen(['../mat', tarpath], stdout=subprocess.PIPE) | ||
| 116 | stdout, _ = proc.communicate() | ||
| 117 | self.assertTrue('It contains unsupported filetypes:'\ | ||
| 118 | '\n- mat.desktop\n- README.security\n- setup.py\n' | ||
| 119 | in str(stdout)) | ||
| 105 | 120 | ||
| 106 | def get_tests(): | 121 | def get_tests(): |
| 107 | ''' Return every clitests''' | 122 | ''' Return every clitests''' |
| @@ -109,4 +124,5 @@ def get_tests(): | |||
| 109 | suite.addTest(unittest.makeSuite(TestRemovecli)) | 124 | suite.addTest(unittest.makeSuite(TestRemovecli)) |
| 110 | suite.addTest(unittest.makeSuite(TestListcli)) | 125 | suite.addTest(unittest.makeSuite(TestListcli)) |
| 111 | suite.addTest(unittest.makeSuite(TestisCleancli)) | 126 | suite.addTest(unittest.makeSuite(TestisCleancli)) |
| 127 | suite.addTest(unittest.makeSuite(TestUnsupported)) | ||
| 112 | return suite | 128 | return suite |
