| 1 | #!/usr/bin/env python2
 | 
| 2 | """
 | 
| 3 | sh_spec_test.py: Tests for sh_spec.py
 | 
| 4 | """
 | 
| 5 | 
 | 
| 6 | import cStringIO
 | 
| 7 | import optparse
 | 
| 8 | import pprint
 | 
| 9 | import unittest
 | 
| 10 | 
 | 
| 11 | from test.sh_spec import *  # module under test
 | 
| 12 | from test import spec_lib
 | 
| 13 | 
 | 
| 14 | TEST1 = """\
 | 
| 15 | #### Env binding in readonly/declare disallowed
 | 
| 16 | FOO=foo readonly v=$(tests/printenv.py FOO)
 | 
| 17 | echo "v=$v"
 | 
| 18 | # Shells allow this misleading construct, but the correct behavior is to
 | 
| 19 | # disallow it at parse time.
 | 
| 20 | ## OK bash/dash/mksh stdout: v=None
 | 
| 21 | ## OK bash/dash/mksh status: 0
 | 
| 22 | ## status: 2
 | 
| 23 | """
 | 
| 24 | 
 | 
| 25 | 
 | 
| 26 | TEST2 = """\
 | 
| 27 | #### Multiline test case
 | 
| 28 | echo one
 | 
| 29 | echo two
 | 
| 30 | ## status: 1
 | 
| 31 | ## stderr-json: ""
 | 
| 32 | ## STDOUT:
 | 
| 33 | one
 | 
| 34 | two
 | 
| 35 | ## OK dash STDOUT:
 | 
| 36 | dash1
 | 
| 37 | dash2
 | 
| 38 | ## END
 | 
| 39 | ## OK mksh STDOUT:
 | 
| 40 | mksh1
 | 
| 41 | mksh2
 | 
| 42 | ## END
 | 
| 43 | """
 | 
| 44 | 
 | 
| 45 | 
 | 
| 46 | def Slurp(s):
 | 
| 47 |   t = Tokenizer(cStringIO.StringIO(s))
 | 
| 48 |   tokens = []
 | 
| 49 |   while True:
 | 
| 50 |     tok = t.peek()
 | 
| 51 |     print(tok)
 | 
| 52 |     tokens.append(tok)
 | 
| 53 |     if tok[1] == EOF:
 | 
| 54 |       break
 | 
| 55 |     t.next()
 | 
| 56 |   return tokens
 | 
| 57 | 
 | 
| 58 | 
 | 
| 59 | class ShSpecTest(unittest.TestCase):
 | 
| 60 | 
 | 
| 61 |   def setUp(self):
 | 
| 62 |     self.TOKENS1 = Slurp(TEST1)
 | 
| 63 |     t = Tokenizer(cStringIO.StringIO(TEST1))
 | 
| 64 |     self.CASE1 = ParseTestCase(t)
 | 
| 65 |     assert self.CASE1 is not None
 | 
| 66 | 
 | 
| 67 |     self.TOKENS2 = Slurp(TEST2)
 | 
| 68 |     t = Tokenizer(cStringIO.StringIO(TEST2))
 | 
| 69 |     self.CASE2 = ParseTestCase(t)
 | 
| 70 |     assert self.CASE2 is not None
 | 
| 71 | 
 | 
| 72 |   def testTokenizer(self):
 | 
| 73 |     #pprint.pprint(TOKENS1)
 | 
| 74 | 
 | 
| 75 |     types = [type_ for line_num, type_, value in self.TOKENS1]
 | 
| 76 |     self.assertEqual(
 | 
| 77 |         [ TEST_CASE_BEGIN, PLAIN_LINE, PLAIN_LINE,
 | 
| 78 |           KEY_VALUE, KEY_VALUE, KEY_VALUE,
 | 
| 79 |           EOF], types)
 | 
| 80 | 
 | 
| 81 |     #pprint.pprint(TOKENS2)
 | 
| 82 |     types2 = [type_ for line_num, type_, value in self.TOKENS2]
 | 
| 83 |     self.assertEqual(
 | 
| 84 |         [ TEST_CASE_BEGIN, PLAIN_LINE, PLAIN_LINE,
 | 
| 85 |           KEY_VALUE, KEY_VALUE,
 | 
| 86 |           KEY_VALUE_MULTILINE, PLAIN_LINE, PLAIN_LINE,
 | 
| 87 |           KEY_VALUE_MULTILINE, PLAIN_LINE, PLAIN_LINE, END_MULTILINE,
 | 
| 88 |           KEY_VALUE_MULTILINE, PLAIN_LINE, PLAIN_LINE, END_MULTILINE,
 | 
| 89 |           EOF], types2)
 | 
| 90 | 
 | 
| 91 |   def testParsed(self):
 | 
| 92 |     CASE1 = self.CASE1
 | 
| 93 |     CASE2 = self.CASE2
 | 
| 94 | 
 | 
| 95 |     print('CASE1')
 | 
| 96 |     pprint.pprint(CASE1)
 | 
| 97 |     print()
 | 
| 98 | 
 | 
| 99 |     expected = {'status': '0', 'stdout': 'v=None\n', 'qualifier': 'OK'}
 | 
| 100 |     self.assertEqual(expected, CASE1['bash'])
 | 
| 101 |     self.assertEqual(expected, CASE1['dash'])
 | 
| 102 |     self.assertEqual(expected, CASE1['mksh'])
 | 
| 103 |     self.assertEqual('2', CASE1['status'])
 | 
| 104 |     self.assertEqual(
 | 
| 105 |         'Env binding in readonly/declare disallowed', CASE1['desc'])
 | 
| 106 | 
 | 
| 107 |     print('CASE2')
 | 
| 108 |     pprint.pprint(CASE2)
 | 
| 109 |     print()
 | 
| 110 |     print(CreateAssertions(CASE2, 'bash'))
 | 
| 111 |     self.assertEqual('one\ntwo\n', CASE2['stdout'])
 | 
| 112 |     self.assertEqual(
 | 
| 113 |         {'qualifier': 'OK', 'stdout': 'dash1\ndash2\n'}, CASE2['dash'])
 | 
| 114 |     self.assertEqual(
 | 
| 115 |         {'qualifier': 'OK', 'stdout': 'mksh1\nmksh2\n'}, CASE2['mksh'])
 | 
| 116 | 
 | 
| 117 |   def testCreateAssertions(self):
 | 
| 118 |     print(CreateAssertions(self.CASE1, 'bash'))
 | 
| 119 | 
 | 
| 120 |   def testRunCases(self):
 | 
| 121 |     this_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
 | 
| 122 |     repo_root = os.path.dirname(this_dir)
 | 
| 123 | 
 | 
| 124 |     o = optparse.OptionParser()
 | 
| 125 |     spec_lib.DefineCommon(o)
 | 
| 126 |     spec_lib.DefineShSpec(o)
 | 
| 127 |     opts, _ = o.parse_args(['--tmp-env', os.path.join(repo_root, '_tmp')])
 | 
| 128 | 
 | 
| 129 |     shells = [('bash', '/bin/bash'), ('osh', os.path.join(repo_root, 'bin/osh'))]
 | 
| 130 |     env = {}
 | 
| 131 |     if 0:
 | 
| 132 |       out_f = sys.stdout
 | 
| 133 |     else:
 | 
| 134 |       out_f = cStringIO.StringIO()
 | 
| 135 |     out = AnsiOutput(out_f, False)
 | 
| 136 |     RunCases([self.CASE1], lambda i, case: True, shells, env, out, opts)
 | 
| 137 |     print(repr(out.f.getvalue()))
 | 
| 138 | 
 | 
| 139 |   def testMakeShellPairs(self):
 | 
| 140 |     pairs = spec_lib.MakeShellPairs(['bin/osh', '_bin/osh'])
 | 
| 141 |     print(pairs)
 | 
| 142 |     self.assertEqual(
 | 
| 143 |         [('osh', 'bin/osh'), ('osh_ALT', '_bin/osh')], pairs)
 | 
| 144 | 
 | 
| 145 |     pairs = spec_lib.MakeShellPairs(['bin/osh', '_bin/cxx-dbg/osh'])
 | 
| 146 |     print(pairs)
 | 
| 147 |     self.assertEqual(
 | 
| 148 |         [('osh', 'bin/osh'), ('osh-cpp', '_bin/cxx-dbg/osh')], pairs)
 | 
| 149 | 
 | 
| 150 |     pairs = spec_lib.MakeShellPairs(['bin/osh', '_bin/cxx-dbg-sh/osh'])
 | 
| 151 |     print(pairs)
 | 
| 152 |     self.assertEqual(
 | 
| 153 |         [('osh', 'bin/osh'), ('osh-cpp', '_bin/cxx-dbg-sh/osh')], pairs)
 | 
| 154 | 
 | 
| 155 | 
 | 
| 156 | if __name__ == '__main__':
 | 
| 157 |   unittest.main()
 |