| 1 | #!/usr/bin/env python2
|
| 2 | """state_test.py: Tests for state.py."""
|
| 3 |
|
| 4 | import unittest
|
| 5 | import os.path
|
| 6 |
|
| 7 | from _devbuild.gen.id_kind_asdl import Id
|
| 8 | from _devbuild.gen.runtime_asdl import scope_e
|
| 9 | from _devbuild.gen.syntax_asdl import source, SourceLine
|
| 10 | from _devbuild.gen.value_asdl import (value, value_e, sh_lvalue)
|
| 11 | from asdl import runtime
|
| 12 | from core import error
|
| 13 | from core import test_lib
|
| 14 | from core import state # module under test
|
| 15 | from frontend import lexer
|
| 16 | from frontend import location
|
| 17 |
|
| 18 |
|
| 19 | def _InitMem():
|
| 20 | # empty environment, no arena.
|
| 21 | arena = test_lib.MakeArena('<state_test.py>')
|
| 22 | col = 0
|
| 23 | length = 1
|
| 24 | line_id = arena.AddLine(1, 'foo')
|
| 25 | arena.NewToken(-1, col, length, line_id, '') # unused, could be NewToken()
|
| 26 | mem = state.Mem('', [], arena, [])
|
| 27 |
|
| 28 | parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None)
|
| 29 |
|
| 30 | mem.exec_opts = exec_opts
|
| 31 | return mem
|
| 32 |
|
| 33 |
|
| 34 | class MemTest(unittest.TestCase):
|
| 35 |
|
| 36 | def testGet(self):
|
| 37 | mem = _InitMem()
|
| 38 |
|
| 39 | tok_a = lexer.DummyToken(Id.Lit_Chars, 'a')
|
| 40 | tok_a.line = SourceLine(1, 'a b', source.Interactive)
|
| 41 |
|
| 42 | mem.PushCall('my-func', tok_a, ['a', 'b'])
|
| 43 | print(mem.GetValue('HOME'))
|
| 44 | mem.PopCall(True)
|
| 45 | print(mem.GetValue('NONEXISTENT'))
|
| 46 |
|
| 47 | def testSearchPath(self):
|
| 48 | mem = _InitMem()
|
| 49 | #print(mem)
|
| 50 | search_path = state.SearchPath(mem)
|
| 51 |
|
| 52 | # Relative path works without $PATH
|
| 53 | self.assertEqual(None, search_path.LookupOne('__nonexistent__'))
|
| 54 | self.assertEqual('bin/osh', search_path.LookupOne('bin/osh'))
|
| 55 | self.assertEqual(None, search_path.LookupOne('grep'))
|
| 56 |
|
| 57 | # Set $PATH
|
| 58 | mem.SetValue(location.LName('PATH'), value.Str('/bin:/usr/bin'),
|
| 59 | scope_e.GlobalOnly)
|
| 60 |
|
| 61 | self.assertEqual(None, search_path.LookupOne('__nonexistent__'))
|
| 62 | self.assertEqual('bin/osh', search_path.LookupOne('bin/osh'))
|
| 63 |
|
| 64 | # Not hermetic, but should be true on POSIX systems.
|
| 65 | # Also see https://www.freedesktop.org/wiki/Software/systemd/TheCaseForTheUsrMerge/
|
| 66 | # - on some systems, /bin is a symlink to /usr/bin
|
| 67 | if os.path.isfile('/bin/env'):
|
| 68 | self.assertEqual(search_path.LookupOne('env'), '/bin/env')
|
| 69 | else:
|
| 70 | self.assertEqual(search_path.LookupOne('env'), '/usr/bin/env')
|
| 71 |
|
| 72 | def testPushTemp(self):
|
| 73 | mem = _InitMem()
|
| 74 |
|
| 75 | # x=1
|
| 76 | mem.SetValue(location.LName('x'), value.Str('1'), scope_e.Dynamic)
|
| 77 | self.assertEqual('1', mem.var_stack[-1]['x'].val.s)
|
| 78 |
|
| 79 | mem.PushTemp()
|
| 80 |
|
| 81 | self.assertEqual(2, len(mem.var_stack))
|
| 82 |
|
| 83 | # x=temp E=3 read x <<< 'line'
|
| 84 | mem.SetValue(location.LName('x'), value.Str('temp'), scope_e.LocalOnly)
|
| 85 | mem.SetValue(location.LName('E'), value.Str('3'), scope_e.LocalOnly)
|
| 86 | mem.SetValue(location.LName('x'), value.Str('line'), scope_e.LocalOnly)
|
| 87 |
|
| 88 | self.assertEqual('3', mem.var_stack[-1]['E'].val.s)
|
| 89 | self.assertEqual('line', mem.var_stack[-1]['x'].val.s)
|
| 90 | self.assertEqual('1', mem.var_stack[-2]['x'].val.s)
|
| 91 |
|
| 92 | mem.PopTemp()
|
| 93 | self.assertEqual(1, len(mem.var_stack))
|
| 94 | self.assertEqual('1', mem.var_stack[-1]['x'].val.s)
|
| 95 |
|
| 96 | def testSetVarClearFlag(self):
|
| 97 | mem = _InitMem()
|
| 98 | print(mem)
|
| 99 |
|
| 100 | tok_one = lexer.DummyToken(Id.Lit_Chars, 'ONE')
|
| 101 | tok_one.line = SourceLine(1, 'ONE', source.Interactive)
|
| 102 |
|
| 103 | tok_two = lexer.DummyToken(Id.Lit_Chars, 'TWO')
|
| 104 | tok_two.line = SourceLine(1, 'TWO', source.Interactive)
|
| 105 |
|
| 106 | mem.PushCall('my-func', tok_one, ['ONE'])
|
| 107 | self.assertEqual(2, len(mem.var_stack)) # internal details
|
| 108 |
|
| 109 | # local x=y
|
| 110 | mem.SetValue(location.LName('x'), value.Str('y'), scope_e.LocalOnly)
|
| 111 | self.assertEqual('y', mem.var_stack[-1]['x'].val.s)
|
| 112 |
|
| 113 | # New frame
|
| 114 | mem.PushCall('my-func', tok_two, ['TWO'])
|
| 115 | self.assertEqual(3, len(mem.var_stack)) # internal details
|
| 116 |
|
| 117 | # x=y -- test out dynamic scope
|
| 118 | mem.SetValue(location.LName('x'), value.Str('YYY'), scope_e.Dynamic)
|
| 119 | self.assertEqual('YYY', mem.var_stack[-2]['x'].val.s)
|
| 120 | self.assertEqual(None, mem.var_stack[-1].get('x'))
|
| 121 |
|
| 122 | # myglobal=g
|
| 123 | mem.SetValue(location.LName('myglobal'), value.Str('g'),
|
| 124 | scope_e.Dynamic)
|
| 125 | self.assertEqual('g', mem.var_stack[0]['myglobal'].val.s)
|
| 126 | self.assertEqual(False, mem.var_stack[0]['myglobal'].exported)
|
| 127 |
|
| 128 | # 'export PYTHONPATH=/'
|
| 129 | mem.SetValue(location.LName('PYTHONPATH'),
|
| 130 | value.Str('/'),
|
| 131 | scope_e.Dynamic,
|
| 132 | flags=state.SetExport)
|
| 133 | self.assertEqual('/', mem.var_stack[0]['PYTHONPATH'].val.s)
|
| 134 | self.assertEqual(True, mem.var_stack[0]['PYTHONPATH'].exported)
|
| 135 |
|
| 136 | cmd_ev = mem.GetExported()
|
| 137 | self.assertEqual('/', cmd_ev['PYTHONPATH'])
|
| 138 |
|
| 139 | mem.SetValue(location.LName('PYTHONPATH'),
|
| 140 | None,
|
| 141 | scope_e.Dynamic,
|
| 142 | flags=state.SetExport)
|
| 143 | self.assertEqual(True, mem.var_stack[0]['PYTHONPATH'].exported)
|
| 144 |
|
| 145 | # 'export myglobal'. None means don't touch it. Undef would be confusing
|
| 146 | # because it might mean "unset", but we have a separated API for that.
|
| 147 | mem.SetValue(location.LName('myglobal'),
|
| 148 | None,
|
| 149 | scope_e.Dynamic,
|
| 150 | flags=state.SetExport)
|
| 151 | self.assertEqual(True, mem.var_stack[0]['myglobal'].exported)
|
| 152 |
|
| 153 | # export g2 -- define and export empty
|
| 154 | mem.SetValue(location.LName('g2'),
|
| 155 | None,
|
| 156 | scope_e.Dynamic,
|
| 157 | flags=state.SetExport)
|
| 158 | self.assertEqual(value_e.Undef, mem.var_stack[0]['g2'].val.tag())
|
| 159 | self.assertEqual(True, mem.var_stack[0]['g2'].exported)
|
| 160 |
|
| 161 | # readonly myglobal
|
| 162 | self.assertEqual(False, mem.var_stack[0]['myglobal'].readonly)
|
| 163 | mem.SetValue(location.LName('myglobal'),
|
| 164 | None,
|
| 165 | scope_e.Dynamic,
|
| 166 | flags=state.SetReadOnly)
|
| 167 | self.assertEqual(True, mem.var_stack[0]['myglobal'].readonly)
|
| 168 |
|
| 169 | mem.SetValue(location.LName('PYTHONPATH'), value.Str('/lib'),
|
| 170 | scope_e.Dynamic)
|
| 171 | self.assertEqual('/lib', mem.var_stack[0]['PYTHONPATH'].val.s)
|
| 172 | self.assertEqual(True, mem.var_stack[0]['PYTHONPATH'].exported)
|
| 173 |
|
| 174 | # COMPREPLY=(1 2 3)
|
| 175 | # invariant to enforce: arrays can't be exported
|
| 176 | mem.SetValue(location.LName('COMPREPLY'),
|
| 177 | value.BashArray(['1', '2', '3']), scope_e.GlobalOnly)
|
| 178 | self.assertEqual(['1', '2', '3'],
|
| 179 | mem.var_stack[0]['COMPREPLY'].val.strs)
|
| 180 |
|
| 181 | # export COMPREPLY
|
| 182 | try:
|
| 183 | mem.SetValue(location.LName('COMPREPLY'),
|
| 184 | None,
|
| 185 | scope_e.Dynamic,
|
| 186 | flags=state.SetExport)
|
| 187 | except error.FatalRuntime as e:
|
| 188 | pass
|
| 189 | else:
|
| 190 | self.fail("Expected failure")
|
| 191 |
|
| 192 | # readonly r=1
|
| 193 | mem.SetValue(location.LName('r'),
|
| 194 | value.Str('1'),
|
| 195 | scope_e.Dynamic,
|
| 196 | flags=state.SetReadOnly)
|
| 197 | self.assertEqual('1', mem.var_stack[0]['r'].val.s)
|
| 198 | self.assertEqual(False, mem.var_stack[0]['r'].exported)
|
| 199 | self.assertEqual(True, mem.var_stack[0]['r'].readonly)
|
| 200 | print(mem)
|
| 201 |
|
| 202 | # r=newvalue
|
| 203 | try:
|
| 204 | mem.SetValue(location.LName('r'), value.Str('newvalue'),
|
| 205 | scope_e.Dynamic)
|
| 206 | except error.FatalRuntime as e:
|
| 207 | pass
|
| 208 | else:
|
| 209 | self.fail("Expected failure")
|
| 210 |
|
| 211 | # readonly r2 -- define empty readonly
|
| 212 | mem.SetValue(location.LName('r2'),
|
| 213 | None,
|
| 214 | scope_e.Dynamic,
|
| 215 | flags=state.SetReadOnly)
|
| 216 | self.assertEqual(value_e.Undef, mem.var_stack[0]['r2'].val.tag())
|
| 217 | self.assertEqual(True, mem.var_stack[0]['r2'].readonly)
|
| 218 |
|
| 219 | # export -n PYTHONPATH
|
| 220 | # Remove the exported property. NOTE: scope is LocalOnly for Oil?
|
| 221 | self.assertEqual(True, mem.var_stack[0]['PYTHONPATH'].exported)
|
| 222 | mem.ClearFlag('PYTHONPATH', state.ClearExport)
|
| 223 | self.assertEqual(False, mem.var_stack[0]['PYTHONPATH'].exported)
|
| 224 |
|
| 225 | lhs = sh_lvalue.Indexed('a', 1, runtime.NO_SPID)
|
| 226 | # a[1]=2
|
| 227 | mem.SetValue(lhs, value.Str('2'), scope_e.Dynamic)
|
| 228 | self.assertEqual([None, '2'], mem.var_stack[0]['a'].val.strs)
|
| 229 |
|
| 230 | # a[1]=3
|
| 231 | mem.SetValue(lhs, value.Str('3'), scope_e.Dynamic)
|
| 232 | self.assertEqual([None, '3'], mem.var_stack[0]['a'].val.strs)
|
| 233 |
|
| 234 | # a[1]=(x y z) # illegal but doesn't parse anyway
|
| 235 | if 0:
|
| 236 | try:
|
| 237 | mem.SetValue(lhs, value.BashArray(['x', 'y', 'z']),
|
| 238 | scope_e.Dynamic)
|
| 239 | except error.FatalRuntime as e:
|
| 240 | pass
|
| 241 | else:
|
| 242 | self.fail("Expected failure")
|
| 243 |
|
| 244 | # readonly a
|
| 245 | mem.SetValue(location.LName('a'),
|
| 246 | None,
|
| 247 | scope_e.Dynamic,
|
| 248 | flags=state.SetReadOnly)
|
| 249 | self.assertEqual(True, mem.var_stack[0]['a'].readonly)
|
| 250 |
|
| 251 | try:
|
| 252 | # a[1]=3
|
| 253 | mem.SetValue(lhs, value.Str('3'), scope_e.Dynamic)
|
| 254 | except error.FatalRuntime as e:
|
| 255 | pass
|
| 256 | else:
|
| 257 | self.fail("Expected failure")
|
| 258 |
|
| 259 | def testGetValue(self):
|
| 260 | mem = _InitMem()
|
| 261 |
|
| 262 | # readonly a=x
|
| 263 | mem.SetValue(location.LName('a'),
|
| 264 | value.Str('x'),
|
| 265 | scope_e.Dynamic,
|
| 266 | flags=state.SetReadOnly)
|
| 267 |
|
| 268 | val = mem.GetValue('a', scope_e.Dynamic)
|
| 269 | test_lib.AssertAsdlEqual(self, value.Str('x'), val)
|
| 270 |
|
| 271 | val = mem.GetValue('undef', scope_e.Dynamic)
|
| 272 | test_lib.AssertAsdlEqual(self, value.Undef, val)
|
| 273 |
|
| 274 | def testExportThenAssign(self):
|
| 275 | """Regression Test."""
|
| 276 | mem = _InitMem()
|
| 277 |
|
| 278 | # export U
|
| 279 | mem.SetValue(location.LName('U'),
|
| 280 | None,
|
| 281 | scope_e.Dynamic,
|
| 282 | flags=state.SetExport)
|
| 283 | print(mem)
|
| 284 |
|
| 285 | # U=u
|
| 286 | mem.SetValue(location.LName('U'), value.Str('u'), scope_e.Dynamic)
|
| 287 | print(mem)
|
| 288 | e = mem.GetExported()
|
| 289 | self.assertEqual('u', e['U'])
|
| 290 |
|
| 291 | def testUnset(self):
|
| 292 | mem = _InitMem()
|
| 293 | # unset a
|
| 294 | mem.Unset(location.LName('a'), scope_e.Shopt)
|
| 295 |
|
| 296 | return # not implemented yet
|
| 297 |
|
| 298 | # unset a[1]
|
| 299 | mem.Unset(sh_lvalue.Indexed('a', 1, runtime.NO_SPID), False)
|
| 300 |
|
| 301 | def testArgv(self):
|
| 302 | mem = _InitMem()
|
| 303 | src = source.Interactive
|
| 304 |
|
| 305 | tok_a = lexer.DummyToken(Id.Lit_Chars, 'a')
|
| 306 | tok_a.line = SourceLine(1, 'a b', src)
|
| 307 |
|
| 308 | mem.PushCall('my-func', tok_a, ['a', 'b'])
|
| 309 | self.assertEqual(['a', 'b'], mem.GetArgv())
|
| 310 |
|
| 311 | tok_x = lexer.DummyToken(Id.Lit_Chars, 'x')
|
| 312 | tok_x.line = SourceLine(2, 'x y', src)
|
| 313 |
|
| 314 | mem.PushCall('my-func', tok_x, ['x', 'y'])
|
| 315 | self.assertEqual(['x', 'y'], mem.GetArgv())
|
| 316 |
|
| 317 | status = mem.Shift(1)
|
| 318 | self.assertEqual(['y'], mem.GetArgv())
|
| 319 | self.assertEqual(0, status)
|
| 320 |
|
| 321 | status = mem.Shift(1)
|
| 322 | self.assertEqual([], mem.GetArgv())
|
| 323 | self.assertEqual(0, status)
|
| 324 |
|
| 325 | status = mem.Shift(1)
|
| 326 | self.assertEqual([], mem.GetArgv())
|
| 327 | self.assertEqual(1, status) # error
|
| 328 |
|
| 329 | mem.PopCall(True)
|
| 330 | self.assertEqual(['a', 'b'], mem.GetArgv())
|
| 331 |
|
| 332 | def testArgv2(self):
|
| 333 | mem = state.Mem('', ['x', 'y'], None, [])
|
| 334 |
|
| 335 | mem.Shift(1)
|
| 336 | self.assertEqual(['y'], mem.GetArgv())
|
| 337 |
|
| 338 | mem.SetArgv(['i', 'j', 'k'])
|
| 339 | self.assertEqual(['i', 'j', 'k'], mem.GetArgv())
|
| 340 |
|
| 341 |
|
| 342 | if __name__ == '__main__':
|
| 343 | unittest.main()
|