public void VSRestoreSettingsUtilities_FallbackFolder(string[] fallbackFolders, string[] expectedFallbackFolders) { using (var mockBaseDirectory = TestDirectory.Create()) { //Set Up var spec = new PackageSpec { RestoreMetadata = new ProjectRestoreMetadata { ProjectPath = @"C:\project\projectPath.csproj", FallbackFolders = fallbackFolders.ToList() } }; var settings = new Settings(mockBaseDirectory); settings.AddOrUpdate(ConfigurationConstants.FallbackPackageFolders, new AddItem("defaultFallback", @"C:\defaultFallback")); //Act var actualFallbackFolders = VSRestoreSettingsUtilities.GetFallbackFolders(settings, spec); //Assert Assert.True( Enumerable.SequenceEqual(expectedFallbackFolders.OrderBy(t => t), actualFallbackFolders.OrderBy(t => t)), "expected: " + string.Join(",", expectedFallbackFolders.ToArray()) + "\nactual: " + string.Join(",", actualFallbackFolders.ToArray())); } }
private async Task <FallbackPackagePathResolver> GetPackagesPathResolverAsync(BuildIntegratedNuGetProject project) { // To get packagesPath for build integrated projects, first read the packageSpec to know if // RestorePackagesPath property was specified. If yes, then use that property to get packages path // otherwise use global user cache folder from _settings. var context = new DependencyGraphCacheContext(); var packageSpecs = await project.GetPackageSpecsAsync(context); var packageSpec = packageSpecs.Single(e => e.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference || e.RestoreMetadata.ProjectStyle == ProjectStyle.ProjectJson); var packagesPath = VSRestoreSettingsUtilities.GetPackagesPath(_settings, packageSpec); return(new FallbackPackagePathResolver(packagesPath, VSRestoreSettingsUtilities.GetFallbackFolders(_settings, packageSpec))); }
public async Task VSSolutionRestoreService_VSRestoreSettingsUtilities_Integration(string restoreSources, string restoreAdditionalProjectSources, string expectedRestoreSources, string restoreFallbackFolders, string restoreAdditionalFallbackFolders, string expectedFallbackFolders) { var cps = NewCpsProject(@"{ }"); var pri = cps.Builder .WithTool("Foo.Test", "2.0.0") .WithTargetFrameworkInfo( new VsTargetFrameworkInfo( "netcoreapp2.0", Enumerable.Empty <IVsReferenceItem>(), Enumerable.Empty <IVsReferenceItem>(), new[] { new VsProjectProperty("RestoreSources", restoreSources), new VsProjectProperty("RestoreFallbackFolders", restoreFallbackFolders), new VsProjectProperty("RestoreAdditionalProjectSources", restoreAdditionalProjectSources), new VsProjectProperty("RestoreAdditionalProjectFallbackFolders", restoreAdditionalFallbackFolders) })) .Build(); var projectFullPath = cps.ProjectFullPath; // Act var actualRestoreSpec = await CaptureNominateResultAsync(projectFullPath, pri); // Assert SpecValidationUtility.ValidateDependencySpec(actualRestoreSpec); var actualProjectSpec = actualRestoreSpec.GetProjectSpec(projectFullPath); Assert.NotNull(actualProjectSpec); var specSources = VSRestoreSettingsUtilities.GetSources(NullSettings.Instance, actualProjectSpec).Select(e => e.Source); var expectedSources = MSBuildStringUtility.Split(expectedRestoreSources); Assert.True(Enumerable.SequenceEqual(expectedSources.OrderBy(t => t), specSources.OrderBy(t => t))); var specFallback = VSRestoreSettingsUtilities.GetFallbackFolders(NullSettings.Instance, actualProjectSpec); var expectedFallback = MSBuildStringUtility.Split(expectedFallbackFolders); Assert.True( Enumerable.SequenceEqual(expectedFallback.OrderBy(t => t), specFallback.OrderBy(t => t)), "expected: " + string.Join(",", expectedFallback.ToArray()) + "\nactual: " + string.Join(",", specFallback.ToArray())); }
private async Task <(InstalledPackageResultStatus, IReadOnlyCollection <NuGetInstalledPackage>)> GetInstalledPackagesAsync(BuildIntegratedNuGetProject project, CancellationToken cancellationToken) { NuGetInstalledPackage ToNuGetInstalledPackage(PackageReference packageReference, FallbackPackagePathResolver pathResolver) { var id = packageReference.PackageIdentity.Id; var versionRange = packageReference.AllowedVersions; string requestedRange = packageReference.AllowedVersions.OriginalString // most packages ?? packageReference.AllowedVersions.ToShortString(); string version = versionRange.MinVersion.OriginalVersion ?? versionRange.MinVersion.ToNormalizedString(); var installPath = pathResolver.GetPackageDirectory(id, version); bool directDependency = true; return(NuGetContractsFactory.CreateNuGetInstalledPackage(id, requestedRange, version, installPath, directDependency)); } InstalledPackageResultStatus status; IReadOnlyCollection <NuGetInstalledPackage> installedPackages; var cacheContext = new DependencyGraphCacheContext(); var(packageSpecs, messages) = await project.GetPackageSpecsAndAdditionalMessagesAsync(cacheContext); if (messages?.Any(m => m.Level == LogLevel.Error) == true) { // Although we know that the project will fail to restore, we may still know about some direct dependencies, so let's return the packages that we know about. status = InstalledPackageResultStatus.ProjectInvalid; } else { status = InstalledPackageResultStatus.Successful; } var packageSpec = packageSpecs.Single(s => s.RestoreMetadata.ProjectStyle == ProjectModel.ProjectStyle.PackageReference || s.RestoreMetadata.ProjectStyle == ProjectModel.ProjectStyle.ProjectJson); var packagesPath = VSRestoreSettingsUtilities.GetPackagesPath(_settings, packageSpec); FallbackPackagePathResolver pathResolver = new FallbackPackagePathResolver(packagesPath, VSRestoreSettingsUtilities.GetFallbackFolders(_settings, packageSpec)); var packageReferences = await project.GetInstalledPackagesAsync(cancellationToken); installedPackages = packageReferences.Select(p => ToNuGetInstalledPackage(p, pathResolver)) .ToList(); return(status, installedPackages); }
private async Task <(InstalledPackageResultStatus, IReadOnlyCollection <NuGetInstalledPackage>)> GetInstalledPackagesAsync(BuildIntegratedNuGetProject project, CancellationToken cancellationToken) { NuGetInstalledPackage ToNuGetInstalledPackage(PackageReference packageReference, FallbackPackagePathResolver pathResolver, bool directDependency) { var id = packageReference.PackageIdentity.Id; string requestedRange = null; if (directDependency) { requestedRange = packageReference.AllowedVersions?.OriginalString // When Version is specified ?? packageReference.AllowedVersions?.ToShortString(); // Probably only when Version is not specified in msbuild } string version = packageReference.PackageIdentity.Version?.ToNormalizedString() ?? string.Empty; var installPath = version != null ? pathResolver.GetPackageDirectory(id, version) : null; return(NuGetContractsFactory.CreateNuGetInstalledPackage(id, requestedRange, version, installPath, directDependency)); } InstalledPackageResultStatus status; List <NuGetInstalledPackage> installedPackages; (InstalledPackageResultStatus, IReadOnlyCollection <NuGetInstalledPackage>) ErrorResult(InstalledPackageResultStatus status) { return(status, null); } var cacheContext = new DependencyGraphCacheContext(); IReadOnlyList <ProjectModel.PackageSpec> packageSpecs; IReadOnlyList <ProjectModel.IAssetsLogMessage> messages; try { (packageSpecs, messages) = await project.GetPackageSpecsAndAdditionalMessagesAsync(cacheContext); } catch (ProjectNotNominatedException) { return(ErrorResult(InstalledPackageResultStatus.ProjectNotReady)); } catch (InvalidDataException) { return(ErrorResult(InstalledPackageResultStatus.ProjectInvalid)); } if (messages?.Any(m => m.Level == LogLevel.Error) == true) { // Although we know that the project will fail to restore, we may still know about some direct dependencies, so let's return the packages that we know about. status = InstalledPackageResultStatus.ProjectInvalid; } else { status = InstalledPackageResultStatus.Successful; } var packageSpec = packageSpecs.Single(s => s.RestoreMetadata.ProjectStyle == ProjectModel.ProjectStyle.PackageReference || s.RestoreMetadata.ProjectStyle == ProjectModel.ProjectStyle.ProjectJson); var packagesPath = VSRestoreSettingsUtilities.GetPackagesPath(_settings, packageSpec); FallbackPackagePathResolver pathResolver = new FallbackPackagePathResolver(packagesPath, VSRestoreSettingsUtilities.GetFallbackFolders(_settings, packageSpec)); IReadOnlyCollection <PackageReference> directPackages; IReadOnlyCollection <PackageReference> transitivePackages; if (project is IPackageReferenceProject packageReferenceProject) { var installed = await packageReferenceProject.GetInstalledAndTransitivePackagesAsync(cancellationToken); directPackages = installed.InstalledPackages; transitivePackages = installed.TransitivePackages; } else { directPackages = (await project.GetInstalledPackagesAsync(cancellationToken)).ToList(); transitivePackages = Array.Empty <PackageReference>(); } installedPackages = new List <NuGetInstalledPackage>(directPackages.Count + (transitivePackages?.Count ?? 0)); installedPackages.AddRange(directPackages.Select(p => ToNuGetInstalledPackage(p, pathResolver, directDependency: true))); if (transitivePackages != null) { installedPackages.AddRange(transitivePackages.Select(p => ToNuGetInstalledPackage(p, pathResolver, directDependency: false))); } return(status, installedPackages); }