Exemple #1
0
        private ToolRestoreResult InstallPackages(
            ToolManifestPackage package,
            FilePath?configFile)
        {
            string targetFramework = BundledTargetFramework.GetTargetFrameworkMoniker();

            if (PackageHasBeenRestored(package, targetFramework))
            {
                return(ToolRestoreResult.Success(
                           saveToCache: Array.Empty <(RestoredCommandIdentifier, RestoredCommand)>(),
                           message: string.Format(
                               LocalizableStrings.RestoreSuccessful, package.PackageId,
                               package.Version.ToNormalizedString(), string.Join(", ", package.CommandNames))));
            }

            try
            {
                IToolPackage toolPackage =
                    _toolPackageInstaller.InstallPackageToExternalManagedLocation(
                        new PackageLocation(
                            nugetConfig: configFile,
                            additionalFeeds: _sources,
                            rootConfigDirectory: package.FirstEffectDirectory),
                        package.PackageId, ToVersionRangeWithOnlyOneVersion(package.Version), targetFramework,
                        verbosity: _verbosity);

                if (!ManifestCommandMatchesActualInPackage(package.CommandNames, toolPackage.Commands))
                {
                    return(ToolRestoreResult.Failure(
                               string.Format(LocalizableStrings.CommandsMismatch,
                                             JoinBySpaceWithQuote(package.CommandNames.Select(c => c.Value.ToString())),
                                             package.PackageId,
                                             JoinBySpaceWithQuote(toolPackage.Commands.Select(c => c.Name.ToString())))));
                }

                return(ToolRestoreResult.Success(
                           saveToCache: toolPackage.Commands.Select(command => (
                                                                        new RestoredCommandIdentifier(
                                                                            toolPackage.Id,
                                                                            toolPackage.Version,
                                                                            NuGetFramework.Parse(targetFramework),
                                                                            Constants.AnyRid,
                                                                            command.Name),
                                                                        command)).ToArray(),
                           message: string.Format(
                               LocalizableStrings.RestoreSuccessful,
                               package.PackageId,
                               package.Version.ToNormalizedString(),
                               string.Join(" ", package.CommandNames))));
            }
            catch (ToolPackageException e)
            {
                return(ToolRestoreResult.Failure(package.PackageId, e));
            }
        }
        public async Task RestoreResult_WritesToolCommitToDebug()
        {
            // Arrange
            using (var td = TestFileSystemUtility.CreateRandomTestFolder())
            {
                var path       = Path.Combine(td, ".tools", "project.lock.json");
                var logger     = new TestLogger();
                var toolResult = new ToolRestoreResult(
                    toolName: null,
                    success: true,
                    lockFileTarget: null,
                    fileTargetLibrary: null,
                    lockFilePath: path,
                    lockFile: new LockFile(),
                    previousLockFile: null);
                var result = new RestoreResult(
                    success: true,
                    restoreGraphs: null,
                    compatibilityCheckResults: null,
                    lockFile: new LockFile(),
                    previousLockFile: new LockFile(), // same lock file
                    lockFilePath: null,
                    msbuild: new MSBuildRestoreResult("project", td, true),
                    toolRestoreResults: new[] { toolResult });

                // Act
                await result.CommitAsync(logger, CancellationToken.None);

                // Assert
                Assert.Contains(
                    $"Writing tool lock file to disk. Path: {path}",
                    logger.DebugMessages);
                Assert.True(File.Exists(path), $"The tool lock file should have been written: {path}");
                Assert.Equal(1, logger.DebugMessages.Count);
            }
        }