Ejemplo n.º 1
0
        public void Create_IfProjectMetadataIsNull_Throws()
        {
            ArgumentNullException exception = Assert.Throws <ArgumentNullException>(
                () => ProjectMetadataContextInfo.Create(projectMetadata: null));

            Assert.Equal("projectMetadata", exception.ParamName);
        }
        public void Create_IfProjectMetadataIsNull_Throws()
        {
            ArgumentNullException exception = Assert.Throws <ArgumentNullException>(
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
                () => ProjectMetadataContextInfo.Create(projectMetadata: null));

#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.

            Assert.Equal("projectMetadata", exception.ParamName);
        }
        public void Create_IfTargetFrameworkValueIsString_ReturnsNuGetFrameworkValue()
        {
            NuGetFramework expectedFramework = NuGetFramework.Parse("net50");

            var metadata = new Dictionary <string, object?>()
            {
                { NuGetProjectMetadataKeys.TargetFramework, expectedFramework.DotNetFrameworkName }
            };

            ProjectMetadataContextInfo projectMetadata = ProjectMetadataContextInfo.Create(metadata);

            Assert.Equal(expectedFramework, projectMetadata.TargetFramework);
        }
Ejemplo n.º 4
0
        public async ValueTask <IProjectMetadataContextInfo> GetMetadataAsync(string projectId, CancellationToken cancellationToken)
        {
            Assumes.NotNullOrEmpty(projectId);

            cancellationToken.ThrowIfCancellationRequested();

            NuGetProject?project = await SolutionUtility.GetNuGetProjectAsync(
                _sharedState.SolutionManager,
                projectId,
                cancellationToken);

            Assumes.NotNull(project);

            return(ProjectMetadataContextInfo.Create(project.Metadata));
        }
        public void Create_IfProjectMetadataValuesAreNonNullAndValid_ReturnsNonNullPropertyValues()
        {
            var metadata = new Dictionary <string, object?>()
            {
                { NuGetProjectMetadataKeys.FullPath, "a" },
                { NuGetProjectMetadataKeys.Name, "b" },
                { NuGetProjectMetadataKeys.ProjectId, "c" },
                { NuGetProjectMetadataKeys.SupportedFrameworks, new[] { NuGetFramework.Parse("net48"), NuGetFramework.Parse("net50") } },
                { NuGetProjectMetadataKeys.TargetFramework, NuGetFramework.Parse("net50") },
                { NuGetProjectMetadataKeys.UniqueName, "d" }
            };

            ProjectMetadataContextInfo projectMetadata = ProjectMetadataContextInfo.Create(metadata);

            AssertEqual(metadata, projectMetadata);
        }
        public void Create_IfProjectMetadataValuesAreNull_ReturnsNullPropertyValues()
        {
            var metadata = new Dictionary <string, object?>()
            {
                { NuGetProjectMetadataKeys.FullPath, null },
                { NuGetProjectMetadataKeys.Name, null },
                { NuGetProjectMetadataKeys.ProjectId, null },
                { NuGetProjectMetadataKeys.SupportedFrameworks, null },
                { NuGetProjectMetadataKeys.TargetFramework, null },
                { NuGetProjectMetadataKeys.UniqueName, null }
            };

            ProjectMetadataContextInfo projectMetadata = ProjectMetadataContextInfo.Create(metadata);

            AssertAllPropertiesAreNull(projectMetadata);
        }
        public void Create_IfProjectMetadataIsEmpty_DoesNotThrow()
        {
            ProjectMetadataContextInfo projectMetadata = ProjectMetadataContextInfo.Create(new Dictionary <string, object?>());

            AssertAllPropertiesAreNull(projectMetadata);
        }
Ejemplo n.º 8
0
        public async Task CreateInstallAction_OnInstallingProject_EmitsPkgWasTransitiveTelemetryAndTabAndIsSolutionPropertiesAsync(ContractsItemFilter activeTab, bool isSolutionLevel, string packageIdToInstall, bool?expectedPkgWasTransitive)
        {
            // Arrange
            var            telemetrySession   = new Mock <ITelemetrySession>();
            TelemetryEvent lastTelemetryEvent = null;

            telemetrySession
            .Setup(x => x.PostEvent(It.IsAny <TelemetryEvent>()))
            .Callback <TelemetryEvent>(x => lastTelemetryEvent = x);
            var telemetryService = new NuGetVSTelemetryService(telemetrySession.Object);

            TelemetryActivity.NuGetTelemetryService = telemetryService;

            var sourceProvider = new Mock <ISourceRepositoryProvider>();
            var settings       = new Mock <ISettings>();
            var nugetPM        = new NuGetPackageManager(sourceProvider.Object, settings.Object, @"\packagesFolder");
            var lockService    = new NuGetLockService(ThreadHelper.JoinableTaskContext);
            var uiEngine       = new UIActionEngine(sourceProvider.Object, nugetPM, lockService);

            var installedAndTransitive = new InstalledAndTransitivePackages(
                new[] {
                new PackageReferenceContextInfo(new PackageIdentity("installedA", NuGetVersion.Parse("1.0.0")), NuGetFramework.Parse("net472")),
                new PackageReferenceContextInfo(new PackageIdentity("installedB", NuGetVersion.Parse("1.0.0")), NuGetFramework.Parse("net472"))
            },
                new[] {
                new TransitivePackageReferenceContextInfo(new PackageIdentity("transitiveA", NuGetVersion.Parse("1.0.0")), NuGetFramework.Parse("net472"))
            });
            var prjMgrSvc = new Mock <INuGetProjectManagerService>();

            prjMgrSvc
            .Setup(mgr => mgr.GetInstalledAndTransitivePackagesAsync(It.IsAny <IReadOnlyCollection <string> >(), It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <IInstalledAndTransitivePackages>(installedAndTransitive));
            var dictMetadata = new Dictionary <string, object>
            {
                [NuGetProjectMetadataKeys.UniqueName] = "a",
                [NuGetProjectMetadataKeys.ProjectId]  = "a"
            };
            ProjectMetadataContextInfo metadataCtxtInfo = ProjectMetadataContextInfo.Create(dictMetadata);

            prjMgrSvc
            .Setup(mgr => mgr.GetMetadataAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <IProjectMetadataContextInfo>(metadataCtxtInfo));
            var uiService      = new Mock <INuGetUI>();
            var uiContext      = new Mock <INuGetUIContext>();
            var projectContext = new Mock <INuGetProjectContext>();
            var serviceBroker  = new Mock <IServiceBroker>();

            _ = serviceBroker.Setup(sb => sb.GetProxyAsync <INuGetProjectManagerService>(
                                        It.Is <ServiceRpcDescriptor>(s => s == NuGetServices.ProjectManagerService),
                                        It.IsAny <ServiceActivationOptions>(),
                                        It.IsAny <CancellationToken>()))
                .Returns(new ValueTask <INuGetProjectManagerService>(prjMgrSvc.Object));
            uiContext.Setup(ctx => ctx.ServiceBroker).Returns(serviceBroker.Object);
            uiService.Setup(ui => ui.UIContext).Returns(uiContext.Object);
            uiService.Setup(ui => ui.ProjectContext).Returns(projectContext.Object);
            uiService.Setup(ui => ui.Settings).Returns(settings.Object);
            uiService.Setup(ui => ui.Projects).Returns(new[] { new ProjectContextInfo("a", ProjectModel.ProjectStyle.PackageReference, NuGetProjectKind.PackageReference) });

            var action = UserAction.CreateInstallAction(packageIdToInstall, NuGetVersion.Parse("1.0.0"), isSolutionLevel, activeTab);

            // Act
            await uiEngine.PerformInstallOrUninstallAsync(uiService.Object, action, CancellationToken.None);

            // Assert
            Assert.NotNull(lastTelemetryEvent);
            // expect failed action because we mocked just enough objects to emit telemetry
            Assert.Equal(NuGetOperationStatus.Failed, lastTelemetryEvent[nameof(ActionEventBase.Status)]);
            Assert.Equal(NuGetOperationType.Install, lastTelemetryEvent[nameof(ActionsTelemetryEvent.OperationType)]);
            Assert.Equal(isSolutionLevel, lastTelemetryEvent[nameof(VSActionsTelemetryEvent.IsSolutionLevel)]);
            Assert.Equal(activeTab, lastTelemetryEvent[nameof(VSActionsTelemetryEvent.Tab)]);
            Assert.Equal(expectedPkgWasTransitive, lastTelemetryEvent[nameof(VSActionsTelemetryEvent.PackageToInstallWasTransitive)]);
        }