<s> from __future__ import print_function <EOL> import unittest <EOL> from glob import glob <EOL> from os import devnull , path <EOL> import re <EOL> import sys <EOL> import warnings <EOL> with warnings . catch_warnings ( ) : <EOL> try : <EOL> from IPython . utils . shimmodule import ShimWarning <EOL> warnings . filterwarnings ( '<STR_LIT:error>' , '<STR_LIT>' , ShimWarning ) <EOL> except ImportError : <EOL> class ShimWarning ( Warning ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> try : <EOL> from IPython . nbformat import reads , NBFormatError <EOL> except ShimWarning : <EOL> from nbformat import reads , NBFormatError <EOL> except ImportError : <EOL> from IPython . nbformat . current import reads , NBFormatError <EOL> finally : <EOL> warnings . resetwarnings ( ) <EOL> from runipy . main import main <EOL> from runipy . notebook_runner import NotebookRunner <EOL> class TestRunipy ( unittest . TestCase ) : <EOL> maxDiff = <NUM_LIT> <EOL> def prepare_cell ( self , cell ) : <EOL> cell = dict ( cell ) <EOL> if '<STR_LIT>' in cell : <EOL> del cell [ '<STR_LIT>' ] <EOL> if '<STR_LIT:text>' in cell : <EOL> cell [ '<STR_LIT:text>' ] = re . sub ( '<STR_LIT>' , '<STR_LIT:object>' , cell [ '<STR_LIT:text>' ] ) <EOL> if '<STR_LIT>' in cell : <EOL> trsub = lambda l : re . sub ( '<STR_LIT>' , '<STR_LIT>' , l ) <EOL> cell [ '<STR_LIT>' ] = [ trsub ( line ) for line in cell [ '<STR_LIT>' ] ] <EOL> cell [ '<STR_LIT>' ] = u'<STR_LIT:\n>' . join ( cell [ '<STR_LIT>' ] ) <EOL> cell [ '<STR_LIT>' ] = re . sub ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> cell [ '<STR_LIT>' ] <EOL> ) <EOL> cell [ '<STR_LIT>' ] = re . sub ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> cell [ '<STR_LIT>' ] <EOL> ) <EOL> return cell <EOL> def assert_notebooks_equal ( self , expected , actual ) : <EOL> self . assertEqual ( <EOL> len ( expected [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , <EOL> len ( actual [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> ) <EOL> for expected_out , actual_out in zip ( <EOL> expected [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> actual [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> ) : <EOL> for k in set ( expected_out ) . union ( actual_out ) : <EOL> if k == '<STR_LIT>' : <EOL> self . assertEqual ( len ( expected_out [ k ] ) , len ( actual_out [ k ] ) ) <EOL> for e , a in zip ( expected_out [ k ] , actual_out [ k ] ) : <EOL> e = self . prepare_cell ( e ) <EOL> a = self . prepare_cell ( a ) <EOL> self . assertEqual ( a , e ) <EOL> def testRunNotebooks ( self ) : <EOL> notebook_dir = path . join ( '<STR_LIT>' , '<STR_LIT:input>' ) <EOL> for notebook_path in glob ( path . join ( notebook_dir , '<STR_LIT>' ) ) : <EOL> notebook_file = path . basename ( notebook_path ) <EOL> print ( notebook_file ) <EOL> expected_file = path . join ( '<STR_LIT>' , '<STR_LIT>' , notebook_file ) <EOL> notebook = "<STR_LIT>" <EOL> with open ( notebook_path ) as notebook_file : <EOL> notebook = notebook_file . read ( ) <EOL> try : <EOL> notebook = reads ( notebook , <NUM_LIT:3> ) <EOL> except ( TypeError , NBFormatError ) : <EOL> notebook = reads ( notebook , '<STR_LIT>' ) <EOL> runner = NotebookRunner ( notebook , working_dir = notebook_dir ) <EOL> runner . run_notebook ( True ) <EOL> expected = "<STR_LIT>" <EOL> with open ( expected_file ) as notebook_file : <EOL> expected = notebook_file . read ( ) <EOL> try : <EOL> expected = reads ( expected , <NUM_LIT:3> ) <EOL> except ( TypeError , NBFormatError ) : <EOL> expected = reads ( expected , '<STR_LIT>' ) <EOL> self . assert_notebooks_equal ( expected , runner . nb ) <EOL> def testUseCLI ( self ) : <EOL> notebook_dir = path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for notebook_path in glob ( path . join ( notebook_dir , '<STR_LIT>' ) ) : <EOL> notebook_file = path . basename ( notebook_path ) <EOL> print ( notebook_file ) <EOL> expected = "<STR_LIT>" <EOL> with open ( notebook_path ) as notebook_file : <EOL> expected = notebook_file . read ( ) <EOL> try : <EOL> expected = reads ( expected , <NUM_LIT:3> ) <EOL> except ( TypeError , NBFormatError ) : <EOL> expected = reads ( expected , '<STR_LIT>' ) <EOL> exit_code = <NUM_LIT:1> <EOL> argv = sys . argv <EOL> stdout = sys . stdout <EOL> stderr = sys . stderr <EOL> try : <EOL> with open ( devnull , "<STR_LIT:w>" ) as devnull_filehandle : <EOL> sys . stdout = sys . stderr = devnull_filehandle <EOL> sys . argv = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , notebook_path , <EOL> "<STR_LIT>" , notebook_path . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ] <EOL> main ( ) <EOL> except SystemExit as e : <EOL> exit_code = e . code <EOL> finally : <EOL> sys . argv = argv <EOL> sys . stdout = stdout <EOL> sys . stderr = stderr <EOL> notebook = "<STR_LIT>" <EOL> with open ( notebook_path ) as notebook_file : <EOL> notebook = notebook_file . read ( ) <EOL> try : <EOL> notebook = reads ( notebook , <NUM_LIT:3> ) <EOL> except ( TypeError , NBFormatError ) : <EOL> notebook = reads ( notebook , '<STR_LIT>' ) <EOL> self . assert_notebooks_equal ( expected , notebook ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
