Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TempFile usage while temp dir was deleted in same process #1132

Merged
merged 29 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9535672
Fix TempFile usage while temp dir was deleted in same process
Bykiev Jul 27, 2023
8f86d2e
Update TempFile.cs
Bykiev Jul 27, 2023
77fd8d8
Fix CI test execution
Bykiev Jul 27, 2023
1c8a56a
Trying to fix tests2
Bykiev Jul 27, 2023
b293c63
trying to fix test 3
Bykiev Jul 27, 2023
2aea9f9
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev Sep 1, 2023
6c8fc86
Update TempFile.cs
Bykiev Sep 1, 2023
1daa483
Update TempFile.cs
Bykiev Sep 1, 2023
928fa26
Another one try
Bykiev Sep 1, 2023
e7faafb
Remove RunSerialyAndSweepTmpFilesAttribute
Bykiev Sep 14, 2023
949d395
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev Sep 14, 2023
a35231b
Update SXSSFWorkbookTests.cs
Bykiev Sep 14, 2023
99c5605
Update SXSSFWorkbookTests.cs
Bykiev Sep 14, 2023
23060c1
Call dispose on SheeDataWriter.Close()
Bykiev Sep 14, 2023
366130a
Comment all SXSSFWorkbookTests
Bykiev Sep 14, 2023
114434f
test
Bykiev Sep 14, 2023
ec9bb51
Update TempFile.cs
Bykiev Sep 14, 2023
874dee8
Comment TestSXSSFWorkbook
Bykiev Sep 14, 2023
e5d0d20
Update TestSXSSFWorkbook.cs
Bykiev Sep 14, 2023
f03e78d
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev Jan 3, 2024
4e11700
Update TestTempFile.cs
Bykiev Jan 3, 2024
ede309e
Update TestTempFile.cs
Bykiev Jan 3, 2024
d061292
Update TestTempFile.cs
Bykiev Jan 3, 2024
358e23c
Update TestTempFile.cs
Bykiev Jan 3, 2024
8e5a924
Update TestXSSFWorkbook.cs
Bykiev Jan 3, 2024
9f69b61
More tries
Bykiev Jan 3, 2024
ae31cf0
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev Mar 12, 2024
9c02753
Update TestSXSSFWorkbook.cs
Bykiev Mar 12, 2024
dedde39
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev Apr 26, 2024
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
19 changes: 14 additions & 5 deletions main/Util/TempFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public class TempFile
*/
public static FileInfo CreateTempFile(String prefix, String suffix)
{

if (dir == null)
if (string.IsNullOrWhiteSpace(dir))
{
dir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "poifiles")).FullName;
string tempDir = Path.Combine(Path.GetTempPath(), "poifiles");
dir = Directory.CreateDirectory(tempDir).FullName;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateDirectory always called so shouldn't actually be dir be assigned tempdirs value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't change much code, but dir variable doesn't needed at all

}

if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);

// Generate a unique new filename
string file = Path.Combine(dir, prefix + Guid.NewGuid().ToString() + suffix);
while (File.Exists(file))
Expand All @@ -38,10 +42,15 @@ public static FileInfo CreateTempFile(String prefix, String suffix)

public static string GetTempFilePath(String prefix, String suffix)
{
if (dir == null)
if (string.IsNullOrWhiteSpace(dir))
{
dir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "poifiles")).FullName;
string tempDir = Path.Combine(Path.GetTempPath(), "poifiles");
dir = Directory.CreateDirectory(tempDir).FullName;
}

if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);

Random rnd = new Random(DateTime.Now.Millisecond);
Thread.Sleep(10);
Copy link
Collaborator Author

@Bykiev Bykiev Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of Thread.Sleep?

//return prefix + rnd.Next() + suffix;
Expand Down
53 changes: 53 additions & 0 deletions testcases/main/Util/TestTempFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using NPOI.Util;
using NUnit.Framework;
using System.IO;

namespace TestCases.Util
{
/// <summary>
/// Tests of creating temp files
/// </summary>
[TestFixture]
[NonParallelizable]
internal class TestTempFile
{
[Test]
public void TestCreateTempFile()
{
FileInfo fileInfo = null;
Assert.DoesNotThrow(() => fileInfo = TempFile.CreateTempFile("test", ".xls"));

Assert.IsTrue(File.Exists(fileInfo.FullName));

string tempDirPath = Path.GetDirectoryName(fileInfo.FullName);

Check failure on line 22 in testcases/main/Util/TestTempFile.cs

View workflow job for this annotation

GitHub Actions / windows-latest

TestCreateTempFile

System.IO.IOException : The process cannot access the file 'poi-sxssf-sheet10775931-3dc6-47f6-bfbc-ba2d638de04a.xml' because it is being used by another process.

if (Directory.Exists(tempDirPath))
Directory.Delete(tempDirPath, true);

Assert.IsFalse(File.Exists(fileInfo.FullName));
Assert.IsFalse(Directory.Exists(tempDirPath));

Assert.DoesNotThrow(() => TempFile.CreateTempFile("test2", ".xls"));
Assert.IsTrue(Directory.Exists(tempDirPath));
}

[Test]
public void TestGetTempFilePath()
{
string path = "";
Assert.DoesNotThrow(() => path = TempFile.GetTempFilePath("test", ".xls"));

Assert.IsTrue(!string.IsNullOrWhiteSpace(path));

string tempDirPath = Path.GetDirectoryName(path);

if (Directory.Exists(tempDirPath))
Directory.Delete(tempDirPath, true);

Check failure on line 45 in testcases/main/Util/TestTempFile.cs

View workflow job for this annotation

GitHub Actions / windows-latest

TestGetTempFilePath

System.IO.IOException : The process cannot access the file 'poi-sxssf-sheet10775931-3dc6-47f6-bfbc-ba2d638de04a.xml' because it is being used by another process.

Assert.IsFalse(Directory.Exists(tempDirPath));

Assert.DoesNotThrow(() => TempFile.GetTempFilePath("test", ".xls"));
Assert.IsTrue(Directory.Exists(tempDirPath));
}
}
}
1 change: 1 addition & 0 deletions testcases/ooxml/XSSF/Streaming/GZIPSheetDataWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
namespace TestCases.XSSF.Streaming
{
[TestFixture]
[NonParallelizable]
public class GZIPSheetDataWriterTests
{
private GZIPSheetDataWriter _objectToTest;
Expand Down
1 change: 1 addition & 0 deletions testcases/ooxml/XSSF/Streaming/SheetDataWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
namespace TestCases.XSSF.Streaming
{
[TestFixture]
[NonParallelizable]
public class SheetDataWriterTests
{
private SheetDataWriter _objectToTest;
Expand Down
1 change: 1 addition & 0 deletions testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace TestCases.XSSF.Streaming
using TestCases.Util;

[TestFixture]
[NonParallelizable]
public class TestSXSSFWorkbook : BaseTestXWorkbook
{

Expand Down
Loading