def get_output(file_content, lang_to, outfile_name): """Returns PLC output for given input parameters""" # Make new temporary file with TemporaryFile(suffix=".py", dir=".", mode="x+") as file: # Write test program to file file.write("\n".join(file_content)) # Save file name file_name = file.name # Seek start of file file.seek(0) # Set file overwrite to true overwrite = "Y" # Make list of all inputs input_list = [file_name, lang_to, outfile_name, overwrite] # Make GetIO object (to stub input) io_stream = GetIO() # Stub input with patch("builtins.input", side_effect=input_list): # Stub output to StringIO io_stream.stub_output() try: # Run PLC with parameters PLC() finally: # Reset output to standard output io_stream.reset_output() # Close the file file.close() # Delete file after using it # remove(file_name) # Open output file with open(outfile_name) as file: # Read and return lines from output file return [ line.strip() for line in file.readlines() if line.strip() != "" ]
def test_stub_output(self): """Test case for GetIO.stub_output""" # Define expected result list res_tup = (True, True, False) # Initiate class iostream = GetIO() # Run method iostream.stub_output() # Check function output self.assertEqual( (isinstance(sys.stdout, StringIO), isinstance( sys.stderr, StringIO), iostream.reset), res_tup) # Reset stdout and stderr sys.stdout, sys.stderr = self.default_stdout, self.default_stderr