Skip to content

Commit a9b769d

Browse files
committed
add tests for cli bag creation
1 parent 403f4e8 commit a9b769d

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

test_cli.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# encoding: utf-8
2+
3+
from __future__ import absolute_import, division, print_function, unicode_literals
4+
import argparse
5+
6+
import codecs
7+
import datetime
8+
import hashlib
9+
import logging
10+
import os
11+
import shutil
12+
import stat
13+
import sys
14+
import tempfile
15+
import unicodedata
16+
import unittest
17+
from unittest.mock import patch
18+
from os.path import join as j
19+
from io import StringIO
20+
21+
import mock
22+
23+
import bagit
24+
25+
logging.basicConfig(filename="test.log", level=logging.DEBUG)
26+
stderr = logging.StreamHandler()
27+
stderr.setLevel(logging.WARNING)
28+
logging.getLogger().addHandler(stderr)
29+
30+
# But we do want any exceptions raised in the logging path to be raised:
31+
logging.raiseExceptions = True
32+
33+
34+
def slurp_text_file(filename):
35+
with bagit.open_text_file(filename) as f:
36+
return f.read()
37+
38+
39+
class SelfCleaningTestCase(unittest.TestCase):
40+
"""TestCase subclass which cleans up self.tmpdir after each test"""
41+
42+
def setUp(self):
43+
super(SelfCleaningTestCase, self).setUp()
44+
45+
self.starting_directory = (
46+
os.getcwd()
47+
) # FIXME: remove this after we stop changing directories in bagit.py
48+
self.tmpdir = tempfile.mkdtemp()
49+
if os.path.isdir(self.tmpdir):
50+
shutil.rmtree(self.tmpdir)
51+
shutil.copytree("test-data", self.tmpdir)
52+
53+
def tearDown(self):
54+
# FIXME: remove this after we stop changing directories in bagit.py
55+
os.chdir(self.starting_directory)
56+
if os.path.isdir(self.tmpdir):
57+
# Clean up after tests which leave inaccessible files behind:
58+
59+
os.chmod(self.tmpdir, 0o700)
60+
61+
for dirpath, subdirs, filenames in os.walk(self.tmpdir, topdown=True):
62+
for i in subdirs:
63+
os.chmod(os.path.join(dirpath, i), 0o700)
64+
65+
shutil.rmtree(self.tmpdir)
66+
67+
super(SelfCleaningTestCase, self).tearDown()
68+
69+
class TestSingleProcessValidation(SelfCleaningTestCase):
70+
71+
@mock.patch('sys.stderr', new_callable=StringIO)
72+
def test_missing_directory(self, mock_stderr):
73+
testargs = ["bagit.py"]
74+
75+
with self.assertRaises(SystemExit) as cm:
76+
with mock.patch.object(sys, 'argv', testargs):
77+
bagit.main()
78+
79+
self.assertEqual(cm.exception.code, 2)
80+
self.assertIn(
81+
"error: the following arguments are required: directory",
82+
mock_stderr.getvalue()
83+
)
84+
85+
@mock.patch('sys.stderr', new_callable=StringIO)
86+
def test_not_enough_processes(self, mock_stderr):
87+
testargs = ["bagit.py", "--processes", "0", self.tmpdir]
88+
89+
with self.assertRaises(SystemExit) as cm:
90+
with mock.patch.object(sys, 'argv', testargs):
91+
bagit.main()
92+
93+
self.assertEqual(cm.exception.code, 2)
94+
self.assertIn(
95+
"error: The number of processes must be greater than 0",
96+
mock_stderr.getvalue()
97+
)
98+
99+
@mock.patch('sys.stderr', new_callable=StringIO)
100+
def test_fast_flag_without_validate(self, mock_stderr):
101+
bag = bagit.make_bag(self.tmpdir)
102+
testargs = ["bagit.py", "--fast", self.tmpdir]
103+
104+
with self.assertRaises(SystemExit) as cm:
105+
with mock.patch.object(sys, 'argv', testargs):
106+
bagit.main()
107+
108+
self.assertEqual(cm.exception.code, 2)
109+
self.assertIn(
110+
"error: --fast is only allowed as an option for --validate!",
111+
mock_stderr.getvalue()
112+
)
113+
114+
def test_invalid_fast_validate(self):
115+
assert True
116+
117+
def test_valid_fast_validate(self):
118+
bag = bagit.make_bag(self.tmpdir)
119+
testargs = ["bagit.py", "--validate", "--fast", self.tmpdir]
120+
121+
with self.assertLogs() as captured:
122+
with self.assertRaises(SystemExit) as cm:
123+
with mock.patch.object(sys, 'argv', testargs):
124+
bagit.main()
125+
126+
self.assertEqual(cm.exception.code, 0)
127+
self.assertEqual(
128+
"%s valid according to Payload-Oxum" % self.tmpdir,
129+
captured.records[0].getMessage()
130+
)
131+
132+
def test_completeness_flag_without_validate(self):
133+
assert True
134+
135+
def test_invalid_completeness_validate(self):
136+
assert True
137+
138+
def test_valid_completeness_validate(self):
139+
assert True
140+
141+
def test_valid_full_validate(self):
142+
assert True
143+
144+
def test_invalid_full_validate(self):
145+
bag = bagit.make_bag(self.tmpdir)
146+
readme = j(self.tmpdir, "data", "README")
147+
txt = slurp_text_file(readme)
148+
txt = "A" + txt[1:]
149+
with open(readme, "w") as r:
150+
r.write(txt)
151+
152+
testargs = ["bagit.py", "--validate", self.tmpdir]
153+
154+
with self.assertLogs() as captured:
155+
with self.assertRaises(SystemExit) as cm:
156+
with mock.patch.object(sys, 'argv', testargs):
157+
bagit.main()
158+
159+
self.assertEqual(cm.exception.code, 1)
160+
self.assertEqual(
161+
"%s is valid" % self.tmpdir,
162+
captured.records[-1].getMessage()
163+
)
164+
165+
def test_failed_create_bag(self):
166+
os.chmod(self.tmpdir, 0)
167+
168+
testargs = ["bagit.py", self.tmpdir]
169+
170+
with self.assertLogs() as captured:
171+
with self.assertRaises(SystemExit) as cm:
172+
with mock.patch.object(sys, 'argv', testargs):
173+
bagit.main()
174+
175+
self.assertEqual(cm.exception.code, 1)
176+
self.assertIn(
177+
"Failed to create bag in %s" % self.tmpdir,
178+
captured.records[-1].getMessage()
179+
)
180+
181+
def test_create_bag(self):
182+
testargs = ["bagit.py", self.tmpdir]
183+
184+
with self.assertLogs() as captured:
185+
with self.assertRaises(SystemExit) as cm:
186+
with mock.patch.object(sys, 'argv', testargs):
187+
bagit.main()
188+
189+
for rec in captured.records:
190+
print(rec.getMessage())
191+
192+
self.assertEqual(cm.exception.code, 0)

0 commit comments

Comments
 (0)