|
2 | 2 | using ByteSync.Common.Business.Misc; |
3 | 3 | using ByteSync.Interfaces.Controls.Inventories; |
4 | 4 | using ByteSync.Services.Inventories; |
| 5 | +using ByteSync.TestsCommon; |
5 | 6 | using FluentAssertions; |
6 | 7 | using Moq; |
7 | 8 | using NUnit.Framework; |
8 | 9 |
|
9 | 10 | namespace ByteSync.Client.UnitTests.Services.Inventories; |
10 | 11 |
|
11 | | -public class FileSystemInspectorTests |
| 12 | +public class FileSystemInspectorTests : AbstractTester |
12 | 13 | { |
| 14 | + [SetUp] |
| 15 | + public void SetUp() |
| 16 | + { |
| 17 | + CreateTestDirectory(); |
| 18 | + } |
| 19 | + |
| 20 | + [TearDown] |
| 21 | + public void TearDown() |
| 22 | + { |
| 23 | + if (TestDirectory?.Exists == true) |
| 24 | + { |
| 25 | + TestDirectory.Delete(true); |
| 26 | + } |
| 27 | + } |
| 28 | + |
13 | 29 | [Test] |
14 | 30 | public void ClassifyEntry_ReturnsDirectory_ForDirectoryInfo() |
15 | 31 | { |
16 | 32 | var posix = new Mock<IPosixFileTypeClassifier>(MockBehavior.Strict); |
17 | 33 | posix.Setup(p => p.ClassifyPosixEntry(It.IsAny<string>())).Returns(FileSystemEntryKind.Unknown); |
18 | 34 | var inspector = new FileSystemInspector(posix.Object); |
19 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
20 | | - |
21 | | - try |
22 | | - { |
23 | | - var result = inspector.ClassifyEntry(tempDirectory); |
24 | | - |
25 | | - result.Should().Be(FileSystemEntryKind.Directory); |
26 | | - } |
27 | | - finally |
28 | | - { |
29 | | - Directory.Delete(tempDirectory.FullName, true); |
30 | | - } |
| 35 | + var tempDirectory = Directory.CreateDirectory(Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N"))); |
| 36 | + |
| 37 | + var result = inspector.ClassifyEntry(tempDirectory); |
| 38 | + |
| 39 | + result.Should().Be(FileSystemEntryKind.Directory); |
31 | 40 | } |
32 | | - |
| 41 | + |
33 | 42 | [Test] |
34 | 43 | public void ClassifyEntry_ReturnsRegularFile_ForFileInfo() |
35 | 44 | { |
36 | 45 | var posix = new Mock<IPosixFileTypeClassifier>(MockBehavior.Strict); |
37 | 46 | posix.Setup(p => p.ClassifyPosixEntry(It.IsAny<string>())).Returns(FileSystemEntryKind.Unknown); |
38 | 47 | var inspector = new FileSystemInspector(posix.Object); |
39 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
| 48 | + var tempDirectory = Directory.CreateDirectory(Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N"))); |
40 | 49 | var tempFilePath = Path.Combine(tempDirectory.FullName, "file.txt"); |
41 | 50 | File.WriteAllText(tempFilePath, "x"); |
42 | 51 | var fileInfo = new FileInfo(tempFilePath); |
43 | | - |
44 | | - try |
45 | | - { |
46 | | - var result = inspector.ClassifyEntry(fileInfo); |
47 | | - |
48 | | - result.Should().Be(FileSystemEntryKind.RegularFile); |
49 | | - } |
50 | | - finally |
51 | | - { |
52 | | - Directory.Delete(tempDirectory.FullName, true); |
53 | | - } |
| 52 | + |
| 53 | + var result = inspector.ClassifyEntry(fileInfo); |
| 54 | + |
| 55 | + result.Should().Be(FileSystemEntryKind.RegularFile); |
54 | 56 | } |
55 | | - |
| 57 | + |
56 | 58 | [Test] |
57 | 59 | public void ClassifyEntry_ReturnsSymlink_WhenLinkTargetExists() |
58 | 60 | { |
59 | 61 | var posix = new Mock<IPosixFileTypeClassifier>(MockBehavior.Strict); |
60 | 62 | posix.Setup(p => p.ClassifyPosixEntry(It.IsAny<string>())).Returns(FileSystemEntryKind.Unknown); |
61 | 63 | var inspector = new FileSystemInspector(posix.Object); |
62 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
| 64 | + var tempDirectory = Directory.CreateDirectory(Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N"))); |
63 | 65 | var targetPath = Path.Combine(tempDirectory.FullName, "target.txt"); |
64 | 66 | File.WriteAllText(targetPath, "x"); |
65 | 67 | var linkPath = Path.Combine(tempDirectory.FullName, "link.txt"); |
66 | | - |
| 68 | + |
67 | 69 | try |
68 | 70 | { |
69 | | - try |
70 | | - { |
71 | | - File.CreateSymbolicLink(linkPath, targetPath); |
72 | | - } |
73 | | - catch (Exception ex) |
74 | | - { |
75 | | - Assert.Ignore($"Symbolic link creation failed: {ex.GetType().Name}"); |
76 | | - } |
77 | | - |
78 | | - var result = inspector.ClassifyEntry(new FileInfo(linkPath)); |
79 | | - |
80 | | - result.Should().Be(FileSystemEntryKind.Symlink); |
| 71 | + File.CreateSymbolicLink(linkPath, targetPath); |
81 | 72 | } |
82 | | - finally |
| 73 | + catch (Exception ex) |
83 | 74 | { |
84 | | - Directory.Delete(tempDirectory.FullName, true); |
| 75 | + Assert.Ignore($"Symbolic link creation failed: {ex.GetType().Name}"); |
85 | 76 | } |
| 77 | + |
| 78 | + var result = inspector.ClassifyEntry(new FileInfo(linkPath)); |
| 79 | + |
| 80 | + result.Should().Be(FileSystemEntryKind.Symlink); |
86 | 81 | } |
87 | | - |
| 82 | + |
88 | 83 | [Test] |
89 | 84 | [Platform(Include = "Linux,MacOsX")] |
90 | 85 | public void ClassifyEntry_ReturnsPosixSpecialKind_WhenClassifierProvidesOne() |
91 | 86 | { |
92 | 87 | var posix = new Mock<IPosixFileTypeClassifier>(MockBehavior.Strict); |
93 | 88 | posix.Setup(p => p.ClassifyPosixEntry(It.IsAny<string>())).Returns(FileSystemEntryKind.Fifo); |
94 | 89 | var inspector = new FileSystemInspector(posix.Object); |
95 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
| 90 | + var tempDirectory = Directory.CreateDirectory(Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N"))); |
96 | 91 | var tempFilePath = Path.Combine(tempDirectory.FullName, "file.txt"); |
97 | 92 | File.WriteAllText(tempFilePath, "x"); |
98 | 93 | var fileInfo = new FileInfo(tempFilePath); |
99 | | - |
100 | | - try |
101 | | - { |
102 | | - var result = inspector.ClassifyEntry(fileInfo); |
103 | | - |
104 | | - result.Should().Be(FileSystemEntryKind.Fifo); |
105 | | - } |
106 | | - finally |
107 | | - { |
108 | | - Directory.Delete(tempDirectory.FullName, true); |
109 | | - } |
| 94 | + |
| 95 | + var result = inspector.ClassifyEntry(fileInfo); |
| 96 | + |
| 97 | + result.Should().Be(FileSystemEntryKind.Fifo); |
110 | 98 | } |
111 | | - |
| 99 | + |
112 | 100 | [Test] |
113 | 101 | [Platform(Include = "Linux,MacOsX")] |
114 | 102 | public void ClassifyEntry_FallsBackToRegularFile_WhenPosixClassifierThrows() |
115 | 103 | { |
116 | 104 | var posix = new Mock<IPosixFileTypeClassifier>(MockBehavior.Strict); |
117 | 105 | posix.Setup(p => p.ClassifyPosixEntry(It.IsAny<string>())).Throws(new InvalidOperationException("boom")); |
118 | 106 | var inspector = new FileSystemInspector(posix.Object); |
119 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
| 107 | + var tempDirectory = Directory.CreateDirectory(Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N"))); |
120 | 108 | var tempFilePath = Path.Combine(tempDirectory.FullName, "file.txt"); |
121 | 109 | File.WriteAllText(tempFilePath, "x"); |
122 | 110 | var fileInfo = new FileInfo(tempFilePath); |
123 | | - |
124 | | - try |
125 | | - { |
126 | | - var result = inspector.ClassifyEntry(fileInfo); |
127 | | - |
128 | | - result.Should().Be(FileSystemEntryKind.RegularFile); |
129 | | - } |
130 | | - finally |
131 | | - { |
132 | | - Directory.Delete(tempDirectory.FullName, true); |
133 | | - } |
| 111 | + |
| 112 | + var result = inspector.ClassifyEntry(fileInfo); |
| 113 | + |
| 114 | + result.Should().Be(FileSystemEntryKind.RegularFile); |
134 | 115 | } |
135 | 116 |
|
136 | 117 | [Test] |
137 | 118 | public void IsNoiseDirectoryName_ShouldReturnTrue_ForKnownNoiseDirectory() |
138 | 119 | { |
139 | 120 | var inspector = new FileSystemInspector(); |
140 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
141 | | - var noiseDirectory = Directory.CreateDirectory(Path.Combine(tempDirectory.FullName, "$RECYCLE.BIN")); |
| 121 | + var noiseDirectory = Directory.CreateDirectory(Path.Combine(TestDirectory.FullName, "$RECYCLE.BIN")); |
142 | 122 |
|
143 | | - try |
144 | | - { |
145 | | - var result = inspector.IsNoiseDirectoryName(noiseDirectory, OSPlatforms.Windows); |
| 123 | + var result = inspector.IsNoiseDirectoryName(noiseDirectory, OSPlatforms.Windows); |
146 | 124 |
|
147 | | - result.Should().BeTrue(); |
148 | | - } |
149 | | - finally |
150 | | - { |
151 | | - Directory.Delete(tempDirectory.FullName, true); |
152 | | - } |
| 125 | + result.Should().BeTrue(); |
153 | 126 | } |
154 | 127 |
|
155 | 128 | [Test] |
156 | 129 | public void IsNoiseDirectoryName_ShouldReturnFalse_ForUnknownDirectory() |
157 | 130 | { |
158 | 131 | var inspector = new FileSystemInspector(); |
159 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
160 | | - var regularDirectory = Directory.CreateDirectory(Path.Combine(tempDirectory.FullName, "regular")); |
| 132 | + var regularDirectory = Directory.CreateDirectory(Path.Combine(TestDirectory.FullName, "regular")); |
161 | 133 |
|
162 | | - try |
163 | | - { |
164 | | - var result = inspector.IsNoiseDirectoryName(regularDirectory, OSPlatforms.Windows); |
| 134 | + var result = inspector.IsNoiseDirectoryName(regularDirectory, OSPlatforms.Windows); |
165 | 135 |
|
166 | | - result.Should().BeFalse(); |
167 | | - } |
168 | | - finally |
169 | | - { |
170 | | - Directory.Delete(tempDirectory.FullName, true); |
171 | | - } |
| 136 | + result.Should().BeFalse(); |
172 | 137 | } |
173 | 138 |
|
174 | 139 | [Test] |
175 | 140 | public void IsNoiseFileName_ShouldReturnTrue_ForKnownNoiseFile() |
176 | 141 | { |
177 | 142 | var inspector = new FileSystemInspector(); |
178 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
179 | | - var filePath = Path.Combine(tempDirectory.FullName, "thumbs.db"); |
| 143 | + var filePath = Path.Combine(TestDirectory.FullName, "thumbs.db"); |
180 | 144 | File.WriteAllText(filePath, "x"); |
181 | 145 | var fileInfo = new FileInfo(filePath); |
182 | 146 |
|
183 | | - try |
184 | | - { |
185 | | - var result = inspector.IsNoiseFileName(fileInfo, OSPlatforms.Windows); |
| 147 | + var result = inspector.IsNoiseFileName(fileInfo, OSPlatforms.Windows); |
186 | 148 |
|
187 | | - result.Should().BeTrue(); |
188 | | - } |
189 | | - finally |
190 | | - { |
191 | | - Directory.Delete(tempDirectory.FullName, true); |
192 | | - } |
| 149 | + result.Should().BeTrue(); |
193 | 150 | } |
194 | 151 |
|
195 | 152 | [Test] |
196 | 153 | public void IsNoiseFileName_ShouldReturnFalse_ForUnknownFile() |
197 | 154 | { |
198 | 155 | var inspector = new FileSystemInspector(); |
199 | | - var tempDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))); |
200 | | - var filePath = Path.Combine(tempDirectory.FullName, "regular.txt"); |
| 156 | + var filePath = Path.Combine(TestDirectory.FullName, "regular.txt"); |
201 | 157 | File.WriteAllText(filePath, "x"); |
202 | 158 | var fileInfo = new FileInfo(filePath); |
203 | 159 |
|
204 | | - try |
205 | | - { |
206 | | - var result = inspector.IsNoiseFileName(fileInfo, OSPlatforms.Windows); |
| 160 | + var result = inspector.IsNoiseFileName(fileInfo, OSPlatforms.Windows); |
207 | 161 |
|
208 | | - result.Should().BeFalse(); |
209 | | - } |
210 | | - finally |
211 | | - { |
212 | | - Directory.Delete(tempDirectory.FullName, true); |
213 | | - } |
| 162 | + result.Should().BeFalse(); |
214 | 163 | } |
215 | 164 | } |
0 commit comments