def test_env_override(self): old_environ = os.environ.copy() os.environ["HELLO"] = "bad" cmd = Command(["env"], env={"HELLO": "pakit"}) cmd.wait() "HELLO=pakit" in cmd.output() os.environ = old_environ
def get_hash(target): """ Required because with now forces right commit """ cmd = Command("hg identify", target) cmd.wait() return cmd.output()[0].split()[0]
def test_env_override(self): old_environ = os.environ.copy() os.environ['HELLO'] = 'bad' cmd = Command(['env'], env={'HELLO': 'pakit'}) cmd.wait() 'HELLO=pakit' in cmd.output() os.environ = old_environ
def get_hash(target): """ Required because with now forces right commit """ cmd = Command('git log -1 ', target) cmd.wait() return cmd.output()[0].split()[-1]
def test_command_dir(self): try: os.makedirs("dummy") with open("dummy/hello", "wb") as fout: fout.write("this is a sample line".encode()) cmd = Command("ls", os.path.abspath("./dummy")) cmd.wait() assert cmd.rcode == 0 assert cmd.output() == ["hello"] finally: tc.delete_it("dummy")
def test_command_dir(self): try: os.makedirs('dummy') with open('dummy/hello', 'wb') as fout: fout.write('this is a sample line'.encode()) cmd = Command('ls', os.path.abspath('./dummy')) cmd.wait() assert cmd.rcode == 0 assert cmd.output() == ['hello'] finally: tc.delete_it('dummy')
def cmd(self, cmd, **kwargs): """ Wrapper around pakit.shell.Command. Behaves the same except: - Expand all dictionary markers in *cmd* against *self.opts*. Arg *cmd* may be a string or a list of strings. - If no *cmd_dir* in kwargs, then execute in current directory. - If no *timeout* in kwargs, use default pakit Command timeout. - Command will block until completed or Exception raised. Args: cmd: A string or list of strings that forms the command. Dictionary markers like '{prefix}' will be expanded against self.opts. Kwargs: cmd_dir: The directory to execute command in. prev_cmd: The previous Command, use it for stdin. timeout: When no stdout/stderr recieved for timeout kill command and raise exception. Returns: Command object that was running as a subprocess. Raises: PakitCmdError: The return code indicated failure. PakitCmdTimeout: The timeout interval was reached. """ if isinstance(cmd, type('')): cmd = cmd.format(**self.opts) else: cmd = [word.format(**self.opts) for word in cmd] timeout = kwargs.pop('timeout', None) cmd_dir = kwargs.get('cmd_dir', os.getcwd()) PLOG('Executing in %s: %s', cmd_dir, cmd) cmd = Command(cmd, **kwargs) if timeout: cmd.wait(timeout) else: cmd.wait() return cmd
def test_update(self): def get_hash(target): """ Required because with now forces right commit """ cmd = Command("hg identify", target) cmd.wait() return cmd.output()[0].split()[0] self.repo.branch = "default" with self.repo: # Lop off history to ensure updateable latest_hash = self.repo.src_hash cmd = Command("hg strip tip", self.repo.target) cmd.wait() assert get_hash(self.repo.target) != latest_hash self.repo.update() assert get_hash(self.repo.target) == latest_hash
def test_prev_cmd_stdin(self): cmd = Command('echo -e "Hello\nGoodbye!"') cmd.wait() cmd2 = Command('grep "ood"', prev_cmd=cmd) cmd2.wait() assert cmd2.output() == ["Goodbye!"]
def test_terminate(self): cmd = Command("sleep 4") assert cmd.alive cmd.terminate() assert not cmd.alive
def test_output(self): cmd = Command('echo "Hello py.test"') cmd.wait() lines = cmd.output() assert lines == ["Hello py.test"]
def test_simple_command_list(self): cmd = Command(["echo", '"Hello"']) cmd.wait() assert cmd.rcode == 0
def test_simple_command(self): cmd = Command('echo "Hello"') cmd.wait() assert cmd.rcode == 0