|
| 1 | +namespace System.IO.Abstractions.TestingHelpers.Tests |
| 2 | +{ |
| 3 | + using Collections.Generic; |
| 4 | + |
| 5 | + using NUnit.Framework; |
| 6 | + |
| 7 | + using XFS = MockUnixSupport; |
| 8 | + class MockFileLockTests |
| 9 | + { |
| 10 | + [Test] |
| 11 | + public void MockFile_Lock_FileShareNoneThrows() |
| 12 | + { |
| 13 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 14 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 15 | + { |
| 16 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 17 | + }); |
| 18 | + |
| 19 | + Assert.Throws(typeof(IOException), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)); |
| 20 | + } |
| 21 | + [Test] |
| 22 | + public void MockFile_Lock_FileShareReadDoesNotThrowOnRead() |
| 23 | + { |
| 24 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 25 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 26 | + { |
| 27 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Read }} |
| 28 | + }); |
| 29 | + |
| 30 | + Assert.DoesNotThrow(() => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)); |
| 31 | + } |
| 32 | + [Test] |
| 33 | + public void MockFile_Lock_FileShareReadThrowsOnWrite() |
| 34 | + { |
| 35 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 36 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 37 | + { |
| 38 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Read }} |
| 39 | + }); |
| 40 | + |
| 41 | + Assert.Throws(typeof(IOException), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Write, FileShare.Read)); |
| 42 | + } |
| 43 | + [Test] |
| 44 | + public void MockFile_Lock_FileShareWriteThrowsOnRead() |
| 45 | + { |
| 46 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 47 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 48 | + { |
| 49 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Write }} |
| 50 | + }); |
| 51 | + |
| 52 | + Assert.Throws(typeof(IOException), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)); |
| 53 | + } |
| 54 | + [Test] |
| 55 | + public void MockFile_Lock_FileShareWriteDoesNotThrowOnWrite() |
| 56 | + { |
| 57 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 58 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 59 | + { |
| 60 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Write }} |
| 61 | + }); |
| 62 | + |
| 63 | + Assert.DoesNotThrow(() => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Write, FileShare.Read)); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + [Test] |
| 68 | + public void MockFile_Lock_FileShareNoneThrowsOnOpenRead() |
| 69 | + { |
| 70 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 71 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 72 | + { |
| 73 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 74 | + }); |
| 75 | + |
| 76 | + var exception = Assert.Throws(typeof(IOException), () => filesystem.File.OpenRead(filepath)); |
| 77 | + Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); |
| 78 | + } |
| 79 | + [Test] |
| 80 | + public void MockFile_Lock_FileShareNoneThrowsOnWriteAllLines() |
| 81 | + { |
| 82 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 83 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 84 | + { |
| 85 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 86 | + }); |
| 87 | + |
| 88 | + var exception = Assert.Throws(typeof(IOException), () => filesystem.File.WriteAllLines(filepath, new string[] { "hello", "world" })); |
| 89 | + Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); |
| 90 | + } |
| 91 | + [Test] |
| 92 | + public void MockFile_Lock_FileShareNoneThrowsOnReadAllLines() |
| 93 | + { |
| 94 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 95 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 96 | + { |
| 97 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 98 | + }); |
| 99 | + |
| 100 | + var exception = Assert.Throws(typeof(IOException), () => filesystem.File.ReadAllLines(filepath)); |
| 101 | + Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); |
| 102 | + } |
| 103 | + [Test] |
| 104 | + public void MockFile_Lock_FileShareNoneThrowsOnReadAllText() |
| 105 | + { |
| 106 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 107 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 108 | + { |
| 109 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 110 | + }); |
| 111 | + |
| 112 | + var exception = Assert.Throws(typeof(IOException), () => filesystem.File.ReadAllText(filepath)); |
| 113 | + Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); |
| 114 | + } |
| 115 | + [Test] |
| 116 | + public void MockFile_Lock_FileShareNoneThrowsOnReadAllBytes() |
| 117 | + { |
| 118 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 119 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 120 | + { |
| 121 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 122 | + }); |
| 123 | + |
| 124 | + var exception = Assert.Throws(typeof(IOException), () => filesystem.File.ReadAllBytes(filepath)); |
| 125 | + Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); |
| 126 | + } |
| 127 | + [Test] |
| 128 | + public void MockFile_Lock_FileShareNoneThrowsOnAppendLines() |
| 129 | + { |
| 130 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 131 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 132 | + { |
| 133 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 134 | + }); |
| 135 | + |
| 136 | + var exception = Assert.Throws(typeof(IOException), () => filesystem.File.AppendAllLines(filepath, new string[] { "hello", "world" })); |
| 137 | + Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); |
| 138 | + } |
| 139 | + |
| 140 | + [Test] |
| 141 | + public void MockFile_Lock_FileShareNoneThrowsFileMove() |
| 142 | + { |
| 143 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 144 | + string target = XFS.Path(@"c:\something\does\notexist.txt"); |
| 145 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 146 | + { |
| 147 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 148 | + }); |
| 149 | + |
| 150 | + var exception = Assert.Throws(typeof(IOException), () => filesystem.File.Move(filepath, target)); |
| 151 | + Assert.That(exception.Message, Is.EqualTo("The process cannot access the file because it is being used by another process.")); |
| 152 | + } |
| 153 | + [Test] |
| 154 | + public void MockFile_Lock_FileShareDeleteDoesNotThrowFileMove() |
| 155 | + { |
| 156 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 157 | + string target = XFS.Path(@"c:\something\does\notexist.txt"); |
| 158 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 159 | + { |
| 160 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Delete }} |
| 161 | + }); |
| 162 | + |
| 163 | + Assert.DoesNotThrow(() => filesystem.File.Move(filepath, target)); |
| 164 | + } |
| 165 | + [Test] |
| 166 | + public void MockFile_Lock_FileShareNoneThrowsDelete() |
| 167 | + { |
| 168 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 169 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 170 | + { |
| 171 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} |
| 172 | + }); |
| 173 | + |
| 174 | + var exception = Assert.Throws(typeof(IOException), () => filesystem.File.Delete(filepath)); |
| 175 | + Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); |
| 176 | + } |
| 177 | + [Test] |
| 178 | + public void MockFile_Lock_FileShareDeleteDoesNotThrowDelete() |
| 179 | + { |
| 180 | + string filepath = XFS.Path(@"c:\something\does\exist.txt"); |
| 181 | + var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 182 | + { |
| 183 | + { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Delete }} |
| 184 | + }); |
| 185 | + |
| 186 | + Assert.DoesNotThrow(() => filesystem.File.Delete(filepath)); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments