Skip to content

Commit 245e045

Browse files
author
Sven Erb
committed
Updated to TempDir usage.
1 parent 465a145 commit 245e045

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

ReaderWriter/UnitTests/TempDir.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace OpenVectorFormat.ReaderWriter.UnitTests
10+
{
11+
public class TempDir : IDisposable
12+
{
13+
private string path;
14+
private bool disposedValue;
15+
16+
public TempDir()
17+
{
18+
path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
19+
Directory.CreateDirectory(path);
20+
}
21+
22+
public override string ToString() => path;
23+
24+
public static implicit operator string(TempDir tDir) => tDir.path;
25+
public static implicit operator DirectoryInfo(TempDir tDir) => new DirectoryInfo(tDir.path);
26+
27+
protected virtual void Dispose(bool disposing)
28+
{
29+
if (!disposedValue)
30+
{
31+
if (disposing)
32+
{
33+
// TODO: dispose managed state (managed objects)
34+
}
35+
36+
try
37+
{
38+
Directory.Delete(path, true);
39+
}
40+
catch
41+
{
42+
//wait shortly to give concurrent threads etc. the chance to release the files
43+
Thread.Sleep(100);
44+
try
45+
{
46+
Directory.Delete(path, true);
47+
}
48+
catch { }
49+
}
50+
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
51+
// TODO: set large fields to null
52+
disposedValue = true;
53+
}
54+
}
55+
56+
~TempDir()
57+
{
58+
Dispose(disposing: false);
59+
}
60+
61+
public void Dispose()
62+
{
63+
Dispose(disposing: true);
64+
GC.SuppressFinalize(this);
65+
}
66+
}
67+
}

ReaderWriter/UnitTests/TestAbstractVectorFileHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public void TestConvertAndCompare(FileInfo testFile)
8585

8686
foreach (string extension in FileWriterFactory.SupportedFileFormats)
8787
{
88-
FileInfo target = new FileInfo(Path.GetTempFileName() + extension);
88+
using TempDir tempDir = new TempDir();
89+
string filename = Path.Combine(tempDir, testFile.Name);
90+
FileInfo target = new FileInfo(filename + extension);
8991
Console.WriteLine("Converting from {0} to {1}", testFile.Extension, target.Extension);
9092
Debug.WriteLine("Converting from {0} to {1}", testFile.Extension, target.Extension);
9193

0 commit comments

Comments
 (0)