From dfe156a57576bd95253e6fdfe78685543f1cba10 Mon Sep 17 00:00:00 2001 From: erri120 Date: Mon, 4 Nov 2024 18:52:12 +0100 Subject: [PATCH 1/3] Better throw helper and switch/matches --- .../FileSystemAbstraction/BaseFileSystem.cs | 2 +- src/NexusMods.Paths/IOSInformation.cs | 122 ++++++++---------- .../OSInformationTests.cs | 21 --- 3 files changed, 58 insertions(+), 87 deletions(-) diff --git a/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs b/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs index ed9d602..b1442a6 100644 --- a/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs +++ b/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs @@ -202,7 +202,7 @@ public virtual AbsolutePath GetKnownPath(KnownPath knownPath) // ReSharper disable once InconsistentNaming AbsolutePath GetXDGBaseDirectory(string environmentVariable, Func defaultFunc) { - if (!OS.IsLinux) throw OS.CreatePlatformNotSupportedException(); + if (!OS.IsLinux) OS.ThrowUnsupported(); var value = Environment.GetEnvironmentVariable(environmentVariable, EnvironmentVariableTarget.Process); return value is null ? defaultFunc(this) : FromUnsanitizedFullPath(value); diff --git a/src/NexusMods.Paths/IOSInformation.cs b/src/NexusMods.Paths/IOSInformation.cs index ba9d399..c8f5e3e 100644 --- a/src/NexusMods.Paths/IOSInformation.cs +++ b/src/NexusMods.Paths/IOSInformation.cs @@ -52,10 +52,15 @@ TOut MatchPlatform( Func? onLinux = null, Func? onOSX = null) { - if (IsWindows) return onWindows is null ? throw CreatePlatformNotSupportedException() : onWindows(); - if (IsLinux) return onLinux is null ? throw CreatePlatformNotSupportedException() : onLinux(); - if (IsOSX) return onOSX is null ? throw CreatePlatformNotSupportedException() : onOSX(); - throw CreatePlatformNotSupportedException(); + Func? func = null; + + if (IsWindows) func = onWindows; + else if (IsLinux) func = onLinux; + else if (IsOSX) func = onOSX; + else ThrowUnsupported(); + + if (func is null) ThrowUnsupported(); + return func(); } /// @@ -80,10 +85,15 @@ TOut MatchPlatform( FuncRef? onLinux = null, FuncRef? onOSX = null) { - if (IsWindows) return onWindows is null ? throw CreatePlatformNotSupportedException() : onWindows(ref state); - if (IsLinux) return onLinux is null ? throw CreatePlatformNotSupportedException() : onLinux(ref state); - if (IsOSX) return onOSX is null ? throw CreatePlatformNotSupportedException() : onOSX(ref state); - throw CreatePlatformNotSupportedException(); + FuncRef? func = null; + + if (IsWindows) func = onWindows; + else if (IsLinux) func = onLinux; + else if (IsOSX) func = onOSX; + else ThrowUnsupported(); + + if (func is null) ThrowUnsupported(); + return func(ref state); } /// @@ -102,39 +112,20 @@ void SwitchPlatform( Action? onLinux = null, Action? onOSX = null) { - if (IsWindows) - { - if (onWindows is null) - throw CreatePlatformNotSupportedException(); - - onWindows(); - return; - } - - if (IsLinux) - { - if (onLinux is null) - throw CreatePlatformNotSupportedException(); - - onLinux(); - return; - } - - if (IsOSX) - { - if (onOSX is null) - throw CreatePlatformNotSupportedException(); - - onOSX(); - return; - } - - throw CreatePlatformNotSupportedException(); + Action? action = null; + + if (IsWindows) action = onWindows; + else if (IsLinux) action = onLinux; + else if (IsOSX) action = onOSX; + else ThrowUnsupported(); + + if (action is null) ThrowUnsupported(); + action(); } /// /// Switches on the current platform and allows to be - /// passed to the each handler, preventing lambda allocations. + /// passed to each handler, preventing lambda allocations. /// /// /// @@ -152,34 +143,15 @@ void SwitchPlatform( ActionRef? onLinux = null, ActionRef? onOSX = null) { - if (IsWindows) - { - if (onWindows is null) - throw CreatePlatformNotSupportedException(); - - onWindows(ref state); - return; - } - - if (IsLinux) - { - if (onLinux is null) - throw CreatePlatformNotSupportedException(); - - onLinux(ref state); - return; - } - - if (IsOSX) - { - if (onOSX is null) - throw CreatePlatformNotSupportedException(); - - onOSX(ref state); - return; - } - - throw CreatePlatformNotSupportedException(); + ActionRef? action = null; + + if (IsWindows) action = onWindows; + else if (IsLinux) action = onLinux; + else if (IsOSX) action = onOSX; + else ThrowUnsupported(); + + if (action is null) ThrowUnsupported(); + action(ref state); } /// @@ -197,10 +169,29 @@ bool IsPlatformSupported() /// bool IsUnix() => IsLinux || IsOSX; + /// + /// Throws for the current platform. + /// + /// + [DoesNotReturn] + void ThrowUnsupported() => throw new PlatformNotSupportedException($"The operation or feature isn't unsupported on the current platform `{Platform}`"); + + /// + /// Throws for the current platform. + /// + /// + [DoesNotReturn] + T ThrowUnsupported() + { + ThrowUnsupported(); + return default; + } + /// /// Guard statement for platform support. /// /// Thrown when the current platform is not supported. + [Obsolete(message: $"Use {nameof(ThrowUnsupported)} instead")] void PlatformSupportedGuard() { if (!IsPlatformSupported()) @@ -211,6 +202,7 @@ void PlatformSupportedGuard() /// Creates a new . /// /// + [Obsolete(message: $"Use {nameof(ThrowUnsupported)} instead")] PlatformNotSupportedException CreatePlatformNotSupportedException() { return new PlatformNotSupportedException($"The current platform is not supported: {Platform}"); diff --git a/tests/NexusMods.Paths.Tests/OSInformationTests.cs b/tests/NexusMods.Paths.Tests/OSInformationTests.cs index 5b365cb..d34ca0e 100644 --- a/tests/NexusMods.Paths.Tests/OSInformationTests.cs +++ b/tests/NexusMods.Paths.Tests/OSInformationTests.cs @@ -6,27 +6,6 @@ namespace NexusMods.Paths.Tests; // ReSharper disable once InconsistentNaming public class OSInformationTests { - [Theory] - [MemberData(nameof(PlatformsMemberData))] - public void Test_IsPlatformSupported_True(OSPlatform platform) - { - IOSInformation info = new OSInformation(platform); - info.IsPlatformSupported().Should().BeTrue(); - - info.Invoking(x => x.PlatformSupportedGuard()) - .Should().NotThrow(); - } - - [Theory, AutoData] - public void Test_IsPlatformSupported_False(string platformName) - { - IOSInformation info = new OSInformation(OSPlatform.Create(platformName)); - info.IsPlatformSupported().Should().BeFalse(); - - info.Invoking(x => x.PlatformSupportedGuard()) - .Should().ThrowExactly(); - } - [Theory] [MemberData(nameof(PlatformsMemberData))] public void Test_IsWindows(OSPlatform platform) From 70fbf657e1f8ae00ff6d5d8a5ddc49769ab369b8 Mon Sep 17 00:00:00 2001 From: halgari Date: Mon, 11 Nov 2024 15:46:50 -0700 Subject: [PATCH 2/3] Implement `/` as an operator alias for `Join` and `Combine`, switch useages of these over to this new operator --- .../Extensions/NxUnpackerBuilderExtensions.cs | 2 +- .../Benchmarks/Paths.cs | 4 +- src/NexusMods.Paths/AbsolutePath.cs | 6 +++ .../FileSystemAbstraction/BaseFileSystem.cs | 30 +++++------ src/NexusMods.Paths/RelativePath.cs | 5 ++ src/NexusMods.Paths/TemporaryFileManager.cs | 4 +- .../Extensions/NxBuilderExtensionsTests.cs | 14 ++--- .../FromAbsolutePathProviderTests.cs | 2 +- .../OutputAbsolutePathProviderTests.cs | 2 +- .../FileSystem/BaseFileSystemTests.cs | 8 +-- .../FileSystem/FileSystemTests.cs | 18 +++---- .../FileSystem/InMemoryFileSystemTests.cs | 54 +++++++++---------- .../RelativePathTests.cs | 4 +- .../TemporaryPathTests.cs | 6 +-- 14 files changed, 85 insertions(+), 74 deletions(-) diff --git a/src/Extensions/NexusMods.Paths.Extensions.Nx/Extensions/NxUnpackerBuilderExtensions.cs b/src/Extensions/NexusMods.Paths.Extensions.Nx/Extensions/NxUnpackerBuilderExtensions.cs index 4849278..1d13526 100644 --- a/src/Extensions/NexusMods.Paths.Extensions.Nx/Extensions/NxUnpackerBuilderExtensions.cs +++ b/src/Extensions/NexusMods.Paths.Extensions.Nx/Extensions/NxUnpackerBuilderExtensions.cs @@ -37,7 +37,7 @@ public static NxUnpackerBuilder AddFilesWithFileSystemOutput(this NxUnpackerBuil for (var x = 0; x < entries.Length; x++) { var entry = entries[x]; - var outputPath = outputDirectory.Combine(entry.FilePath); + var outputPath = outputDirectory / entry.FilePath; outputProviders[x] = new OutputAbsolutePathProvider(outputPath, entry.FilePath, entry.Entry); } builder.Outputs.AddRange(outputProviders); diff --git a/src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs b/src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs index 7d29449..5f55c2e 100644 --- a/src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs +++ b/src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs @@ -92,7 +92,7 @@ public AbsolutePath NexusJoinSmall() { // Not a fair test here since one is a string concat; other includes // normalization to OS path. - return CurrentPath.AbsolutePath.Combine("foo"); + return CurrentPath.AbsolutePath / "foo"; } [Benchmark] @@ -104,7 +104,7 @@ public string SystemJoinLarge() [Benchmark] public AbsolutePath NexusJoinLarge() { - return CurrentPath.AbsolutePath.Combine("foo/bar/baz/quz/qax"); + return CurrentPath.AbsolutePath/ "foo/bar/baz/quz/qax"; } [Benchmark] diff --git a/src/NexusMods.Paths/AbsolutePath.cs b/src/NexusMods.Paths/AbsolutePath.cs index 96e0cc5..aabbf53 100644 --- a/src/NexusMods.Paths/AbsolutePath.cs +++ b/src/NexusMods.Paths/AbsolutePath.cs @@ -240,6 +240,12 @@ public AbsolutePath Combine(RelativePath path) var res = PathHelpers.JoinParts(GetFullPath(), path.Path, FileSystem.OS); return FromSanitizedFullPath(res, FileSystem); } + + /// + /// Combines the current path with a relative path. + /// + public static AbsolutePath operator / (AbsolutePath path, RelativePath relativePath) + => path.Combine(relativePath); /// [Obsolete(message: "This will be removed once dependents have updated.", error: true)] diff --git a/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs b/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs index b1442a6..18f59bd 100644 --- a/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs +++ b/src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs @@ -72,7 +72,7 @@ internal AbsolutePath GetMappedPath(AbsolutePath originalPath) if (newParentDirectory == default) return originalPath; var relativePath = originalPath.RelativeTo(originalParentDirectory); - var newPath = newParentDirectory.Combine(relativePath); + var newPath = newParentDirectory / relativePath; return newPath; } @@ -189,10 +189,10 @@ public virtual AbsolutePath GetKnownPath(KnownPath knownPath) KnownPath.MyDocumentsDirectory => FromUnsanitizedFullPath(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)), KnownPath.MyGamesDirectory => FromUnsanitizedDirectoryAndFileName(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games"), - KnownPath.XDG_CONFIG_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_CONFIG_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory).Combine(".config")), - KnownPath.XDG_CACHE_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_CACHE_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory).Combine(".cache")), - KnownPath.XDG_DATA_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_DATA_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory).Combine(".local").Combine("share")), - KnownPath.XDG_STATE_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_STATE_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory).Combine(".local").Combine("state")), + KnownPath.XDG_CONFIG_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_CONFIG_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory) / ".config"), + KnownPath.XDG_CACHE_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_CACHE_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory) / ".cache"), + KnownPath.XDG_DATA_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_DATA_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory) / ".local" / "share"), + KnownPath.XDG_STATE_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_STATE_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory) / ".local" / "state"), KnownPath.XDG_RUNTIME_DIR => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_RUNTIME_DIR)}", static fs => fs.GetKnownPath(KnownPath.TempDirectory)), }; @@ -244,18 +244,18 @@ public static (Dictionary pathMappings, Dictionary fileSystem.GetKnownPath(knownPath), KnownPath.CurrentDirectory => fileSystem.GetKnownPath(knownPath), - KnownPath.CommonApplicationDataDirectory => rootDirectory.Combine("ProgramData"), - KnownPath.ProgramFilesDirectory => rootDirectory.Combine("Program Files"), - KnownPath.ProgramFilesX86Directory => rootDirectory.Combine("Program Files (x86)"), - KnownPath.CommonProgramFilesDirectory => rootDirectory.Combine("Program Files/Common Files"), - KnownPath.CommonProgramFilesX86Directory => rootDirectory.Combine("Program Files (x86)/Common Files"), + KnownPath.CommonApplicationDataDirectory => rootDirectory / "ProgramData", + KnownPath.ProgramFilesDirectory => rootDirectory / "Program Files", + KnownPath.ProgramFilesX86Directory => rootDirectory / "Program Files (x86)", + KnownPath.CommonProgramFilesDirectory => rootDirectory / "Program Files/Common Files", + KnownPath.CommonProgramFilesX86Directory => rootDirectory / "Program Files (x86)/Common Files", KnownPath.HomeDirectory => newHomeDirectory, - KnownPath.MyDocumentsDirectory => newHomeDirectory.Combine("Documents"), - KnownPath.MyGamesDirectory => newHomeDirectory.Combine("Documents/My Games"), - KnownPath.LocalApplicationDataDirectory => newHomeDirectory.Combine("AppData/Local"), - KnownPath.ApplicationDataDirectory => newHomeDirectory.Combine("AppData/Roaming"), - KnownPath.TempDirectory => newHomeDirectory.Combine("AppData/Local/Temp"), + KnownPath.MyDocumentsDirectory => newHomeDirectory / "Documents", + KnownPath.MyGamesDirectory => newHomeDirectory / "Documents/My Games", + KnownPath.LocalApplicationDataDirectory => newHomeDirectory / "AppData/Local", + KnownPath.ApplicationDataDirectory => newHomeDirectory / "AppData/Roaming", + KnownPath.TempDirectory => newHomeDirectory / "AppData/Local/Temp", _ => default }; diff --git a/src/NexusMods.Paths/RelativePath.cs b/src/NexusMods.Paths/RelativePath.cs index 7779e17..fc3c87e 100644 --- a/src/NexusMods.Paths/RelativePath.cs +++ b/src/NexusMods.Paths/RelativePath.cs @@ -164,6 +164,11 @@ public RelativePath Join(RelativePath other) { return new RelativePath(PathHelpers.JoinParts(Path, other.Path, OS)); } + + /// + /// Appends another path to an existing path. + /// + public static RelativePath operator /(RelativePath self, RelativePath other) => self.Join(other); /// /// Returns true if the relative path starts with a given string. diff --git a/src/NexusMods.Paths/TemporaryFileManager.cs b/src/NexusMods.Paths/TemporaryFileManager.cs index 8b7c279..8ee7d99 100644 --- a/src/NexusMods.Paths/TemporaryFileManager.cs +++ b/src/NexusMods.Paths/TemporaryFileManager.cs @@ -43,7 +43,7 @@ public TemporaryPath CreateFile(Extension? ext = default, bool deleteOnDispose = if (_isDisposed) throw new ObjectDisposedException(nameof(TemporaryFileManager)); - var path = _basePath.Combine(Guid.NewGuid().ToString()); + var path = _basePath / Guid.NewGuid().ToString(); if (path.Extension != default) path = path.AppendExtension(ext ?? KnownExtensions.Tmp); @@ -58,7 +58,7 @@ public TemporaryPath CreateFolder(string prefix = "", bool deleteOnDispose = tru if (_isDisposed) throw new ObjectDisposedException(nameof(TemporaryFileManager)); - var path = _basePath.Combine(prefix + Guid.NewGuid()); + var path = _basePath / (prefix + Guid.NewGuid()); _fileSystem.CreateDirectory(path); return new TemporaryPath(_fileSystem, path, deleteOnDispose); } diff --git a/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/Extensions/NxBuilderExtensionsTests.cs b/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/Extensions/NxBuilderExtensionsTests.cs index 8e7e1ea..d81b553 100644 --- a/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/Extensions/NxBuilderExtensionsTests.cs +++ b/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/Extensions/NxBuilderExtensionsTests.cs @@ -16,13 +16,13 @@ public class NxBuilderExtensionsTests public async Task NxPackerBuilder_CanAddFolderFromIFileSystem_AndExtractToIFileSystem(InMemoryFileSystem fs, AbsolutePath folderPath) { // Arrange - var file1 = folderPath.Combine("file1.txt"); - var file2 = folderPath.Combine("subfolder/file2.txt"); + var file1 = folderPath / "file1.txt"; + var file2 = folderPath / "subfolder/file2.txt"; await fs.WriteAllTextAsync(file1, "Content 1"); await fs.WriteAllTextAsync(file2, "Content 2"); var builder = new NxPackerBuilder(); - var outputPath = folderPath.Parent.Combine("output.nx"); + var outputPath = folderPath.Parent / "output.nx"; // Act builder.AddFolder(folderPath) @@ -38,11 +38,11 @@ public async Task NxPackerBuilder_CanAddFolderFromIFileSystem_AndExtractToIFileS entries.Should().Contain(e => e.FilePath == "subfolder/file2.txt"); // Verify we can extract all files - var extractFolder = folderPath.Parent.Combine("extracted"); + var extractFolder = folderPath.Parent / "extracted"; unpacker.AddAllFilesWithFileSystemOutput(extractFolder).Extract(); - var extractedFile1 = extractFolder.Combine("file1.txt"); - var extractedFile2 = extractFolder.Combine("subfolder/file2.txt"); + var extractedFile1 = extractFolder / "file1.txt"; + var extractedFile2 = extractFolder / "subfolder/file2.txt"; fs.FileExists(extractedFile1).Should().BeTrue(); fs.FileExists(extractedFile2).Should().BeTrue(); @@ -52,7 +52,7 @@ public async Task NxPackerBuilder_CanAddFolderFromIFileSystem_AndExtractToIFileS // Verify we can extract a single file. unpacker = NxUnpackerBuilderExtensions.FromFile(outputPath); - var extractedFile1Copy = extractFolder.Combine("file1-copy.txt"); + var extractedFile1Copy = extractFolder / "file1-copy.txt"; var file1Entry = entries.First(x => x.FilePath == "file1.txt"); unpacker.AddFileWithFileSystemOutput(file1Entry, extractedFile1Copy).Extract(); (await fs.ReadAllTextAsync(extractedFile1Copy)).Should().Be("Content 1"); diff --git a/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/FileProviders/FromAbsolutePathProviderTests.cs b/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/FileProviders/FromAbsolutePathProviderTests.cs index 5433ba9..ff0e63d 100644 --- a/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/FileProviders/FromAbsolutePathProviderTests.cs +++ b/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/FileProviders/FromAbsolutePathProviderTests.cs @@ -101,7 +101,7 @@ public async Task GetFileData_HandlesOutOfBoundsOverflow(IFileSystem fileSystem) private static async Task CreateTestFile(IFileSystem fileSystem, byte[] testData) { - var path = fileSystem.GetKnownPath(KnownPath.TempDirectory).Combine(Guid.NewGuid().ToString()); + var path = fileSystem.GetKnownPath(KnownPath.TempDirectory) / Guid.NewGuid().ToString(); await fileSystem.WriteAllBytesAsync(path, testData); return path; } diff --git a/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/FileProviders/OutputAbsolutePathProviderTests.cs b/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/FileProviders/OutputAbsolutePathProviderTests.cs index 01b7394..040b79b 100644 --- a/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/FileProviders/OutputAbsolutePathProviderTests.cs +++ b/tests/Extensions/NexusMods.Paths.Extensions.Nx.Tests/FileProviders/OutputAbsolutePathProviderTests.cs @@ -75,7 +75,7 @@ public void Constructor_CreatesFile(IFileSystem fileSystem) private static AbsolutePath CreateTestPath(IFileSystem fileSystem) { - return fileSystem.GetKnownPath(KnownPath.TempDirectory).Combine(Guid.NewGuid().ToString()); + return fileSystem.GetKnownPath(KnownPath.TempDirectory) / Guid.NewGuid().ToString(); } private static void CleanupTestFile(IFileSystem fileSystem, AbsolutePath path) diff --git a/tests/NexusMods.Paths.Tests/FileSystem/BaseFileSystemTests.cs b/tests/NexusMods.Paths.Tests/FileSystem/BaseFileSystemTests.cs index bac9c72..9765447 100644 --- a/tests/NexusMods.Paths.Tests/FileSystem/BaseFileSystemTests.cs +++ b/tests/NexusMods.Paths.Tests/FileSystem/BaseFileSystemTests.cs @@ -21,8 +21,8 @@ public void Test_PathMapping(InMemoryFileSystem fs, AbsolutePath originalPath, A public void Test_PathMapping_WithDirectory(InMemoryFileSystem fs, AbsolutePath originalDirectoryPath, AbsolutePath newDirectoryPath, string fileName) { - var originalFilePath = originalDirectoryPath.Combine(fileName); - var newFilePath = newDirectoryPath.Combine(fileName); + var originalFilePath = originalDirectoryPath / fileName; + var newFilePath = newDirectoryPath / fileName; var overlayFileSystem = (BaseFileSystem)fs.CreateOverlayFileSystem( new Dictionary @@ -116,7 +116,7 @@ public void Test_EnumerateRootDirectories_Windows() { var driveLetter = (char)iDriveLetter; var originalPath = fs.FromUnsanitizedFullPath($"{driveLetter}:/"); - var newPath = rootDirectory.Combine(Guid.NewGuid().ToString("D")); + var newPath = rootDirectory / Guid.NewGuid().ToString("D"); return (originalPath, newPath); }).ToDictionary(x => x.originalPath, x => x.newPath); @@ -167,7 +167,7 @@ public void Test_EnumerateRootDirectories_WithCrossPlatformPathMappings() { var driveLetter = (char)iDriveLetter; var originalPath = fs.FromUnsanitizedDirectoryAndFileName("/", driveLetter.ToString()); - var newPath = rootDirectory.Combine(Guid.NewGuid().ToString("D")); + var newPath = rootDirectory / Guid.NewGuid().ToString("D"); return (originalPath, newPath); }).ToDictionary(x => x.originalPath, x => x.newPath); diff --git a/tests/NexusMods.Paths.Tests/FileSystem/FileSystemTests.cs b/tests/NexusMods.Paths.Tests/FileSystem/FileSystemTests.cs index 7301afa..f3746e1 100644 --- a/tests/NexusMods.Paths.Tests/FileSystem/FileSystemTests.cs +++ b/tests/NexusMods.Paths.Tests/FileSystem/FileSystemTests.cs @@ -36,7 +36,7 @@ public void Test_DeleteDirectoryShouldDeleteEmpty() { var fs = new Paths.FileSystem(); - var directory = fs.FromUnsanitizedFullPath(AppContext.BaseDirectory).Combine("TestDirectory"); + var directory = fs.FromUnsanitizedFullPath(AppContext.BaseDirectory) / "TestDirectory"; fs.CreateDirectory(directory); fs.DeleteDirectory(directory, recursive: false); @@ -49,8 +49,8 @@ public void Test_DeleteDirectoryShouldNotDeleteNonEmpty() { var fs = new Paths.FileSystem(); - var directory = fs.FromUnsanitizedFullPath(AppContext.BaseDirectory).Combine("TestDirectory"); - var child = directory.Combine("Child"); + var directory = fs.FromUnsanitizedFullPath(AppContext.BaseDirectory) / ("TestDirectory"); + var child = directory / "Child"; fs.CreateDirectory(directory); fs.CreateDirectory(child); @@ -85,7 +85,7 @@ public void Test_EnumerateFileEntries() public async Task Test_CreateMemoryMappedFile_CanOpen(RelativePath relativePath, byte[] contents) { var fs = new Paths.FileSystem(); - var file = fs.GetKnownPath(KnownPath.TempDirectory).Combine(relativePath); + var file = fs.GetKnownPath(KnownPath.TempDirectory) / relativePath; await using (var stream = fs.CreateFile(file)) { stream.Write(contents); @@ -107,7 +107,7 @@ public async Task Test_CreateMemoryMappedFile_CanOpen(RelativePath relativePath, public async Task Test_CreateMemoryMappedFile_CanCreateAndWrite() { var fs = new Paths.FileSystem(); - var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName()); + var tempFile = fs.GetKnownPath(KnownPath.TempDirectory) / Path.GetRandomFileName(); var contents = new byte[] { 1, 2, 3, 4, 5 }; // Create a new MemoryMappedFile @@ -155,7 +155,7 @@ and we propagate this when opening the MemoryMappedFile. public void Test_ReadBytesRandom() { var fs = new Paths.FileSystem(); - var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName()); + var tempFile = fs.GetKnownPath(KnownPath.TempDirectory) / Path.GetRandomFileName(); var contents = new byte[] { 1, 2, 3, 4, 5 }; using (var stream = fs.CreateFile(tempFile)) { @@ -171,7 +171,7 @@ public void Test_ReadBytesRandom() public void Test_ReadBytesRandomWithOffset() { var fs = new Paths.FileSystem(); - var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName()); + var tempFile = fs.GetKnownPath(KnownPath.TempDirectory) / Path.GetRandomFileName(); var contents = new byte[] { 1, 2, 3, 4, 5 }; using (var stream = fs.CreateFile(tempFile)) { @@ -188,7 +188,7 @@ public void Test_ReadBytesRandomWithOffset() public async Task Test_ReadBytesRandomAsync() { var fs = new Paths.FileSystem(); - var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName()); + var tempFile = fs.GetKnownPath(KnownPath.TempDirectory) / Path.GetRandomFileName(); var contents = new byte[] { 1, 2, 3, 4, 5 }; await using (var stream = fs.CreateFile(tempFile)) { @@ -204,7 +204,7 @@ public async Task Test_ReadBytesRandomAsync() public async Task Test_ReadBytesRandomAsyncWithOffset() { var fs = new Paths.FileSystem(); - var tempFile = fs.GetKnownPath(KnownPath.TempDirectory).Combine(Path.GetRandomFileName()); + var tempFile = fs.GetKnownPath(KnownPath.TempDirectory) / Path.GetRandomFileName(); var contents = new byte[] { 1, 2, 3, 4, 5 }; await using (var stream = fs.CreateFile(tempFile)) { diff --git a/tests/NexusMods.Paths.Tests/FileSystem/InMemoryFileSystemTests.cs b/tests/NexusMods.Paths.Tests/FileSystem/InMemoryFileSystemTests.cs index be11e8c..f69e911 100644 --- a/tests/NexusMods.Paths.Tests/FileSystem/InMemoryFileSystemTests.cs +++ b/tests/NexusMods.Paths.Tests/FileSystem/InMemoryFileSystemTests.cs @@ -28,10 +28,10 @@ public void Test_EnumerateFiles( string fileName2, string subDirectoryName1) { - var subDirectory1 = parentDirectory.Combine(subDirectoryName1); + var subDirectory1 = parentDirectory / subDirectoryName1; - var file1 = parentDirectory.Combine(fileName1); - var file2 = subDirectory1.Combine(fileName2); + var file1 = parentDirectory / fileName1; + var file2 = subDirectory1 / fileName2; fs.AddDirectory(parentDirectory); fs.AddDirectory(subDirectory1); @@ -53,12 +53,12 @@ public void Test_EnumerateFiles_Recursive( string subDirectoryName1, string subDirectoryName2) { - var subDirectory1 = parentDirectory.Combine(subDirectoryName1); - var subDirectory2 = parentDirectory.Combine(subDirectoryName2); + var subDirectory1 = parentDirectory / subDirectoryName1; + var subDirectory2 = parentDirectory / subDirectoryName2; - var file1 = parentDirectory.Combine(fileName1); - var file2 = subDirectory1.Combine(fileName2); - var file3 = subDirectory2.Combine(fileName3); + var file1 = parentDirectory / fileName1; + var file2 = subDirectory1 / fileName2; + var file3 = subDirectory2 / fileName3; fs.AddDirectory(parentDirectory); fs.AddDirectory(subDirectory1); @@ -80,9 +80,9 @@ public void Test_EnumerateDirectories( string subDirectoryName2, string subDirectoryName3) { - var subDirectory1 = parentDirectory.Combine(subDirectoryName1); - var subDirectory2 = parentDirectory.Combine(subDirectoryName2); - var subDirectory3 = subDirectory1.Combine(subDirectoryName3); + var subDirectory1 = parentDirectory / subDirectoryName1; + var subDirectory2 = parentDirectory / subDirectoryName2; + var subDirectory3 = subDirectory1 / subDirectoryName3; fs.AddDirectory(parentDirectory); fs.AddDirectory(subDirectory1); @@ -102,9 +102,9 @@ public void Test_EnumerateDirectories_Recursive( string subDirectoryName2, string subDirectoryName3) { - var subDirectory1 = parentDirectory.Combine(subDirectoryName1); - var subDirectory2 = parentDirectory.Combine(subDirectoryName2); - var subDirectory3 = subDirectory1.Combine(subDirectoryName3); + var subDirectory1 = parentDirectory / subDirectoryName1; + var subDirectory2 = parentDirectory / subDirectoryName2; + var subDirectory3 = subDirectory1 / subDirectoryName3; fs.AddDirectory(parentDirectory); fs.AddDirectory(subDirectory1); @@ -124,10 +124,10 @@ public void Test_EnumerateFilesEntries( string fileName2, string subDirectoryName1) { - var subDirectory1 = parentDirectory.Combine(subDirectoryName1); + var subDirectory1 = parentDirectory / (subDirectoryName1); - var file1 = parentDirectory.Combine(fileName1); - var file2 = subDirectory1.Combine(fileName2); + var file1 = parentDirectory / (fileName1); + var file2 = subDirectory1 / fileName2; fs.AddDirectory(parentDirectory); fs.AddDirectory(subDirectory1); @@ -149,12 +149,12 @@ public void Test_EnumerateFilesEntries_Recursive( string subDirectoryName1, string subDirectoryName2) { - var subDirectory1 = parentDirectory.Combine(subDirectoryName1); - var subDirectory2 = parentDirectory.Combine(subDirectoryName2); + var subDirectory1 = parentDirectory / (subDirectoryName1); + var subDirectory2 = parentDirectory / (subDirectoryName2); - var file1 = parentDirectory.Combine(fileName1); - var file2 = subDirectory1.Combine(fileName2); - var file3 = subDirectory2.Combine(fileName3); + var file1 = parentDirectory / (fileName1); + var file2 = subDirectory1 / fileName2; + var file3 = subDirectory2 / fileName3; fs.AddDirectory(parentDirectory); fs.AddDirectory(subDirectory1); @@ -180,7 +180,7 @@ public void Test_CreateDirectory_SubDirectory(InMemoryFileSystem fs, AbsolutePath parentDirectory, string subDirectoryName) { - var subDirectory = parentDirectory.Combine(subDirectoryName); + var subDirectory = parentDirectory / subDirectoryName; fs.CreateDirectory(subDirectory); @@ -249,11 +249,11 @@ public void Test_DeleteDirectory_Recursive( { fs.CreateDirectory(directory); - var subDirectory = directory.Combine(subDirectoryName); + var subDirectory = directory / subDirectoryName; fs.CreateDirectory(subDirectory); - var file1 = directory.Combine(fileName1); - var file2 = subDirectory.Combine(fileName2); + var file1 = directory / (fileName1); + var file2 = subDirectory / fileName2; fs.AddEmptyFile(file1); fs.AddEmptyFile(file2); @@ -279,7 +279,7 @@ public void Test_DeleteDirectory_NotEmpty( { fs.CreateDirectory(directory); - var file = directory.Combine(fileName); + var file = directory / fileName; fs.AddEmptyFile(file); fs.DirectoryExists(directory).Should().BeTrue(); diff --git a/tests/NexusMods.Paths.Tests/RelativePathTests.cs b/tests/NexusMods.Paths.Tests/RelativePathTests.cs index deabcc3..02343cd 100644 --- a/tests/NexusMods.Paths.Tests/RelativePathTests.cs +++ b/tests/NexusMods.Paths.Tests/RelativePathTests.cs @@ -28,7 +28,7 @@ public void Test_FromStringImplicitCast(string input, string expected) // A little roundabout, but I wanted to make sure the cast happens as part // of a method call. var basePath = Paths.FileSystem.Shared.EnumerateRootDirectories().First(); - var path = basePath.Combine(input).RelativeTo(basePath); + var path = (basePath / input).RelativeTo(basePath); path.ToString().Should().Be(expected); } @@ -207,7 +207,7 @@ public void Test_Join(string left, string right, string expectedOutput) { var leftPath = new RelativePath(left); var rightPath = new RelativePath(right); - var actualOutput = leftPath.Join(rightPath); + var actualOutput = leftPath / rightPath; actualOutput.Should().Be(expectedOutput); } diff --git a/tests/NexusMods.Paths.Tests/TemporaryPathTests.cs b/tests/NexusMods.Paths.Tests/TemporaryPathTests.cs index bc4bd5b..ab9036e 100644 --- a/tests/NexusMods.Paths.Tests/TemporaryPathTests.cs +++ b/tests/NexusMods.Paths.Tests/TemporaryPathTests.cs @@ -36,12 +36,12 @@ public async Task DirectoriesAreDeleted(InMemoryFileSystem fs, TemporaryFileMana var tempDirPath = manager.CreateFolder(); // Create some children - var childFileA = tempDirPath.Path.Combine("File A"); + var childFileA = tempDirPath.Path / "File A"; fs.CreateFile(childFileA); - var childFolder = tempDirPath.Path.Combine("Child Folder"); + var childFolder = tempDirPath.Path / "Child Folder"; fs.CreateDirectory(childFolder); - var childFileB = childFolder.Combine("File B"); + var childFileB = childFolder / "File B"; fs.CreateFile(childFileB); From 746330147a169718bbb97781cdf33aa50202db2f Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Mon, 11 Nov 2024 16:00:14 -0700 Subject: [PATCH 3/3] Update Paths.cs --- src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs b/src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs index 5f55c2e..d93c437 100644 --- a/src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs +++ b/src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs @@ -104,7 +104,7 @@ public string SystemJoinLarge() [Benchmark] public AbsolutePath NexusJoinLarge() { - return CurrentPath.AbsolutePath/ "foo/bar/baz/quz/qax"; + return CurrentPath.AbsolutePath / "foo/bar/baz/quz/qax"; } [Benchmark]