public async Task GetPackageSpecsAsync_WithPackageReference_Succeeds()
        {
            // Arrange
            using (var randomTestFolder = TestDirectory.Create())
            {
                var framework = NuGetFramework.Parse("netstandard13");

                var projectAdapter = CreateProjectAdapter(randomTestFolder);

                var projectServices = new TestProjectSystemServices();
                projectServices.SetupInstalledPackages(
                    framework,
                    new LibraryDependency
                {
                    LibraryRange = new LibraryRange(
                        "packageA",
                        VersionRange.Parse("1.*"),
                        LibraryDependencyTarget.Package)
                });

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var testDependencyGraphCacheContext = new DependencyGraphCacheContext();

                await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Act
                var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext);

                // Assert
                Assert.NotNull(packageSpecs);

                var actualRestoreSpec = packageSpecs.Single();
                SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);
                //No top level dependencies
                Assert.Equal(0, actualRestoreSpec.Dependencies.Count);

                var actualDependency = actualRestoreSpec.TargetFrameworks.SingleOrDefault().Dependencies.Single();
                Assert.NotNull(actualDependency);
                Assert.Equal("packageA", actualDependency.LibraryRange.Name);
                Assert.Equal(VersionRange.Parse("1.*"), actualDependency.LibraryRange.VersionRange);

                // Verify
                Mock.Get(projectServices.ReferencesReader)
                .Verify(
                    x => x.GetPackageReferencesAsync(framework, CancellationToken.None),
                    Times.AtLeastOnce);
            }
        }
        public async Task GetPackageSpecsAsync_ReadLockFileSettings(
            string restorePackagesWithLockFile,
            string lockFilePath,
            bool restoreLockedMode)
        {
            // Arrange
            using (var testDirectory = TestDirectory.Create())
            {
                var projectAdapter = CreateProjectAdapter(testDirectory);
                Mock.Get(projectAdapter)
                .Setup(x => x.GetRestorePackagesWithLockFileAsync())
                .ReturnsAsync(restorePackagesWithLockFile);

                Mock.Get(projectAdapter)
                .Setup(x => x.GetNuGetLockFilePathAsync())
                .ReturnsAsync(lockFilePath);

                Mock.Get(projectAdapter)
                .Setup(x => x.IsRestoreLockedAsync())
                .ReturnsAsync(restoreLockedMode);

                var projectServices = new TestProjectSystemServices();

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var settings = NullSettings.Instance;
                var testDependencyGraphCacheContext = new DependencyGraphCacheContext(NullLogger.Instance, settings);

                await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Act
                var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext);

                // Assert
                Assert.NotNull(packageSpecs);
                var actualRestoreSpec = packageSpecs.Single();
                SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);

                // Assert restorePackagesWithLockFile
                Assert.Equal(restorePackagesWithLockFile, actualRestoreSpec.RestoreMetadata.RestoreLockProperties.RestorePackagesWithLockFile);

                // assert lockFilePath
                Assert.Equal(lockFilePath, actualRestoreSpec.RestoreMetadata.RestoreLockProperties.NuGetLockFilePath);

                // assert restoreLockedMode
                Assert.Equal(restoreLockedMode, actualRestoreSpec.RestoreMetadata.RestoreLockProperties.RestoreLockedMode);
            }
        }
Example #3
0
        private NetCorePackageReferenceProject CreateNetCorePackageReferenceProject(string projectName, string projectFullPath, ProjectSystemCache projectSystemCache)
        {
            var projectServices = new TestProjectSystemServices();

            return(new NetCorePackageReferenceProject(
                       projectName: projectName,
                       projectUniqueName: projectName,
                       projectFullPath: projectFullPath,
                       projectSystemCache: projectSystemCache,
                       unconfiguredProject: null,
                       projectServices: projectServices,
                       projectId: projectName));
        }
        public async Task InstallPackageAsync_AddsPackageReference()
        {
            // Arrange
            using (var randomTestFolder = TestDirectory.Create())
            {
                var projectAdapter = CreateProjectAdapter(randomTestFolder);

                var projectServices = new TestProjectSystemServices();

                LibraryDependency actualDependency = null;
                Mock.Get(projectServices.References)
                .Setup(x => x.AddOrUpdatePackageReferenceAsync(
                           It.IsAny <LibraryDependency>(), CancellationToken.None))
                .Callback <LibraryDependency, CancellationToken>((d, _) => actualDependency = d)
                .Returns(Task.CompletedTask);

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var buildIntegratedInstallationContext = new BuildIntegratedInstallationContext(
                    Enumerable.Empty <NuGetFramework>(),
                    Enumerable.Empty <NuGetFramework>(),
                    new Dictionary <NuGetFramework, string>());

                // Act
                var result = await testProject.InstallPackageAsync(
                    "packageA",
                    VersionRange.Parse("1.*"),
                    null,
                    buildIntegratedInstallationContext,
                    CancellationToken.None);

                // Assert
                Assert.True(result);

                Assert.NotNull(actualDependency);
                Assert.Equal("packageA", actualDependency.LibraryRange.Name);
                Assert.Equal(VersionRange.Parse("1.*"), actualDependency.LibraryRange.VersionRange);

                // Verify
                Mock.Get(projectServices.References)
                .Verify(
                    x => x.AddOrUpdatePackageReferenceAsync(It.IsAny <LibraryDependency>(), CancellationToken.None),
                    Times.Once);
            }
        }
        public async Task GetPackageSpecsAsync_WithProjectReference_Succeeds()
        {
            // Arrange
            using (var randomTestFolder = TestDirectory.Create())
            {
                var framework = NuGetFramework.Parse("netstandard13");

                var projectAdapter = CreateProjectAdapter(randomTestFolder);

                var projectServices = new TestProjectSystemServices();
                projectServices.SetupProjectDependencies(
                    new ProjectRestoreReference
                {
                    ProjectUniqueName = "TestProjectA",
                    ProjectPath       = Path.Combine(randomTestFolder, "TestProjectA")
                });

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var testDependencyGraphCacheContext = new DependencyGraphCacheContext();

                await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Act
                var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext);

                // Assert
                Assert.NotNull(packageSpecs);

                var actualRestoreSpec = packageSpecs.Single();
                SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);

                var actualDependency = actualRestoreSpec.RestoreMetadata.TargetFrameworks.Single().ProjectReferences.Single();
                Assert.NotNull(actualDependency);
                Assert.Equal("TestProjectA", actualDependency.ProjectUniqueName);

                // Verify
                Mock.Get(projectServices.ReferencesReader)
                .Verify(
                    x => x.GetProjectReferencesAsync(It.IsAny <Common.ILogger>(), CancellationToken.None),
                    Times.AtLeastOnce);
            }
        }
        public async Task GetPackageSpecsAsync_WithDefaultVersion_Succeeds()
        {
            // Arrange
            using (var testDirectory = TestDirectory.Create())
            {
                var projectAdapter  = CreateProjectAdapter(testDirectory);
                var projectServices = new TestProjectSystemServices();

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var testDependencyGraphCacheContext = new DependencyGraphCacheContext();

                await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Act
                var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext);

                // Assert
                Assert.NotNull(packageSpecs);

                var actualRestoreSpec = packageSpecs.Single();
                SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);

                Assert.Equal("1.0.0", actualRestoreSpec.Version.ToString());

                // Verify
                Mock.Get(projectAdapter)
                .VerifyGet(x => x.Version, Times.AtLeastOnce);
                Mock.Get(projectAdapter)
                .VerifyGet(x => x.ProjectName, Times.AtLeastOnce);
                Mock.Get(projectAdapter)
                .Verify(x => x.GetRuntimeIdentifiersAsync(), Times.AtLeastOnce);
                Mock.Get(projectAdapter)
                .Verify(x => x.GetRuntimeSupportsAsync(), Times.AtLeastOnce);
                Mock.Get(projectAdapter)
                .VerifyGet(x => x.FullProjectPath, Times.AtLeastOnce);
                Mock.Get(projectAdapter)
                .Verify(x => x.GetTargetFrameworkAsync(), Times.AtLeastOnce);
            }
        }
        public async Task GetInstalledPackagesAsync_WhenValid_ReturnsPackageReferences()
        {
            // Arrange
            using (var randomTestFolder = TestDirectory.Create())
            {
                var framework = NuGetFramework.Parse("netstandard13");

                var projectAdapter = CreateProjectAdapter(randomTestFolder);

                var projectServices = new TestProjectSystemServices();
                projectServices.SetupInstalledPackages(
                    framework,
                    new LibraryDependency
                {
                    LibraryRange = new LibraryRange(
                        "packageA",
                        VersionRange.Parse("1.*"),
                        LibraryDependencyTarget.Package)
                });

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                // Act
                var packageReferences = await testProject.GetInstalledPackagesAsync(CancellationToken.None);

                // Assert
                var packageReference = packageReferences.Single();
                Assert.NotNull(packageReference);
                Assert.Equal(
                    "packageA.1.0.0",
                    packageReference.PackageIdentity.ToString());

                // Verify
                Mock.Get(projectServices.ReferencesReader)
                .Verify(
                    x => x.GetPackageReferencesAsync(framework, CancellationToken.None),
                    Times.AtLeastOnce);
            }
        }
Example #8
0
        internal static LegacyPackageReferenceProject CreateLegacyPackageReferenceProject(TestDirectory testDirectory, string projectId, IVsProjectThreadingService threadingService, LibraryDependency[] pkgDependencies)
        {
            var framework = NuGetFramework.Parse("netstandard13");
            IVsProjectAdapter projectAdapter = CreateProjectAdapter(testDirectory);

            var projectServices = new TestProjectSystemServices();

            projectServices.SetupInstalledPackages(
                framework,
                pkgDependencies);

            var testProject = new LegacyPackageReferenceProject(
                projectAdapter,
                projectId,
                projectServices,
                threadingService);

            return(testProject);
        }
        public async Task GetPackageSpecsAsync_SkipContentFilesAlwaysTrue()
        {
            // Arrange
            using (var randomTestFolder = TestDirectory.Create())
            {
                var framework = NuGetFramework.Parse("netstandard13");

                var projectAdapter = CreateProjectAdapter(randomTestFolder);

                var projectServices = new TestProjectSystemServices();
                projectServices.SetupInstalledPackages(
                    framework,
                    new LibraryDependency
                {
                    LibraryRange = new LibraryRange(
                        "packageA",
                        VersionRange.Parse("1.*"),
                        LibraryDependencyTarget.Package)
                });

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var testDependencyGraphCacheContext = new DependencyGraphCacheContext();

                await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Act
                var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext);

                // Assert
                Assert.NotNull(packageSpecs);

                var actualRestoreSpec = packageSpecs.Single();
                SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);

                Assert.True(actualRestoreSpec.RestoreMetadata.SkipContentFileWrite);
            }
        }
Example #10
0
        public async Task GetPackageSpecsAsync_WithNoFallbackFrameworks_Succeeds()
        {
            // Arrange
            using (var randomTestFolder = TestDirectory.Create())
            {
                var framework = NuGetFramework.Parse("netcoreapp2.0");

                var projectAdapter = CreateProjectAdapter(randomTestFolder);

                Mock.Get(projectAdapter)
                .SetupGet(x => x.PackageTargetFallback)
                .Returns("");

                Mock.Get(projectAdapter)
                .SetupGet(x => x.AssetTargetFallback)
                .Returns("");

                var projectServices = new TestProjectSystemServices();

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var testDependencyGraphCacheContext = new DependencyGraphCacheContext();

                await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Act
                var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext);

                // Assert
                Assert.NotNull(packageSpecs);

                var actualRestoreSpec = packageSpecs.Single();

                actualRestoreSpec.TargetFrameworks[0].Imports.Should().BeEmpty();
                actualRestoreSpec.TargetFrameworks[0].AssetTargetFallback.Should().BeEmpty();
            }
        }
        public async Task UninstallPackageAsync_Always_RemovesPackageReference()
        {
            // Arrange
            using (var randomTestFolder = TestDirectory.Create())
            {
                var projectAdapter = CreateProjectAdapter(randomTestFolder);

                var projectServices = new TestProjectSystemServices();

                string actualPackageId = null;
                Mock.Get(projectServices.References)
                .Setup(x => x.RemovePackageReferenceAsync(It.IsAny <string>()))
                .Callback <string>(p => actualPackageId = p)
                .Returns(Task.CompletedTask);

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                // Act
                var result = await testProject.UninstallPackageAsync(
                    new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")),
                    null,
                    CancellationToken.None);

                // Assert
                Assert.True(result);
                Assert.Equal("packageA", actualPackageId);

                // Verify
                Mock.Get(projectServices.References)
                .Verify(
                    x => x.RemovePackageReferenceAsync(It.IsAny <string>()),
                    Times.Once);
            }
        }
        public async void DependencyGraphRestoreUtility_LegacyPackageRef_Restore_PackageShaValidationFailed()
        {
            using (var packageSource = TestDirectory.Create())
            {
                // Arrange
                var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(
                    new List <Configuration.PackageSource>()
                {
                    new Configuration.PackageSource(packageSource.Path)
                });

                using (var testSolutionManager = new TestSolutionManager(true))
                    using (var randomProjectFolderPath = TestDirectory.Create())
                    {
                        var testSettings            = PopulateSettingsWithSources(sourceRepositoryProvider, randomProjectFolderPath);
                        var testNuGetProjectContext = new TestNuGetProjectContext();
                        var deleteOnRestartManager  = new TestDeleteOnRestartManager();
                        var nuGetPackageManager     = new NuGetPackageManager(
                            sourceRepositoryProvider,
                            testSettings,
                            testSolutionManager,
                            deleteOnRestartManager);

                        var projectTargetFrameworkStr = "net45";
                        var fullProjectPath           = Path.Combine(randomProjectFolderPath, "project1.csproj");
                        var projectNames = new ProjectNames(
                            fullName: fullProjectPath,
                            uniqueName: Path.GetFileName(fullProjectPath),
                            shortName: Path.GetFileNameWithoutExtension(fullProjectPath),
                            customUniqueName: Path.GetFileName(fullProjectPath));
                        var vsProjectAdapter = new TestVSProjectAdapter(
                            fullProjectPath,
                            projectNames,
                            projectTargetFrameworkStr);

                        var projectServices = new TestProjectSystemServices();
                        projectServices.SetupInstalledPackages(
                            NuGetFramework.Parse(projectTargetFrameworkStr),
                            new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(
                                "packageA",
                                VersionRange.Parse("1.*"),
                                LibraryDependencyTarget.Package)
                        });

                        var legacyPRProject = new LegacyPackageReferenceProject(
                            vsProjectAdapter,
                            Guid.NewGuid().ToString(),
                            projectServices,
                            _threadingService);
                        testSolutionManager.NuGetProjects.Add(legacyPRProject);

                        var testLogger     = new TestLogger();
                        var restoreContext = new DependencyGraphCacheContext(testLogger, testSettings);

                        var packageContext = new SimpleTestPackageContext("packageA", "1.0.0");
                        packageContext.AddFile("lib/net45/a.dll");
                        SimpleTestPackageUtility.CreateOPCPackage(packageContext, packageSource);

                        var dgSpec = await DependencyGraphRestoreUtility.GetSolutionRestoreSpec(testSolutionManager, restoreContext);

                        var projectLockFilePath = Path.Combine(randomProjectFolderPath, "packages.project1.lock.json");
                        File.Create(projectLockFilePath).Close();

                        var restoreSummaries = await DependencyGraphRestoreUtility.RestoreAsync(
                            testSolutionManager,
                            dgSpec,
                            restoreContext,
                            new RestoreCommandProvidersCache(),
                            (c) => { },
                            sourceRepositoryProvider.GetRepositories(),
                            Guid.Empty,
                            false,
                            true,
                            testLogger,
                            CancellationToken.None);

                        foreach (var restoreSummary in restoreSummaries)
                        {
                            Assert.True(restoreSummary.Success);
                            Assert.False(restoreSummary.NoOpRestore);
                        }

                        Assert.True(File.Exists(projectLockFilePath));

                        // delete existing restore output files
                        File.Delete(Path.Combine(vsProjectAdapter.MSBuildProjectExtensionsPath, "project.assets.json"));
                        File.Delete(Path.Combine(vsProjectAdapter.MSBuildProjectExtensionsPath, "project1.csproj.nuget.cache"));

                        // clean packages folder
                        var packagesFolder = Path.Combine(randomProjectFolderPath, "packages");
                        Directory.Delete(packagesFolder, true);
                        Directory.CreateDirectory(packagesFolder);

                        // add a new package
                        var newPackageContext = new SimpleTestPackageContext("packageA", "1.0.0");
                        newPackageContext.AddFile("lib/net45/a1.dll");
                        SimpleTestPackageUtility.CreateOPCPackage(newPackageContext, packageSource);

                        // Act
                        restoreSummaries = await DependencyGraphRestoreUtility.RestoreAsync(
                            testSolutionManager,
                            dgSpec,
                            restoreContext,
                            new RestoreCommandProvidersCache(),
                            (c) => { },
                            sourceRepositoryProvider.GetRepositories(),
                            Guid.Empty,
                            false,
                            true,
                            testLogger,
                            CancellationToken.None);

                        // Assert
                        foreach (var restoreSummary in restoreSummaries)
                        {
                            Assert.False(restoreSummary.Success);
                            Assert.True(restoreSummary.Errors.Count > 0);
                            Assert.NotNull(restoreSummary.Errors.FirstOrDefault(message => (message as RestoreLogMessage).Code == NuGetLogCode.NU1403));
                        }
                    }
            }
        }
        public async void DependencyGraphRestoreUtility_LegacyPackageRef_Restore_LockedMode()
        {
            using (var packageSource = TestDirectory.Create())
            {
                // Arrange
                var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(
                    new List <Configuration.PackageSource>()
                {
                    new Configuration.PackageSource(packageSource.Path)
                });

                using (var testSolutionManager = new TestSolutionManager(true))
                    using (var randomProjectFolderPath = TestDirectory.Create())
                    {
                        var testSettings            = PopulateSettingsWithSources(sourceRepositoryProvider, randomProjectFolderPath);
                        var testNuGetProjectContext = new TestNuGetProjectContext();
                        var deleteOnRestartManager  = new TestDeleteOnRestartManager();
                        var nuGetPackageManager     = new NuGetPackageManager(
                            sourceRepositoryProvider,
                            testSettings,
                            testSolutionManager,
                            deleteOnRestartManager);

                        var projectTargetFrameworkStr = "net45";
                        var fullProjectPath           = Path.Combine(randomProjectFolderPath, "project1.csproj");

                        var projectNames = new ProjectNames(
                            fullName: fullProjectPath,
                            uniqueName: Path.GetFileName(fullProjectPath),
                            shortName: Path.GetFileNameWithoutExtension(fullProjectPath),
                            customUniqueName: Path.GetFileName(fullProjectPath));
                        var vsProjectAdapter = new TestVSProjectAdapter(
                            fullProjectPath,
                            projectNames,
                            projectTargetFrameworkStr,
                            restorePackagesWithLockFile: "true",
                            restoreLockedMode: true);

                        var projectServices = new TestProjectSystemServices();
                        projectServices.SetupInstalledPackages(
                            NuGetFramework.Parse(projectTargetFrameworkStr),
                            new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(
                                "packageA",
                                VersionRange.Parse("1.*"),
                                LibraryDependencyTarget.Package)
                        });

                        var legacyPRProject = new LegacyPackageReferenceProject(
                            vsProjectAdapter,
                            Guid.NewGuid().ToString(),
                            projectServices,
                            _threadingService);
                        testSolutionManager.NuGetProjects.Add(legacyPRProject);

                        var testLogger     = new TestLogger();
                        var restoreContext = new DependencyGraphCacheContext(testLogger, testSettings);

                        var packageContextA = new SimpleTestPackageContext("packageA", "1.0.0");
                        packageContextA.AddFile("lib/net45/a.dll");
                        SimpleTestPackageUtility.CreateOPCPackage(packageContextA, packageSource);
                        var packageContextB = new SimpleTestPackageContext("packageB", "1.0.0");
                        packageContextB.AddFile("lib/net45/b.dll");
                        SimpleTestPackageUtility.CreateOPCPackage(packageContextB, packageSource);

                        var dgSpec = await DependencyGraphRestoreUtility.GetSolutionRestoreSpec(testSolutionManager, restoreContext);

                        var restoreSummaries = await DependencyGraphRestoreUtility.RestoreAsync(
                            testSolutionManager,
                            dgSpec,
                            restoreContext,
                            new RestoreCommandProvidersCache(),
                            (c) => { },
                            sourceRepositoryProvider.GetRepositories(),
                            Guid.Empty,
                            false,
                            true,
                            testLogger,
                            CancellationToken.None);

                        foreach (var restoreSummary in restoreSummaries)
                        {
                            Assert.True(restoreSummary.Success);
                            Assert.False(restoreSummary.NoOpRestore);
                        }

                        Assert.True(File.Exists(Path.Combine(randomProjectFolderPath, "packages.lock.json")));

                        // install a new package
                        projectServices.SetupInstalledPackages(
                            NuGetFramework.Parse(projectTargetFrameworkStr),
                            new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(
                                "packageA",
                                VersionRange.Parse("1.*"),
                                LibraryDependencyTarget.Package)
                        },
                            new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(
                                "packageB",
                                VersionRange.Parse("1.0.0"),
                                LibraryDependencyTarget.Package)
                        });

                        // update the proeject with new ProjectService instance
                        restoreContext.PackageSpecCache.Clear();
                        dgSpec = await DependencyGraphRestoreUtility.GetSolutionRestoreSpec(testSolutionManager, restoreContext);

                        // Act
                        restoreSummaries = await DependencyGraphRestoreUtility.RestoreAsync(
                            testSolutionManager,
                            dgSpec,
                            restoreContext,
                            new RestoreCommandProvidersCache(),
                            (c) => { },
                            sourceRepositoryProvider.GetRepositories(),
                            Guid.Empty,
                            false,
                            true,
                            testLogger,
                            CancellationToken.None);

                        // Assert
                        foreach (var restoreSummary in restoreSummaries)
                        {
                            Assert.False(restoreSummary.Success);
                        }
                    }
            }
        }
        public async void DependencyGraphRestoreUtility_LegacyPackageRef_Restore_GenerateLockFile()
        {
            using (var packageSource = TestDirectory.Create())
            {
                // Arrange
                var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(
                    new List <Configuration.PackageSource>()
                {
                    new Configuration.PackageSource(packageSource.Path)
                });

                using (var testSolutionManager = new TestSolutionManager(true))
                    using (var randomProjectFolderPath = TestDirectory.Create())
                    {
                        var testSettings            = PopulateSettingsWithSources(sourceRepositoryProvider, randomProjectFolderPath);
                        var testNuGetProjectContext = new TestNuGetProjectContext();
                        var deleteOnRestartManager  = new TestDeleteOnRestartManager();
                        var nuGetPackageManager     = new NuGetPackageManager(
                            sourceRepositoryProvider,
                            testSettings,
                            testSolutionManager,
                            deleteOnRestartManager);

                        // set up projects
                        var projectTargetFrameworkStr = "net45";
                        var fullProjectPathB          = Path.Combine(randomProjectFolderPath, "ProjectB", "project2.csproj");
                        var projectNamesB             = new ProjectNames(
                            fullName: fullProjectPathB,
                            uniqueName: Path.GetFileName(fullProjectPathB),
                            shortName: Path.GetFileNameWithoutExtension(fullProjectPathB),
                            customUniqueName: Path.GetFileName(fullProjectPathB));
                        var vsProjectAdapterB = new TestVSProjectAdapter(
                            fullProjectPathB,
                            projectNamesB,
                            projectTargetFrameworkStr);

                        var projectServicesB = new TestProjectSystemServices();
                        projectServicesB.SetupInstalledPackages(
                            NuGetFramework.Parse(projectTargetFrameworkStr),
                            new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(
                                "packageB",
                                VersionRange.Parse("1.*"),
                                LibraryDependencyTarget.Package)
                        });

                        var legacyPRProjectB = new LegacyPackageReferenceProject(
                            vsProjectAdapterB,
                            Guid.NewGuid().ToString(),
                            projectServicesB,
                            _threadingService);

                        var projectPathA     = Path.Combine(randomProjectFolderPath, "ProjectA");
                        var fullProjectPathA = Path.Combine(projectPathA, "project1.csproj");
                        var projectNamesA    = new ProjectNames(
                            fullName: fullProjectPathA,
                            uniqueName: Path.GetFileName(fullProjectPathA),
                            shortName: Path.GetFileNameWithoutExtension(fullProjectPathA),
                            customUniqueName: Path.GetFileName(fullProjectPathA));
                        var vsProjectAdapterA = new TestVSProjectAdapter(
                            fullProjectPathA,
                            projectNamesA,
                            projectTargetFrameworkStr,
                            restorePackagesWithLockFile: "true");

                        var projectServicesA = new TestProjectSystemServices();
                        projectServicesA.SetupInstalledPackages(
                            NuGetFramework.Parse(projectTargetFrameworkStr),
                            new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(
                                "packageA",
                                VersionRange.Parse("1.*"),
                                LibraryDependencyTarget.Package)
                        });
                        projectServicesA.SetupProjectDependencies(
                            new ProjectRestoreReference
                        {
                            ProjectUniqueName = fullProjectPathB,
                            ProjectPath       = fullProjectPathB
                        });

                        var legacyPRProjectA = new LegacyPackageReferenceProject(
                            vsProjectAdapterA,
                            Guid.NewGuid().ToString(),
                            projectServicesA,
                            _threadingService);
                        testSolutionManager.NuGetProjects.Add(legacyPRProjectB);
                        testSolutionManager.NuGetProjects.Add(legacyPRProjectA);

                        var testLogger     = new TestLogger();
                        var restoreContext = new DependencyGraphCacheContext(testLogger, testSettings);
                        var providersCache = new RestoreCommandProvidersCache();

                        var packageContextA = new SimpleTestPackageContext("packageA", "1.0.0");
                        packageContextA.AddFile("lib/net45/a.dll");
                        var packageContextB = new SimpleTestPackageContext("packageB", "1.0.0");
                        packageContextB.AddFile("lib/net45/b.dll");
                        var packages = new List <SimpleTestPackageContext>()
                        {
                            packageContextA, packageContextB
                        };
                        SimpleTestPackageUtility.CreateOPCPackages(packages, packageSource);

                        var dgSpec = await DependencyGraphRestoreUtility.GetSolutionRestoreSpec(testSolutionManager, restoreContext);

                        // Act
                        var restoreSummaries = await DependencyGraphRestoreUtility.RestoreAsync(
                            testSolutionManager,
                            dgSpec,
                            restoreContext,
                            providersCache,
                            (c) => { },
                            sourceRepositoryProvider.GetRepositories(),
                            Guid.Empty,
                            false,
                            true,
                            testLogger,
                            CancellationToken.None);

                        // Assert
                        foreach (var restoreSummary in restoreSummaries)
                        {
                            Assert.True(restoreSummary.Success);
                            Assert.False(restoreSummary.NoOpRestore);
                        }

                        Assert.True(File.Exists(Path.Combine(projectPathA, "packages.lock.json")));
                        var lockFile = PackagesLockFileFormat.Read(Path.Combine(projectPathA, "packages.lock.json"));
                        Assert.Equal(1, lockFile.Targets.Count);

                        Assert.Equal(".NETFramework,Version=v4.5", lockFile.Targets[0].Name);
                        Assert.Equal(3, lockFile.Targets[0].Dependencies.Count);
                        Assert.Equal("packageA", lockFile.Targets[0].Dependencies[0].Id);
                        Assert.Equal(PackageDependencyType.Direct, lockFile.Targets[0].Dependencies[0].Type);
                        Assert.Equal("packageB", lockFile.Targets[0].Dependencies[1].Id);
                        Assert.Equal(PackageDependencyType.Transitive, lockFile.Targets[0].Dependencies[1].Type);
                        Assert.Equal("project2", lockFile.Targets[0].Dependencies[2].Id);
                        Assert.Equal(PackageDependencyType.Project, lockFile.Targets[0].Dependencies[2].Type);
                    }
            }
        }
        public async Task GetPackageSpecsAsync_ReadSettingsWithFullPaths(string restorePackagesPath, string sources, string fallbackFolders)
        {
            // Arrange
            using (var testDirectory = TestDirectory.Create())
            {
                var projectAdapter = CreateProjectAdapter(testDirectory);
                Mock.Get(projectAdapter)
                .SetupGet(x => x.RestorePackagesPath)
                .Returns(restorePackagesPath);

                Mock.Get(projectAdapter)
                .SetupGet(x => x.RestoreSources)
                .Returns(sources);

                Mock.Get(projectAdapter)
                .SetupGet(x => x.RestoreFallbackFolders)
                .Returns(fallbackFolders);

                var projectServices = new TestProjectSystemServices();

                var testProject = new LegacyPackageReferenceProject(
                    projectAdapter,
                    Guid.NewGuid().ToString(),
                    projectServices,
                    _threadingService);

                var settings = NullSettings.Instance;
                var testDependencyGraphCacheContext = new DependencyGraphCacheContext(NullLogger.Instance, settings);

                await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Act
                var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext);

                // Assert
                Assert.NotNull(packageSpecs);
                var actualRestoreSpec = packageSpecs.Single();
                SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);

                // Assert packagespath
                Assert.Equal(restorePackagesPath != null ? restorePackagesPath : SettingsUtility.GetGlobalPackagesFolder(settings), actualRestoreSpec.RestoreMetadata.PackagesPath);

                // assert sources
                var specSources     = actualRestoreSpec.RestoreMetadata.Sources.Select(e => e.Source);
                var expectedSources = sources != null?MSBuildStringUtility.Split(sources) : SettingsUtility.GetEnabledSources(settings).Select(e => e.Source);

                Assert.True(Enumerable.SequenceEqual(expectedSources.OrderBy(t => t), specSources.OrderBy(t => t)));

                // assert fallbackfolders
                var specFallback    = actualRestoreSpec.RestoreMetadata.FallbackFolders;
                var expectedFolders = fallbackFolders != null?MSBuildStringUtility.Split(fallbackFolders) : SettingsUtility.GetFallbackPackageFolders(settings);

                Assert.True(Enumerable.SequenceEqual(expectedFolders.OrderBy(t => t), specFallback.OrderBy(t => t)));

                // Verify
                Mock.Get(projectAdapter)
                .Verify(x => x.RestorePackagesPath, Times.Once);
                Mock.Get(projectAdapter)
                .Verify(x => x.RestoreSources, Times.Once);
                Mock.Get(projectAdapter)
                .Verify(x => x.RestoreFallbackFolders, Times.Once);
            }
        }