99from __future__ import (absolute_import , division , print_function ,
1010 unicode_literals )
1111
12- import io
1312import os
1413import shutil
1514import sys
1615import tempfile
1716import unittest
1817import warnings
1918
19+ # Avoid str/unicode issues when traceback.print_exc tries to write to StringIO
20+ # (see https://stackoverflow.com/a/34872005)
21+ try :
22+ # Python 2
23+ from cStringIO import StringIO
24+ except ImportError :
25+ # Python 3
26+ from io import StringIO
27+
2028import numpy as np
2129
2230import mrcfile
@@ -43,15 +51,16 @@ def setUp(self):
4351 self .ext_header_mrc_name = os .path .join (self .test_data , 'EMD-3001.map' )
4452 self .fei1_ext_header_mrc_name = os .path .join (self .test_data , 'fei-extended.mrc' )
4553 self .fei2_ext_header_mrc_name = os .path .join (self .test_data , 'epu2.9_example.mrc' )
54+ self .not_an_mrc_name = os .path .join (self .test_data , 'README.txt' )
4655
4756 # Set up stream to catch print output from validate()
48- self .print_stream = io . StringIO ()
57+ self .print_stream = StringIO ()
4958
5059 # Replace stdout and stderr to capture output for checking
5160 self .orig_stdout = sys .stdout
5261 self .orig_stderr = sys .stderr
53- sys .stdout = io . StringIO ()
54- sys .stderr = io . StringIO ()
62+ sys .stdout = StringIO ()
63+ sys .stderr = StringIO ()
5564
5665 def tearDown (self ):
5766 # Restore stdout and stderr
@@ -562,6 +571,7 @@ def test_validate_good_files(self):
562571
563572 def test_validate_bad_files (self ):
564573 bad_files = [
574+ self .not_an_mrc_name ,
565575 self .example_mrc_name ,
566576 self .ext_header_mrc_name ,
567577 self .gzip_mrc_name
@@ -570,11 +580,17 @@ def test_validate_bad_files(self):
570580 assert result == False
571581 print_output = self .print_stream .getvalue ()
572582 assert len (print_output ) > 0
583+ assert "Checking if " + bad_files [0 ] + " is a valid MRC2014 file..." in print_output
584+ assert "Checking if " + bad_files [1 ] + " is a valid MRC2014 file..." in print_output
585+ assert "Checking if " + bad_files [2 ] + " is a valid MRC2014 file..." in print_output
586+ assert "Checking if " + bad_files [3 ] + " is a valid MRC2014 file..." in print_output
587+ assert "ValueError:" in print_output
573588 assert len (sys .stdout .getvalue ()) == 0
574589 assert len (sys .stderr .getvalue ()) == 0
575590
576591 def test_validate_good_and_bad_files (self ):
577592 files = self .create_good_files () + [
593+ self .not_an_mrc_name ,
578594 self .example_mrc_name ,
579595 self .ext_header_mrc_name ,
580596 self .gzip_mrc_name
0 commit comments