From 55edb7eefeb9e771fb5ddb30effa2da9863a43bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Thu, 29 Oct 2026 23:21:03 +0100 Subject: [PATCH] fix: resolve statistics keys with the simulated path instead of the host path When simulating another operating system, the host Path produced wrong keys and fell back to the process's current directory, which made watcher tests fail on macOS whenever a real file system test had left it pointing at a deleted directory. The directory cleaner now also resets the current directory when a test moved it into a subdirectory, so it no longer leaves the process in a deleted directory. --- .../Initializer/DirectoryCleaner.cs | 6 ++-- .../Statistics/PathStatistics.cs | 6 ++-- .../Statistics/PathStatisticsTests.cs | 33 +++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Source/Testably.Abstractions.Testing/Initializer/DirectoryCleaner.cs b/Source/Testably.Abstractions.Testing/Initializer/DirectoryCleaner.cs index d3f7cb54a..7703dadda 100644 --- a/Source/Testably.Abstractions.Testing/Initializer/DirectoryCleaner.cs +++ b/Source/Testably.Abstractions.Testing/Initializer/DirectoryCleaner.cs @@ -45,9 +45,9 @@ public void Dispose() { // It is important to reset the current directory, as otherwise deleting the BasePath // results in a IOException, because the process cannot access the file. - if (string.Equals( - _fileSystem.Directory.GetCurrentDirectory(), - BasePath, + // On Unix the deletion succeeds and leaves the process in a deleted current directory. + if (_fileSystem.Directory.GetCurrentDirectory().StartsWith( + _pathToDelete, _fileSystem.ExecuteOrDefault().StringComparisonMode)) { _fileSystem.Directory.SetCurrentDirectory(_fileSystem.Path.GetTempPath()); diff --git a/Source/Testably.Abstractions.Testing/Statistics/PathStatistics.cs b/Source/Testably.Abstractions.Testing/Statistics/PathStatistics.cs index 777a54343..6375e2905 100644 --- a/Source/Testably.Abstractions.Testing/Statistics/PathStatistics.cs +++ b/Source/Testably.Abstractions.Testing/Statistics/PathStatistics.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Concurrent; -using System.IO; using Testably.Abstractions.Testing.Helpers; namespace Testably.Abstractions.Testing.Statistics; @@ -238,7 +237,7 @@ internal IDisposable RegisterPathProperty(string path, string name, PropertyAcce return release; } - private static string CreateKey(string currentDirectory, string path) + private string CreateKey(string currentDirectory, string path) { string key = string.Empty; if (string.IsNullOrEmpty(path)) @@ -254,7 +253,8 @@ private static string CreateKey(string currentDirectory, string path) } else { - key = Path.GetFullPath(Path.Combine(currentDirectory, path)); + IPath mockPath = _fileSystem.Execute.Path; + key = mockPath.GetFullPath(mockPath.Combine(currentDirectory, path)); } return key.TrimEnd('/', '\\'); diff --git a/Tests/Testably.Abstractions.Testing.Tests/Statistics/PathStatisticsTests.cs b/Tests/Testably.Abstractions.Testing.Tests/Statistics/PathStatisticsTests.cs index 172a63edf..13d39a885 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/Statistics/PathStatisticsTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/Statistics/PathStatisticsTests.cs @@ -66,6 +66,39 @@ public async Task Key_ShouldSimplifyRelativePaths() await That(absolutPath).IsEqualTo(relativePath); } +#if CAN_SIMULATE_OTHER_OS + [Test] + [Arguments(SimulationMode.Linux)] + [Arguments(SimulationMode.MacOS)] + public async Task Key_WhenSimulatingUnix_BackslashShouldBePartOfFileName( + SimulationMode simulationMode) + { + MockFileSystem fileSystem = new(o => o.SimulatingOperatingSystem(simulationMode)); + IPathStatistics sut = fileSystem.Statistics.FileInfo; + + IStatistics withBackslash = sut[@"foo\bar"]; + IStatistics withSlash = sut["foo/bar"]; + + await That(withBackslash).IsNotEqualTo(withSlash) + .Because("on Unix a backslash is a valid file name character and not a directory separator"); + } +#endif + +#if CAN_SIMULATE_OTHER_OS + [Test] + public async Task Key_WhenSimulatingWindows_BackslashAndSlashShouldBeDirectorySeparators() + { + MockFileSystem fileSystem = new(o => o.SimulatingOperatingSystem(SimulationMode.Windows)); + IPathStatistics sut = fileSystem.Statistics.FileInfo; + + IStatistics withBackslash = sut[@"foo\bar"]; + IStatistics withSlash = sut["foo/bar"]; + + await That(withBackslash).IsEqualTo(withSlash) + .Because("on Windows both are directory separators"); + } +#endif + [Test] [Arguments("/")] [Arguments("\\")]