diff --git a/src/Microsoft.DotNet.InternalAbstractions/DirectoryPath.cs b/src/Microsoft.DotNet.InternalAbstractions/DirectoryPath.cs index 3d578432e2..c05177f617 100644 --- a/src/Microsoft.DotNet.InternalAbstractions/DirectoryPath.cs +++ b/src/Microsoft.DotNet.InternalAbstractions/DirectoryPath.cs @@ -10,8 +10,17 @@ public struct DirectoryPath { public string Value { get; } + /// + /// Create DirectoryPath to repesent a absolute directory path. Note it may not exist. + /// + /// If the value is not rooted. Path.GetFullPath will be called during the consturctor. public DirectoryPath(string value) { + if (!Path.IsPathRooted(value)) + { + value = Path.GetFullPath(value); + } + Value = value; } @@ -46,7 +55,7 @@ public override string ToString() public DirectoryPath GetParentPath() { - return new DirectoryPath(Directory.GetParent(Path.GetFullPath(Value)).FullName); + return new DirectoryPath(Path.GetDirectoryName(Value)); } } } diff --git a/src/Microsoft.DotNet.InternalAbstractions/FilePath.cs b/src/Microsoft.DotNet.InternalAbstractions/FilePath.cs index 9dbbf8c83e..7b7caee289 100644 --- a/src/Microsoft.DotNet.InternalAbstractions/FilePath.cs +++ b/src/Microsoft.DotNet.InternalAbstractions/FilePath.cs @@ -9,8 +9,17 @@ public struct FilePath { public string Value { get; } + /// + /// Create FilePath to repesent a absolute file path. Note it may not exist. + /// + /// If the value is not rooted. Path.GetFullPath will be called during the consturctor. public FilePath(string value) { + if (!Path.IsPathRooted(value)) + { + value = Path.GetFullPath(value); + } + Value = value; } diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs index 4320d0da67..7f3166bf21 100644 --- a/test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs @@ -39,6 +39,17 @@ public FileSystemMockBuilder AddFiles(string basePath, params string[] files) return this; } + /// + /// Just a "home" means different path on Windows and Unix. + /// Create a platform dependent Temporary directory path and use it to avoid further mis interpretation in + /// later tests. Like "c:/home vs /home". Instead always use Path.Combine(TempraryDirectory, "home") + /// + internal FileSystemMockBuilder UseCurrentSystemTemporaryDirectory() + { + TemporaryFolder = Path.GetTempPath(); + return this; + } + internal IFileSystem Build() { return new FileSystemMock(_files, TemporaryFolder); diff --git a/test/dotnet.Tests/BuildServerTests/BuildServerProviderTests.cs b/test/dotnet.Tests/BuildServerTests/BuildServerProviderTests.cs index 15f9a94ea2..0c93cd041e 100644 --- a/test/dotnet.Tests/BuildServerTests/BuildServerProviderTests.cs +++ b/test/dotnet.Tests/BuildServerTests/BuildServerProviderTests.cs @@ -81,37 +81,39 @@ public void GivenNoEnvironmentVariableItUsesTheDefaultPidDirectory() [Fact] public void GivenEnvironmentVariableItUsesItForThePidDirectory() { - const string PidDirectory = "path/to/some/directory"; - + IFileSystem fileSystem = new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build(); + var pidDirectory = Path.Combine(fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath, "path/to/some/directory"); var provider = new BuildServerProvider( - new FileSystemMockBuilder().Build(), - CreateEnvironmentProviderMock(PidDirectory).Object); + fileSystem, + CreateEnvironmentProviderMock(pidDirectory).Object); provider .GetPidFileDirectory() .Value .Should() - .Be(PidDirectory); + .Be(pidDirectory); } [Fact] public void GivenARazorPidFileItReturnsARazorBuildServer() { const int ProcessId = 1234; - const string ServerPath = "/path/to/rzc.dll"; const string PipeName = "some-pipe-name"; string pidDirectory = Path.GetFullPath("var/pids/build"); string pidFilePath = Path.Combine(pidDirectory, $"{RazorPidFile.FilePrefix}{ProcessId}"); - var fileSystemMock = new FileSystemMockBuilder() - .AddFile( + var fileSystemMockBuilder = new FileSystemMockBuilder(); + fileSystemMockBuilder.UseCurrentSystemTemporaryDirectory(); + var serverPath = Path.Combine(fileSystemMockBuilder.TemporaryFolder, "path/to/rzc.dll"); + + IFileSystem fileSystemMock = fileSystemMockBuilder.AddFile( pidFilePath, - $"{ProcessId}{Environment.NewLine}{RazorPidFile.RazorServerType}{Environment.NewLine}{ServerPath}{Environment.NewLine}{PipeName}") + $"{ProcessId}{Environment.NewLine}{RazorPidFile.RazorServerType}{Environment.NewLine}{serverPath}{Environment.NewLine}{PipeName}") .AddFile( Path.Combine(pidDirectory, $"{RazorPidFile.FilePrefix}not-a-pid-file"), "not-a-pid-file") - .Build(); + .Build(); var provider = new BuildServerProvider( fileSystemMock, @@ -127,7 +129,7 @@ public void GivenARazorPidFileItReturnsARazorBuildServer() razorServer.PidFile.Should().NotBeNull(); razorServer.PidFile.Path.Value.Should().Be(pidFilePath); razorServer.PidFile.ProcessId.Should().Be(ProcessId); - razorServer.PidFile.ServerPath.Value.Should().Be(ServerPath); + razorServer.PidFile.ServerPath.Value.Should().Be(serverPath); razorServer.PidFile.PipeName.Should().Be(PipeName); } diff --git a/test/dotnet.Tests/BuildServerTests/RazorServerTests.cs b/test/dotnet.Tests/BuildServerTests/RazorServerTests.cs index f9b1ea52ff..3161c89c08 100644 --- a/test/dotnet.Tests/BuildServerTests/RazorServerTests.cs +++ b/test/dotnet.Tests/BuildServerTests/RazorServerTests.cs @@ -24,7 +24,6 @@ public class RazorServerTests public void GivenAFailedShutdownCommandItThrows() { const int ProcessId = 1234; - const string ServerPath = "path/to/rzc.dll"; const string PipeName = "some-pipe-name"; const string ErrorMessage = "error!"; @@ -33,17 +32,20 @@ public void GivenAFailedShutdownCommandItThrows() var fileSystemMock = new FileSystemMockBuilder() .AddFile(pidFilePath, "") + .UseCurrentSystemTemporaryDirectory() .Build(); fileSystemMock.File.Exists(pidFilePath).Should().BeTrue(); + var serverPath = Path.Combine(fileSystemMock.Directory.CreateTemporaryDirectory().DirectoryPath, "path/to/rzc.dll"); + var server = new RazorServer( pidFile: new RazorPidFile( path: new FilePath(pidFilePath), processId: ProcessId, - serverPath: new FilePath(ServerPath), + serverPath: new FilePath(serverPath), pipeName: PipeName), - commandFactory: CreateCommandFactoryMock(ServerPath, PipeName, exitCode: 1, stdErr: ErrorMessage).Object, + commandFactory: CreateCommandFactoryMock(serverPath, PipeName, exitCode: 1, stdErr: ErrorMessage).Object, fileSystem: fileSystemMock); Action a = () => server.Shutdown(); @@ -60,7 +62,6 @@ public void GivenAFailedShutdownCommandItThrows() public void GivenASuccessfulShutdownItDoesNotThrow() { const int ProcessId = 1234; - const string ServerPath = "path/to/rzc.dll"; const string PipeName = "some-pipe-name"; string pidDirectory = Path.GetFullPath("var/pids/build"); @@ -68,17 +69,20 @@ public void GivenASuccessfulShutdownItDoesNotThrow() var fileSystemMock = new FileSystemMockBuilder() .AddFile(pidFilePath, "") + .UseCurrentSystemTemporaryDirectory() .Build(); fileSystemMock.File.Exists(pidFilePath).Should().BeTrue(); + var serverPath = Path.Combine(fileSystemMock.Directory.CreateTemporaryDirectory().DirectoryPath, "path/to/rzc.dll"); + var server = new RazorServer( pidFile: new RazorPidFile( path: new FilePath(pidFilePath), processId: ProcessId, - serverPath: new FilePath(ServerPath), + serverPath: new FilePath(serverPath), pipeName: PipeName), - commandFactory: CreateCommandFactoryMock(ServerPath, PipeName).Object, + commandFactory: CreateCommandFactoryMock(serverPath, PipeName).Object, fileSystem: fileSystemMock); server.Shutdown(); diff --git a/test/dotnet.Tests/CommandTests/ToolInstallCommandTests.cs b/test/dotnet.Tests/CommandTests/ToolInstallCommandTests.cs index eb9914064b..c111e6104d 100644 --- a/test/dotnet.Tests/CommandTests/ToolInstallCommandTests.cs +++ b/test/dotnet.Tests/CommandTests/ToolInstallCommandTests.cs @@ -35,24 +35,28 @@ public class ToolInstallCommandTests private readonly AppliedOption _appliedCommand; private readonly ParseResult _parseResult; private readonly BufferedReporter _reporter; - private const string PathToPlaceShim = "pathToPlace"; - private const string PathToPlacePackages = PathToPlaceShim + "pkg"; + private readonly string _temporaryDirectory; + private readonly string _pathToPlaceShim; + private readonly string _pathToPlacePackages; private const string PackageId = "global.tool.console.demo"; private const string PackageVersion = "1.0.4"; public ToolInstallCommandTests() { _reporter = new BufferedReporter(); - _fileSystem = new FileSystemMockBuilder().Build(); - _toolPackageStore = new ToolPackageStoreMock(new DirectoryPath(PathToPlacePackages), _fileSystem); + _fileSystem = new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build(); + _temporaryDirectory = _fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath; + _pathToPlaceShim = Path.Combine(_temporaryDirectory, "pathToPlace"); + _pathToPlacePackages = _pathToPlaceShim + "Packages"; + _toolPackageStore = new ToolPackageStoreMock(new DirectoryPath(_pathToPlacePackages), _fileSystem); _createShellShimRepository = (nonGlobalLocation) => new ShellShimRepository( - new DirectoryPath(PathToPlaceShim), + new DirectoryPath(_pathToPlaceShim), fileSystem: _fileSystem, appHostShellShimMaker: new AppHostShellShimMakerMock(_fileSystem), filePermissionSetter: new NoOpFilePermissionSetter()); _environmentPathInstructionMock = - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim); + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim); _createToolPackageStoreAndInstaller = (_) => (_toolPackageStore, CreateToolPackageInstaller()); ParseResult result = Parser.Instance.Parse($"dotnet tool install -g {PackageId}"); @@ -197,7 +201,7 @@ public void GivenFailedPackageInstallWhenRunWithPackageIdItShouldFail() Environment.NewLine + string.Format(LocalizableStrings.ToolInstallationFailedWithRestoreGuidance, PackageId)); - _fileSystem.Directory.Exists(Path.Combine(PathToPlacePackages, PackageId)).Should().BeFalse(); + _fileSystem.Directory.Exists(Path.Combine(_pathToPlacePackages, PackageId)).Should().BeFalse(); } [Fact] @@ -220,7 +224,7 @@ public void GivenCreateShimItShouldHaveNoBrokenFolderOnDisk() CommonLocalizableStrings.ShellShimConflict, ProjectRestorerMock.FakeCommandName)); - _fileSystem.Directory.Exists(Path.Combine(PathToPlacePackages, PackageId)).Should().BeFalse(); + _fileSystem.Directory.Exists(Path.Combine(_pathToPlacePackages, PackageId)).Should().BeFalse(); } [Fact] @@ -257,7 +261,7 @@ public void WhenRunWithPackageIdItShouldShowSuccessMessage() _parseResult, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true), _reporter); installCommand.Execute().Should().Be(0); @@ -284,7 +288,7 @@ public void WhenRunWithInvalidVersionItShouldThrow() result, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true), _reporter); Action action = () => installCommand.Execute(); @@ -307,7 +311,7 @@ public void WhenRunWithExactVersionItShouldSucceed() result, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true), _reporter); installCommand.Execute().Should().Be(0); @@ -333,7 +337,7 @@ public void WhenRunWithValidVersionRangeItShouldSucceed() result, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true), _reporter); installCommand.Execute().Should().Be(0); @@ -359,7 +363,7 @@ public void WhenRunWithoutAMatchingRangeItShouldFail() result, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true), _reporter); Action a = () => installCommand.Execute(); @@ -369,7 +373,7 @@ public void WhenRunWithoutAMatchingRangeItShouldFail() LocalizableStrings.ToolInstallationRestoreFailed + Environment.NewLine + string.Format(LocalizableStrings.ToolInstallationFailedWithRestoreGuidance, PackageId)); - _fileSystem.Directory.Exists(Path.Combine(PathToPlacePackages, PackageId)).Should().BeFalse(); + _fileSystem.Directory.Exists(Path.Combine(_pathToPlacePackages, PackageId)).Should().BeFalse(); } [Fact] @@ -383,7 +387,7 @@ public void WhenRunWithValidVersionWildcardItShouldSucceed() result, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true), _reporter); installCommand.Execute().Should().Be(0); @@ -411,7 +415,7 @@ public void WhenRunWithBothGlobalAndToolPathShowErrorMessage() parseResult, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true), _reporter); Action a = () => installCommand.Execute(); @@ -433,7 +437,7 @@ public void WhenRunWithNeitherOfGlobalNorToolPathShowErrorMessage() parseResult, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim, true), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim, true), _reporter); Action a = () => installCommand.Execute(); @@ -454,7 +458,7 @@ public void WhenRunWithPackageIdAndBinPathItShouldNoteHaveEnvironmentPathInstruc parseResult, _createToolPackageStoreAndInstaller, _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim), _reporter); installCommand.Execute().Should().Be(0); @@ -466,7 +470,7 @@ public void WhenRunWithPackageIdAndBinPathItShouldNoteHaveEnvironmentPathInstruc public void AndPackagedShimIsProvidedWhenRunWithPackageIdItCreateShimUsingPackagedShim() { var extension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : string.Empty; - var prepackagedShimPath = "packagedShimDirectory/" + ProjectRestorerMock.FakeCommandName + extension; + var prepackagedShimPath = Path.Combine (_temporaryDirectory, ProjectRestorerMock.FakeCommandName + extension); var tokenToIdentifyPackagedShim = "packagedShim"; _fileSystem.File.WriteAllText(prepackagedShimPath, tokenToIdentifyPackagedShim); @@ -490,7 +494,7 @@ [new PackageId(PackageId)] = new[] {new FilePath(prepackagedShimPath)} fileSystem: _fileSystem, reporter: _reporter))), _createShellShimRepository, - new EnvironmentPathInstructionMock(_reporter, PathToPlaceShim), + new EnvironmentPathInstructionMock(_reporter, _pathToPlaceShim), _reporter); installCommand.Execute().Should().Be(0); @@ -512,11 +516,11 @@ private IToolPackageInstaller CreateToolPackageInstaller( installCallback: installCallback); } - private static string ExpectedCommandPath() + private string ExpectedCommandPath() { var extension = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : string.Empty; return Path.Combine( - "pathToPlace", + _pathToPlaceShim, ProjectRestorerMock.FakeCommandName + extension); } diff --git a/test/dotnet.Tests/CommandTests/ToolUninstallCommandTests.cs b/test/dotnet.Tests/CommandTests/ToolUninstallCommandTests.cs index 902d4eb1df..516c2e26ad 100644 --- a/test/dotnet.Tests/CommandTests/ToolUninstallCommandTests.cs +++ b/test/dotnet.Tests/CommandTests/ToolUninstallCommandTests.cs @@ -34,14 +34,17 @@ public class ToolUninstallCommandTests private const string PackageId = "global.tool.console.demo"; private const string PackageVersion = "1.0.4"; - private const string ShimsDirectory = "shims"; - private const string ToolsDirectory = "tools"; + private readonly string _shimsDirectory; + private readonly string _toolsDirectory; public ToolUninstallCommandTests() { _reporter = new BufferedReporter(); - _fileSystem = new FileSystemMockBuilder().Build(); - _environmentPathInstructionMock = new EnvironmentPathInstructionMock(_reporter, ShimsDirectory); + _fileSystem = new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build(); + var tempDirectory = _fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath; + _shimsDirectory = Path.Combine(tempDirectory, "shims"); + _toolsDirectory = Path.Combine(tempDirectory, "tools"); + _environmentPathInstructionMock = new EnvironmentPathInstructionMock(_reporter, _shimsDirectory); } [Fact] @@ -74,10 +77,10 @@ public void GivenAPackageItUninstalls() PackageId, PackageVersion)); - var packageDirectory = new DirectoryPath(Path.GetFullPath(ToolsDirectory)) + var packageDirectory = new DirectoryPath(Path.GetFullPath(_toolsDirectory)) .WithSubDirectories(PackageId, PackageVersion); var shimPath = Path.Combine( - ShimsDirectory, + _shimsDirectory, ProjectRestorerMock.FakeCommandName + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : "")); @@ -116,10 +119,10 @@ public void GivenAFailureToUninstallItLeavesItInstalled() PackageId, PackageVersion)); - var packageDirectory = new DirectoryPath(Path.GetFullPath(ToolsDirectory)) + var packageDirectory = new DirectoryPath(Path.GetFullPath(_toolsDirectory)) .WithSubDirectories(PackageId, PackageVersion); var shimPath = Path.Combine( - ShimsDirectory, + _shimsDirectory, ProjectRestorerMock.FakeCommandName + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : "")); @@ -192,7 +195,7 @@ private ToolInstallCommand CreateInstallCommand(string options) { ParseResult result = Parser.Instance.Parse("dotnet tool install " + options); - var store = new ToolPackageStoreMock(new DirectoryPath(ToolsDirectory), _fileSystem); + var store = new ToolPackageStoreMock(new DirectoryPath(_toolsDirectory), _fileSystem); var packageInstallerMock = new ToolPackageInstallerMock( _fileSystem, store, @@ -205,7 +208,7 @@ private ToolInstallCommand CreateInstallCommand(string options) result, (_) => (store, packageInstallerMock), (_) => new ShellShimRepository( - new DirectoryPath(ShimsDirectory), + new DirectoryPath(_shimsDirectory), fileSystem: _fileSystem, appHostShellShimMaker: new AppHostShellShimMakerMock(_fileSystem)), _environmentPathInstructionMock, @@ -220,11 +223,11 @@ private ToolUninstallCommand CreateUninstallCommand(string options, Action unins result["dotnet"]["tool"]["uninstall"], result, (_) => new ToolPackageStoreMock( - new DirectoryPath(ToolsDirectory), + new DirectoryPath(_toolsDirectory), _fileSystem, uninstallCallback), (_) => new ShellShimRepository( - new DirectoryPath(ShimsDirectory), + new DirectoryPath(_shimsDirectory), fileSystem: _fileSystem, appHostShellShimMaker: new AppHostShellShimMakerMock(_fileSystem)), _reporter); diff --git a/test/dotnet.Tests/CommandTests/ToolUpdateCommandTests.cs b/test/dotnet.Tests/CommandTests/ToolUpdateCommandTests.cs index 32a64274f1..17b969f8c3 100644 --- a/test/dotnet.Tests/CommandTests/ToolUpdateCommandTests.cs +++ b/test/dotnet.Tests/CommandTests/ToolUpdateCommandTests.cs @@ -18,6 +18,7 @@ using Parser = Microsoft.DotNet.Cli.Parser; using LocalizableStrings = Microsoft.DotNet.Tools.Tool.Update.LocalizableStrings; using Microsoft.DotNet.ShellShim; +using System.IO; namespace Microsoft.DotNet.Tests.Commands { @@ -31,15 +32,18 @@ public class ToolUpdateCommandTests private readonly List _mockFeeds; private const string LowerPackageVersion = "1.0.4"; private const string HigherPackageVersion = "1.0.5"; - private const string ShimsDirectory = "shims"; - private const string ToolsDirectory = "tools"; + private readonly string _shimsDirectory; + private readonly string _toolsDirectory; public ToolUpdateCommandTests() { _reporter = new BufferedReporter(); - _fileSystem = new FileSystemMockBuilder().Build(); - _environmentPathInstructionMock = new EnvironmentPathInstructionMock(_reporter, ShimsDirectory); - _store = new ToolPackageStoreMock(new DirectoryPath(ToolsDirectory), _fileSystem); + _fileSystem = new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build(); + var tempDirectory = _fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath; + _shimsDirectory = Path.Combine(tempDirectory, "shims"); + _toolsDirectory = Path.Combine(tempDirectory, "tools"); + _environmentPathInstructionMock = new EnvironmentPathInstructionMock(_reporter, _shimsDirectory); + _store = new ToolPackageStoreMock(new DirectoryPath(_toolsDirectory), _fileSystem); _mockFeeds = new List { new MockFeed @@ -244,7 +248,7 @@ private ToolUpdateCommand CreateUpdateCommand(string options) private ShellShimRepository GetMockedShellShimRepository() { return new ShellShimRepository( - new DirectoryPath(ShimsDirectory), + new DirectoryPath(_shimsDirectory), fileSystem: _fileSystem, appHostShellShimMaker: new AppHostShellShimMakerMock(_fileSystem)); }