def test_start(self): p = Process(self.command, self.args) self.assertFalse(p.is_running) p.start() self.assertTrue(p.is_running) p.wait() self.assertFalse(p.is_running)
def test_kill(self): p = Process(self.command, self.args) self.assertFalse(p.is_running) start = time.time() p.start() self.assertTrue(p.is_running) p.kill() self.assertFalse(p.is_running) end = time.time() self.assertTrue(end - start < self.timeout)
def test_write(): p = Process(sys.executable, [ '-c', '''a = {}(); print('Said: ' + a)'''.format( 'raw_input' if six.PY2 else 'input') ]) p.start() p.write(b'my hello text') p.wait() assert re.match('^Said: my hello text\s*$', p.read().decode('ascii')) assert re.match('^$', p.eread().decode('ascii'))
def test_write(self): p = Process(sys.executable, ["-c", """a = %s(); print('Said: ' + a)""" % self.input]) p.start() p.write(b"my hello text") p.wait() self.assertRegexpMatches(p.read().decode("ascii"), "^Said: my hello text\s*$") self.assertRegexpMatches(p.eread().decode("ascii"), "^$")
def test_environment(): p = Process(sys.executable, ['-c', PRINT_VAR.format(var='MY_VAR')], env={'MY_VAR': 'My value'}) p.start() p.wait() assert p.exit_code == 0 assert re.match('^My value\s*$', p.read().decode('ascii')) assert re.match('^$', p.eread().decode('ascii'))
def test_working_dir(): working_dir = sys.exec_prefix.strip(os.pathsep) p = Process(sys.executable, ['-c', '''import os; print(os.getcwd())'''], working_dir=working_dir) p.start() p.wait() assert p.read().decode('ascii').strip() == working_dir
def test_write(): p = Process( sys.executable, [ "-c", """a = {}(); print('Said: ' + a)""".format( "raw_input" if six.PY2 else "input" ), ], ) p.start() p.write(b"my hello text") p.wait() assert re.match(r"^Said: my hello text\s*$", p.read().decode("ascii")) assert re.match(r"^$", p.eread().decode("ascii"))
def test_stderr(tmpdir): stderr = tmpdir.mkdir('__tests__').join('std.err') p = Process(sys.executable, ['-c', WRITER.format(out='stderr')], stderr=stderr.strpath) p.start() p.wait() assert p.eread() == b'foobar' assert stderr.open('rb').read() == b'foobar'
def test_environment(): p = Process( sys.executable, ["-c", PRINT_VAR.format(var="MY_VAR")], env={"MY_VAR": "My value"}, ) p.start() p.wait() assert p.exit_code == 0 assert re.match(r"^My value\s*$", p.read().decode("ascii")) assert re.match(r"^$", p.eread().decode("ascii"))
def test_working_dir(): working_dir = sys.exec_prefix.strip(os.pathsep) p = Process( sys.executable, ["-c", """import os; print(os.getcwd())"""], working_dir=working_dir, ) p.start() p.wait() assert p.read().decode("ascii").strip() == working_dir
def test_stderr(tmpdir): stderr = tmpdir.mkdir("__tests__").join("std.err") p = Process( sys.executable, ["-c", WRITER.format(out="stderr")], stderr=stderr.strpath, ) p.start() p.wait() assert p.eread() == b"foobar" assert stderr.open("rb").read() == b"foobar"
def test_environment(self): env = {"MY_VAR": "My value"} p = Process( sys.executable, [ "-c", """import os print(os.environ.get('MY_VAR', '')) """, ], env=env, ) p.start() p.wait() self.assertEqual(p.exit_code, 0) self.assertRegexpMatches(p.read().decode("ascii"), "^My value\s*$") self.assertRegexpMatches(p.eread().decode("ascii"), "^$")
def test_eread(): p = Process(sys.executable, ["-c", WRITER.format(out="stderr")]) p.start() if platform.is_a(platform.DOTNET): # TODO: .NET does not flush the outputs p.wait() assert p.read() == b"" assert p.eread() == b"foobar" else: time.sleep(1) assert p.read() == b"" assert p.eread() == b"foo" p.wait() assert p.read() == b"" assert p.eread() == b"bar"
def test_eread(self): p = Process( sys.executable, [ "-c", """import sys, time sys.stderr.write('foo') sys.stderr.flush() time.sleep(2) sys.stderr.write('bar') """, ], ) p.start() if platform.is_a(platform.DOTNET): # TODO: .NET does not flush the outputs p.wait() self.assertEqual(p.read(), b"") self.assertEqual(p.eread(), b"foobar") else: time.sleep(1) self.assertEqual(p.read(), b"") self.assertEqual(p.eread(), b"foo") p.wait() self.assertEqual(p.read(), b"") self.assertEqual(p.eread(), b"bar")
def test_eread(): p = Process(sys.executable, ['-c', WRITER.format(out='stderr')]) p.start() if platform.is_a(platform.DOTNET): # TODO: .NET does not flush the outputs p.wait() assert p.read() == b'' assert p.eread() == b'foobar' else: time.sleep(1) assert p.read() == b'' assert p.eread() == b'foo' p.wait() assert p.read() == b'' assert p.eread() == b'bar'
def process(): return Process('sleep', ['2'])