forked from alexcouper/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
58 lines (46 loc) · 1.81 KB
/
tests.py
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
import unittest
try:
from subprocess import TimeoutExpired
except ImportError:
try:
from subprocess32 import TimeoutExpired
except ImportError:
pass
import bash as bash_module
from bash import bash
class TestBash(unittest.TestCase):
def test_bash_concatenation_by_method(self):
result = bash('ls .').bash('grep "tests.py$"').value()
self.assertEqual(result, 'tests.py')
def test_bash_concatenation_within_command(self):
result = bash('ls . | grep "tests.py$"').value()
self.assertEqual(result, 'tests.py')
def test_bash_repr(self):
result = bash('ls . | grep "tests.py$"')
self.assertEqual(repr(result), 'tests.py')
def test_bash_stdout(self):
result = bash('ls . | grep "tests.py$"')
self.assertEqual(result.stdout, b'tests.py\n')
self.assertEqual(result.code, 0)
def test_bash_stderr(self):
result = bash('./missing_command')
self.assertEqual(result.stdout, b'')
self.assertTrue(result.stderr in [
# Mac OSX
b'/bin/sh: ./missing_command: No such file or directory\n',
# Travis
b'/bin/sh: 1: ./missing_command: not found\n'
])
self.assertEqual(result.code, 127)
def test_passing_env(self):
result = bash('echo $NAME', env={'NAME': 'Fred'})
self.assertEqual(result.stdout, b'Fred\n')
def test_output_to_stdout(self):
b = bash('ls .', stdout=None)
self.assertEqual(str(b), '')
# Shouldn't find anything because we haven't piped it.
self.assertEqual(str(b.bash('grep setup')), '')
def test_timeout_works(self):
if not bash_module.SUBPROCESS_HAS_TIMEOUT:
raise unittest.SkipTest()
self.assertRaises(TimeoutExpired, bash, 'sleep 2; echo 1', timeout=1)