def do_command(self, root, environment): for cmd in self.command: output, status = command_output(cmd.format(**environment), cwd=root, timeout=self.timeout, verbose=True) if status != 0: raise BadCommand("Failed to run command", cmd=cmd.format(**environment), output=output, status=status)
from bespin.processes import command_output from tests.helpers import BespinCase from textwrap import dedent import sys describe BespinCase, "command_output": it "returns the output and exit code from running a command": with self.a_temp_file() as filename: with open(filename, 'w') as fle: fle.write(dedent(""" print("hello") print("there") """)) output, exit_code = command_output("{0} {1}".format(sys.executable, filename)) self.assertEqual(output, ["hello", "there"]) self.assertEqual(exit_code, 0) it "can kill a command after a certain timeout": with self.a_temp_file() as filename: with open(filename, 'w') as fle: fle.write(dedent(""" import time print("hello") import sys; sys.stdout.flush() time.sleep(3) print("there") """)) output, exit_code = command_output("{0} {1}".format(sys.executable, filename), timeout=0.05)