ShUtil.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # RUN: %{python} %s
  2. import unittest
  3. from lit.ShUtil import Command, Pipeline, Seq, ShLexer, ShParser
  4. class TestShLexer(unittest.TestCase):
  5. def lex(self, str, *args, **kwargs):
  6. return list(ShLexer(str, *args, **kwargs).lex())
  7. def test_basic(self):
  8. self.assertEqual(self.lex('a|b>c&d<e;f'),
  9. ['a', ('|',), 'b', ('>',), 'c', ('&',), 'd',
  10. ('<',), 'e', (';',), 'f'])
  11. def test_redirection_tokens(self):
  12. self.assertEqual(self.lex('a2>c'),
  13. ['a2', ('>',), 'c'])
  14. self.assertEqual(self.lex('a 2>c'),
  15. ['a', ('>',2), 'c'])
  16. def test_quoting(self):
  17. self.assertEqual(self.lex(""" 'a' """),
  18. ['a'])
  19. self.assertEqual(self.lex(""" "hello\\"world" """),
  20. ['hello"world'])
  21. self.assertEqual(self.lex(""" "hello\\'world" """),
  22. ["hello\\'world"])
  23. self.assertEqual(self.lex(""" "hello\\\\world" """),
  24. ["hello\\world"])
  25. self.assertEqual(self.lex(""" he"llo wo"rld """),
  26. ["hello world"])
  27. self.assertEqual(self.lex(""" a\\ b a\\\\b """),
  28. ["a b", "a\\b"])
  29. self.assertEqual(self.lex(""" "" "" """),
  30. ["", ""])
  31. self.assertEqual(self.lex(""" a\\ b """, win32Escapes = True),
  32. ['a\\', 'b'])
  33. class TestShParse(unittest.TestCase):
  34. def parse(self, str):
  35. return ShParser(str).parse()
  36. def test_basic(self):
  37. self.assertEqual(self.parse('echo hello'),
  38. Pipeline([Command(['echo', 'hello'], [])], False))
  39. self.assertEqual(self.parse('echo ""'),
  40. Pipeline([Command(['echo', ''], [])], False))
  41. self.assertEqual(self.parse("""echo -DFOO='a'"""),
  42. Pipeline([Command(['echo', '-DFOO=a'], [])], False))
  43. self.assertEqual(self.parse('echo -DFOO="a"'),
  44. Pipeline([Command(['echo', '-DFOO=a'], [])], False))
  45. def test_redirection(self):
  46. self.assertEqual(self.parse('echo hello > c'),
  47. Pipeline([Command(['echo', 'hello'],
  48. [((('>'),), 'c')])], False))
  49. self.assertEqual(self.parse('echo hello > c >> d'),
  50. Pipeline([Command(['echo', 'hello'], [(('>',), 'c'),
  51. (('>>',), 'd')])], False))
  52. self.assertEqual(self.parse('a 2>&1'),
  53. Pipeline([Command(['a'], [(('>&',2), '1')])], False))
  54. def test_pipeline(self):
  55. self.assertEqual(self.parse('a | b'),
  56. Pipeline([Command(['a'], []),
  57. Command(['b'], [])],
  58. False))
  59. self.assertEqual(self.parse('a | b | c'),
  60. Pipeline([Command(['a'], []),
  61. Command(['b'], []),
  62. Command(['c'], [])],
  63. False))
  64. def test_list(self):
  65. self.assertEqual(self.parse('a ; b'),
  66. Seq(Pipeline([Command(['a'], [])], False),
  67. ';',
  68. Pipeline([Command(['b'], [])], False)))
  69. self.assertEqual(self.parse('a & b'),
  70. Seq(Pipeline([Command(['a'], [])], False),
  71. '&',
  72. Pipeline([Command(['b'], [])], False)))
  73. self.assertEqual(self.parse('a && b'),
  74. Seq(Pipeline([Command(['a'], [])], False),
  75. '&&',
  76. Pipeline([Command(['b'], [])], False)))
  77. self.assertEqual(self.parse('a || b'),
  78. Seq(Pipeline([Command(['a'], [])], False),
  79. '||',
  80. Pipeline([Command(['b'], [])], False)))
  81. self.assertEqual(self.parse('a && b || c'),
  82. Seq(Seq(Pipeline([Command(['a'], [])], False),
  83. '&&',
  84. Pipeline([Command(['b'], [])], False)),
  85. '||',
  86. Pipeline([Command(['c'], [])], False)))
  87. self.assertEqual(self.parse('a; b'),
  88. Seq(Pipeline([Command(['a'], [])], False),
  89. ';',
  90. Pipeline([Command(['b'], [])], False)))
  91. if __name__ == '__main__':
  92. unittest.main()