|
1
|
|
|
from contextlib import ExitStack |
|
2
|
|
|
import os |
|
3
|
|
|
import sys |
|
4
|
|
|
from tempfile import NamedTemporaryFile |
|
5
|
|
|
import unittest |
|
6
|
|
|
|
|
7
|
|
|
from coalib.misc.Shell import run_interactive_shell_command, run_shell_command |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class RunShellCommandTest(unittest.TestCase): |
|
11
|
|
|
|
|
12
|
|
|
@staticmethod |
|
13
|
|
|
def construct_testscript_command(scriptname): |
|
14
|
|
|
return (sys.executable, |
|
15
|
|
|
os.path.join(os.path.dirname(os.path.realpath(__file__)), |
|
16
|
|
|
"run_shell_command_testfiles", |
|
17
|
|
|
scriptname)) |
|
18
|
|
|
|
|
19
|
|
|
def test_run_interactive_shell_command(self): |
|
20
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
|
21
|
|
|
"test_interactive_program.py") |
|
22
|
|
|
|
|
23
|
|
|
with run_interactive_shell_command(command) as p: |
|
24
|
|
|
self.assertEqual(p.stdout.readline(), "test_program X\n") |
|
25
|
|
|
self.assertEqual(p.stdout.readline(), "Type in a number:\n") |
|
26
|
|
|
p.stdin.write("33\n") |
|
27
|
|
|
p.stdin.flush() |
|
28
|
|
|
self.assertEqual(p.stdout.readline(), "33\n") |
|
29
|
|
|
self.assertEqual(p.stdout.readline(), "Exiting program.\n") |
|
30
|
|
|
|
|
31
|
|
|
self.assertEqual(p.stdout.read(), "") |
|
32
|
|
|
self.assertEqual(p.stderr.read(), "") |
|
33
|
|
|
|
|
34
|
|
|
def test_run_interactive_shell_command_custom_streams(self): |
|
35
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
|
36
|
|
|
"test_interactive_program.py") |
|
37
|
|
|
|
|
38
|
|
|
with ExitStack() as stack: |
|
39
|
|
|
streams = {s: stack.enter_context(NamedTemporaryFile(mode="w+")) |
|
40
|
|
|
for s in ["stdout", "stderr", "stdin"]} |
|
41
|
|
|
|
|
42
|
|
|
with run_interactive_shell_command(command, **streams) as p: |
|
43
|
|
|
streams["stdin"].write("712\n") |
|
44
|
|
|
streams["stdin"].flush() |
|
45
|
|
|
streams["stdin"].seek(0) |
|
46
|
|
|
|
|
47
|
|
|
self.assertFalse(streams["stdout"].closed) |
|
48
|
|
|
self.assertFalse(streams["stderr"].closed) |
|
49
|
|
|
self.assertFalse(streams["stdin"].closed) |
|
50
|
|
|
|
|
51
|
|
|
streams["stdout"].seek(0) |
|
52
|
|
|
self.assertEqual(streams["stdout"].read(), |
|
53
|
|
|
"test_program X\nType in a number:\n712\n" |
|
54
|
|
|
"Exiting program.\n") |
|
55
|
|
|
|
|
56
|
|
|
streams["stderr"].seek(0) |
|
57
|
|
|
self.assertEqual(streams["stderr"].read(), "") |
|
58
|
|
|
|
|
59
|
|
|
def test_run_interactive_shell_command_kwargs_delegation(self): |
|
60
|
|
|
with self.assertRaises(TypeError): |
|
61
|
|
|
with run_interactive_shell_command("some_command", |
|
62
|
|
|
weird_parameter=30): |
|
63
|
|
|
pass |
|
64
|
|
|
|
|
65
|
|
|
def test_run_shell_command_without_stdin(self): |
|
66
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
|
67
|
|
|
"test_program.py") |
|
68
|
|
|
|
|
69
|
|
|
stdout, stderr = run_shell_command(command) |
|
70
|
|
|
|
|
71
|
|
|
expected = ("test_program Z\n" |
|
72
|
|
|
"non-interactive mode.\n" |
|
73
|
|
|
"Exiting...\n") |
|
74
|
|
|
self.assertEqual(stdout, expected) |
|
75
|
|
|
self.assertEqual(stderr, "") |
|
76
|
|
|
|
|
77
|
|
|
def test_run_shell_command_with_stdin(self): |
|
78
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
|
79
|
|
|
"test_input_program.py") |
|
80
|
|
|
|
|
81
|
|
|
stdout, stderr = run_shell_command(command, "1 4 10 22") |
|
82
|
|
|
|
|
83
|
|
|
self.assertEqual(stdout, "37\n") |
|
84
|
|
|
self.assertEqual(stderr, "") |
|
85
|
|
|
|
|
86
|
|
|
stdout, stderr = run_shell_command(command, "1 p 5") |
|
87
|
|
|
|
|
88
|
|
|
self.assertEqual(stdout, "") |
|
89
|
|
|
self.assertEqual(stderr, "INVALID INPUT\n") |
|
90
|
|
|
|
|
91
|
|
|
def test_run_shell_command_kwargs_delegation(self): |
|
92
|
|
|
with self.assertRaises(TypeError): |
|
93
|
|
|
run_shell_command("super-cool-command", weird_parameter2="abc") |
|
94
|
|
|
|