summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin2018-09-05 17:22:17 +0200
committerjvoisin2018-09-05 17:26:09 +0200
commit46bb1b83ea0843c25783f86e2033a10aeaed79d2 (patch)
tree3446382de51c0a861f4158f6be9e65bcf94b06ad
parent1d7e374e5b0c6a84b3319d9d1e7c6f5c222af878 (diff)
Improve the previous commit
Diffstat (limited to '')
-rw-r--r--libmat2/office.py8
-rwxr-xr-xmat24
-rw-r--r--tests/test_climat2.py4
-rw-r--r--tests/test_policy.py31
4 files changed, 40 insertions, 7 deletions
diff --git a/libmat2/office.py b/libmat2/office.py
index e79fe58..224067c 100644
--- a/libmat2/office.py
+++ b/libmat2/office.py
@@ -84,6 +84,11 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
84 84
85 def remove_all(self) -> bool: 85 def remove_all(self) -> bool:
86 # pylint: disable=too-many-branches 86 # pylint: disable=too-many-branches
87
88 if self.unknown_member_policy not in ['omit', 'keep', 'abort']:
89 logging.error("The policy %s is invalid.", self.unknown_member_policy)
90 raise ValueError
91
87 with zipfile.ZipFile(self.filename) as zin,\ 92 with zipfile.ZipFile(self.filename) as zin,\
88 zipfile.ZipFile(self.output_filename, 'w') as zout: 93 zipfile.ZipFile(self.output_filename, 'w') as zout:
89 94
@@ -120,9 +125,6 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
120 logging.warning("In file %s, keeping unknown element %s (format: %s)", 125 logging.warning("In file %s, keeping unknown element %s (format: %s)",
121 self.filename, item.filename, mtype) 126 self.filename, item.filename, mtype)
122 else: 127 else:
123 if self.unknown_member_policy != 'abort':
124 logging.warning("Invalid unknown_member_policy %s, " +
125 "treating as 'abort'", self.unknown_member_policy)
126 logging.error("In file %s, element %s's format (%s) " + 128 logging.error("In file %s, element %s's format (%s) " +
127 "isn't supported", 129 "isn't supported",
128 self.filename, item.filename, mtype) 130 self.filename, item.filename, mtype)
diff --git a/mat2 b/mat2
index b45892e..c7f7a73 100755
--- a/mat2
+++ b/mat2
@@ -41,9 +41,9 @@ def create_arg_parser():
41 help='check if MAT2 has all the dependencies it needs') 41 help='check if MAT2 has all the dependencies it needs')
42 parser.add_argument('-V', '--verbose', action='store_true', 42 parser.add_argument('-V', '--verbose', action='store_true',
43 help='show more verbose status information') 43 help='show more verbose status information')
44 parser.add_argument('-u', '--unknown-members', metavar='POLICY', default='abort', 44 parser.add_argument('-u', '--unknown-members', metavar='policy', default='abort',
45 help='how to handle unknown members of archive-style files ' + 45 help='how to handle unknown members of archive-style files ' +
46 '(POLICY should be abort, omit, or keep)') 46 '(policy should be abort, omit, or keep)')
47 47
48 48
49 info = parser.add_mutually_exclusive_group() 49 info = parser.add_mutually_exclusive_group()
diff --git a/tests/test_climat2.py b/tests/test_climat2.py
index 6ee84d5..9614347 100644
--- a/tests/test_climat2.py
+++ b/tests/test_climat2.py
@@ -8,13 +8,13 @@ class TestHelp(unittest.TestCase):
8 def test_help(self): 8 def test_help(self):
9 proc = subprocess.Popen(['./mat2', '--help'], stdout=subprocess.PIPE) 9 proc = subprocess.Popen(['./mat2', '--help'], stdout=subprocess.PIPE)
10 stdout, _ = proc.communicate() 10 stdout, _ = proc.communicate()
11 self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-V] [-u POLICY] [-s | -L] [files [files ...]]', 11 self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-V] [-u policy] [-s | -L] [files [files ...]]',
12 stdout) 12 stdout)
13 13
14 def test_no_arg(self): 14 def test_no_arg(self):
15 proc = subprocess.Popen(['./mat2'], stdout=subprocess.PIPE) 15 proc = subprocess.Popen(['./mat2'], stdout=subprocess.PIPE)
16 stdout, _ = proc.communicate() 16 stdout, _ = proc.communicate()
17 self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-V] [-u POLICY] [-s | -L] [files [files ...]]', 17 self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-V] [-u policy] [-s | -L] [files [files ...]]',
18 stdout) 18 stdout)
19 19
20 20
diff --git a/tests/test_policy.py b/tests/test_policy.py
new file mode 100644
index 0000000..39282b1
--- /dev/null
+++ b/tests/test_policy.py
@@ -0,0 +1,31 @@
1#!/usr/bin/python3
2
3import unittest
4import shutil
5import os
6
7from libmat2 import office
8
9class TestPolicy(unittest.TestCase):
10 def test_policy_omit(self):
11 shutil.copy('./tests/data/embedded.docx', './tests/data/clean.docx')
12 p = office.MSOfficeParser('./tests/data/clean.docx')
13 p.unknown_member_policy = 'omit'
14 self.assertTrue(p.remove_all())
15 os.remove('./tests/data/clean.docx')
16
17 def test_policy_keep(self):
18 shutil.copy('./tests/data/embedded.docx', './tests/data/clean.docx')
19 p = office.MSOfficeParser('./tests/data/clean.docx')
20 p.unknown_member_policy = 'keep'
21 self.assertTrue(p.remove_all())
22 os.remove('./tests/data/clean.docx')
23
24 def test_policy_unknown(self):
25 shutil.copy('./tests/data/embedded.docx', './tests/data/clean.docx')
26 p = office.MSOfficeParser('./tests/data/clean.docx')
27 p.unknown_member_policy = 'unknown_policy_name_totally_invalid'
28 with self.assertRaises(ValueError):
29 p.remove_all()
30 os.remove('./tests/data/clean.docx')
31