summaryrefslogtreecommitdiff
path: root/creole/tests/test_subprocess.py
blob: 6babded5608669f2e096c289b2eac056510f3bb1 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# coding: utf-8

"""
    unittest
    ~~~~~~~~

    :copyleft: 2015 by python-creole team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""

from __future__ import division, absolute_import, print_function, unicode_literals

import unittest
import sys
import os

from creole.tests.utils.unittest_subprocess import SubprocessMixin


class TestSubprocessMixin(unittest.TestCase, SubprocessMixin):
    def test_find_executable(self):
        filepath = self.find_executable("python")
        if not hasattr(self, "assertRegex"): # New in version 3.1
            self.assertRegex = self.assertRegexpMatches
        self.assertRegex(filepath, ".*?python.*?")

    def test_executable_not_exists(self):
        with self.assertRaisesRegexp(AssertionError, """Program "doesn't exists!" not found in:.*"""):
            self.find_executable("doesn't exists!")

    def test_executable_with_path(self):
        msg = "'%s' unexpectedly found in '%s'" % (
            os.sep, sys.executable
        )
        with self.assertRaisesRegexp(AssertionError, msg):
            self.find_executable(sys.executable)

    def test_subprocess(self):
        popen_args, retcode, stdout = self.subprocess(
            popen_args=[sys.executable, "-c", "import sys;sys.stdout.write('to stdout')"],
            verbose=False
        )
        self.assertEqual(stdout, "to stdout")
        self.assertEqual(retcode, 0)

    def test_assertSubprocess(self):
        code = (
            "import sys;"
            "sys.stdout.write('to stdout 1\\n');"
            "sys.stdout.flush();"
            "sys.stderr.write('to stderr 1\\n');"
            "sys.stderr.flush();"
            "sys.stdout.write('to stdout 2\\n');"
            "sys.stdout.flush();"
            "sys.stderr.write('to stderr 2\\n');"
            "sys.stderr.flush();"
        )
        self.assertSubprocess(
            popen_args=(sys.executable, "-c", code),
            retcode=0,
            stdout=(
                "to stdout 1\n"
                "to stderr 1\n"
                "to stdout 2\n"
                "to stderr 2\n"
            ),
            verbose=True
        )