Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/System.IO.FileSystem/tests/FileStream/ctor_str_fm_fa_fs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using Xunit;

namespace System.IO.Tests
Expand Down Expand Up @@ -117,5 +118,26 @@ public void FileShareOpenOrCreate()
}
}
}

[Theory]
[InlineData(FileMode.Create)]
[InlineData(FileMode.Truncate)]
public void NoTruncateOnFileShareViolation(FileMode fileMode)
{
string fileName = GetTestFilePath();

using (FileStream fs = CreateFileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
fs.Write(new byte[] { 42 }, 0, 1);
fs.Flush();
FSAssert.ThrowsSharingViolation(() => CreateFileStream(fileName, fileMode, FileAccess.Write, FileShare.None).Dispose());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the Dispose shouldn't be necessary

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if it doesn't throw then we leave the file open until a GC occurs, does that not matter?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if it doesn't throw

That's true, but if it doesn't throw, we have bigger problems :) I'm ok with it either way, but in general we haven't worried about such things elsewhere.

}
using (FileStream reader = CreateFileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte[] buf = new byte[1];
Assert.Equal(1, reader.Read(buf, 0, 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: let's also assert that buf[0] == 42

Assert.Equal(42, buf[0]);
}
}
}
}