|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using PG.StarWarsGame.Engine.Utilities; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace PG.StarWarsGame.Engine.FileSystem.Test.IO; |
| 8 | + |
| 9 | +public partial class PetroglyphFileSystemTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void FileExists_EmptyGameDirectory_Works() |
| 13 | + { |
| 14 | + var tempFile = Path.GetTempFileName(); |
| 15 | + try |
| 16 | + { |
| 17 | + var vsb = new ValueStringBuilder(); |
| 18 | + var exists = _pgFileSystem.FileExists(tempFile.AsSpan(), ref vsb, ReadOnlySpan<char>.Empty); |
| 19 | + Assert.True(exists); |
| 20 | + Assert.Equal(tempFile, vsb.ToString()); |
| 21 | + } |
| 22 | + finally |
| 23 | + { |
| 24 | + if (File.Exists(tempFile)) |
| 25 | + File.Delete(tempFile); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void FileExists_FileExists() |
| 31 | + { |
| 32 | + var tempFile = Path.GetTempFileName(); |
| 33 | + try |
| 34 | + { |
| 35 | + var vsb = new ValueStringBuilder(); |
| 36 | + var exists = _pgFileSystem.FileExists(tempFile.AsSpan(), ref vsb, string.Empty.AsSpan()); |
| 37 | + Assert.True(exists); |
| 38 | + Assert.Equal(tempFile, vsb.ToString()); |
| 39 | + } |
| 40 | + finally |
| 41 | + { |
| 42 | + if (File.Exists(tempFile)) |
| 43 | + File.Delete(tempFile); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void FileExists_FileDoesNotExist() |
| 49 | + { |
| 50 | + var tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 51 | + var vsb = new ValueStringBuilder(); |
| 52 | + var exists = _pgFileSystem.FileExists(tempFile.AsSpan(), ref vsb, string.Empty.AsSpan()); |
| 53 | + Assert.False(exists); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void FileExists_RelativePath() |
| 58 | + { |
| 59 | + var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 60 | + Directory.CreateDirectory(tempDir); |
| 61 | + var tempFile = Path.Combine(tempDir, "test.txt"); |
| 62 | + File.WriteAllText(tempFile, "test"); |
| 63 | + try |
| 64 | + { |
| 65 | + var vsb = new ValueStringBuilder(); |
| 66 | + var exists = _pgFileSystem.FileExists("test.txt".AsSpan(), ref vsb, tempDir.AsSpan()); |
| 67 | + Assert.True(exists); |
| 68 | + |
| 69 | + // On Windows, JoinPath might use backslashes. |
| 70 | + // PetroglyphFileSystem.JoinPath uses _underlyingFileSystem.Path.DirectorySeparatorChar if no separator is present. |
| 71 | + // Since _fileSystem is RealFileSystem, it will be \ on Windows. |
| 72 | + var expected = Path.Combine(tempDir, "test.txt"); |
| 73 | + Assert.Equal(expected, vsb.ToString()); |
| 74 | + } |
| 75 | + finally |
| 76 | + { |
| 77 | + Directory.Delete(tempDir, true); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + [Theory] |
| 82 | + [InlineData("test.txt", "TEST.txt")] |
| 83 | + [InlineData("dir/test.txt", "DIR/TEST.txt")] |
| 84 | + [InlineData("a/b/c.txt", "A/B/C.txt")] |
| 85 | + [InlineData("A/B/C.txt", "a/b/c.txt")] |
| 86 | + [InlineData("a/B/c.txt", "A/b/C.txt")] |
| 87 | + public void FileExists_CaseInsensitive(string inputPath, string actualPathOnDisk) |
| 88 | + { |
| 89 | + var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 90 | + Directory.CreateDirectory(tempDir); |
| 91 | + |
| 92 | + var fullPathOnDisk = Path.Combine(tempDir, actualPathOnDisk.Replace('/', Path.DirectorySeparatorChar)); |
| 93 | + var fullPathOnDiskDir = Path.GetDirectoryName(fullPathOnDisk); |
| 94 | + if (fullPathOnDiskDir != null) |
| 95 | + Directory.CreateDirectory(fullPathOnDiskDir); |
| 96 | + |
| 97 | + File.WriteAllText(fullPathOnDisk, "test"); |
| 98 | + |
| 99 | + try |
| 100 | + { |
| 101 | + var vsb = new ValueStringBuilder(); |
| 102 | + // On Windows, CreateFile is case-insensitive by default. |
| 103 | + // On Linux, FileExistsCaseInsensitive handles it. |
| 104 | + var exists = _pgFileSystem.FileExists(inputPath.AsSpan(), ref vsb, tempDir.AsSpan()); |
| 105 | + Assert.True(exists); |
| 106 | + |
| 107 | + var resultPath = vsb.ToString(); |
| 108 | + |
| 109 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 110 | + { |
| 111 | + // Windows CreateFile doesn't change the path in the string builder to the actual case-sensitive path if it just found it. |
| 112 | + // It stays as what was passed to it (with gameDirectory joined). |
| 113 | + var expected = _fileSystem.Path.Combine(tempDir, inputPath); |
| 114 | + Assert.Equal(expected, resultPath); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + // On Linux, FileExistsCaseInsensitive DOES update the string builder: |
| 119 | + // stringBuilder.Length = 0; |
| 120 | + // stringBuilder.Append(file); |
| 121 | + // It should be the exact path on disk. |
| 122 | + Assert.Equal(fullPathOnDisk, resultPath); |
| 123 | + } |
| 124 | + } |
| 125 | + finally |
| 126 | + { |
| 127 | + Directory.Delete(tempDir, true); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void FileExists_GameDirectory_WithTrailingSeparator() |
| 133 | + { |
| 134 | + var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) + Path.DirectorySeparatorChar; |
| 135 | + Directory.CreateDirectory(tempDir); |
| 136 | + var tempFile = Path.Combine(tempDir, "test.txt"); |
| 137 | + File.WriteAllText(tempFile, "test"); |
| 138 | + try |
| 139 | + { |
| 140 | + var vsb = new ValueStringBuilder(); |
| 141 | + var exists = _pgFileSystem.FileExists("test.txt".AsSpan(), ref vsb, tempDir.AsSpan()); |
| 142 | + Assert.True(exists); |
| 143 | + Assert.Equal(tempFile, vsb.ToString()); |
| 144 | + } |
| 145 | + finally |
| 146 | + { |
| 147 | + Directory.Delete(tempDir, true); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + [Theory] |
| 152 | +#if Windows |
| 153 | + [InlineData("C:\\test.txt", true)] |
| 154 | + [InlineData("/test.txt", false)] // On Windows, /test.txt is NOT fully qualified (it's root-relative to current drive) |
| 155 | + [InlineData("\\test.txt", false)] |
| 156 | +#else |
| 157 | + [InlineData("/test.txt", true)] |
| 158 | + [InlineData("C:\\test.txt", false)] // On Linux, C:\ is not a root |
| 159 | +#endif |
| 160 | + [InlineData("test.txt", false)] |
| 161 | + public void IsPathFullyQualified_Exists_Internal(string path, bool expected) |
| 162 | + { |
| 163 | + // This method is internal/private, but we can indirectly test it through FileExists or use reflection if we want to be explicit. |
| 164 | + // Actually, FileExists calls it. |
| 165 | + // If it's fully qualified, it doesn't join with gameDirectory. |
| 166 | + |
| 167 | + var gameDir = "Z:\\non-existent-dir"; |
| 168 | + var vsb = new ValueStringBuilder(); |
| 169 | + _pgFileSystem.FileExists(path.AsSpan(), ref vsb, gameDir.AsSpan()); |
| 170 | + |
| 171 | + var resultPath = vsb.ToString(); |
| 172 | + if (expected) |
| 173 | + { |
| 174 | + Assert.StartsWith(path, resultPath); |
| 175 | + Assert.DoesNotContain(gameDir, resultPath); |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + Assert.Contains(gameDir, resultPath); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments