1 | #!/usr/bin/env python2
|
2 | """
|
3 | builtin_lib_test.py: Tests for readline_osh.py
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | import cStringIO
|
8 | import unittest
|
9 | # We use native/line_input.c, a fork of readline.c, but this is good enough for
|
10 | # unit testing
|
11 | import readline
|
12 |
|
13 | from builtin import readline_osh # module under test
|
14 | from core import test_lib
|
15 | from core import state
|
16 | from core import alloc
|
17 | from display import ui
|
18 | from frontend import flag_def # side effect: flags are defined!
|
19 |
|
20 | _ = flag_def
|
21 |
|
22 |
|
23 | class BuiltinTest(unittest.TestCase):
|
24 |
|
25 | def testHistoryBuiltin(self):
|
26 | test_path = '_tmp/builtin_test_history.txt'
|
27 | with open(test_path, 'w') as f:
|
28 | f.write("""\
|
29 | echo hello
|
30 | ls one/
|
31 | ls two/
|
32 | echo bye
|
33 | """)
|
34 | readline.read_history_file(test_path)
|
35 |
|
36 | # Show all
|
37 | f = cStringIO.StringIO()
|
38 | out = _TestHistory(['history'])
|
39 |
|
40 | self.assertEqual(
|
41 | out, """\
|
42 | 1 echo hello
|
43 | 2 ls one/
|
44 | 3 ls two/
|
45 | 4 echo bye
|
46 | """)
|
47 |
|
48 | # Show two history items
|
49 | out = _TestHistory(['history', '2'])
|
50 | # Note: whitespace here is exact
|
51 | self.assertEqual(out, """\
|
52 | 3 ls two/
|
53 | 4 echo bye
|
54 | """)
|
55 |
|
56 | # Delete single history item.
|
57 | # This functionality is *silent*
|
58 | # so call history again after
|
59 | # this to feed the test assertion
|
60 |
|
61 | _TestHistory(['history', '-d', '4'])
|
62 |
|
63 | # Call history
|
64 | out = _TestHistory(['history'])
|
65 |
|
66 | # Note: whitespace here is exact
|
67 | self.assertEqual(
|
68 | out, """\
|
69 | 1 echo hello
|
70 | 2 ls one/
|
71 | 3 ls two/
|
72 | """)
|
73 |
|
74 | # Clear history
|
75 | # This functionality is *silent*
|
76 | # so call history again after
|
77 | # this to feed the test assertion
|
78 |
|
79 | _TestHistory(['history', '-c'])
|
80 |
|
81 | # Call history
|
82 | out = _TestHistory(['history'])
|
83 |
|
84 | self.assertEqual(out, """\
|
85 | """)
|
86 |
|
87 |
|
88 | def _TestHistory(argv):
|
89 | f = cStringIO.StringIO()
|
90 | arena = alloc.Arena()
|
91 | mem = state.Mem('', [], arena, [])
|
92 | errfmt = ui.ErrorFormatter()
|
93 | b = readline_osh.History(readline, mem, errfmt, f)
|
94 | cmd_val = test_lib.MakeBuiltinArgv(argv)
|
95 | b.Run(cmd_val)
|
96 | return f.getvalue()
|
97 |
|
98 |
|
99 | if __name__ == '__main__':
|
100 | unittest.main()
|