private CommandSpec GetPackageCommandSpecUsingMuxer(CommandResolverArguments arguments, ToolCommandName toolCommandName) { if (!_toolManifest.TryFind(toolCommandName, out var toolManifestPackage)) { return(null); } if (_localToolsResolverCache.TryLoad( new RestoredCommandIdentifier( toolManifestPackage.PackageId, toolManifestPackage.Version, NuGetFramework.Parse(BundledTargetFramework.GetTargetFrameworkMoniker()), Constants.AnyRid, toolCommandName), out var restoredCommand)) { if (!_fileSystem.File.Exists(restoredCommand.Executable.Value)) { throw new GracefulException(string.Format(LocalizableStrings.NeedRunToolRestore, toolCommandName.ToString())); } return(MuxerCommandSpecMaker.CreatePackageCommandSpecUsingMuxer( restoredCommand.Executable.Value, arguments.CommandArguments)); } else { throw new GracefulException(string.Format(LocalizableStrings.NeedRunToolRestore, toolCommandName.ToString())); } }
public ToolInstallLocalCommandTests() { _packageVersionA = NuGetVersion.Parse("1.0.4"); _reporter = new BufferedReporter(); _fileSystem = new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build(); _nugetGlobalPackagesFolder = new DirectoryPath(NuGetGlobalPackagesFolder.GetLocation()); _temporaryDirectory = _fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath; _pathToPlacePackages = Path.Combine(_temporaryDirectory, "pathToPlacePackage"); ToolPackageStoreMock toolPackageStoreMock = new ToolPackageStoreMock(new DirectoryPath(_pathToPlacePackages), _fileSystem); _toolPackageStore = toolPackageStoreMock; _toolPackageInstallerMock = new ToolPackageInstallerMock( _fileSystem, _toolPackageStore, new ProjectRestorerMock( _fileSystem, _reporter, new[] { new MockFeed { Type = MockFeedType.ImplicitAdditionalFeed, Packages = new List <MockFeedPackage> { new MockFeedPackage { PackageId = _packageIdA.ToString(), Version = _packageVersionA.ToNormalizedString(), ToolCommandName = _toolCommandNameA.ToString() } } } })); _localToolsResolverCache = new LocalToolsResolverCache( _fileSystem, new DirectoryPath(Path.Combine(_temporaryDirectory, "cache")), 1); _manifestFilePath = Path.Combine(_temporaryDirectory, "dotnet-tools.json"); _fileSystem.File.WriteAllText(Path.Combine(_temporaryDirectory, _manifestFilePath), _jsonContent); _toolManifestFinder = new ToolManifestFinder(new DirectoryPath(_temporaryDirectory), _fileSystem); _toolManifestEditor = new ToolManifestEditor(_fileSystem); ParseResult result = Parser.Instance.Parse($"dotnet tool install {_packageIdA.ToString()}"); _appliedCommand = result["dotnet"]["tool"]["install"]; Cli.CommandLine.Parser parser = Parser.Instance; _parseResult = parser.ParseFrom("dotnet tool", new[] { "install", _packageIdA.ToString() }); _localToolsResolverCache = new LocalToolsResolverCache( _fileSystem, new DirectoryPath(Path.Combine(_temporaryDirectory, "cache")), 1); }
public void WhenNuGetGlobalPackageLocationIsCleanedAfterRestoreItShowError() { ToolCommandName toolCommandNameA = new ToolCommandName("a"); NuGetVersion packageVersionA = NuGetVersion.Parse("1.0.4"); _fileSystem.File.WriteAllText(Path.Combine(_testDirectoryRoot, ManifestFilename), _jsonContent.Replace("$TOOLCOMMAND$", toolCommandNameA.Value)); ToolManifestFinder toolManifest = new ToolManifestFinder(new DirectoryPath(_testDirectoryRoot), _fileSystem); var fakeExecutable = _nugetGlobalPackagesFolder.WithFile("fakeExecutable.dll"); _fileSystem.Directory.CreateDirectory(_nugetGlobalPackagesFolder.Value); _fileSystem.File.CreateEmptyFile(fakeExecutable.Value); _localToolsResolverCache.Save( new Dictionary <RestoredCommandIdentifier, RestoredCommand> { [new RestoredCommandIdentifier( new PackageId("local.tool.console.a"), packageVersionA, NuGetFramework.Parse(BundledTargetFramework.GetTargetFrameworkMoniker()), Constants.AnyRid, toolCommandNameA)] = new RestoredCommand(toolCommandNameA, "dotnet", fakeExecutable) }, _nugetGlobalPackagesFolder); var localToolsCommandResolver = new LocalToolsCommandResolver( toolManifest, _localToolsResolverCache, _fileSystem, _nugetGlobalPackagesFolder); _fileSystem.File.Delete(fakeExecutable.Value); Action action = () => localToolsCommandResolver.Resolve(new CommandResolverArguments() { CommandName = $"dotnet-{toolCommandNameA.ToString()}", }); action.ShouldThrow <GracefulException>(string.Format(CommandFactory.LocalizableStrings.NeedRunToolRestore, toolCommandNameA.ToString())); }
public void WhenNuGetGlobalPackageLocationIsNotRestoredItThrowsGracefulException() { ToolCommandName toolCommandNameA = new ToolCommandName("a"); NuGetVersion packageVersionA = NuGetVersion.Parse("1.0.4"); _fileSystem.File.WriteAllText(Path.Combine(_testDirectoryRoot, ManifestFilename), _jsonContent.Replace("$TOOLCOMMAND$", toolCommandNameA.Value)); ToolManifestFinder toolManifest = new ToolManifestFinder(new DirectoryPath(_testDirectoryRoot), _fileSystem); var localToolsCommandResolver = new LocalToolsCommandResolver( toolManifest, _localToolsResolverCache, _fileSystem); Action action = () => localToolsCommandResolver.ResolveStrict(new CommandResolverArguments() { CommandName = $"dotnet-{toolCommandNameA.ToString()}", }); action.ShouldThrow <GracefulException>(string.Format(CommandFactory.LocalizableStrings.NeedRunToolRestore, toolCommandNameA.ToString())); }
public void RemoveShim(ToolCommandName commandName) { var files = new Dictionary <string, string>(); TransactionalAction.Run( action: () => { try { foreach (var file in GetShimFiles(commandName).Where(f => _fileSystem.File.Exists(f.Value))) { var tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); FileAccessRetrier.RetryOnMoveAccessFailure(() => _fileSystem.File.Move(file.Value, tempPath)); files[file.Value] = tempPath; } } catch (Exception ex) when(ex is UnauthorizedAccessException || ex is IOException) { throw new ShellShimException( string.Format( CommonLocalizableStrings.FailedToRemoveShellShim, commandName.ToString(), ex.Message ), ex); } }, commit: () => { foreach (var value in files.Values) { _fileSystem.File.Delete(value); } }, rollback: () => { foreach (var kvp in files) { FileAccessRetrier.RetryOnMoveAccessFailure(() => _fileSystem.File.Move(kvp.Value, kvp.Key)); } }); }
public ToolUpdateLocalCommandTests() { _reporter = new BufferedReporter(); _fileSystem = new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build(); _temporaryDirectoryParent = _fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath; _temporaryDirectory = Path.Combine(_temporaryDirectoryParent, "sub"); _fileSystem.Directory.CreateDirectory(_temporaryDirectory); _pathToPlacePackages = Path.Combine(_temporaryDirectory, "pathToPlacePackage"); _packageOriginalVersionA = NuGetVersion.Parse("1.0.0"); _packageNewVersionA = NuGetVersion.Parse("2.0.0"); ToolPackageStoreMock toolPackageStoreMock = new ToolPackageStoreMock(new DirectoryPath(_pathToPlacePackages), _fileSystem); _toolPackageStore = toolPackageStoreMock; _mockFeed = new MockFeed { Type = MockFeedType.ImplicitAdditionalFeed, Packages = new List <MockFeedPackage> { new MockFeedPackage { PackageId = _packageIdA.ToString(), Version = _packageOriginalVersionA.ToNormalizedString(), ToolCommandName = _toolCommandNameA.ToString() } } }; _toolPackageInstallerMock = new ToolPackageInstallerMock( _fileSystem, _toolPackageStore, new ProjectRestorerMock( _fileSystem, _reporter, new List <MockFeed> { _mockFeed })); _localToolsResolverCache = new LocalToolsResolverCache( _fileSystem, new DirectoryPath(Path.Combine(_temporaryDirectory, "cache")), 1); _manifestFilePath = Path.Combine(_temporaryDirectory, "dotnet-tools.json"); _fileSystem.File.WriteAllText(Path.Combine(_temporaryDirectory, _manifestFilePath), _jsonContent); _toolManifestFinder = new ToolManifestFinder(new DirectoryPath(_temporaryDirectory), _fileSystem, new FakeDangerousFileDetector()); _toolManifestEditor = new ToolManifestEditor(_fileSystem, new FakeDangerousFileDetector()); _parseResult = Parser.Instance.Parse($"dotnet tool update {_packageIdA.ToString()}"); _toolRestoreCommand = new ToolRestoreCommand( _parseResult, _toolPackageInstallerMock, _toolManifestFinder, _localToolsResolverCache, _fileSystem, _reporter ); _defaultToolUpdateLocalCommand = new ToolUpdateLocalCommand( _parseResult, _toolPackageInstallerMock, _toolManifestFinder, _toolManifestEditor, _localToolsResolverCache, _reporter); }