Skip to content

Commit fb13e57

Browse files
Fedoranimusfgreinacher
authored andcommitted
Implement Async File Methods (#481)
Fixes #441
1 parent d7256ef commit fb13e57

14 files changed

Lines changed: 1298 additions & 8 deletions

System.IO.Abstractions.TestingHelpers.Tests/MockFileAppendAllLinesTests.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
using NUnit.Framework;
33
using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport;
44

5+
#if NETCOREAPP2_0
6+
using System.Threading.Tasks;
7+
#endif
8+
59
namespace System.IO.Abstractions.TestingHelpers.Tests
610
{
711
public class MockFileAppendAllLinesTests
@@ -118,5 +122,120 @@ public void MockFile_AppendAllLines_ShouldThrowArgumentNullExceptionIfEncodingIs
118122
var exception = Assert.Throws<ArgumentNullException>(action);
119123
Assert.That(exception.ParamName, Is.EqualTo("encoding"));
120124
}
125+
126+
#if NETCOREAPP2_0
127+
[Test]
128+
public async Task MockFile_AppendAllLinesAsync_ShouldPersistNewLinesToExistingFile()
129+
{
130+
// Arrange
131+
string path = XFS.Path(@"c:\something\demo.txt");
132+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
133+
{
134+
{ path, new MockFileData("Demo text content") }
135+
});
136+
137+
var file = new MockFile(fileSystem);
138+
139+
// Act
140+
await file.AppendAllLinesAsync(path, new[] { "line 1", "line 2", "line 3" });
141+
142+
// Assert
143+
Assert.AreEqual(
144+
"Demo text contentline 1" + Environment.NewLine + "line 2" + Environment.NewLine + "line 3" + Environment.NewLine,
145+
file.ReadAllText(path));
146+
}
147+
148+
[Test]
149+
public async Task MockFile_AppendAllLinesAsync_ShouldPersistNewLinesToNewFile()
150+
{
151+
// Arrange
152+
string path = XFS.Path(@"c:\something\demo.txt");
153+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
154+
{
155+
{ XFS.Path(@"c:\something\"), new MockDirectoryData() }
156+
});
157+
var file = new MockFile(fileSystem);
158+
159+
// Act
160+
await file.AppendAllLinesAsync(path, new[] { "line 1", "line 2", "line 3" });
161+
162+
// Assert
163+
Assert.AreEqual(
164+
"line 1" + Environment.NewLine + "line 2" + Environment.NewLine + "line 3" + Environment.NewLine,
165+
file.ReadAllText(path));
166+
}
167+
168+
[Test]
169+
public void MockFile_AppendAllLinesAsync_ShouldThrowArgumentExceptionIfPathIsZeroLength()
170+
{
171+
// Arrange
172+
var fileSystem = new MockFileSystem();
173+
174+
// Act
175+
AsyncTestDelegate action = async () => await fileSystem.File.AppendAllLinesAsync(string.Empty, new[] { "does not matter" });
176+
177+
// Assert
178+
Assert.ThrowsAsync<ArgumentException>(action);
179+
}
180+
181+
[TestCase(" ")]
182+
[TestCase(" ")]
183+
public void MockFile_AppendAllLinesAsync_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
184+
{
185+
// Arrange
186+
var fileSystem = new MockFileSystem();
187+
188+
// Act
189+
AsyncTestDelegate action = async () => await fileSystem.File.AppendAllLinesAsync(path, new[] { "does not matter" });
190+
191+
// Assert
192+
Assert.ThrowsAsync<ArgumentException>(action);
193+
}
194+
195+
[TestCase("\"")]
196+
[TestCase("<")]
197+
[TestCase(">")]
198+
[TestCase("|")]
199+
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
200+
public void MockFile_AppendAllLinesAsync_ShouldThrowArgumentExceptionIfPathContainsInvalidChar(string path)
201+
{
202+
// Arrange
203+
var fileSystem = new MockFileSystem();
204+
205+
// Act
206+
AsyncTestDelegate action = async () => await fileSystem.File.AppendAllLinesAsync(path, new[] { "does not matter" });
207+
208+
// Assert
209+
Assert.ThrowsAsync<ArgumentException>(action);
210+
}
211+
212+
[Test]
213+
public void MockFile_AppendAllLinesAsync_ShouldThrowArgumentNullExceptionIfContentIsNull()
214+
{
215+
// Arrange
216+
var fileSystem = new MockFileSystem();
217+
218+
// Act
219+
AsyncTestDelegate action = async () => await fileSystem.File.AppendAllLinesAsync("foo", null);
220+
221+
// Assert
222+
var exception = Assert.ThrowsAsync<ArgumentNullException>(action);
223+
Assert.That(exception.ParamName, Is.EqualTo("contents"));
224+
}
225+
226+
[Test]
227+
public void MockFile_AppendAllLinesAsync_ShouldThrowArgumentNullExceptionIfEncodingIsNull()
228+
{
229+
// Arrange
230+
var fileSystem = new MockFileSystem();
231+
232+
// Act
233+
AsyncTestDelegate action = async () => await fileSystem.File.AppendAllLinesAsync("foo.txt", new[] { "bar" }, null);
234+
235+
// Assert
236+
var exception = Assert.ThrowsAsync<ArgumentNullException>(action);
237+
Assert.That(exception.ParamName, Is.EqualTo("encoding"));
238+
}
239+
#endif
121240
}
122241
}

System.IO.Abstractions.TestingHelpers.Tests/MockFileAppendAllTextTests.cs

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ namespace System.IO.Abstractions.TestingHelpers.Tests
55
using Globalization;
66

77
using NUnit.Framework;
8-
98
using Text;
109

1110
using XFS = MockUnixSupport;
1211

12+
#if NETCOREAPP2_0
13+
using System.Threading.Tasks;
14+
#endif
15+
1316
public class MockFileAppendAllTextTests
1417
{
1518
[Test]
@@ -154,5 +157,150 @@ public void MockFile_AppendAllText_ShouldWorkWithRelativePath()
154157

155158
Assert.That(fileSystem.File.Exists(file));
156159
}
160+
161+
#if NETCOREAPP2_0
162+
[Test]
163+
public async Task MockFile_AppendAllTextAsync_ShouldPersistNewText()
164+
{
165+
// Arrange
166+
string path = XFS.Path(@"c:\something\demo.txt");
167+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
168+
{
169+
{path, new MockFileData("Demo text content")}
170+
});
171+
172+
var file = new MockFile(fileSystem);
173+
174+
// Act
175+
await file.AppendAllTextAsync(path, "+ some text");
176+
177+
// Assert
178+
Assert.AreEqual(
179+
"Demo text content+ some text",
180+
file.ReadAllText(path));
181+
}
182+
183+
[Test]
184+
public async Task MockFile_AppendAllTextAsync_ShouldPersistNewTextWithDifferentEncoding()
185+
{
186+
// Arrange
187+
const string Path = @"c:\something\demo.txt";
188+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
189+
{
190+
{Path, new MockFileData("AA", Encoding.UTF32)}
191+
});
192+
193+
var file = new MockFile(fileSystem);
194+
195+
// Act
196+
await file.AppendAllTextAsync(Path, "BB", Encoding.UTF8);
197+
198+
// Assert
199+
CollectionAssert.AreEqual(
200+
new byte[] { 255, 254, 0, 0, 65, 0, 0, 0, 65, 0, 0, 0, 66, 66 },
201+
fileSystem.GetFile(Path).Contents);
202+
}
203+
204+
[Test]
205+
public async Task MockFile_AppendAllTextAsync_ShouldCreateIfNotExist()
206+
{
207+
// Arrange
208+
string path = XFS.Path(@"c:\something\demo.txt");
209+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
210+
{
211+
{path, new MockFileData("Demo text content")}
212+
});
213+
214+
// Act
215+
await fileSystem.File.AppendAllTextAsync(path, " some text");
216+
217+
// Assert
218+
Assert.AreEqual(
219+
"Demo text content some text",
220+
fileSystem.File.ReadAllText(path));
221+
}
222+
223+
[Test]
224+
public async Task MockFile_AppendAllTextAsync_ShouldCreateIfNotExistWithBom()
225+
{
226+
// Arrange
227+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
228+
var path = XFS.Path(@"c:\something\demo3.txt");
229+
fileSystem.AddDirectory(XFS.Path(@"c:\something\"));
230+
231+
// Act
232+
await fileSystem.File.AppendAllTextAsync(path, "AA", Encoding.UTF32);
233+
234+
// Assert
235+
CollectionAssert.AreEqual(
236+
new byte[] { 255, 254, 0, 0, 65, 0, 0, 0, 65, 0, 0, 0 },
237+
fileSystem.GetFile(path).Contents);
238+
}
239+
240+
[Test]
241+
public void MockFile_AppendAllTextAsync_ShouldFailIfNotExistButDirectoryAlsoNotExist()
242+
{
243+
// Arrange
244+
string path = XFS.Path(@"c:\something\demo.txt");
245+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
246+
{
247+
{path, new MockFileData("Demo text content")}
248+
});
249+
250+
// Act
251+
path = XFS.Path(@"c:\something2\demo.txt");
252+
253+
// Assert
254+
Exception ex;
255+
ex = Assert.ThrowsAsync<DirectoryNotFoundException>(async () => await fileSystem.File.AppendAllTextAsync(path, "some text"));
256+
Assert.That(ex.Message,
257+
Is.EqualTo(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path)));
258+
259+
ex =
260+
Assert.ThrowsAsync<DirectoryNotFoundException>(
261+
async () => await fileSystem.File.AppendAllTextAsync(path, "some text", Encoding.Unicode));
262+
Assert.That(ex.Message,
263+
Is.EqualTo(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path)));
264+
}
265+
266+
[Test]
267+
public async Task MockFile_AppendAllTextAsync_ShouldPersistNewTextWithCustomEncoding()
268+
{
269+
// Arrange
270+
string path = XFS.Path(@"c:\something\demo.txt");
271+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
272+
{
273+
{path, new MockFileData("Demo text content")}
274+
});
275+
276+
var file = new MockFile(fileSystem);
277+
278+
// Act
279+
await file.AppendAllTextAsync(path, "+ some text", Encoding.BigEndianUnicode);
280+
281+
// Assert
282+
var expected = new byte[]
283+
{
284+
68, 101, 109, 111, 32, 116, 101, 120, 116, 32, 99, 111, 110, 116,
285+
101, 110, 116, 0, 43, 0, 32, 0, 115, 0, 111, 0, 109, 0, 101,
286+
0, 32, 0, 116, 0, 101, 0, 120, 0, 116
287+
};
288+
289+
CollectionAssert.AreEqual(
290+
expected,
291+
file.ReadAllBytes(path));
292+
}
293+
294+
[Test]
295+
public async Task MockFile_AppendAllTextAsync_ShouldWorkWithRelativePath()
296+
{
297+
var file = "file.txt";
298+
var fileSystem = new MockFileSystem();
299+
300+
await fileSystem.File.AppendAllTextAsync(file, "Foo");
301+
302+
Assert.That(fileSystem.File.Exists(file));
303+
}
304+
#endif
157305
}
158306
}

System.IO.Abstractions.TestingHelpers.Tests/MockFileReadAllBytesTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
using NUnit.Framework;
33
using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport;
44

5+
#if NETCOREAPP2_0
6+
using System.Threading.Tasks;
7+
#endif
8+
59
namespace System.IO.Abstractions.TestingHelpers.Tests
610
{
711
public class MockFileReadAllBytesTests
@@ -60,5 +64,61 @@ public void MockFile_ReadAllBytes_ShouldTolerateAltDirectorySeparatorInPath()
6064

6165
Assert.AreEqual(data, fileSystem.File.ReadAllBytes(altPath));
6266
}
67+
#if NETCOREAPP2_0
68+
[Test]
69+
public async Task MockFile_ReadAllBytesAsync_ShouldReturnOriginalByteData()
70+
{
71+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
72+
{
73+
{ XFS.Path(@"c:\something\demo.txt"), new MockFileData("Demo text content") },
74+
{ XFS.Path(@"c:\something\other.gif"), new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) }
75+
});
76+
77+
var file = new MockFile(fileSystem);
78+
79+
var result = await file.ReadAllBytesAsync(XFS.Path(@"c:\something\other.gif"));
80+
81+
CollectionAssert.AreEqual(
82+
new byte[] { 0x21, 0x58, 0x3f, 0xa9 },
83+
result);
84+
}
85+
86+
[Test]
87+
public async Task MockFile_ReadAllBytesAsync_ShouldReturnDataSavedByWriteAllBytes()
88+
{
89+
string path = XFS.Path(@"c:\something\demo.txt");
90+
var fileSystem = new MockFileSystem();
91+
var fileContent = new byte[] { 1, 2, 3, 4 };
92+
fileSystem.AddDirectory(XFS.Path(@"c:\something"));
93+
94+
fileSystem.File.WriteAllBytes(path, fileContent);
95+
96+
Assert.AreEqual(fileContent, await fileSystem.File.ReadAllBytesAsync(path));
97+
}
98+
99+
[Test]
100+
public void MockFile_ReadAllBytesAsync_ShouldThrowFileNotFoundExceptionIfFileDoesNotExist()
101+
{
102+
var fileSystem = new MockFileSystem();
103+
var file = new MockFile(fileSystem);
104+
105+
AsyncTestDelegate action = async () => await file.ReadAllBytesAsync(@"C:\a.txt");
106+
107+
Assert.ThrowsAsync<FileNotFoundException>(action);
108+
}
109+
110+
[Test]
111+
public async Task MockFile_ReadAllBytesAsync_ShouldTolerateAltDirectorySeparatorInPath()
112+
{
113+
var fileSystem = new MockFileSystem();
114+
var path = XFS.Path("C:" + fileSystem.Path.DirectorySeparatorChar + "test.dat");
115+
var altPath = XFS.Path("C:" + fileSystem.Path.AltDirectorySeparatorChar + "test.dat");
116+
var data = new byte[] { 0x12, 0x34, 0x56, 0x78 };
117+
118+
fileSystem.AddFile(path, new MockFileData(data));
119+
120+
Assert.AreEqual(data, await fileSystem.File.ReadAllBytesAsync(altPath));
121+
}
122+
#endif
63123
}
64124
}

0 commit comments

Comments
 (0)