def test_basicVerbose(self): capturedOutput = io.StringIO() sys.stdout = capturedOutput printer = Printer(verbose=True) cmdString = r"python -c \"print('Hello World')\"" cmdExe = RunBlock(cmdString) res = cmdExe.run(printer=printer) sys.stdout = sys.__stdout__ expectedResult = 'Run: python -c \\"print(\'Hello World\')\\"' + "\n" + "cwd: " + os.getcwd( ) + "\n" + 'call: ' + cmdString + "\n" self.assertEqual(res, True) self.assertEqual(expectedResult, capturedOutput.getvalue())
def test_exitCodeNone(self): printer = Printer(verbose=True) capturedOutput = io.StringIO() sys.stdout = capturedOutput cmdString = '"' + sys.executable + '"' + ' -c "exit(1)"' cmdExe = RunBlock(cmdString) res = cmdExe.run(printer=printer) sys.stdout = sys.__stdout__ expectedResult = 'Run: ' + cmdString + "\n" + "cwd: " + os.getcwd( ) + "\n" + 'call: ' + cmdString + "\n" self.assertEqual(res, True) resStr = capturedOutput.getvalue() self.assertEqual(expectedResult, capturedOutput.getvalue())
def test_unexpectedExitCodeVerbose(self): printer = Printer(verbose=True) capturedOutput = io.StringIO() sys.stdout = capturedOutput cmdString = '"' + sys.executable + '"' + ' -c "exit(1)"' cmdExe = RunBlock(cmdString, exitcode=0) res = cmdExe.run(printer=printer) sys.stdout = sys.__stdout__ expectedResult = 'Run: ' + cmdString + "\n" + "cwd: " + os.getcwd( ) + "\n" + 'call: ' + cmdString + "\n" + 'Error: different exitcode received: 1 != 0 for command \'' + cmdString + "\'\n" self.assertEqual(res, False) resStr = capturedOutput.getvalue() self.assertEqual(expectedResult, capturedOutput.getvalue())
def test_inputfileNoutputfile(self): rmtree("Output") printer = Printer(verbose=True) capturedOutput = io.StringIO() sys.stdout = capturedOutput cmdExe = RunBlock( '"' + sys.executable + '"' + " RunBlock_test/stdinTostdout.py", inputfile="RunBlock_test/stdinTostdout.py", outputfile="RunBlock_test/Output/inputfileNoutputfile.txt") res = cmdExe.run(workingDir="TestFiles", printer=printer) sys.stdout = sys.__stdout__ self.assertEqual(res, True) with open("TestFiles/RunBlock_test/stdinTostdout.py", "r") as fh1: with open( "TestFiles/RunBlock_test/Output/inputfileNoutputfile.txt", "r") as fh2: self.assertEqual(fh1.read(), fh2.read()) rmtree("TestFiles/RunBlock_test/Output")
def generateRunBlock(data): if isinstance(data, str): return RunBlock(cmd=data) if not isinstance(data, dict): raise GeneratorException(f"'run' block not recognized") args = {} key = "" knownkeys = { 'cmd': lambda _data: getYamlString(_data, data['__line__'], key), 'outputfile': lambda _data: getYamlString(_data, data['__line__'], key), 'inputfile': lambda _data: getYamlString(_data, data['__line__'], key), 'exitcode': lambda _data: getYamlInt(_data, data['__line__'], key), 'shell': lambda _data: getYamlString(_data, data['__line__'], key) } for key in data: if key == "__line__": continue if not key.lower() in knownkeys.keys(): raise GeneratorException( f"Only one of the following keys are allowed: {[*knownkeys.keys()]} at line {data['__line__']}" ) args[key.lower()] = knownkeys[key.lower()](data[key]) return RunBlock(**args)
def test_basic(self): cmdExe = RunBlock(r"python -c \"print('Hello World')\"") res = cmdExe.run() self.assertEqual(res, True)