|
1 | 1 | import contextlib |
2 | 2 | import os |
| 3 | +import os.path |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import tempfile |
3 | 7 | import threading |
4 | 8 | from textwrap import dedent |
5 | 9 | import unittest |
6 | 10 |
|
| 11 | +from test import support |
| 12 | +from test.support import os_helper |
| 13 | + |
7 | 14 | from test.support import interpreters |
8 | 15 |
|
9 | 16 |
|
@@ -71,5 +78,70 @@ def ensure_closed(fd): |
71 | 78 | self.addCleanup(lambda: ensure_closed(w)) |
72 | 79 | return r, w |
73 | 80 |
|
| 81 | + def temp_dir(self): |
| 82 | + tempdir = tempfile.mkdtemp() |
| 83 | + tempdir = os.path.realpath(tempdir) |
| 84 | + self.addCleanup(lambda: os_helper.rmtree(tempdir)) |
| 85 | + return tempdir |
| 86 | + |
| 87 | + def make_script(self, filename, dirname=None, text=None): |
| 88 | + if text: |
| 89 | + text = dedent(text) |
| 90 | + if dirname is None: |
| 91 | + dirname = self.temp_dir() |
| 92 | + filename = os.path.join(dirname, filename) |
| 93 | + |
| 94 | + os.makedirs(os.path.dirname(filename), exist_ok=True) |
| 95 | + with open(filename, 'w', encoding='utf-8') as outfile: |
| 96 | + outfile.write(text or '') |
| 97 | + return filename |
| 98 | + |
| 99 | + def make_module(self, name, pathentry=None, text=None): |
| 100 | + if text: |
| 101 | + text = dedent(text) |
| 102 | + if pathentry is None: |
| 103 | + pathentry = self.temp_dir() |
| 104 | + else: |
| 105 | + os.makedirs(pathentry, exist_ok=True) |
| 106 | + *subnames, basename = name.split('.') |
| 107 | + |
| 108 | + dirname = pathentry |
| 109 | + for subname in subnames: |
| 110 | + dirname = os.path.join(dirname, subname) |
| 111 | + if os.path.isdir(dirname): |
| 112 | + pass |
| 113 | + elif os.path.exists(dirname): |
| 114 | + raise Exception(dirname) |
| 115 | + else: |
| 116 | + os.mkdir(dirname) |
| 117 | + initfile = os.path.join(dirname, '__init__.py') |
| 118 | + if not os.path.exists(initfile): |
| 119 | + with open(initfile, 'w'): |
| 120 | + pass |
| 121 | + filename = os.path.join(dirname, basename + '.py') |
| 122 | + |
| 123 | + with open(filename, 'w', encoding='utf-8') as outfile: |
| 124 | + outfile.write(text or '') |
| 125 | + return filename |
| 126 | + |
| 127 | + @support.requires_subprocess() |
| 128 | + def run_python(self, *argv): |
| 129 | + proc = subprocess.run( |
| 130 | + [sys.executable, *argv], |
| 131 | + capture_output=True, |
| 132 | + text=True, |
| 133 | + ) |
| 134 | + return proc.returncode, proc.stdout, proc.stderr |
| 135 | + |
| 136 | + def assert_python_ok(self, *argv): |
| 137 | + exitcode, stdout, stderr = self.run_python(*argv) |
| 138 | + self.assertNotEqual(exitcode, 1) |
| 139 | + return stdout, stderr |
| 140 | + |
| 141 | + def assert_python_failure(self, *argv): |
| 142 | + exitcode, stdout, stderr = self.run_python(*argv) |
| 143 | + self.assertNotEqual(exitcode, 0) |
| 144 | + return stdout, stderr |
| 145 | + |
74 | 146 | def tearDown(self): |
75 | 147 | clean_up_interpreters() |
0 commit comments