Skip to content

Commit 9222647

Browse files
pianomanjhfgreinacher
authored andcommitted
Fixes #532 throw IOException: File already exists, if the file already exists when creating a FileSystem with FileMode.CreateNew
1 parent 28e5d7d commit 9222647

5 files changed

Lines changed: 50 additions & 14 deletions

File tree

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public void MockFileStreamFactory_CreateForNonExistingFile_ShouldReturnStream(Fi
4747

4848
[Test]
4949
[TestCase(FileMode.Create)]
50-
[TestCase(FileMode.CreateNew)]
51-
public void MockFileStreamFactory_CreateForAnExistingFile_ShouldTruncateExistingFile(FileMode fileMode)
50+
public void MockFileStreamFactory_CreateForAnExistingFile_ShouldReplaceFileContents(FileMode fileMode)
5251
{
5352
var fileSystem = new MockFileSystem();
5453
string FilePath = XFS.Path("C:\\File.txt");
@@ -82,7 +81,23 @@ public void MockFileStreamFactory_CreateInNonExistingDirectory_ShouldThrowDirect
8281
var fileStreamFactory = new MockFileStreamFactory(fileSystem);
8382

8483
// Assert
85-
Assert.Throws<DirectoryNotFoundException>(() => fileStreamFactory.Create(@"C:\Test\NonExistingDirectory\some_random_file.txt", fileMode));
84+
Assert.Throws<DirectoryNotFoundException>(() => fileStreamFactory.Create(XFS.Path(@"C:\Test\NonExistingDirectory\some_random_file.txt"), fileMode));
85+
}
86+
87+
[Test]
88+
[TestCase(FileMode.CreateNew)]
89+
public void MockFileStreamFactory_CreateExistingFile_Should_Throw_IOException(FileMode fileMode)
90+
{
91+
// Arrange
92+
var fileSystem = new MockFileSystem();
93+
fileSystem.AddFile(XFS.Path(@"C:\Test\some_random_file.txt"), MockFileData.NullObject);
94+
95+
// Act
96+
var fileStreamFactory = new MockFileStreamFactory(fileSystem);
97+
98+
// Assert
99+
Assert.Throws<IOException>(() => fileStreamFactory.Create(XFS.Path(@"C:\Test\some_random_file.txt"), fileMode));
100+
86101
}
87102
}
88103
}

System.IO.Abstractions.TestingHelpers/CommonExceptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,8 @@ public static IOException ProcessCannotAccessFileInUse(string paramName = null)
6161
paramName != null
6262
? new IOException(string.Format(StringResources.Manager.GetString("PROCESS_CANNOT_ACCESS_FILE_IN_USE_WITH_FILENAME"), paramName), _fileLockHResult)
6363
: new IOException(StringResources.Manager.GetString("PROCESS_CANNOT_ACCESS_FILE_IN_USE"), _fileLockHResult);
64+
65+
public static IOException FileAlreadyExists(string paramName) =>
66+
new IOException(string.Format(StringResources.Manager.GetString("FILE_ALREADY_EXISTS"), paramName));
6467
}
6568
}

System.IO.Abstractions.TestingHelpers/MockFileStream.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,41 @@ public enum StreamType
1818
TRUNCATE
1919
}
2020

21+
public MockFileStream(
22+
IMockFileDataAccessor mockFileDataAccessor,
23+
string path,
24+
StreamType streamType,
25+
FileMode fileMode)
26+
: this(mockFileDataAccessor, path, streamType, FileOptions.None, fileMode)
27+
{
28+
}
29+
2130
public MockFileStream(
2231
IMockFileDataAccessor mockFileDataAccessor,
2332
string path,
2433
StreamType streamType)
25-
: this(mockFileDataAccessor, path, streamType, FileOptions.None)
34+
: this(mockFileDataAccessor, path, streamType, FileOptions.None, FileMode.Append)
2635
{
2736
}
2837

2938
public MockFileStream(
3039
IMockFileDataAccessor mockFileDataAccessor,
3140
string path,
3241
StreamType streamType,
33-
FileOptions options)
42+
FileOptions options,
43+
FileMode fileMode = FileMode.Append)
3444
{
3545
this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
3646
this.path = path;
3747
this.options = options;
3848

3949
if (mockFileDataAccessor.FileExists(path))
4050
{
51+
if (fileMode.Equals(FileMode.CreateNew))
52+
{
53+
throw CommonExceptions.FileAlreadyExists(path);
54+
}
55+
4156
var fileData = mockFileDataAccessor.GetFile(path);
4257
fileData.CheckFileAccess(path, streamType != StreamType.READ ? FileAccess.Write : FileAccess.Read);
4358

System.IO.Abstractions.TestingHelpers/MockFileStreamFactory.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ public MockFileStreamFactory(IMockFileDataAccessor mockFileSystem)
1212
=> this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
1313

1414
public Stream Create(string path, FileMode mode)
15-
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode));
15+
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode), mode);
1616

1717
public Stream Create(string path, FileMode mode, FileAccess access)
18-
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access));
18+
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access), mode);
1919

2020
public Stream Create(string path, FileMode mode, FileAccess access, FileShare share)
21-
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access));
21+
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access), mode);
2222

2323
public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
24-
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access));
24+
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access), mode);
2525

2626
public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
27-
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access), options);
27+
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access), options, mode);
2828

2929
public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync)
30-
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access));
30+
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode, access), mode);
3131

3232
public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity)
33-
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode), options);
33+
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode), options, mode);
3434

3535
public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options)
36-
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode), options);
36+
=> new MockFileStream(mockFileSystem, path, GetStreamType(mode), options, mode);
3737

3838
[Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")]
3939
public Stream Create(IntPtr handle, FileAccess access)

System.IO.Abstractions.TestingHelpers/Properties/Resources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,7 @@
153153
<data name="PROCESS_CANNOT_ACCESS_FILE_IN_USE_WITH_FILENAME" xml:space="preserve">
154154
<value>The process cannot access the file '{0}' because it is being used by another process.</value>
155155
</data>
156-
</root>
156+
<data name="FILE_ALREADY_EXISTS" xml:space="preserve">
157+
<value>The file '{0}' already exists.</value>
158+
</data>
159+
</root>

0 commit comments

Comments
 (0)