diff --git a/testcases/main/Util/TestTempFile.cs b/testcases/main/Util/TestTempFile.cs index b989570a5..544654260 100644 --- a/testcases/main/Util/TestTempFile.cs +++ b/testcases/main/Util/TestTempFile.cs @@ -1,4 +1,4 @@ -using NPOI.Util; +using NPOI.Util; using NUnit.Framework;using NUnit.Framework.Legacy; using System.IO; using System.Threading; @@ -19,62 +19,60 @@ public void TestCreateTempFile() ClassicAssert.IsTrue(fileInfo!=null && fileInfo.Exists); - string tempDirPath = Path.GetDirectoryName(fileInfo.FullName); - - while(Directory.Exists(tempDirPath)) - { - try - { - Directory.Delete(tempDirPath, true); - } - catch - { - Thread.Sleep(5); - } - } - - ClassicAssert.IsFalse(Directory.Exists(tempDirPath)); - - if(fileInfo!=null) - { - fileInfo.Refresh(); - ClassicAssert.IsFalse(fileInfo.Exists); - } + // Clean up only the file we created, not the shared directory + if(fileInfo != null && fileInfo.Exists) + fileInfo.Delete(); + // Verify CreateTempFile can still create files after cleanup FileInfo file = null; Assert.DoesNotThrow(() => file = TempFile.CreateTempFile("test2", ".xls")); - ClassicAssert.IsTrue(Directory.Exists(tempDirPath)); + ClassicAssert.IsTrue(file != null && file.Exists); if(file !=null && file.Exists) file.Delete(); } [Test] - public void TestGetTempFilePath() + public void TestCreateTempFileRecreatesDirectory() { - string path = ""; - Assert.DoesNotThrow(() => path = TempFile.GetTempFilePath("test", ".xls")); + // Use an isolated subdirectory to test directory recreation + // without interfering with other tests using the shared poifiles dir + string isolatedDir = Path.Combine(Path.GetTempPath(), "poifiles_test_" + System.Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(isolatedDir); - ClassicAssert.IsTrue(!string.IsNullOrWhiteSpace(path)); + try + { + // Create a file in the isolated directory to verify it works + string testFile = Path.Combine(isolatedDir, "test.xls"); + File.WriteAllBytes(testFile, new byte[0]); + ClassicAssert.IsTrue(File.Exists(testFile)); - string tempDirPath = Path.GetDirectoryName(path); + // Delete the isolated directory + Directory.Delete(isolatedDir, true); + ClassicAssert.IsFalse(Directory.Exists(isolatedDir)); - while(Directory.Exists(tempDirPath)) + // Verify we can recreate it + Directory.CreateDirectory(isolatedDir); + ClassicAssert.IsTrue(Directory.Exists(isolatedDir)); + } + finally { - try + if (Directory.Exists(isolatedDir)) { - Directory.Delete(tempDirPath, true); - } - catch - { - Thread.Sleep(10); + try { Directory.Delete(isolatedDir, true); } + catch { /* best effort cleanup */ } } } + } - ClassicAssert.IsFalse(Directory.Exists(tempDirPath)); + [Test] + public void TestGetTempFilePath() + { + string path = ""; + Assert.DoesNotThrow(() => path = TempFile.GetTempFilePath("test", ".xls")); - Assert.DoesNotThrow(() => TempFile.GetTempFilePath("test", ".xls")); - ClassicAssert.IsTrue(Directory.Exists(tempDirPath)); + ClassicAssert.IsTrue(!string.IsNullOrWhiteSpace(path)); + ClassicAssert.IsTrue(Directory.Exists(Path.GetDirectoryName(path))); } } }