-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ReadWriteAllLines.cs
370 lines (317 loc) · 12.1 KB
/
ReadWriteAllLines.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
namespace System.IO.Tests
{
public class File_ReadWriteAllLines_Enumerable : FileSystemTest
{
#region Utilities
protected virtual bool IsAppend { get; }
protected virtual void Write(string path, string[] content)
{
File.WriteAllLines(path, (IEnumerable<string>)content);
}
protected virtual string[] Read(string path)
{
return File.ReadAllLines(path);
}
#endregion
#region UniversalTests
[Fact]
public void InvalidPath()
{
Assert.Throws<ArgumentNullException>(() => Write(null, new string[] { "Text" }));
Assert.Throws<ArgumentException>(() => Write(string.Empty, new string[] { "Text" }));
Assert.Throws<ArgumentNullException>(() => Read(null));
Assert.Throws<ArgumentException>(() => Read(string.Empty));
}
[Fact]
public void NullLines()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => Write(path, null));
Write(path, new string[] { null });
Assert.Equal(new string[] {""}, Read(path));
}
[Fact]
public void EmptyStringCreatesFile()
{
string path = GetTestFilePath();
Write(path, new string[] { });
Assert.True(File.Exists(path));
Assert.Empty(Read(path));
}
[Theory]
[InlineData(0)]
[InlineData(100)]
public void ValidWrite(int size)
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', size) };
File.Create(path).Dispose();
Write(path, lines);
Assert.Equal(lines, Read(path));
}
[Theory]
[InlineData(200, 100)]
[InlineData(50_000, 40_000)] // tests a different code path than the line above
public void AppendOrOverwrite(int linesSizeLength, int overwriteLinesLength)
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', linesSizeLength) };
string[] overwriteLines = new string[] { new string('b', overwriteLinesLength) };
Write(path, lines);
Write(path, overwriteLines);
if (IsAppend)
{
Assert.Equal(new string[] { lines[0], overwriteLines[0] }, Read(path));
}
else
{
Assert.DoesNotContain("Append", GetType().Name); // ensure that all "Append" types override this property
Assert.Equal(overwriteLines, Read(path));
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsFileLockingEnabled))]
public void OpenFile_ThrowsIOException()
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', 200) };
using (File.Create(path))
{
Assert.Throws<IOException>(() => Write(path, lines));
Assert.Throws<IOException>(() => Read(path));
}
}
[Fact]
public void Read_FileNotFound()
{
string path = GetTestFilePath();
Assert.Throws<FileNotFoundException>(() => Read(path));
}
/// <summary>
/// On Unix, modifying a file that is ReadOnly will fail under normal permissions.
/// If the test is being run under the superuser, however, modification of a ReadOnly
/// file is allowed. On Windows, modifying a file that is ReadOnly will always fail.
/// </summary>
[Fact]
public void WriteToReadOnlyFile()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
File.SetAttributes(path, FileAttributes.ReadOnly);
try
{
if (PlatformDetection.IsNotWindows && PlatformDetection.IsPrivilegedProcess)
{
Write(path, new string[] { "text" });
Assert.Equal(new string[] { "text" }, Read(path));
}
else
Assert.Throws<UnauthorizedAccessException>(() => Write(path, new string[] { "text" }));
}
finally
{
File.SetAttributes(path, FileAttributes.Normal);
}
}
[Fact]
public void DisposingEnumeratorClosesFile()
{
string path = GetTestFilePath();
Write(path, new[] { "line1", "line2", "line3" });
IEnumerable<string> readLines = File.ReadLines(path);
using (IEnumerator<string> e1 = readLines.GetEnumerator())
using (IEnumerator<string> e2 = readLines.GetEnumerator())
{
Assert.Same(readLines, e1);
Assert.NotSame(e1, e2);
}
// File should be closed deterministically; this shouldn't throw.
File.OpenWrite(path).Dispose();
}
#endregion
}
public class File_ReadWriteAllLines_Enumerable_Encoded : File_ReadWriteAllLines_Enumerable
{
protected override void Write(string path, string[] content)
{
File.WriteAllLines(path, (IEnumerable<string>)content, new UTF8Encoding(false));
}
protected override string[] Read(string path)
{
return File.ReadAllLines(path, new UTF8Encoding(false));
}
[Fact]
public void NullEncoding()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => File.WriteAllLines(path, (IEnumerable<string>)new string[] { "Text" }, null));
Assert.Throws<ArgumentNullException>(() => File.ReadAllLines(path, null));
}
}
public class File_ReadLines : File_ReadWriteAllLines_Enumerable
{
protected override string[] Read(string path)
{
return File.ReadLines(path).ToArray();
}
}
public class File_ReadLines_Encoded : File_ReadWriteAllLines_Enumerable
{
protected override void Write(string path, string[] content)
{
File.WriteAllLines(path, (IEnumerable<string>)content, new UTF8Encoding(false));
}
protected override string[] Read(string path)
{
return File.ReadLines(path, new UTF8Encoding(false)).ToArray();
}
[Fact]
public void NullEncoding()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => File.WriteAllLines(path, (IEnumerable<string>)new string[] { "Text" }, null));
Assert.Throws<ArgumentNullException>(() => File.ReadLines(path, null));
}
}
public class File_ReadWriteAllLines_StringArray : FileSystemTest
{
#region Utilities
protected virtual void Write(string path, string[] content)
{
File.WriteAllLines(path, content);
}
protected virtual string[] Read(string path)
{
return File.ReadAllLines(path);
}
#endregion
#region UniversalTests
[Fact]
public void InvalidPath()
{
Assert.Throws<ArgumentNullException>(() => Write(null, new string[] { "Text" }));
Assert.Throws<ArgumentException>(() => Write(string.Empty, new string[] { "Text" }));
Assert.Throws<ArgumentNullException>(() => Read(null));
Assert.Throws<ArgumentException>(() => Read(string.Empty));
}
[Fact]
public void NullLines()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => Write(path, null));
Write(path, new string[] { null });
Assert.Equal(new string[] {""}, Read(path));
}
[Fact]
public void EmptyStringCreatesFile()
{
string path = GetTestFilePath();
Write(path, new string[] { });
Assert.True(File.Exists(path));
Assert.Empty(Read(path));
}
[Theory]
[InlineData(0)]
[InlineData(100)]
public void ValidWrite(int size)
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', size) };
File.Create(path).Dispose();
Write(path, lines);
Assert.Equal(lines, Read(path));
}
[Fact]
public virtual void Overwrite()
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', 200) };
string[] overwriteLines = new string[] { new string('b', 100) };
Write(path, lines);
Write(path, overwriteLines);
Assert.Equal(overwriteLines, Read(path));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsFileLockingEnabled))]
public void OpenFile_ThrowsIOException()
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', 200) };
using (File.Create(path))
{
Assert.Throws<IOException>(() => Write(path, lines));
Assert.Throws<IOException>(() => Read(path));
}
}
[Fact]
public void Read_FileNotFound()
{
string path = GetTestFilePath();
Assert.Throws<FileNotFoundException>(() => Read(path));
}
/// <summary>
/// On Unix, modifying a file that is ReadOnly will fail under normal permissions.
/// If the test is being run under the superuser, however, modification of a ReadOnly
/// file is allowed. On Windows, modifying a file that is ReadOnly will always fail.
/// </summary>
[Fact]
public void WriteToReadOnlyFile()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
File.SetAttributes(path, FileAttributes.ReadOnly);
try
{
if (PlatformDetection.IsNotWindows && PlatformDetection.IsPrivilegedProcess)
{
Write(path, new string[] { "text" });
Assert.Equal(new string[] { "text" }, Read(path));
}
else
Assert.Throws<UnauthorizedAccessException>(() => Write(path, new string[] { "text" }));
}
finally
{
File.SetAttributes(path, FileAttributes.Normal);
}
}
[Fact]
public void DisposingEnumeratorClosesFile()
{
string path = GetTestFilePath();
Write(path, new[] { "line1", "line2", "line3" });
IEnumerable<string> readLines = File.ReadLines(path);
using (IEnumerator<string> e1 = readLines.GetEnumerator())
using (IEnumerator<string> e2 = readLines.GetEnumerator())
{
Assert.Same(readLines, e1);
Assert.NotSame(e1, e2);
}
// File should be closed deterministically; this shouldn't throw.
File.OpenWrite(path).Dispose();
}
#endregion
}
public class File_ReadWriteAllLines_StringArray_Encoded : File_ReadWriteAllLines_StringArray
{
protected override void Write(string path, string[] content)
{
File.WriteAllLines(path, content, new UTF8Encoding(false));
}
protected override string[] Read(string path)
{
return File.ReadAllLines(path, new UTF8Encoding(false));
}
[Fact]
public void NullEncoding()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => File.WriteAllLines(path, new string[] { "Text" }, null));
Assert.Throws<ArgumentNullException>(() => File.ReadAllLines(path, null));
}
}
}