Exemple #1
0
        private IProjectContextInfo SetupProject(
            Mock <INuGetProjectManagerService> projectManagerService,
            string packageId,
            string packageVersion,
            string allowedVersions = null)
        {
            var packageIdentity = new PackageIdentity(packageId, NuGetVersion.Parse(packageVersion));

            var installedPackages = new PackageReferenceContextInfo[]
            {
                PackageReferenceContextInfo.Create(
                    new PackageReference(
                        packageIdentity,
                        NuGetFramework.Parse("net45"),
                        userInstalled: true,
                        developmentDependency: false,
                        requireReinstallation: false,
                        allowedVersions: allowedVersions != null ? VersionRange.Parse(allowedVersions) : null))
            };

            string projectId = Guid.NewGuid().ToString();

            projectManagerService.Setup(
                x => x.GetInstalledPackagesAsync(
                    It.Is <IReadOnlyCollection <string> >(projectIds => projectIds.Single() == projectId),
                    It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <IReadOnlyCollection <IPackageReferenceContextInfo> >(installedPackages));

            var project = new Mock <IProjectContextInfo>();

            project.SetupGet(x => x.ProjectId)
            .Returns(projectId);

            return(project.Object);
        }
Exemple #2
0
        public async Task GetInstalledPackagesAsync_WhenProjectReturnsNullPackageReference_NullIsRemoved()
        {
            const string projectName = "a";
            string       projectId   = Guid.NewGuid().ToString();

            using (TestDirectory testDirectory = TestDirectory.Create())
            {
                Initialize();

                string         projectFullPath           = Path.Combine(testDirectory.Path, $"{projectName}.csproj");
                NuGetFramework targetFramework           = NuGetFramework.Parse("net46");
                var            msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(targetFramework, new TestNuGetProjectContext());
                var            project          = new TestMSBuildNuGetProject(msBuildNuGetProjectSystem, testDirectory.Path, projectFullPath, projectId);
                var            packageReference = new PackageReference(
                    new PackageIdentity(id: "b", NuGetVersion.Parse("1.0.0")),
                    targetFramework);
                project.InstalledPackageReferences = Task.FromResult <IEnumerable <PackageReference> >(new[]
                {
                    null,
                    packageReference
                });

                _solutionManager.NuGetProjects.Add(project);

                var telemetrySession = new Mock <ITelemetrySession>();
                var telemetryEvents  = new ConcurrentQueue <TelemetryEvent>();

                telemetrySession
                .Setup(x => x.PostEvent(It.IsAny <TelemetryEvent>()))
                .Callback <TelemetryEvent>(x => telemetryEvents.Enqueue(x));

                TelemetryActivity.NuGetTelemetryService = new NuGetVSTelemetryService(telemetrySession.Object);

                IReadOnlyCollection <IPackageReferenceContextInfo> packages = await _projectManager.GetInstalledPackagesAsync(
                    new[] { projectId },
                    CancellationToken.None);

                Assert.Equal(1, packages.Count);
                IPackageReferenceContextInfo expected = PackageReferenceContextInfo.Create(packageReference);
                IPackageReferenceContextInfo actual   = packages.Single();

                Assert.Equal(expected.AllowedVersions, actual.AllowedVersions);
                Assert.Equal(expected.Framework, actual.Framework);
                Assert.Equal(expected.Identity, actual.Identity);
                Assert.Equal(expected.IsAutoReferenced, actual.IsAutoReferenced);
                Assert.Equal(expected.IsDevelopmentDependency, actual.IsDevelopmentDependency);
                Assert.Equal(expected.IsUserInstalled, actual.IsUserInstalled);

                Assert.Equal(1, telemetryEvents.Count);
            }
        }
Exemple #3
0
        public async ValueTask <IReadOnlyCollection <IPackageReferenceContextInfo> > GetInstalledPackagesAsync(
            IReadOnlyCollection <string> projectIds,
            CancellationToken cancellationToken)
        {
            Assumes.NotNullOrEmpty(projectIds);

            cancellationToken.ThrowIfCancellationRequested();

            IReadOnlyList <NuGetProject> projects = await GetProjectsAsync(projectIds, cancellationToken);

            // Read package references from all projects.
            IEnumerable <Task <IEnumerable <PackageReference> > > tasks = projects.Select(project => project.GetInstalledPackagesAsync(cancellationToken));

            IEnumerable <PackageReference>[] packageReferences = await Task.WhenAll(tasks);

            return(packageReferences.SelectMany(e => e).Select(pr => PackageReferenceContextInfo.Create(pr)).ToArray());
        }
Exemple #4
0
        private IProjectContextInfo SetupProject(string packageId, string packageVersion, string allowedVersions = null)
        {
            var packageIdentity = new PackageIdentity(packageId, NuGetVersion.Parse(packageVersion));

            var installedPackages = new[]
            {
                PackageReferenceContextInfo.Create(
                    new PackageReference(
                        packageIdentity,
                        NuGetFramework.Parse("net45"),
                        userInstalled: true,
                        developmentDependency: false,
                        requireReinstallation: false,
                        allowedVersions: allowedVersions != null ? VersionRange.Parse(allowedVersions) : null))
            };

            var project = Mock.Of <IProjectContextInfo>();

            Mock.Get(project)
            .Setup(x => x.GetInstalledPackagesAsync(It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <IReadOnlyCollection <IPackageReferenceContextInfo> >(installedPackages));
            return(project);
        }
Exemple #5
0
        public async ValueTask <IInstalledAndTransitivePackages> GetInstalledAndTransitivePackagesAsync(
            IReadOnlyCollection <string> projectIds,
            CancellationToken cancellationToken)
        {
            Assumes.NotNullOrEmpty(projectIds);

            cancellationToken.ThrowIfCancellationRequested();

            IReadOnlyList <NuGetProject> projects = await GetProjectsAsync(projectIds, cancellationToken);

            // If this is a PR-style project, get installed and transitive package references. Otherwise, just get installed package references.
            var prStyleTasks    = new List <Task <ProjectPackages> >();
            var nonPrStyleTasks = new List <Task <IEnumerable <PackageReference> > >();

            foreach (NuGetProject?project in projects)
            {
                if (project is IPackageReferenceProject packageReferenceProject)
                {
                    prStyleTasks.Add(packageReferenceProject.GetInstalledAndTransitivePackagesAsync(cancellationToken));
                }
                else
                {
                    nonPrStyleTasks.Add(project.GetInstalledPackagesAsync(cancellationToken));
                }
            }
            ProjectPackages[] prStyleReferences = await Task.WhenAll(prStyleTasks);

            IEnumerable <PackageReference>[] nonPrStyleReferences = await Task.WhenAll(nonPrStyleTasks);

            // combine all of the installed package references
            IEnumerable <IEnumerable <PackageReference> > installedPackages = nonPrStyleReferences
                                                                              .Concat(prStyleReferences
                                                                                      .Select(p => p.InstalledPackages));

            PackageReferenceContextInfo[]           installedPackagesContextInfos = installedPackages.SelectMany(e => e).Select(pr => PackageReferenceContextInfo.Create(pr)).ToArray();
            TransitivePackageReferenceContextInfo[] transitivePackageContextInfos = prStyleReferences.SelectMany(e => e.TransitivePackages).Select(pr => TransitivePackageReferenceContextInfo.Create(pr)).ToArray();
            return(new InstalledAndTransitivePackages(installedPackagesContextInfos, transitivePackageContextInfos));
        }
Exemple #6
0
        public async ValueTask <IReadOnlyCollection <IPackageReferenceContextInfo> > GetInstalledPackagesAsync(
            IReadOnlyCollection <string> projectIds,
            CancellationToken cancellationToken)
        {
            Assumes.NotNullOrEmpty(projectIds);

            cancellationToken.ThrowIfCancellationRequested();

            IReadOnlyList <NuGetProject> projects = await GetProjectsAsync(projectIds, cancellationToken);

            List <Task <IEnumerable <PackageReference> > > tasks = projects
                                                                   .Select(project => project.GetInstalledPackagesAsync(cancellationToken))
                                                                   .ToList();

            IEnumerable <PackageReference>[] results = await Task.WhenAll(tasks);

            var installedPackages = new List <PackageReferenceContextInfo>();
            GetInstalledPackagesAsyncTelemetryEvent?telemetryEvent = null;

            for (var i = 0; i < results.Length; ++i)
            {
                IEnumerable <PackageReference> packageReferences = results[i];
                int totalCount = 0;
                int nullCount  = 0;

                foreach (PackageReference?packageReference in packageReferences)
                {
                    ++totalCount;

                    if (packageReference is null)
                    {
                        ++nullCount;

                        continue;
                    }

                    PackageReferenceContextInfo installedPackage = PackageReferenceContextInfo.Create(packageReference);

                    installedPackages.Add(installedPackage);
                }

                if (nullCount > 0)
                {
                    telemetryEvent ??= new GetInstalledPackagesAsyncTelemetryEvent();

                    NuGetProject project = projects[i];

                    string           projectId   = project.GetMetadata <string>(NuGetProjectMetadataKeys.ProjectId);
                    NuGetProjectType projectType = VSTelemetryServiceUtility.GetProjectType(project);

                    telemetryEvent.AddProject(projectType, projectId, nullCount, totalCount);
                }
            }

            if (telemetryEvent is object)
            {
                TelemetryActivity.EmitTelemetryEvent(telemetryEvent);
            }

            return(installedPackages);
        }