def test_runner_internal_error(tmpdir, monkeypatch, capsys): '''Test that the script deals with an internal error as expected. Provide the same file twice to check that the parser continues to parse any additional files. ''' # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # Create a dummy function that replaces the parser def dummy_parser(_self, std="f2003"): ''' dummy function that simply raises an internal error ''' raise InternalError(std) # monkeypatch the parser so that it returns an InternalError exception. from fparser.two.parser import ParserFactory from fparser.two.utils import InternalError monkeypatch.setattr(ParserFactory, "create", dummy_parser) # run the relevant script method (runner()) fparser2.runner(None, DummyArgs(), [my_file.strpath, my_file.strpath]) # capture the output and check that the appropriate error has been reported stdout, stderr = capsys.readouterr() assert stdout == "" assert stderr.count("Internal error in fparser: 'f2003'") == 2
def test_runner_no_files(capsys): '''Test that the script deals with no files provided as expected.''' # run the relevant script method (runner()) with pytest.raises(SystemExit) as excinfo: fparser2.runner(None, DummyArgs(), []) assert str(excinfo.value) == "1" # capture the output and check that the appropriate error has been reported stdout, _ = capsys.readouterr() assert "Error: No fortran files specified" in stdout
def test_runner_non_existant_file(capsys): '''Test that the script reports an error when the file name that is provided does not exist. ''' # run the relevant script method (runner()) fparser2.runner(None, DummyArgs(), ["idontexist.txt"]) # capture the output and check that the appropriate error has been reported stdout, _ = capsys.readouterr() assert "No such file or directory" in stdout
def test_runner_output(tmpdir, capsys): '''Test that the script outputs the code it has parsed ''' from fparser.scripts import fparser2 # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # run the relevant script method (runner()) fparser2.runner(None, DummyArgs(), [my_file.strpath]) # capture the output and check that the code has been output stdout, _ = capsys.readouterr() assert "PROGRAM hello\nEND PROGRAM hello\n" in stdout
def test_runner_syntax_error(tmpdir, capsys): '''Test that the script deals with code with an invalid syntax.''' # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("prog error\nend program error\n") # run the relevant script method (runner()) fparser2.runner(None, DummyArgs(), [my_file.strpath]) # capture the output and check that the appropriate error has been # reported stdout, stderr = capsys.readouterr() assert stdout == "" assert "File: '" in stderr and "hello.f90'" in stderr assert "Syntax error: at line 1\n>>>prog error" in stderr
def test_runner_syntax_error(tmpdir, capsys): '''Test that the script deals with code with an invalid syntax.''' # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("prog error\nend program error\n") # run the relevant script method (runner()) with pytest.raises(SystemExit) as excinfo: fparser2.runner(None, DummyArgs(), [my_file.strpath]) assert str(excinfo.value) == "1" # capture the output and check that the appropriate error has been # reported stdout, _ = capsys.readouterr() assert "Syntax error: at line 1\n>>>prog error" in stdout assert "failed at line #1'prog error'" in stdout
def test_runner_multi_output(tmpdir, capsys): '''Test that the script outputs the code it has parsed when there are multiple files specified ''' # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # run the relevant script method (runner()) fparser2.runner(None, DummyArgs(), [my_file.strpath, my_file.strpath]) # capture the output and check that the code has been output stdout, stderr = capsys.readouterr() assert ("PROGRAM hello\nEND PROGRAM hello\n" "PROGRAM hello\nEND PROGRAM hello\n") in stdout assert stderr.count("File: '") == 2 assert stderr.count("hello.f90'") == 2
def test_runner_no_file_multi(capsys, tmpdir): '''Test that the script reports an error when the file name that is provided does not exist and continues to parse any subsequent file(s). ''' # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # run the relevant script method (runner()) fparser2.runner(None, DummyArgs(), ["idontexist.txt", my_file.strpath]) # capture the output and check that the appropriate error has been # reported stdout, stderr = capsys.readouterr() assert "PROGRAM hello\nEND PROGRAM hello\n" in stdout assert "File: 'idontexist.txt'" in stderr assert "No such file or directory: 'idontexist.txt'" in stderr assert "File: '" in stderr and "hello.f90'" in stderr
def test_runner_set_mode(tmpdir, capsys): '''Test that the script can change mode.''' # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # Create a dummy class with the required attribute to pass into # runner() as an argument options class class DummyArgsFree(object): ''' dummy object pretending to be the argument options ''' mode = "free" task = "show" # run the relevant script method (runner()) fparser2.runner(None, DummyArgsFree(), [my_file.strpath]) # capture the output and check that the code has been output stdout, _ = capsys.readouterr() assert "PROGRAM hello\nEND PROGRAM hello\n" in stdout
def test_runner_output_task_none(tmpdir, capsys): '''Test that the script outputs nothing when the 'task' option is set to "none". ''' class DummyArgsTask(object): ''' dummy object pretending to be the argument options ''' mode = "free" task = "none" # Create a temporary file containing Fortran code to pass into # runner(). my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # Run the relevant script method (runner()). fparser2.runner(None, DummyArgsTask(), [my_file.strpath]) # Capture the output and check that nothig has been produced. stdout, _ = capsys.readouterr() assert stdout == ""
def test_runner_output_task_show(tmpdir, capsys): '''Test that the script outputs the code it has parsed with the 'task' option set to "show". ''' class DummyArgsTask(object): ''' dummy object pretending to be the argument options ''' mode = "free" task = "show" # Create a temporary file containing Fortran code to pass into # runner(). my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # Run the relevant script method (runner()). fparser2.runner(None, DummyArgsTask(), [my_file.strpath]) # Capture the output and check that the code has been output. stdout, _ = capsys.readouterr() assert "PROGRAM hello\nEND PROGRAM hello\n" in stdout
def test_runner_multi_output_except(tmpdir, capsys): '''Test that if the script encounters a file with a syntax error then it continues trying to parse subsequent files. ''' # Create a temporary file containing Fortran code to pass into runner() my_file1 = tmpdir.mkdir("sub1").join("broken.f90") my_file1.write("prog hello\nen\n") my_file2 = tmpdir.mkdir("sub2").join("hello.f90") my_file2.write("program hello\nend program hello\n") # run the relevant script method (runner()) fparser2.runner(None, DummyArgs(), [my_file1.strpath, my_file2.strpath]) # capture the output and check that the valid code has been output # and the syntax error has been raised. stdout, stderr = capsys.readouterr() assert stdout == "PROGRAM hello\nEND PROGRAM hello\n" assert "Syntax error: at line 1\n>>>prog hello" in stderr assert stderr.count("File: '") == 2 assert "hello.f90'" in stderr assert "broken.f90'" in stderr
def test_runner_syntax_error_2(tmpdir, capsys): '''Test that the script deals with code with an invalid syntax and where there is no information in the fifo buffer. I'm not sure why this happens but this checks the associated code that handles it works. ''' # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program error\nif (.true.) then\nend if label\n" "end program error\n") # run the relevant script method (runner()) with pytest.raises(SystemExit) as excinfo: fparser2.runner(None, DummyArgs(), [my_file.strpath]) assert str(excinfo.value) == "1" # capture the output and check that the appropriate error has been reported # There should be no file information (output by the script) stdout, _ = capsys.readouterr() assert (stdout == "Syntax error: at line 3\n>>>end if label\nName " "'label' has no corresponding starting name\n")
def test_runner_output_task_repr(tmpdir, capsys): '''Test that the script outputs the repr representation of the code it has parsed with the 'task' option set to "repr". ''' class DummyArgsTask(object): ''' dummy object pretending to be the argument options ''' mode = "free" task = "repr" # Create a temporary file containing Fortran code to pass into # runner(). my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # Run the relevant script method (runner()). fparser2.runner(None, DummyArgsTask(), [my_file.strpath]) # Capture the output and check that the repr of the code has been # produced. stdout, _ = capsys.readouterr() assert (stdout == "Program(Main_Program(Program_Stmt('PROGRAM'," " Name('hello')), End_Program_Stmt('PROGRAM', Name('hello'))))\n")
def test_runner_internal_error(tmpdir, monkeypatch, capsys): '''Test that the script deals with an internal error as expected.''' # Create a temporary file containing Fortran code to pass into runner() my_file = tmpdir.mkdir("sub").join("hello.f90") my_file.write("program hello\nend program hello\n") # Create a dummy function that replaces the parser error_string = "monkey trouble" def dummy_parser(_): ''' dummy function that simply raises an internal error ''' raise InternalError(error_string) # monkeypatch the parser so that it returns an InternalError exception. from fparser.two.parser import ParserFactory from fparser.two.utils import InternalError monkeypatch.setattr(ParserFactory, "create", dummy_parser) # run the relevant script method (runner()) with pytest.raises(SystemExit) as excinfo: fparser2.runner(None, DummyArgs(), [my_file.strpath]) assert str(excinfo.value) == "1" # capture the output and check that the appropriate error has been reported stdout, _ = capsys.readouterr() assert "Internal error in fparser: '{0}'".format(error_string) in stdout