| 1 | #!/usr/bin/env python
|
| 2 | """
|
| 3 | sh_spec_test.py: Tests for sh_spec.py
|
| 4 | """
|
| 5 |
|
| 6 | import cStringIO
|
| 7 | import pprint
|
| 8 | import unittest
|
| 9 |
|
| 10 | from sh_spec import * # module under test
|
| 11 |
|
| 12 | TEST1 = cStringIO.StringIO("""\
|
| 13 | ### Env binding in readonly/declare disallowed
|
| 14 | FOO=foo readonly v=$(tests/printenv.py FOO)
|
| 15 | echo "v=$v"
|
| 16 | # Shells allow this misleading construct, but the correct behavior is to
|
| 17 | # disallow it at parse time.
|
| 18 | # OK bash/dash/mksh stdout: v=None
|
| 19 | # OK bash/dash/mksh status: 0
|
| 20 | # status: 2
|
| 21 | """)
|
| 22 |
|
| 23 | TOKENS1 = list(LineIter(TEST1))
|
| 24 | CASE1 = ParseTestCase(Tokenizer(iter(TOKENS1)))
|
| 25 |
|
| 26 |
|
| 27 | TEST2 = cStringIO.StringIO("""\
|
| 28 | ### Multiline test case
|
| 29 | echo one
|
| 30 | echo two
|
| 31 | # status: 1
|
| 32 | # stderr-json: ""
|
| 33 | # STDOUT:
|
| 34 | one
|
| 35 | two
|
| 36 | # OK dash STDOUT:
|
| 37 | dash1
|
| 38 | dash2
|
| 39 | # END
|
| 40 | # OK mksh STDOUT:
|
| 41 | mksh1
|
| 42 | mksh2
|
| 43 | # END
|
| 44 | """)
|
| 45 | TOKENS2 = list(LineIter(TEST2))
|
| 46 | CASE2 = ParseTestCase(Tokenizer(iter(TOKENS2)))
|
| 47 |
|
| 48 |
|
| 49 | class ShSpecTest(unittest.TestCase):
|
| 50 |
|
| 51 | def testLineIter(self):
|
| 52 | #pprint.pprint(TOKENS1)
|
| 53 |
|
| 54 | types = [type_ for line_num, type_, value in TOKENS1]
|
| 55 | self.assertEqual(
|
| 56 | [ TEST_CASE_BEGIN, PLAIN_LINE, PLAIN_LINE,
|
| 57 | KEY_VALUE, KEY_VALUE, KEY_VALUE,
|
| 58 | EOF], types)
|
| 59 |
|
| 60 | #pprint.pprint(TOKENS2)
|
| 61 | types2 = [type_ for line_num, type_, value in TOKENS2]
|
| 62 | self.assertEqual(
|
| 63 | [ TEST_CASE_BEGIN, PLAIN_LINE, PLAIN_LINE,
|
| 64 | KEY_VALUE, KEY_VALUE,
|
| 65 | KEY_VALUE_MULTILINE, PLAIN_LINE, PLAIN_LINE,
|
| 66 | KEY_VALUE_MULTILINE, PLAIN_LINE, PLAIN_LINE, END_MULTILINE,
|
| 67 | KEY_VALUE_MULTILINE, PLAIN_LINE, PLAIN_LINE, END_MULTILINE,
|
| 68 | EOF], types2)
|
| 69 |
|
| 70 | def testParsed(self):
|
| 71 | print('CASE1')
|
| 72 | pprint.pprint(CASE1)
|
| 73 | print()
|
| 74 |
|
| 75 | expected = {'status': '0', 'stdout': 'v=None\n', 'qualifier': 'OK'}
|
| 76 | self.assertEqual(expected, CASE1['bash'])
|
| 77 | self.assertEqual(expected, CASE1['dash'])
|
| 78 | self.assertEqual(expected, CASE1['mksh'])
|
| 79 | self.assertEqual('2', CASE1['status'])
|
| 80 | self.assertEqual(
|
| 81 | 'Env binding in readonly/declare disallowed', CASE1['desc'])
|
| 82 |
|
| 83 | print('CASE2')
|
| 84 | pprint.pprint(CASE2)
|
| 85 | print()
|
| 86 | print(CreateAssertions(CASE2, 'bash'))
|
| 87 | self.assertEqual('one\ntwo\n', CASE2['stdout'])
|
| 88 | self.assertEqual(
|
| 89 | {'qualifier': 'OK', 'stdout': 'dash1\ndash2\n'}, CASE2['dash'])
|
| 90 | self.assertEqual(
|
| 91 | {'qualifier': 'OK', 'stdout': 'mksh1\nmksh2\n'}, CASE2['mksh'])
|
| 92 |
|
| 93 | def testCreateAssertions(self):
|
| 94 | print(CreateAssertions(CASE1, 'bash'))
|
| 95 |
|
| 96 | def testRunCases(self):
|
| 97 | shells = [('bash', '/bin/bash'), ('osh', 'bin/osh')]
|
| 98 | env = {}
|
| 99 | out = AnsiOutput(sys.stdout, False)
|
| 100 | RunCases([CASE1], lambda i, case: True, shells, env, out)
|
| 101 |
|
| 102 |
|
| 103 | if __name__ == '__main__':
|
| 104 | unittest.main()
|