def test_raises_on_error(self): with pytest.raises(OSError) as error: posix_spawn(b'no_such_executable', [b'no_such_executable']) assert error.value.errno == 2 assert error.value.strerror == 'No such file or directory' assert error.value.filename == b'no_such_executable'
def test_raises_on_error(self): with self.assertRaises(OSError) as error: posix_spawn(b'no_such_executable', [b'no_such_executable']) self.assertEqual(error.exception.errno, 2) self.assertEqual(error.exception.strerror, 'No such file or directory') self.assertEqual(error.exception.filename, b'no_such_executable')
def test_close_file(self, tmpdir): closefile = tmpdir.join("closefile") fa = FileActions() fa.add_close(0) pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import os import sys import errno try: os.fstat(0) except OSError as e: if e.errno == errno.EBADF: with open(sys.argv[1], 'w') as closefile: closefile.write('is closed') """).encode('ascii'), str(closefile).encode('ascii') ], file_actions=fa) assert exits(pid) == 0 assert "is closed" == closefile.read()
def test_close_file(self, tmpdir): closefile = tmpdir.join("closefile") fa = FileActions() fa.add_close(0) pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import os import sys import errno try: os.fstat(0) except OSError as e: if e.errno == errno.EBADF: with open(sys.argv[1], 'w') as closefile: closefile.write('is closed') """).encode('ascii'), str(closefile).encode('ascii')], file_actions=fa ) assert exits(pid) == 0 assert "is closed" == closefile.read()
def test_empty_actions(self): fa = FileActions() pid = posix_spawn( executable, [executable, b'-c', b'pass'], file_actions=fa ) assert exits(pid) == 0
def test_specify_environment(self, tmpdir): envfile = tmpdir.join("envfile") pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import os with open("{0!s}", "w") as envfile: envfile.write(os.environ['foo']) """).format(envfile).encode('ascii') ], {b"foo": b"bar"}) assert exits(pid) == 0 assert "bar" == envfile.read()
def test_specify_environment(self): with tempfile.NamedTemporaryFile(mode=b'r+b') as envfile: pid = posix_spawn(sys.executable, [ b'python', b'-c', (b'import os; ' b'open({0!r}, "w").write(str(os.environ))'.format( envfile.name)) ], {b'foo': b'bar'}) os.waitpid(pid, 0) self.assertIn(b"'foo': 'bar'", envfile.read())
def test_returns_pid(self, tmpdir): pidfile = tmpdir.join('pidfile') pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import os with open("{0!s}", "w") as pidfile: pidfile.write(str(os.getpid())) """).format(pidfile).encode('ascii') ]) assert exits(pid) == 0 assert pid == int(pidfile.read())
def test_specify_environment(self, tmpdir): envfile = tmpdir.join("envfile") pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import os with open("{0!s}", "w") as envfile: envfile.write(os.environ['foo']) """).format(envfile).encode('ascii')], {b"foo": b"bar"} ) assert exits(pid) == 0 assert "bar" == envfile.read()
def test_returns_pid(self): with tempfile.NamedTemporaryFile(mode=b'r+b') as pidfile: pid = posix_spawn(sys.executable, [ b'python', b'-c', (b'import os; ' b'open({0!r}, "w").write(str(os.getpid()))'.format( pidfile.name)) ]) pid_info = os.waitpid(pid, 0) self.assertEqual(pid, pid_info[0]) self.assertEqual(pid_info[1], 0) self.assertEqual(pid, int(pidfile.read()))
def test_close_file(self): with tempfile.NamedTemporaryFile(mode=b'r+b') as closefile: fa = FileActions() self.assertEqual(0, fa.add_close(1)) pid = posix_spawn(sys.executable, [ b'python', b'-c', (b'import sys; ' b'open({0!r}, "w").write(str(sys.stdout.closed))'.format( closefile.name)) ], file_actions=fa) os.waitpid(pid, 0) self.assertIn(b"True", closefile.read())
def test_environment_is_none_inherits_environment(self, tmpdir): envfile = tmpdir.join("envfile") environ[b'inherits'] = b'environment' pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import os with open("{0!s}", "w") as envfile: envfile.write(os.environ['inherits']) """).format(envfile).encode('ascii') ], env=None) assert exits(pid) == 0 assert "environment" == envfile.read()
def test_environment_is_none_inherits_environment(self): with tempfile.NamedTemporaryFile(mode=b'r+b') as envfile: os.environ[b'inherits'] = 'environment' pid = posix_spawn(sys.executable, [ b'python', b'-c', (b'import os; ' b'open({0!r}, "w").write(str(os.environ))'.format( envfile.name)) ], env=None) os.waitpid(pid, 0) self.assertIn(b"'inherits': 'environment'", envfile.read())
def test_environment_is_none_inherits_environment(self, tmpdir): envfile = tmpdir.join("envfile") environ[b'inherits'] = b'environment' pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import os with open("{0!s}", "w") as envfile: envfile.write(os.environ['inherits']) """).format(envfile).encode('ascii')], env=None ) assert exits(pid) == 0 assert "environment" == envfile.read()
def test_dup2(self, tmpdir): dupfile = tmpdir.join("dupfile") with dupfile.open("w") as childfile: fa = FileActions() fa.add_dup2(childfile.fileno(), 1) pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import sys sys.stdout.write("hello") """).encode('ascii') ], file_actions=fa) assert exits(pid) == 0 assert "hello" == dupfile.read()
def test_open_file(self, tmpdir): outfile = tmpdir.join('outfile') fa = FileActions() fa.add_open(1, str(outfile).encode('ascii'), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, stat.S_IRUSR | stat.S_IWUSR) pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import sys sys.stdout.write("hello") """).encode('ascii') ], file_actions=fa) assert exits(pid) == 0 assert "hello" == outfile.read()
def test_open_file(self): with tempfile.NamedTemporaryFile(mode=b'r+b') as outfile: fa = FileActions() self.assertEqual(0, fa.add_open( 1, outfile.name, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, stat.S_IRUSR | stat.S_IWUSR )) pid = posix_spawn(sys.executable, [ b'python', b'-c', b'print ("hello")', ], file_actions=fa) os.waitpid(pid, 0) self.assertIn(b"hello", outfile.read())
def test_dup2(self, tmpdir): dupfile = tmpdir.join("dupfile") with dupfile.open("w") as childfile: fa = FileActions() fa.add_dup2(childfile.fileno(), 1) pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import sys sys.stdout.write("hello") """).encode('ascii')], file_actions=fa ) assert exits(pid) == 0 assert "hello" == dupfile.read()
def test_open_file(self, tmpdir): outfile = tmpdir.join('outfile') fa = FileActions() fa.add_open( 1, str(outfile).encode('ascii'), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, stat.S_IRUSR | stat.S_IWUSR ) pid = posix_spawn(executable, [ executable, b'-c', textwrap.dedent(""" import sys sys.stdout.write("hello") """).encode('ascii')], file_actions=fa ) assert exits(pid) == 0 assert "hello" == outfile.read()
def test_empty_actions(self): fa = FileActions() pid = posix_spawn(executable, [executable, b'-c', b'pass'], file_actions=fa) assert exits(pid) == 0