1- import os
1+ import tempfile
22import unittest
3+ from pathlib import Path
34
45from bpython .config import getpreferredencoding
56from bpython .history import History
67
78
89class TestHistory (unittest .TestCase ):
910 def setUp (self ):
10- self .history = History ("#%d" % x for x in range (1000 ))
11+ self .history = History (f"# { x } " for x in range (1000 ))
1112
1213 def test_is_at_start (self ):
1314 self .history .first ()
@@ -84,7 +85,8 @@ def test_reset(self):
8485
8586class TestHistoryFileAccess (unittest .TestCase ):
8687 def setUp (self ):
87- self .filename = "history_temp_file"
88+ self .tempdir = tempfile .TemporaryDirectory ()
89+ self .filename = str (Path (self .tempdir .name ) / "history_temp_file" )
8890 self .encoding = getpreferredencoding ()
8991
9092 with open (
@@ -109,21 +111,17 @@ def test_append_reload_and_write(self):
109111
110112 def test_save (self ):
111113 history = History ()
112- history .entries = []
113- for line in ["#1" , "#2" , "#3" , "#4" ]:
114+ for line in ("#1" , "#2" , "#3" , "#4" ):
114115 history .append_to (history .entries , line )
115116
116117 # save only last 2 lines
117118 history .save (self .filename , self .encoding , lines = 2 )
118119
119- # empty the list of entries and load again from the file
120- history . entries = [ "" ]
120+ # load again from the file
121+ history = History ()
121122 history .load (self .filename , self .encoding )
122123
123124 self .assertEqual (history .entries , ["#3" , "#4" ])
124125
125126 def tearDown (self ):
126- try :
127- os .remove (self .filename )
128- except OSError :
129- pass
127+ self .tempdir = None
0 commit comments