aboutsummaryrefslogtreecommitdiff
blob: c73171f04af98fc3d251a8f99253d03dcf503176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright 1998-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import errno
import tempfile
import portage
from portage import os
from portage import _encodings
from portage import _unicode_encode
from portage.const import BASH_BINARY
from portage.tests import TestCase
from portage.util._eventloop.global_event_loop import global_event_loop
from _emerge.SpawnProcess import SpawnProcess


class SpawnTestCase(TestCase):
    def testLogfile(self):
        logfile = None
        try:
            fd, logfile = tempfile.mkstemp()
            os.close(fd)
            null_fd = os.open("/dev/null", os.O_RDWR)
            test_string = 2 * "blah blah blah\n"
            proc = SpawnProcess(
                args=[BASH_BINARY, "-c", f"echo -n '{test_string}'"],
                env={},
                fd_pipes={0: portage._get_stdin().fileno(), 1: null_fd, 2: null_fd},
                scheduler=global_event_loop(),
                logfile=logfile,
            )
            proc.start()
            os.close(null_fd)
            self.assertEqual(proc.wait(), os.EX_OK)
            f = open(
                _unicode_encode(logfile, encoding=_encodings["fs"], errors="strict"),
                encoding=_encodings["content"],
                errors="strict",
            )
            log_content = f.read()
            f.close()
            # When logging passes through a pty, this comparison will fail
            # unless the oflag terminal attributes have the termios.OPOST
            # bit disabled. Otherwise, transformations such as \n -> \r\n
            # may occur.
            self.assertEqual(test_string, log_content)
        finally:
            if logfile:
                try:
                    os.unlink(logfile)
                except OSError as e:
                    if e.errno != errno.ENOENT:
                        raise
                    del e