Exemplo n.º 1
0
    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'
Exemplo n.º 2
0
    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')
Exemplo n.º 3
0
    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'
Exemplo n.º 4
0
    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()
Exemplo n.º 5
0
    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()
Exemplo n.º 6
0
 def test_empty_actions(self):
     fa = FileActions()
     pid = posix_spawn(
         executable,
         [executable, b'-c', b'pass'],
         file_actions=fa
     )
     assert exits(pid) == 0
Exemplo n.º 7
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()
Exemplo n.º 8
0
    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())
Exemplo n.º 9
0
    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())
Exemplo n.º 10
0
    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())
Exemplo n.º 11
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()
Exemplo n.º 12
0
    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()))
Exemplo n.º 13
0
    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())
Exemplo n.º 14
0
    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()
Exemplo n.º 15
0
    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())
Exemplo n.º 16
0
    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()
Exemplo n.º 17
0
    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()
Exemplo n.º 18
0
    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()
Exemplo n.º 19
0
    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())
Exemplo n.º 20
0
    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()
Exemplo n.º 21
0
    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()
Exemplo n.º 22
0
 def test_empty_actions(self):
     fa = FileActions()
     pid = posix_spawn(executable, [executable, b'-c', b'pass'],
                       file_actions=fa)
     assert exits(pid) == 0