Skip to content

Commit 7522073

Browse files
committed
Improve test coverage for command line functions
1 parent e5fc52f commit 7522073

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

mrcfile/validator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
unicode_literals)
1818

1919
import argparse
20+
import sys
2021

2122
from . import load_functions
2223

23-
def main():
24+
def main(args=None):
2425
"""
2526
Validate a list of MRC files given as command arguments.
2627
@@ -32,14 +33,16 @@ def main():
3233
``0`` if all command arguments are names of valid MRC files. ``1`` if
3334
no file names are given or any of the files is not a valid MRC file.
3435
"""
36+
if args is None:
37+
args = sys.argv[1:]
3538
parser = argparse.ArgumentParser(
3639
description="Validate a list of MRC files. Exit status is 0 if all "
3740
"input files are valid, or 1 if any input file is "
3841
"invalid. Descriptions of the problems with any invalid "
3942
"files are written to the standard output."
4043
)
4144
parser.add_argument("filename", nargs='*', help="Input MRC file")
42-
args = parser.parse_args()
45+
args = parser.parse_args(args)
4346
names = args.filename
4447
if validate_all(names):
4548
return 0

tests/test_command_line.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import tempfile
1717
import unittest
1818

19-
from mrcfile import command_line
19+
from mrcfile import command_line, validator
2020
from . import helpers
2121

2222

@@ -56,7 +56,21 @@ def tearDown(self):
5656
if os.path.exists(self.test_output):
5757
shutil.rmtree(self.test_output)
5858
super(CommandLineTest, self).tearDown()
59-
59+
60+
def test_print_header_no_args(self):
61+
command_line.print_headers([], print_file=self.print_stream)
62+
assert len(self.print_stream.getvalue()) == 0
63+
assert len(sys.stdout.getvalue()) == 0
64+
assert len(sys.stderr.getvalue()) == 0
65+
66+
def test_print_header_nonexistent_file(self):
67+
with self.assertRaisesRegex(IOError, "No such file"):
68+
command_line.print_headers(["nonexistent.mrc"],
69+
print_file=self.print_stream)
70+
assert len(self.print_stream.getvalue()) == 0
71+
assert len(sys.stdout.getvalue()) == 0
72+
assert len(sys.stderr.getvalue()) == 0
73+
6074
def test_print_header(self):
6175
command_line.print_headers(self.files, print_file=self.print_stream)
6276
printed = self.print_stream.getvalue()
@@ -67,6 +81,20 @@ def test_print_header(self):
6781
assert len(sys.stdout.getvalue()) == 0
6882
assert len(sys.stderr.getvalue()) == 0
6983

84+
def test_validate_no_args(self):
85+
result = validator.main([])
86+
assert result == 0
87+
assert len(self.print_stream.getvalue()) == 0
88+
assert len(sys.stdout.getvalue()) == 0
89+
assert len(sys.stderr.getvalue()) == 0
90+
91+
def test_validate(self):
92+
result = validator.main(self.files)
93+
assert result == 1
94+
stdout = str(sys.stdout.getvalue())
95+
assert "File does not declare MRC format version 20140" in stdout
96+
assert len(sys.stderr.getvalue()) == 0
97+
7098

7199
if __name__ == '__main__':
72100
unittest.main()

0 commit comments

Comments
 (0)