|
3 | 3 | // SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | 5 | using System.Text; |
| 6 | +using TruePath.SystemIo; |
6 | 7 | using RuntimeInformation = System.Runtime.InteropServices.RuntimeInformation; |
7 | 8 |
|
8 | 9 | namespace TruePath.Tests; |
@@ -43,6 +44,42 @@ public void DiskUtils_OnCaseInsensitiveFs_PassNonCanonicalPath_ReturnCanonicalPa |
43 | 44 | Assert.Equal(expected.Value, actual); |
44 | 45 | } |
45 | 46 |
|
| 47 | + [Fact] |
| 48 | + public void DiskUtils_PassNonExistentPath_KeepFileDescriptorZeroOpen() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return; |
| 52 | + |
| 53 | + var placeholderPath = Temporary.CreateTempFile(); |
| 54 | + try |
| 55 | + { |
| 56 | + // File descriptor 0 (normally the standard input) may be closed in the test process: then the next opened |
| 57 | + // file takes it. |
| 58 | + using var placeholder = IsFileDescriptorOpen(0) ? null : File.OpenHandle(placeholderPath.Value); |
| 59 | + Assert.True(IsFileDescriptorOpen(0), "File descriptor 0 should be open before the test."); |
| 60 | + var nonExistentPath = Temporary.SystemTempDirectory() / Guid.NewGuid().ToString() / "file.txt"; |
| 61 | + |
| 62 | + // Act |
| 63 | + var actual = DiskUtils.GetRealPath(nonExistentPath.Value); |
| 64 | + |
| 65 | + // Assert |
| 66 | + Assert.Equal(nonExistentPath, new AbsolutePath(actual)); |
| 67 | + Assert.True(IsFileDescriptorOpen(0), "File descriptor 0 should stay open."); |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + placeholderPath.Delete(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // ReSharper disable once InconsistentNaming |
| 76 | + private const int F_GETFD = 1; // The same on Linux and macOS. |
| 77 | + |
| 78 | + [DllImport("libc", EntryPoint = "fcntl", SetLastError = true)] |
| 79 | + private static extern int FCntl(int fd, int cmd); |
| 80 | + |
| 81 | + private static bool IsFileDescriptorOpen(int fd) => FCntl(fd, F_GETFD) != -1; |
| 82 | + |
46 | 83 | private static string InvertCase(string path) |
47 | 84 | { |
48 | 85 | var builder = new StringBuilder(); |
|
0 commit comments