Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 36 additions & 38 deletions testcases/main/Util/TestTempFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NPOI.Util;
using NPOI.Util;
using NUnit.Framework;using NUnit.Framework.Legacy;
using System.IO;
using System.Threading;
Expand All @@ -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)));
}
}
}
Loading