def test_ProcessProxy_readlines(self): p = process.ProcessProxy('hello10') output = p.stdout.readlines() self.failUnless(output == ["hello\n"] * 10) p = process.ProcessProxy('hello10noeol') output = p.stdout.readlines() self.failUnless(output == ["hello\n"] * 9 + ["hello"])
def test_ProcessProxy_stdin_binary_mode(self): p = process.ProcessProxy(['sort'], mode='b') p.stdin.write("2\n") p.stdin.write("1\n") p.stdin.write("3\n") p.stdin.close() output = p.stdout.read() expected = "???\r\n" self.failUnless(output == expected, "Unexpected stdout output: %r (expected: %r). "\ "Pipes are not doing text translation."\ " Make sure that the Windows sort.exe is first on the path."\ % (output, expected)) # Note: reusing 'p' here is subtly testing another aspect of the # ProcessProxy stuff. If self._hChildStdinWr (and the # stdout/stderr equivs) are not handled in ProcessProxy.__del__ # then a win32api error on CloseHandle is raised when the # C runtime closes these handles asynchronously. p = process.ProcessProxy(['sort'], mode='b') p.stdin.write("2\r\n") p.stdin.write("1\r\n") p.stdin.write("3\r\n") p.stdin.close() output = p.stdout.read() expected = "1\r\n2\r\n3\r\n" self.failUnless(output == expected, "Unexpected stdout output: %r (expected: %r). "\ "Pipes are not doing text translation."\ % (output, expected))
def test_ProcessProxy_simple_cmd(self): try: p = process.ProcessProxy('echo hi there') except (process.ProcessError, which.WhichError): # On Win9x the shell is not prefixed. p = process.ProcessProxy('command.com /c echo hi there') output = p.stdout.read() self.failUnless(output.strip() == 'hi there')
def test_ProcessProxy_cmd_with_quotes(self): try: p = process.ProcessProxy('echo hi "there"') except (process.ProcessError, which.WhichError): # On Win9x the shell is not prefixed. p = process.ProcessProxy('command.com /c echo hi "there"') output = p.stdout.read() if sys.platform.startswith("win"): expected = 'hi "there"' else: expected = 'hi there' self.failUnless(output.strip() == expected)
def test_ProcessProxy_kill_SIGKILL(self): p = process.ProcessProxy(['hang']) time.sleep(1) p.kill(sig=signal.SIGKILL) retval = p.wait() self.failUnless(os.WIFSIGNALED(retval)) self.failUnless(os.WTERMSIG(retval) == signal.SIGKILL)
def test_ProcessProxy_stdin_handle_cleanup_2(self): p1 = process.ProcessProxy(['ask']) p2 = process.ProcessProxy(['ask']) p1.stdin.write("Trent\n") p2.stdin.write("Andrew\n") p1.stdin.close() p2.stdin.close() p1.wait() p2.wait() p1 = process.ProcessProxy(['ask']) p2 = process.ProcessProxy(['ask']) p1.stdin.write("Mick\n") p1.stdin.close() p2.kill()
def test_ProcessProxy_stdin_buffer(self): p = process.ProcessProxy(['ask']) p.stdin.write("Trent\n") p.stdin.close() output = p.stdout.read() expected = "What is your name?\nYour name is 'Trent'.\n" self.failUnless(output == expected, "Unexpected stdout output: %r" % output)
def test_ProcessProxy_kill_from_parent_subthread(self): p = process.ProcessProxy(['hang']) t = threading.Thread(target=self._KillAndReturn, kwargs={'child': p}) t.start() p.wait() t.join() if self._failedToKill: self.fail("Could not kill the child process from a thread "\ "spawned by the parent *after* the child was spawn.\n")
def test_ProcessProxy_my_stdout_with_buffered_child(self): p = process.ProcessProxy(['talk'], stdout=MyOutFile(), stderr=MyOutFile()) p.wait() p.close() output = p.stdout.getOutput() error = p.stderr.getOutput() self.failUnless(output == 'o0o1o2o3o4') self.failUnless(error == 'e0e1e2e3e4')
def test_ProcessProxy_kill(self): p = process.ProcessProxy(['hang']) time.sleep(2) p.kill() retval = p.wait() if not sys.platform.startswith("win"): # Can check on Unix if the retval indicates that the process # was signaled. Otherwise the test is just to ensure that we # got here (i.e. didn't hang). self.failUnless(os.WIFSIGNALED(retval))
def test_ProcessProxy_cwd_notspecified(self): cwd = os.getcwd() p = process.ProcessProxy(['printcwd']) output = p.stdout.read() pattern = re.compile("CWD is '(.*?)'") match = pattern.search(output) self.failUnless(match, "Could not find '%s' in 'printcwd' output: "\ "output=%r" % (pattern.pattern, output)) self.failUnless(match.group(1) == cwd, "%r != %r" % (match.group(1), cwd))
def test_ProcessProxy_env_unspecified(self): talkenv = '' p = process.ProcessProxy(['printenv']) output = p.stdout.read() pattern = re.compile("TALK_ENV is '(.*?)'") match = pattern.search(output) self.failUnless(match, "Could not find '%s' in 'printenv' output: "\ "output=%r" % (pattern.pattern, output)) self.failUnless(match.group(1) == talkenv, "%r != %r" % (match.group(1), talkenv))
def test_ProcessProxy_kill_twice(self): # Killing an already terminated process should not raise an # exception. p = process.ProcessProxy(['hang']) time.sleep(2) p.kill() retval = p.wait() if not sys.platform.startswith("win"): # Can check on Unix if the retval indicates that the process # was signaled. Otherwise the test is just to ensure that we # got here (i.e. didn't hang). self.failUnless(os.WIFSIGNALED(retval)) p.kill()
def test_ProcessProxy_stdin_handle_cleanup_1(self): p1 = process.ProcessProxy(['ask']) p2 = process.ProcessProxy(['ask']) p1.stdin.write("Trent\n") p2.stdin.write("Andrew\n") p1.stdin.close() p2.stdin.close() p1.wait() del p1 p2.wait() p1 = process.ProcessProxy(['ask']) p2 = process.ProcessProxy(['ask']) p1.stdin.write("Mick\n") p1.stdin.close() output = p1.stdout.read() expected = "What is your name?\nYour name is 'Mick'.\n" self.failUnless(output == expected, "Unexpected stdout output: %r" % output) p2.kill()
def test_ProcessProxy_cwd_specified(self): wd = os.path.expanduser('~') p = process.ProcessProxy(['printcwd'], cwd=wd) output = p.stdout.read() pattern = re.compile("CWD is '(.*?)'") match = pattern.search(output) self.failUnless(match, "Could not find '%s' in 'printcwd' output: "\ "output=%r" % (pattern.pattern, output)) actual = os.path.normcase(match.group(1)) expected = os.path.normcase(wd) self.failUnless(actual == expected, "%r != %r" % (actual, expected))
def test_ProcessProxy_stdin_buffer_nonewline(self): p = process.ProcessProxy(['ask']) p.stdin.write("Tre") # Note that we have not sent a newline, so the scanf() (or # fread() or whatever) in ask.exe is still waiting for input. # This is testing that the subsequent p.stdin.close() # successfully communicates to the child that the pipe is closed # and no more data is forth coming. (This relies on the pipe # inheritability having been properly set.) p.stdin.close() output = p.stdout.read() expected = "What is your name?\nYour name is 'Tre'.\n" self.failUnless(output == expected, "Unexpected stdout output: %r" % output)
def test_ProcessProxy_stdin_text_mode(self): # On Linux: # There is no distinction btwn text- and binary-modes. So this # really is not providing that useful a test. p = process.ProcessProxy(['sort']) p.stdin.write("2\n") p.stdin.write("1\n") p.stdin.write("3\n") p.stdin.close() output = p.stdout.read() expected = "1\n2\n3\n" self.failUnless(output == expected, "Unexpected stdout output: %r (expected: %r). "\ "Pipes are not doing text translation."\ % (output, expected))
def test_ProcessProxy_cwd_specified_withspaces(self): wd = "my tmp relative dir with spaces" testsupport.mkdir(wd) p = process.ProcessProxy(['printcwd'], cwd=wd) output = p.stdout.read() pattern = re.compile("CWD is '(.*?)'") match = pattern.search(output) self.failUnless(match, "Could not find '%s' in 'printcwd' output: "\ "output=%r" % (pattern.pattern, output)) self.failUnless(match.group(1) == os.path.abspath(wd), "%r != %r" % (match.group(1), os.path.abspath(wd))) testsupport.rmtree(wd)
def test_ProcessProxy_my_stdout_with_unbuffered_child(self): p = process.ProcessProxy(['talk_setvbuf'], stdout=MyOutFile(), stderr=MyOutFile()) p.wait() self.failUnless(p.stdout.getOutput() == 'o0o1o2o3o4') self.failUnless(p.stderr.getOutput() == 'e0e1e2e3e4') # Ensure that the writes are spread over about 5 seconds. writeEvents = [e for e in p.stdout.log if e[1] == 'write'] timespan = writeEvents[-1][0] - writeEvents[0][0] epsilon = 1.0 self.failUnless(timespan > epsilon, "Write events were not spread over a few seconds."\ "timespan=%r" % timespan)
def test_ProcessProxy_wait_from_parent_subthread(self): before = time.time() p = process.ProcessProxy(['talk']) try: t = threading.Thread(target=self._WaitAndReturn, kwargs={'child':p}) t.start() t.join() after = time.time() if self._hitLinuxThreadsBug: self.fail("Hit known bug in Linux threads: cannot wait "\ "on a process from a different thread from "\ "which it was spawned.") self.failUnless(4.0 < (after-before) < 10.0) finally: p.kill()
def test_ProcessProxy_stdin_donotrespond(self): p = process.ProcessProxy(['ask_then_talk'], stdout=MyOutFile(), stderr=MyOutFile()) # Expect this to hang, as the child waits for input that we do # not send it. time.sleep(6) # There should be no output, other that output = ''.join([item[2] for item in p.stdout.log]) try: self.failUnless(output == "What is your name?\n", "Stdout has unexpectedly received other than one "\ "'What is your name?' write. The process should "\ "be hung. log=%r" % p.stdout.log) finally: p.kill()
def test_ProcessProxy_env_overridden(self): os.environ['TALK_ENV'] = 'spam' talkenv = 'eggs' env = {'TALK_ENV': talkenv} p = process.ProcessProxy(['printenv'], env=env) output = p.stdout.read() pattern = re.compile("TALK_ENV is '(.*?)'") match = pattern.search(output) self.failUnless(match, "Could not find '%s' in 'printenv' output: "\ "output=%r" % (pattern.pattern, output)) self.failUnless(match.group(1) == talkenv, "%r != %r" % (match.group(1), talkenv)) self.failUnless(os.environ['TALK_ENV'] == 'spam') os.environ['TALK_ENV'] = ''
def test_ProcessProxy_cmd_with_multiples_quoted_args(self): dname = "program files" if not os.path.exists(dname): os.makedirs(dname) if sys.platform.startswith("win"): talk = "talk.exe" else: talk = "talk" talkWithSpaces = os.path.join(dname, talk) shutil.copy(talk, talkWithSpaces) try: p = process.ProcessProxy('"%s" and here are "some" args'\ % talkWithSpaces) output = p.stdout.read() self.failUnless(output.strip() == 'o0o1o2o3o4') finally: if os.path.exists(talkWithSpaces): os.unlink(talkWithSpaces) if os.path.exists(dname): os.removedirs(dname)
def test_ProcessProxy_0(self): p = process.ProcessProxy(['quiet']) retval = p.wait() self._assertRetvalIs(0, retval)
def test_ProcessProxy_wait_multiple_times(self): p = process.ProcessProxy(['log', 'hi']) rv1 = p.wait() rv2 = p.wait() self.failUnless(rv1 == rv2)
def test_ProcessProxy_minus_42(self): p = process.ProcessProxy(['quiet', '-42']) retval = p.wait() self._assertRetvalIs(-42, retval)
def test_ProcessProxy_readline(self): p = process.ProcessProxy('hello10') for i in range(10): output = p.stdout.readline() self.failUnless(output == "hello\n")
def test_ProcessProxy_stdout_buffer(self): p = process.ProcessProxy(['talk']) output = p.stdout.read() self.failUnless(output == 'o0o1o2o3o4') error = p.stderr.read() self.failUnless(error == 'e0e1e2e3e4')
def test_ProcessProxy_wait(self): before = time.time() p = process.ProcessProxy(['talk']) p.wait() after = time.time() self.failUnless(4.0 < (after-before) < 10.0)
def test_ProcessProxy_stdin_buffering_with_mystdin(self): p = process.ProcessProxy(['ask'], stdin=MyInFile("Trent\n")) output = p.stdout.read() expected = "What is your name?\nYour name is 'Trent'.\n" self.failUnless(output == expected, "Unexpected stdout output: %r" % output)