forked from TestableIO/System.IO.Abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockFileSystemEventTests.cs
More file actions
207 lines (168 loc) · 8.1 KB
/
MockFileSystemEventTests.cs
File metadata and controls
207 lines (168 loc) · 8.1 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
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
using System.Collections.Generic;
using NUnit.Framework;
namespace System.IO.Abstractions.TestingHelpers.Tests
{
[TestFixture]
public class MockFileSystemEventTests
{
[Test]
public void OnFileChanging_ThrowExceptionInCallback_ShouldThrowExceptionAndNotCreateFile()
{
var basePath = Path.GetFullPath("/foo/bar");
var fileName = "foo.txt";
var exception = new Exception("the file should not be created");
var expectedPath = Path.Combine(basePath, fileName);
var fs = new MockFileSystem(null, basePath)
.OnFileEvent(_ => throw exception);
var receivedException = Assert.Throws<Exception>(() => fs.File.WriteAllText(fileName, "some content"));
var result = fs.File.Exists(expectedPath);
Assert.That(receivedException, Is.EqualTo(exception));
Assert.That(result, Is.False);
}
[Test]
public void OnFileChanging_WithFileEvent_ShouldCallOnFileChangingWithFullFilePath()
{
var basePath = Path.GetFullPath("/foo/bar");
var fileName = "foo.txt";
var expectedPath = Path.Combine(basePath, fileName);
var calledPath = string.Empty;
var fs = new MockFileSystem(null, basePath)
.OnFileEvent(f => calledPath = f.Path);
fs.File.WriteAllText(fileName, "some content");
Assert.That(calledPath, Is.EqualTo(expectedPath));
}
[Test]
public void OnFileChanging_WithDirectoryEvent_ShouldNotBeCalled()
{
var basePath = Path.GetFullPath("/foo/bar");
var directoryName = "test-directory";
bool isCalled = false;
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ directoryName, new MockFileData("some content") }
}, basePath)
.OnFileEvent(f => isCalled = true);
_ = fs.Directory.CreateDirectory(directoryName);
Assert.That(isCalled, Is.False);
}
[Test]
public void OnDirectoryChanging_ThrowExceptionInCallback_ShouldThrowExceptionAndNotCreateDirectory()
{
var basePath = Path.GetFullPath("/foo/bar");
var directoryName = "test-directory";
var exception = new Exception("the directory should not be created");
var expectedPath = Path.Combine(basePath, directoryName);
var fs = new MockFileSystem(null, basePath)
.OnDirectoryEvent(_ => throw exception);
var receivedException = Assert.Throws<Exception>(() => fs.Directory.CreateDirectory(directoryName));
var result = fs.Directory.Exists(expectedPath);
Assert.That(receivedException, Is.EqualTo(exception));
Assert.That(result, Is.False);
}
[Test]
public void OnDirectoryChanging_WithDirectoryEvent_ShouldCallOnDirectoryChangingWithFullDirectoryPath()
{
var basePath = Path.GetFullPath("/foo/bar");
var directoryName = "test-directory";
var expectedPath = Path.Combine(basePath, directoryName);
var calledPath = string.Empty;
var fs = new MockFileSystem(null, basePath)
.OnDirectoryEvent(f => calledPath = f.Path);
fs.Directory.CreateDirectory(directoryName);
Assert.That(calledPath, Is.EqualTo(expectedPath));
}
[Test]
public void OnDirectoryChanging_WithFileEvent_ShouldNotBeCalled()
{
var basePath = Path.GetFullPath("/foo/bar");
var fileName = "test-directory";
bool isCalled = false;
var fs = new MockFileSystem(null, basePath)
.OnDirectoryEvent(f => isCalled = true);
fs.File.WriteAllText(fileName, "some content");
Assert.That(isCalled, Is.False);
}
[Test]
public void File_WriteAllText_NewFile_ShouldTriggerOnFileChangingWithCreatedType()
{
var fileName = "foo.txt";
MockFileEvent.FileEventType? receivedEventType = null;
var fs = new MockFileSystem()
.OnFileEvent(f => receivedEventType = f.EventType);
fs.File.WriteAllText(fileName, "some content");
Assert.That(receivedEventType, Is.EqualTo(MockFileEvent.FileEventType.Created));
}
[Test]
public void File_WriteAllText_ExistingFile_ShouldTriggerOnFileChangingWithUpdatedType()
{
var fileName = "foo.txt";
MockFileEvent.FileEventType? receivedEventType = null;
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ fileName, new MockFileData("some content") }
}).OnFileEvent(f => receivedEventType = f.EventType);
fs.File.WriteAllText(fileName, "some content");
Assert.That(receivedEventType, Is.EqualTo(MockFileEvent.FileEventType.Updated));
}
[Test]
public void File_Delete_ShouldTriggerOnFileChangingWithDeletedType()
{
var fileName = "foo.txt";
MockFileEvent.FileEventType? receivedEventType = null;
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ fileName, new MockFileData("some content") }
}).OnFileEvent(f => receivedEventType = f.EventType);
fs.File.Delete(fileName);
Assert.That(receivedEventType, Is.EqualTo(MockFileEvent.FileEventType.Deleted));
}
[Test]
public void File_Move_ShouldTriggerOnFileChangingWithDeletedAndCreatedTypes()
{
var fileName = "foo.txt";
var receivedEventTypes = new List<MockFileEvent.FileEventType>();
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ fileName, new MockFileData("some content") }
}).OnFileEvent(f => receivedEventTypes.Add(f.EventType));
fs.File.Move(fileName, "bar.txt");
Assert.That(receivedEventTypes, Contains.Item(MockFileEvent.FileEventType.Deleted));
Assert.That(receivedEventTypes, Contains.Item(MockFileEvent.FileEventType.Created));
}
[Test]
public void Directory_CreateDirectory_ShouldCallOnDirectoryChangingWithFullDirectoryPath()
{
var directoryName = "test-directory";
MockDirectoryEvent.DirectoryEventType? receivedEventType = null;
var fs = new MockFileSystem()
.OnDirectoryEvent(f => receivedEventType = f.EventType);
fs.Directory.CreateDirectory(directoryName);
Assert.That(receivedEventType, Is.EqualTo(MockDirectoryEvent.DirectoryEventType.Created));
}
[Test]
public void Directory_DeleteDirectory_ShouldCallOnDirectoryChangingWithFullDirectoryPath()
{
var directoryName = "test-directory";
MockDirectoryEvent.DirectoryEventType? receivedEventType = null;
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ directoryName, new MockDirectoryData() }
}).OnDirectoryEvent(f => receivedEventType = f.EventType);
fs.Directory.Delete(directoryName);
Assert.That(receivedEventType, Is.EqualTo(MockDirectoryEvent.DirectoryEventType.Deleted));
}
[Test]
public void Directory_Move_ShouldTriggerOnDirectoryChangingWithDeletedAndCreatedTypes()
{
var fileName = "foo.txt";
var receivedEventTypes = new List<MockDirectoryEvent.DirectoryEventType>();
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ fileName, new MockDirectoryData() }
}).OnDirectoryEvent(f => receivedEventTypes.Add(f.EventType));
fs.Directory.Move(fileName, "bar.txt");
Assert.That(receivedEventTypes, Contains.Item(MockDirectoryEvent.DirectoryEventType.Deleted));
Assert.That(receivedEventTypes, Contains.Item(MockDirectoryEvent.DirectoryEventType.Created));
}
}
}