From 4c5b0c16f3c163a18b9276c3ab776ab93a629b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Tue, 22 Aug 2023 16:03:40 +0200 Subject: [PATCH] fix: use full path in `Directory.SetCurrentDirectory` (#1028) The current directory should always be a rooted path, even if the directory is set to a relative directory. --- .../MockDirectory.cs | 2 +- .../MockDirectoryTests.cs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs index 78910a1a9..1f1e3176a 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -602,7 +602,7 @@ public override void SetCreationTimeUtc(string path, DateTime creationTimeUtc) /// public override void SetCurrentDirectory(string path) { - currentDirectory = path; + currentDirectory = mockFileDataAccessor.Path.GetFullPath(path); } /// diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 0ecb83cdc..adfe56a9f 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -1661,6 +1661,17 @@ public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory() Assert.AreEqual(directory, fileSystem.Directory.GetCurrentDirectory()); } + [Test] + public void MockDirectory_SetCurrentDirectory_WithRelativePath_ShouldUseFullPath() + { + var fileSystem = new MockFileSystem(); + fileSystem.Directory.SetCurrentDirectory("."); + + var result = fileSystem.Directory.GetCurrentDirectory(); + + Assert.IsTrue(fileSystem.Path.IsPathRooted(result)); + } + [Test] public void MockDirectory_GetParent_ShouldThrowArgumentNullExceptionIfPathIsNull() {