-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all 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 8f86d2e
Update TempFile.cs
Bykiev 77fd8d8
Fix CI test execution
Bykiev 1c8a56a
Trying to fix tests2
Bykiev b293c63
trying to fix test 3
Bykiev 2aea9f9
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev 6c8fc86
Update TempFile.cs
Bykiev 1daa483
Update TempFile.cs
Bykiev 928fa26
Another one try
Bykiev e7faafb
Remove RunSerialyAndSweepTmpFilesAttribute
Bykiev 949d395
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev a35231b
Update SXSSFWorkbookTests.cs
Bykiev 99c5605
Update SXSSFWorkbookTests.cs
Bykiev 23060c1
Call dispose on SheeDataWriter.Close()
Bykiev 366130a
Comment all SXSSFWorkbookTests
Bykiev 114434f
test
Bykiev ec9bb51
Update TempFile.cs
Bykiev 874dee8
Comment TestSXSSFWorkbook
Bykiev e5d0d20
Update TestSXSSFWorkbook.cs
Bykiev f03e78d
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev 4e11700
Update TestTempFile.cs
Bykiev ede309e
Update TestTempFile.cs
Bykiev d061292
Update TestTempFile.cs
Bykiev 358e23c
Update TestTempFile.cs
Bykiev 8e5a924
Update TestXSSFWorkbook.cs
Bykiev 9f69b61
More tries
Bykiev ae31cf0
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev 9c02753
Update TestSXSSFWorkbook.cs
Bykiev dedde39
Merge remote-tracking branch 'upstream/master' into FixBug926
Bykiev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,31 +18,38 @@ 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; | ||
} | ||
|
||
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)) | ||
{ | ||
file = Path.Combine(dir, prefix + Guid.NewGuid().ToString() + suffix); | ||
Thread.Sleep(1); | ||
} | ||
FileStream newFile = new FileStream(file, FileMode.CreateNew, FileAccess.ReadWrite); | ||
newFile.Close(); | ||
|
||
using (FileStream newFile = new FileStream(file, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) { }; | ||
|
||
return new FileInfo(file); | ||
} | ||
|
||
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); | ||
rnd.Next(); | ||
Thread.Sleep(10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of Thread.Sleep? |
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using NPOI.Util; | ||
using NUnit.Framework; | ||
using System.IO; | ||
using System.Threading; | ||
|
||
namespace TestCases.Util | ||
{ | ||
/// <summary> | ||
/// Tests of creating temp files | ||
/// </summary> | ||
[TestFixture] | ||
internal class TestTempFile | ||
{ | ||
[Test] | ||
public void TestCreateTempFile() | ||
{ | ||
FileInfo fileInfo = null; | ||
Assert.DoesNotThrow(() => fileInfo = TempFile.CreateTempFile("test", ".xls")); | ||
|
||
Assert.IsTrue(fileInfo!=null && fileInfo.Exists); | ||
|
||
string tempDirPath = Path.GetDirectoryName(fileInfo.FullName); | ||
|
||
while(Directory.Exists(tempDirPath)) | ||
{ | ||
try | ||
{ | ||
Directory.Delete(tempDirPath, true); | ||
} | ||
catch | ||
{ | ||
Thread.Sleep(5); | ||
} | ||
} | ||
|
||
Assert.IsFalse(Directory.Exists(tempDirPath)); | ||
|
||
if(fileInfo!=null) | ||
{ | ||
fileInfo.Refresh(); | ||
Assert.IsFalse(fileInfo.Exists); | ||
} | ||
|
||
FileInfo file = null; | ||
Assert.DoesNotThrow(() => file = TempFile.CreateTempFile("test2", ".xls")); | ||
Assert.IsTrue(Directory.Exists(tempDirPath)); | ||
|
||
if(file !=null && file.Exists) | ||
file.Delete(); | ||
} | ||
|
||
[Test] | ||
public void TestGetTempFilePath() | ||
{ | ||
string path = ""; | ||
Assert.DoesNotThrow(() => path = TempFile.GetTempFilePath("test", ".xls")); | ||
|
||
Assert.IsTrue(!string.IsNullOrWhiteSpace(path)); | ||
|
||
string tempDirPath = Path.GetDirectoryName(path); | ||
|
||
while(Directory.Exists(tempDirPath)) | ||
{ | ||
try | ||
{ | ||
Directory.Delete(tempDirPath, true); | ||
} | ||
catch | ||
{ | ||
Thread.Sleep(10); | ||
} | ||
} | ||
|
||
Assert.IsFalse(Directory.Exists(tempDirPath)); | ||
|
||
Assert.DoesNotThrow(() => TempFile.GetTempFilePath("test", ".xls")); | ||
Assert.IsTrue(Directory.Exists(tempDirPath)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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