-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFileSystemExtensionsTests.cs
More file actions
173 lines (143 loc) · 6.05 KB
/
FileSystemExtensionsTests.cs
File metadata and controls
173 lines (143 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using NUnit.Framework;
using Assert = NUnit.Framework.Legacy.ClassicAssert;
namespace System.IO.Abstractions.Extensions.Tests
{
[TestFixture]
public class FileSystemExtensionsTests
{
private class CustomDisposableDirectory : DisposableDirectory
{
public bool DeleteFileSystemInfoWasCalled { get; private set; }
public CustomDisposableDirectory(IDirectoryInfo directoryInfo) : base(directoryInfo)
{
}
protected override void DeleteFileSystemInfo()
{
DeleteFileSystemInfoWasCalled = true;
base.DeleteFileSystemInfo();
}
}
private class CustomDisposableFile : DisposableFile
{
public bool DeleteFileSystemInfoWasCalled { get; private set; }
public CustomDisposableFile(IFileInfo fileInfo) : base(fileInfo)
{
}
protected override void DeleteFileSystemInfo()
{
DeleteFileSystemInfoWasCalled = true;
base.DeleteFileSystemInfo();
}
}
[Test]
public void CurrentDirectoryTest()
{
var fs = new FileSystem();
var fullName = fs.CurrentDirectory().FullName;
Assert.IsFalse(String.IsNullOrWhiteSpace(fullName));
NUnit.Framework.Assert.That(fullName, Is.EqualTo(Environment.CurrentDirectory));
}
[Test]
public void CreateDisposableDirectory_Temp_Path_Test()
{
// Arrange
var fs = new FileSystem();
string path;
// Act
using (_ = fs.CreateDisposableDirectory(out var dir))
{
path = dir.FullName;
Assert.IsTrue(dir.Exists, "Directory should exist");
Assert.IsTrue(
path.StartsWith(fs.Path.GetTempPath(), StringComparison.Ordinal),
"Directory should be in temp path");
}
// Assert directory is deleted
Assert.IsFalse(fs.Directory.Exists(path), "Directory should not exist");
}
[Test]
public void CreateDisposableDirectory_Already_Exists_Test()
{
// Arrange
var fs = new FileSystem();
var path = fs.Path.Combine(fs.Path.GetTempPath(), fs.Path.GetRandomFileName());
fs.Directory.CreateDirectory(path);
// Assert
var ex = Assert.Throws<ArgumentException>(() => fs.CreateDisposableDirectory(path, out _));
Assert.True(ex.Data["path"].ToString() == path, "Exception data should contain colliding path to aid with debugging");
// Delete colliding directory
fs.Directory.Delete(path);
Assert.IsFalse(fs.Directory.Exists(path), "Directory should not exist");
}
[Test]
public void CreateDisposableDirectory_Custom_IDisposable_Test()
{
// Arrange
var fs = new FileSystem();
string path = null;
// Act
CustomDisposableDirectory customDisposable;
using (customDisposable = fs.CreateDisposableDirectory(dir => new CustomDisposableDirectory(dir), out var dirInfo))
{
path = dirInfo.FullName;
Assert.IsTrue(dirInfo.Exists, "Directory should exist");
Assert.IsFalse(customDisposable.DeleteFileSystemInfoWasCalled, "Delete should not have been called yet");
}
// Assert directory is deleted
Assert.IsNotNull(path);
Assert.IsFalse(fs.Directory.Exists(path), "Directory should not exist");
Assert.IsTrue(customDisposable.DeleteFileSystemInfoWasCalled, "Custom disposable delete should have been called");
}
[Test]
public void CreateDisposableFile_Temp_Path_Test()
{
// Arrange
var fs = new FileSystem();
string path;
// Act
using (_ = fs.CreateDisposableFile(out var file))
{
path = file.FullName;
Assert.That(file.Exists, Is.True, "File should exist");
Assert.IsTrue(
path.StartsWith(fs.Path.GetTempPath(), StringComparison.Ordinal),
"File should be in temp path");
}
// Assert file is deleted
Assert.That(fs.File.Exists(path), Is.False, "File should not exist");
}
[Test]
public void CreateDisposableFile_Already_Exists_Test()
{
// Arrange
var fs = new FileSystem();
var path = fs.Path.Combine(fs.Path.GetTempPath(), fs.Path.GetRandomFileName());
fs.File.Create(path).Dispose();
// Assert
var ex = Assert.Throws<ArgumentException>(() => fs.CreateDisposableFile(path, out _));
Assert.True(ex.Data["path"].ToString() == path, "Exception data should contain colliding path to aid with debugging");
// Delete colliding file
fs.File.Delete(path);
Assert.IsFalse(fs.File.Exists(path), "File should not exist");
}
[Test]
public void CreateDisposableFile_Custom_IDisposable_Test()
{
// Arrange
var fs = new FileSystem();
string path = null;
// Act
CustomDisposableFile customDisposable;
using (customDisposable = fs.CreateDisposableFile(dir => new CustomDisposableFile(dir), out var fileInfo))
{
path = fileInfo.FullName;
Assert.IsTrue(fileInfo.Exists, "File should exist");
Assert.IsFalse(customDisposable.DeleteFileSystemInfoWasCalled, "Delete should not have been called yet");
}
// Assert file is deleted
Assert.IsNotNull(path);
Assert.IsFalse(fs.File.Exists(path), "File should not exist");
Assert.IsTrue(customDisposable.DeleteFileSystemInfoWasCalled, "Custom disposable delete should have been called");
}
}
}