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_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_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);
                    }
            }
        }