From c86a3425a4c237763b0a297af17a5d4d2fe2a2a5 Mon Sep 17 00:00:00 2001 From: lucassouzaalff-lang Date: Tue, 18 Aug 2026 18:21:27 -0300 Subject: [PATCH] Fix testFileNameFire on Windows: NamedTemporaryFile can't be reopened while open NamedTemporaryFile defaults to delete-on-close, and on Windows the underlying file cannot be opened a second time while it is still open. MainModuleFileTest keeps self.file/self.file2 open in setUp(), but __main__.main() (via exec_module) and testFileNameModuleDuplication() both need to reopen them by path, which raises PermissionError on Windows. Fix by creating the temp files with delete=False, closing them right after writing, and removing them explicitly via addCleanup. --- fire/main_test.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/fire/main_test.py b/fire/main_test.py index 9e1c382b..b7aa6e81 100644 --- a/fire/main_test.py +++ b/fire/main_test.py @@ -44,11 +44,20 @@ class MainModuleFileTest(testutils.BaseTestCase): def setUp(self): super().setUp() - self.file = tempfile.NamedTemporaryFile(suffix='.py') # pylint: disable=consider-using-with + # NamedTemporaryFile is opened with delete=False and closed explicitly + # here (with cleanup deferred to addCleanup) because on Windows the + # underlying file cannot be reopened by name while still open, and both + # __main__.main() (import) and testFileNameModuleDuplication() (a second + # open()) need to reopen these files by path. + self.file = tempfile.NamedTemporaryFile( # pylint: disable=consider-using-with + suffix='.py', delete=False) self.file.write(b'class Foo:\n def double(self, n):\n return 2 * n\n') - self.file.flush() + self.file.close() + self.addCleanup(os.unlink, self.file.name) - self.file2 = tempfile.NamedTemporaryFile() # pylint: disable=consider-using-with + self.file2 = tempfile.NamedTemporaryFile(delete=False) # pylint: disable=consider-using-with + self.file2.close() + self.addCleanup(os.unlink, self.file2.name) def testFileNameFire(self): # Confirm that the file is correctly imported and doubles the number.