Skip to content

Commit 953dc8d

Browse files
committed
fix tests
1 parent 1788b80 commit 953dc8d

1 file changed

Lines changed: 44 additions & 42 deletions

File tree

Lib/idlelib/idle_test/test_iomenu.py

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
"Test , coverage 17%."
2-
3-
from idlelib import iomenu
1+
import builtins
2+
import os
3+
import tempfile
44
import unittest
5-
from unittest.mock import patch, mock_open
5+
from unittest.mock import patch
6+
67
from test.support import requires
78
from tkinter import Tk
9+
10+
from idlelib import iomenu, util
811
from idlelib.editor import EditorWindow
9-
from idlelib import util
1012
from idlelib.idle_test.mock_idle import Func
1113

1214
# Fail if either tokenize.open and t.detect_encoding does not exist.
1315
# These are used in loadfile and encode.
1416
# Also used in pyshell.MI.execfile and runscript.tabnanny.
15-
from tokenize import open, detect_encoding
17+
from tokenize import open as tokenize_open, detect_encoding
1618
# Remove when we have proper tests that use both.
1719

1820

@@ -33,10 +35,18 @@ def tearDownClass(cls):
3335
del cls.editwin
3436
cls.root.update_idletasks()
3537
for id in cls.root.tk.call('after', 'info'):
36-
cls.root.after_cancel(id) # Need for EditorWindow.
38+
cls.root.after_cancel(id)
3739
cls.root.destroy()
3840
del cls.root
3941

42+
def _create_tempfile(self, content: str) -> str:
43+
fd, filename = tempfile.mkstemp(suffix='.py')
44+
os.close(fd)
45+
self.addCleanup(os.unlink, filename)
46+
with builtins.open(filename, 'w', encoding='utf-8') as f:
47+
f.write(content)
48+
return filename
49+
4050
def test_init(self):
4151
self.assertIs(self.io.editwin, self.editwin)
4252

@@ -46,94 +56,86 @@ def test_fixnewlines_end(self):
4656
fix = io.fixnewlines
4757
text = io.editwin.text
4858

49-
# Make the editor temporarily look like Shell.
5059
self.editwin.interp = None
5160
shelltext = '>>> if 1'
5261
self.editwin.get_prompt_text = Func(result=shelltext)
53-
eq(fix(), shelltext) # Get... call and '\n' not added.
62+
eq(fix(), shelltext)
5463
del self.editwin.interp, self.editwin.get_prompt_text
5564

5665
text.insert(1.0, 'a')
57-
eq(fix(), 'a'+io.eol_convention)
66+
eq(fix(), 'a' + io.eol_convention)
5867
eq(text.get('1.0', 'end-1c'), 'a\n')
59-
eq(fix(), 'a'+io.eol_convention)
68+
eq(fix(), 'a' + io.eol_convention)
6069

6170
def test_reload_no_file(self):
62-
# Test reload when no file is associated
6371
io = self.io
6472
io.filename = None
6573

66-
with patch.object(iomenu.messagebox, 'showinfo') as mock_showinfo:
74+
with patch('idlelib.iomenu.messagebox.showinfo') as mock_showinfo:
6775
result = io.reload(None)
6876
self.assertEqual(result, "break")
6977
mock_showinfo.assert_called_once()
7078
args, kwargs = mock_showinfo.call_args
7179
self.assertIn("File Not Found", args[0])
7280

73-
@patch('idlelib.iomenu.messagebox.showerror')
74-
def test_reload_with_file(self, mock_showerror):
75-
# Test reload with an actual file
81+
def test_reload_with_file(self):
7682
io = self.io
7783
text = io.editwin.text
78-
io.filename = "/dummy/path/test.py"
79-
8084
original_content = "# Original content\n"
8185
modified_content = "# Modified content\n"
8286

83-
m = mock_open()
84-
m.side_effect = [
85-
mock_open(read_data=original_content).return_value,
86-
mock_open(read_data=modified_content).return_value,
87-
]
87+
filename = self._create_tempfile(original_content)
88+
io.filename = filename
8889

89-
with patch('builtins.open', m):
90+
with patch('idlelib.iomenu.messagebox.showerror') as mock_showerror:
9091
io.loadfile(io.filename)
9192
self.assertEqual(text.get('1.0', 'end-1c'), original_content)
93+
94+
with builtins.open(filename, 'w', encoding='utf-8') as f:
95+
f.write(modified_content)
96+
9297
result = io.reload(None)
9398

9499
mock_showerror.assert_not_called()
95100
self.assertEqual(result, "break")
96101
self.assertEqual(text.get('1.0', 'end-1c'), modified_content)
97102

98103
def test_reload_with_unsaved_changes_cancel(self):
99-
# Test reload with unsaved changes and user cancels
100104
io = self.io
101105
text = io.editwin.text
102-
io.filename = "/dummy/path/test.py"
103106
original_content = "# Original content\n"
104107
unsaved_content = original_content + "\n# Unsaved change"
105108

106-
# Mock the initial file load.
107-
with patch('builtins.open', mock_open(read_data=original_content)):
108-
io.loadfile(io.filename)
109+
filename = self._create_tempfile(original_content)
110+
io.filename = filename
111+
io.loadfile(io.filename)
109112

110113
text.insert('end', "\n# Unsaved change")
111114
io.set_saved(False)
112115

113116
with patch('idlelib.iomenu.messagebox.askokcancel', return_value=False) as mock_ask:
114117
result = io.reload(None)
115-
self.assertEqual(result, "break")
116-
# Content should not change.
117-
self.assertEqual(text.get('1.0', 'end-1c'), unsaved_content)
118-
mock_ask.assert_called_once()
118+
119+
self.assertEqual(result, "break")
120+
self.assertEqual(text.get('1.0', 'end-1c'), unsaved_content)
121+
mock_ask.assert_called_once()
119122

120123
def test_reload_with_unsaved_changes_confirm(self):
121-
# Test reload with unsaved changes and user confirms
122124
io = self.io
123125
text = io.editwin.text
124-
io.filename = "/dummy/path/test.py"
125126
original_content = "# Original content\n"
126127

127-
with patch('builtins.open', mock_open(read_data=original_content)):
128-
io.loadfile(io.filename)
129-
text.insert('end', "\n# Unsaved change")
130-
io.set_saved(False)
128+
filename = self._create_tempfile(original_content)
129+
io.filename = filename
130+
io.loadfile(io.filename)
131131

132-
with patch('idlelib.iomenu.messagebox.askokcancel', return_value=True) as mock_ask:
133-
result = io.reload(None)
132+
text.insert('end', "\n# Unsaved change")
133+
io.set_saved(False)
134+
135+
with patch('idlelib.iomenu.messagebox.askokcancel', return_value=True) as mock_ask:
136+
result = io.reload(None)
134137

135138
self.assertEqual(result, "break")
136-
# Content should be reverted to original.
137139
self.assertEqual(text.get('1.0', 'end-1c'), original_content)
138140
mock_ask.assert_called_once()
139141

0 commit comments

Comments
 (0)