|
1 | 1 | # encoding: utf-8
|
2 | 2 |
|
| 3 | +import io |
| 4 | +import os |
| 5 | + |
3 | 6 | from six.moves import range
|
4 | 7 |
|
| 8 | +from bpython.config import getpreferredencoding |
5 | 9 | from bpython.history import History
|
6 | 10 | from bpython.test import unittest
|
7 | 11 |
|
@@ -81,3 +85,46 @@ def test_reset(self):
|
81 | 85 |
|
82 | 86 | self.assertEqual(self.history.back(), '#999')
|
83 | 87 | self.assertEqual(self.history.forward(), '')
|
| 88 | + |
| 89 | + |
| 90 | +class TestHistoryFileAccess(unittest.TestCase): |
| 91 | + def setUp(self): |
| 92 | + self.filename = 'history_temp_file' |
| 93 | + self.encoding = getpreferredencoding() |
| 94 | + |
| 95 | + with io.open(self.filename, 'w', encoding=self.encoding, |
| 96 | + errors='ignore') as f: |
| 97 | + f.write('#1\n#2\n') |
| 98 | + |
| 99 | + def test_load(self): |
| 100 | + history = History() |
| 101 | + |
| 102 | + history.load(self.filename, self.encoding) |
| 103 | + self.assertEqual(history.entries, ['#1', '#2']) |
| 104 | + |
| 105 | + def test_append_reload_and_write(self): |
| 106 | + history = History() |
| 107 | + |
| 108 | + history.append_reload_and_write('#3', self.filename, self.encoding) |
| 109 | + self.assertEqual(history.entries, ['#1', '#2', '#3']) |
| 110 | + |
| 111 | + history.append_reload_and_write('#4', self.filename, self.encoding) |
| 112 | + self.assertEqual(history.entries, ['#1', '#2', '#3', '#4']) |
| 113 | + |
| 114 | + def test_save(self): |
| 115 | + history = History(['#1', '#2', '#3', '#4']) |
| 116 | + |
| 117 | + # save only last 2 lines |
| 118 | + history.save(self.filename, self.encoding, lines=2) |
| 119 | + |
| 120 | + # empty the list of entries and load again from the file |
| 121 | + history.entries = [''] |
| 122 | + history.load(self.filename, self.encoding) |
| 123 | + |
| 124 | + self.assertEqual(history.entries, ['#3', '#4']) |
| 125 | + |
| 126 | + def tearDown(self): |
| 127 | + try: |
| 128 | + os.remove(self.filename) |
| 129 | + except OSError: |
| 130 | + pass |
0 commit comments