def test_remove_exception(self, mock_glob): # Non existent file mock_glob.return_value = ["non_existent_file"] with retrieve_stderr() as stderr: retval = coala_delete_orig.main(section=self.section) output = stderr.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output) # Directory instead of file with tempfile.TemporaryDirectory() as filename, \ retrieve_stderr() as stderr: mock_glob.return_value = [filename] retval = coala_delete_orig.main(section=self.section) output = stderr.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output)
def execute_coala(func, binary, *args, stdout_only=False): """ Executes the main function with the given argument string from given module. :param function: A main function from coala_json, coala_ci module etc. :param binary: A binary to execute coala test :param stdout_only: Get the stdout output only, discard stderr :return: A tuple holding a return value first and a stdout output as second element. """ sys.argv = [binary] + list(args) with retrieve_stdout() as stdout: with retrieve_stderr() as stderr: retval = func() return (retval, stdout.getvalue() if stdout_only else stdout.getvalue() + '\n' + stderr.getvalue())
def test_retrieve_stderr(self): with retrieve_stderr() as sio: print("test", file=sys.stderr) self.assertEqual(sio.getvalue(), "test\n")