-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
TempFileCollectionTests.cs
292 lines (260 loc) · 10 KB
/
TempFileCollectionTests.cs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Tests;
using System.IO;
using Xunit;
namespace System.CodeDom.Tests
{
public class TempFileCollectionTests : ICollection_NonGeneric_Tests
{
protected override ICollection NonGenericICollectionFactory() => new TempFileCollection();
protected override void AddToCollection(ICollection collection, int numberOfItemsToAdd)
{
Random random = new Random(numberOfItemsToAdd);
for (int i = 0; i < numberOfItemsToAdd; i++)
{
((TempFileCollection)collection).AddFile(random.Next().ToString(), keepFile: true);
}
}
protected override bool NullAllowed => false;
protected override bool Enumerator_Current_UndefinedOperation_Throws => true;
protected override bool ICollection_NonGeneric_HasNullSyncRoot => true;
protected override Type ICollection_NonGeneric_CopyTo_ArrayOfEnumType_ThrowType => typeof(InvalidCastException);
protected override Type ICollection_NonGeneric_CopyTo_ArrayOfIncorrectValueType_ThrowType => typeof(InvalidCastException);
protected override Type ICollection_NonGeneric_CopyTo_ArrayOfIncorrectReferenceType_ThrowType => typeof(InvalidCastException);
public override void ICollection_NonGeneric_CopyTo_NonZeroLowerBound(int count)
{
ICollection collection = NonGenericICollectionFactory(count);
Array arr = Array.CreateInstance(typeof(object), new int[1] { count }, new int[1] { 2 });
if (count == 0)
{
collection.CopyTo(arr, 0);
}
else
{
Assert.Throws<IndexOutOfRangeException>(() => collection.CopyTo(arr, 0));
}
}
[Fact]
public void Ctor_Empty()
{
var collection = new TempFileCollection();
Assert.Equal(0, collection.Count);
Assert.Empty(collection.TempDir);
Assert.False(collection.KeepFiles);
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("TempDir")]
public void Ctor_String(string tempDir)
{
var collection = new TempFileCollection(tempDir);
Assert.Equal(0, collection.Count);
Assert.Equal(tempDir ?? string.Empty, collection.TempDir);
Assert.False(collection.KeepFiles);
}
[Theory]
[InlineData(null, false)]
[InlineData("", true)]
[InlineData("TempDir", false)]
public void Ctor_String_Bool(string tempDir, bool keepFiles)
{
var collection = new TempFileCollection(tempDir, keepFiles);
Assert.Equal(0, collection.Count);
Assert.Equal(tempDir ?? string.Empty, collection.TempDir);
Assert.Equal(keepFiles, collection.KeepFiles);
}
public static IEnumerable<object[]> BasePath_TestData()
{
yield return new object[] { "NoSuchDirectory" };
yield return new object[] { TempDirectory() };
}
[Theory]
[MemberData(nameof(BasePath_TestData))]
public void BasePath_Get(string tempDir)
{
var collection = new TempFileCollection(tempDir);
if (Directory.Exists(tempDir))
{
Assert.StartsWith(tempDir, collection.BasePath);
}
else
{
Assert.Throws<DirectoryNotFoundException>(() => collection.BasePath);
Assert.StartsWith(tempDir, collection.BasePath);
}
}
[Fact]
public void AddFileExtension()
{
string tempDirectory = TempDirectory();
using (var collection = new TempFileCollection(tempDirectory))
{
string file = collection.AddExtension("txt");
Assert.False(File.Exists(file));
Assert.Equal(collection.BasePath + "." + "txt", file);
}
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void AddExtension_InvalidFileExtension_ThrowsArgumentException(string fileExtension)
{
using (var collection = new TempFileCollection())
{
AssertExtensions.Throws<ArgumentException>("fileExtension", () => collection.AddExtension(fileExtension));
AssertExtensions.Throws<ArgumentException>("fileExtension", () => collection.AddExtension(fileExtension, keepFile: false));
}
}
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void AddFile(bool fileExists, bool keepFile)
{
string directory = TempDirectory();
const string FileName = "file.extension";
string filePath = Path.Combine(directory, FileName);
if (fileExists)
{
File.Create(filePath).Dispose();
}
try
{
using (var collection = new TempFileCollection(directory))
{
// AddFile(fileName) is a misnomer, and should really be AddFile(filePath),
// as only files added with their full path are deleted.
collection.AddFile(filePath, keepFile);
Assert.Equal(fileExists, File.Exists(filePath));
}
Assert.Equal(fileExists && keepFile, File.Exists(filePath));
}
finally
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void AddFile_MultipleFiles_DeletesAllIfKeepFilesFalse(bool keepFiles)
{
string directory = TempDirectory();
string filePath1 = Path.Combine(directory, "file1.extension");
string filePath2 = Path.Combine(directory, "file2.extension");
File.Create(filePath1).Dispose();
File.Create(filePath2).Dispose();
try
{
using (var collection = new TempFileCollection(directory))
{
collection.AddFile(filePath1, keepFiles);
collection.AddFile(filePath2, keepFiles);
}
Assert.Equal(keepFiles, File.Exists(filePath1));
Assert.Equal(keepFiles, File.Exists(filePath2));
}
finally
{
if (File.Exists(filePath1))
{
File.Delete(filePath1);
}
if (File.Exists(filePath2))
{
File.Delete(filePath2);
}
}
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void AddFile_InvalidFileName_ThrowsArgumentException(string fileName)
{
using (var collection = new TempFileCollection())
{
AssertExtensions.Throws<ArgumentException>("fileName", () => collection.AddFile(fileName, keepFile: false));
}
}
[Fact]
public void AddFile_DuplicateFileName_ThrowsArgumentException()
{
using (var collection = new TempFileCollection())
{
const string FileName = "FileName";
collection.AddFile(FileName, keepFile: false);
AssertExtensions.Throws<ArgumentException>("fileName", () => collection.AddFile(FileName, keepFile: false));
// Case insensitive
AssertExtensions.Throws<ArgumentException>("fileName", () => collection.AddFile(FileName.ToLowerInvariant(), keepFile: false));
}
}
[Fact]
public void GetEnumerator_Empty_ReturnsFalse()
{
using (var collection = new TempFileCollection())
{
Assert.False(collection.GetEnumerator().MoveNext());
}
}
[Fact]
public void CopyTo()
{
using (var collection = new TempFileCollection())
{
const int ArrayIndex = 1;
const string FileName = "File";
collection.AddFile(FileName, keepFile: false);
var array = new string[ArrayIndex + collection.Count];
collection.CopyTo(array, ArrayIndex);
Assert.Null(array[0]);
Assert.Equal(FileName, array[ArrayIndex]);
}
}
[Fact]
public void Delete()
{
string directory = TempDirectory();
string filePath1 = Path.Combine(directory, "file1.extension");
File.Create(filePath1).Dispose();
try
{
using (var collection = new TempFileCollection(directory))
{
collection.AddFile(filePath1, false);
Assert.True(File.Exists(filePath1));
collection.Delete();
Assert.False(File.Exists(filePath1));
Assert.Equal(0, collection.Count);
}
}
finally
{
File.Delete(filePath1);
}
}
private static string s_tempDirectory = null;
private static string TempDirectory()
{
if (s_tempDirectory == null)
{
#if NETCOREAPP
string tempDirectory = Directory.CreateTempSubdirectory().FullName;
#else
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);
#endif
s_tempDirectory = tempDirectory;
}
return s_tempDirectory;
}
}
}