Skip to content

Commit

Permalink
Improve test coverage for command line functions
Browse files Browse the repository at this point in the history
  • Loading branch information
colinpalmer committed Feb 18, 2021
1 parent e5fc52f commit 7522073
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
7 changes: 5 additions & 2 deletions mrcfile/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
unicode_literals)

import argparse
import sys

from . import load_functions

def main():
def main(args=None):
"""
Validate a list of MRC files given as command arguments.
Expand All @@ -32,14 +33,16 @@ def main():
``0`` if all command arguments are names of valid MRC files. ``1`` if
no file names are given or any of the files is not a valid MRC file.
"""
if args is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser(
description="Validate a list of MRC files. Exit status is 0 if all "
"input files are valid, or 1 if any input file is "
"invalid. Descriptions of the problems with any invalid "
"files are written to the standard output."
)
parser.add_argument("filename", nargs='*', help="Input MRC file")
args = parser.parse_args()
args = parser.parse_args(args)
names = args.filename
if validate_all(names):
return 0
Expand Down
32 changes: 30 additions & 2 deletions tests/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import tempfile
import unittest

from mrcfile import command_line
from mrcfile import command_line, validator
from . import helpers


Expand Down Expand Up @@ -56,7 +56,21 @@ def tearDown(self):
if os.path.exists(self.test_output):
shutil.rmtree(self.test_output)
super(CommandLineTest, self).tearDown()


def test_print_header_no_args(self):
command_line.print_headers([], print_file=self.print_stream)
assert len(self.print_stream.getvalue()) == 0
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0

def test_print_header_nonexistent_file(self):
with self.assertRaisesRegex(IOError, "No such file"):
command_line.print_headers(["nonexistent.mrc"],
print_file=self.print_stream)
assert len(self.print_stream.getvalue()) == 0
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0

def test_print_header(self):
command_line.print_headers(self.files, print_file=self.print_stream)
printed = self.print_stream.getvalue()
Expand All @@ -67,6 +81,20 @@ def test_print_header(self):
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0

def test_validate_no_args(self):
result = validator.main([])
assert result == 0
assert len(self.print_stream.getvalue()) == 0
assert len(sys.stdout.getvalue()) == 0
assert len(sys.stderr.getvalue()) == 0

def test_validate(self):
result = validator.main(self.files)
assert result == 1
stdout = str(sys.stdout.getvalue())
assert "File does not declare MRC format version 20140" in stdout
assert len(sys.stderr.getvalue()) == 0


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

0 comments on commit 7522073

Please sign in to comment.