public async Task TestGetMissingPackagesForSolution_NoPackagesInstalled() { // Arrange using (var testSolutionManager = new TestSolutionManager(true)) { var projectA = testSolutionManager.AddNewMSBuildProject(); var projectB = testSolutionManager.AddNewMSBuildProject(); var testNuGetProjectContext = new TestNuGetProjectContext(); var token = CancellationToken.None; var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); var testSettings = Configuration.NullSettings.Instance; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var packageRestoreManager = new PackageRestoreManager( sourceRepositoryProvider, testSettings, testSolutionManager); // Act var packagesFromSolution = (await packageRestoreManager.GetPackagesInSolutionAsync(testSolutionManager.SolutionDirectory, token)); Assert.False(packagesFromSolution.Any()); } }
private async Task <IEnumerable <NuGetProjectAction> > PacManCleanInstall(SourceRepositoryProvider sourceRepositoryProvider, PackageIdentity target) { // Arrange using (var testSolutionManager = new TestSolutionManager()) using (var randomPackagesConfigFolderPath = TestDirectory.Create()) { var testSettings = NullSettings.Instance; var token = CancellationToken.None; var deleteOnRestartManger = new TestDeleteOnRestartManager(); var nuGetPackageManager = new NuGetPackageManager( sourceRepositoryProvider, testSettings, testSolutionManager, deleteOnRestartManger); var packagesFolderPath = PackagesFolderPathUtility.GetPackagesFolderPath(testSolutionManager, testSettings); var randomPackagesConfigPath = Path.Combine(randomPackagesConfigFolderPath, "packages.config"); var projectTargetFramework = NuGetFramework.Parse("net45"); var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(projectTargetFramework, new TestNuGetProjectContext()); var msBuildNuGetProject = new MSBuildNuGetProject(msBuildNuGetProjectSystem, packagesFolderPath, randomPackagesConfigFolderPath); // Act var nugetProjectActions = await nuGetPackageManager.PreviewInstallPackageAsync(msBuildNuGetProject, target, new ResolutionContext(), new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token); return(nugetProjectActions); } }
public async Task TestPackageRestoredEvent() { // Arrange using (var testSolutionManager = new TestSolutionManager(true)) { var projectA = testSolutionManager.AddNewMSBuildProject(); var projectB = testSolutionManager.AddNewMSBuildProject(); var packageIdentity = Packages[0]; var testNuGetProjectContext = new TestNuGetProjectContext(); var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); var testSettings = Configuration.NullSettings.Instance; var resolutionContext = new ResolutionContext(); var token = CancellationToken.None; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var nuGetPackageManager = new NuGetPackageManager( sourceRepositoryProvider, testSettings, testSolutionManager, deleteOnRestartManager); await nuGetPackageManager.InstallPackageAsync(projectA, packageIdentity, resolutionContext, new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token); await nuGetPackageManager.InstallPackageAsync(projectB, packageIdentity, resolutionContext, new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token); var packageRestoreManager = new PackageRestoreManager( sourceRepositoryProvider, testSettings, testSolutionManager); var restoredPackages = new List <PackageIdentity>(); packageRestoreManager.PackageRestoredEvent += delegate(object sender, PackageRestoredEventArgs args) { if (args.Restored) { restoredPackages.Add(args.Package); } }; Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(packageIdentity)); // Delete packages folder TestFileSystemUtility.DeleteRandomTestFolder(Path.Combine(testSolutionManager.SolutionDirectory, "packages")); Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder((packageIdentity))); // Act await packageRestoreManager.RestoreMissingPackagesInSolutionAsync(testSolutionManager.SolutionDirectory, testNuGetProjectContext, new TestLogger(), CancellationToken.None); Assert.Equal(1, restoredPackages.Count); Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder((packageIdentity))); } }
public async Task TestCheckForMissingPackages() { // Arrange using (var testSolutionManager = new TestSolutionManager(true)) using (var randomPackageSourcePath = TestFileSystemUtility.CreateRandomTestFolder()) { var projectA = testSolutionManager.AddNewMSBuildProject(); var projectB = testSolutionManager.AddNewMSBuildProject(); var packageIdentity = new PackageIdentity("packageA", new NuGetVersion("1.0.0")); var packageFileInfo = TestPackagesGroupedByFolder.GetLegacyTestPackage(randomPackageSourcePath, packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); var testNuGetProjectContext = new TestNuGetProjectContext(); var token = CancellationToken.None; using (var packageStream = GetDownloadResult(packageFileInfo)) { // Act await projectA.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token); await projectB.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token); } var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); var testSettings = Configuration.NullSettings.Instance; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var packageRestoreManager = new PackageRestoreManager( sourceRepositoryProvider, testSettings, testSolutionManager); var packagesMissingEventCount = 0; var packagesMissing = false; packageRestoreManager.PackagesMissingStatusChanged += delegate(object sender, PackagesMissingStatusEventArgs args) { packagesMissingEventCount++; packagesMissing = args.PackagesMissing; }; // Act await packageRestoreManager.RaisePackagesMissingEventForSolutionAsync(testSolutionManager.SolutionDirectory, token); // Assert Assert.Equal(1, packagesMissingEventCount); Assert.False(packagesMissing); // Delete packages folder TestFileSystemUtility.DeleteRandomTestFolder(Path.Combine(testSolutionManager.SolutionDirectory, "packages")); // Act await packageRestoreManager.RaisePackagesMissingEventForSolutionAsync(testSolutionManager.SolutionDirectory, token); // Assert Assert.Equal(2, packagesMissingEventCount); Assert.True(packagesMissing); } }
public async Task InstallingPackageShouldCreateLockFile_CustomLockFileName() { // Arrange using (var packageSource = TestDirectory.Create()) using (var testSolutionManager = new TestSolutionManager()) { var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider( new List <PackageSource>() { new PackageSource(packageSource.Path) }); var testSettings = NullSettings.Instance; var token = CancellationToken.None; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var nuGetPackageManager = new NuGetPackageManager( sourceRepositoryProvider, testSettings, testSolutionManager, deleteOnRestartManager); var packagesFolderPath = PackagesFolderPathUtility.GetPackagesFolderPath(testSolutionManager, testSettings); var packageContext = new SimpleTestPackageContext("packageA"); packageContext.AddFile("lib/net45/a.dll"); SimpleTestPackageUtility.CreateOPCPackage(packageContext, packageSource); var msBuildNuGetProject = testSolutionManager.AddNewMSBuildProject(); var msBuildNuGetProjectSystem = msBuildNuGetProject.ProjectSystem as TestMSBuildNuGetProjectSystem; msBuildNuGetProjectSystem.SetPropertyValue("RestorePackagesWithLockFile", "true"); msBuildNuGetProjectSystem.SetPropertyValue("NuGetLockFilePath", "my.lock.json"); var packagesConfigPath = msBuildNuGetProject.PackagesConfigNuGetProject.FullPath; var packagesLockPath = packagesConfigPath.Replace("packages.config", "my.lock.json"); var packageIdentity = packageContext.Identity; // Pre-Assert // Check that the packages.lock.json file does not exist Assert.False(File.Exists(packagesLockPath)); // Act await nuGetPackageManager.InstallPackageAsync(msBuildNuGetProject, packageIdentity, new ResolutionContext(), new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token); // Assert // Check that the packages.lock.json file exists after the installation Assert.True(File.Exists(packagesLockPath)); Assert.True(msBuildNuGetProjectSystem.FileExistsInProject("my.lock.json")); // Check the number of target frameworks and dependencies in the lock file var lockFile = PackagesLockFileFormat.Read(packagesLockPath); Assert.Equal(1, lockFile.Targets.Count); Assert.Equal(1, lockFile.Targets[0].Dependencies.Count); } }
public async Task TestGetMissingPackagesForSolution() { // Arrange using (var testSolutionManager = new TestSolutionManager(true)) using (var randomPackageSourcePath = TestFileSystemUtility.CreateRandomTestFolder()) { var projectA = testSolutionManager.AddNewMSBuildProject(); var projectB = testSolutionManager.AddNewMSBuildProject(); var packageIdentity = new PackageIdentity("packageA", new NuGetVersion("1.0.0")); var packageFileInfo = TestPackagesGroupedByFolder.GetLegacyTestPackage(randomPackageSourcePath, packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); var testNuGetProjectContext = new TestNuGetProjectContext(); var token = CancellationToken.None; using (var packageStream = GetDownloadResult(packageFileInfo)) { // Act await projectA.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token); await projectB.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token); } var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); var testSettings = Configuration.NullSettings.Instance; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var packageRestoreManager = new PackageRestoreManager( sourceRepositoryProvider, testSettings, testSolutionManager); // Act var packagesFromSolution = (await packageRestoreManager.GetPackagesInSolutionAsync(testSolutionManager.SolutionDirectory, token)); var packagesFromSolutionList = packagesFromSolution.ToList(); var missingPackagesFromSolutionList = packagesFromSolution.Where(p => p.IsMissing).ToList(); Assert.Equal(1, packagesFromSolutionList.Count); Assert.Equal(0, missingPackagesFromSolutionList.Count); // Delete packages folder TestFileSystemUtility.DeleteRandomTestFolder(Path.Combine(testSolutionManager.SolutionDirectory, "packages")); packagesFromSolution = (await packageRestoreManager.GetPackagesInSolutionAsync(testSolutionManager.SolutionDirectory, token)); packagesFromSolutionList = packagesFromSolution.ToList(); missingPackagesFromSolutionList = packagesFromSolution.Where(p => p.IsMissing).ToList(); Assert.Equal(1, missingPackagesFromSolutionList.Count); } }
public async Task UnzippedPackageInstall_Basic() { // Arrange var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); using (var testSolutionManager = new TestSolutionManager(true)) using (var randomPackagesConfigFolderPath = TestDirectory.Create()) { var testSettings = new Configuration.NullSettings(); var deleteOnRestartManager = new TestDeleteOnRestartManager(); var nuGetPackageManager = new NuGetPackageManager( sourceRepositoryProvider, testSettings, testSolutionManager, deleteOnRestartManager); var packagesFolderPath = PackagesFolderPathUtility.GetPackagesFolderPath(testSolutionManager, testSettings); var randomPackagesConfigPath = Path.Combine(randomPackagesConfigFolderPath, "packages.config"); var token = CancellationToken.None; var projectTargetFramework = NuGetFramework.Parse("net45"); var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(projectTargetFramework, new TestNuGetProjectContext()); var msBuildNuGetProject = new MSBuildNuGetProject(msBuildNuGetProjectSystem, packagesFolderPath, randomPackagesConfigFolderPath); var packageIdentity = NoDependencyLibPackages[0]; // Pre-Assert // Check that the packages.config file does not exist Assert.False(File.Exists(randomPackagesConfigPath)); // Check that there are no packages returned by PackagesConfigProject var packagesInPackagesConfig = (await msBuildNuGetProject.PackagesConfigNuGetProject.GetInstalledPackagesAsync(token)).ToList(); Assert.Equal(0, packagesInPackagesConfig.Count); Assert.Equal(0, msBuildNuGetProjectSystem.References.Count); // Act await nuGetPackageManager.InstallPackageAsync(msBuildNuGetProject, packageIdentity, new ResolutionContext(), new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token); // Assert // Check that the packages.config file exists after the installation Assert.True(File.Exists(randomPackagesConfigPath)); // Check the number of packages and packages returned by PackagesConfigProject after the installation packagesInPackagesConfig = (await msBuildNuGetProject.PackagesConfigNuGetProject.GetInstalledPackagesAsync(token)).ToList(); Assert.Equal(1, packagesInPackagesConfig.Count); Assert.Equal(packageIdentity, packagesInPackagesConfig[0].PackageIdentity); Assert.Equal(projectTargetFramework, packagesInPackagesConfig[0].TargetFramework); } }
public async Task DoNotCreateLockFileWhenFeatureDisabled() { // Arrange using (var packageSource = TestDirectory.Create()) using (var testSolutionManager = new TestSolutionManager()) { var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider( new List <PackageSource>() { new PackageSource(packageSource.Path) }); var testSettings = NullSettings.Instance; var token = CancellationToken.None; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var nuGetPackageManager = new NuGetPackageManager( sourceRepositoryProvider, testSettings, testSolutionManager, deleteOnRestartManager); var packagesFolderPath = PackagesFolderPathUtility.GetPackagesFolderPath(testSolutionManager, testSettings); var packageContext = new SimpleTestPackageContext("packageA"); packageContext.AddFile("lib/net45/a.dll"); SimpleTestPackageUtility.CreateOPCPackage(packageContext, packageSource); var msBuildNuGetProject = testSolutionManager.AddNewMSBuildProject(); var msBuildNuGetProjectSystem = msBuildNuGetProject.ProjectSystem as TestMSBuildNuGetProjectSystem; msBuildNuGetProjectSystem.SetPropertyValue("RestorePackagesWithLockFile", "false"); var packagesConfigPath = msBuildNuGetProject.PackagesConfigNuGetProject.FullPath; var packagesLockPath = packagesConfigPath.Replace("packages.config", "packages.lock.json"); var packageIdentity = packageContext.Identity; // Pre-Assert // Check that the packages.lock.json file does not exist Assert.False(File.Exists(packagesLockPath)); // Act await nuGetPackageManager.InstallPackageAsync(msBuildNuGetProject, packageIdentity, new ResolutionContext(), new TestNuGetProjectContext(), sourceRepositoryProvider.GetRepositories().First(), null, token); // Assert // Check that the packages.lock.json still does not exist after the installation Assert.False(File.Exists(packagesLockPath)); } }
private void Initialize(IReadOnlyList <PackageSource> packageSources = null) { SourceRepositoryProvider sourceRepositoryProvider; if (packageSources == null || packageSources.Count == 0) { sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); } else { sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(packageSources); } _solutionManager = new TestVsSolutionManager(); _testDirectory = TestDirectory.Create(); ISettings testSettings = CreateSettings(sourceRepositoryProvider, _testDirectory); var deleteOnRestartManager = new TestDeleteOnRestartManager(); _packageManager = new NuGetPackageManager( sourceRepositoryProvider, testSettings, _solutionManager, deleteOnRestartManager); _state = new NuGetProjectManagerServiceState(); _sharedState = new TestSharedServiceState( new Microsoft.VisualStudio.Threading.AsyncLazy <NuGetPackageManager>( () => Task.FromResult(_packageManager)), new Microsoft.VisualStudio.Threading.AsyncLazy <IVsSolutionManager>( () => Task.FromResult <IVsSolutionManager>(_solutionManager)), sourceRepositoryProvider, new Microsoft.VisualStudio.Threading.AsyncLazy <IReadOnlyCollection <SourceRepository> >( () => Task.FromResult <IReadOnlyCollection <SourceRepository> >(sourceRepositoryProvider.GetRepositories().ToList()))); _projectManager = new NuGetProjectManagerService( default(ServiceActivationOptions), Mock.Of <IServiceBroker>(), new AuthorizationServiceClient(Mock.Of <IAuthorizationService>()), _state, _sharedState); }
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 Test_PackageRestoreFailure_WithRaisedEvents() { // Arrange using (var testSolutionManager = new TestSolutionManager(true)) using (var randomTestPackageSourcePath = TestFileSystemUtility.CreateRandomTestFolder()) { var projectA = testSolutionManager.AddNewMSBuildProject("projectA"); var projectB = testSolutionManager.AddNewMSBuildProject("projectB"); var projectC = testSolutionManager.AddNewMSBuildProject("projectC"); var jQuery144 = Packages[0]; var jQueryValidation = Packages[2]; var testNuGetProjectContext = new TestNuGetProjectContext(); var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); var testSettings = Configuration.NullSettings.Instance; var resolutionContext = new ResolutionContext(); var token = CancellationToken.None; var deleteOnRestartManager = new TestDeleteOnRestartManager(); var nuGetPackageManager = new NuGetPackageManager( sourceRepositoryProvider, testSettings, testSolutionManager, deleteOnRestartManager); await nuGetPackageManager.InstallPackageAsync(projectA, jQueryValidation, resolutionContext, testNuGetProjectContext, sourceRepositoryProvider.GetRepositories().First(), null, token); await nuGetPackageManager.InstallPackageAsync(projectB, jQueryValidation, resolutionContext, testNuGetProjectContext, sourceRepositoryProvider.GetRepositories().First(), null, token); var testPackage1 = new PackageIdentity("package1A", new NuGetVersion("1.0.0")); var testPackage2 = new PackageIdentity("package1B", new NuGetVersion("1.0.0")); var packageFileInfo = TestPackagesGroupedByFolder.GetLegacyTestPackage(randomTestPackageSourcePath, testPackage1.Id, testPackage1.Version.ToNormalizedString()); using (var packageStream = GetDownloadResult(packageFileInfo)) { // Act await projectB.InstallPackageAsync(testPackage1, packageStream, testNuGetProjectContext, token); await projectC.InstallPackageAsync(testPackage1, packageStream, testNuGetProjectContext, token); } packageFileInfo = TestPackagesGroupedByFolder.GetLegacyTestPackage(randomTestPackageSourcePath, testPackage2.Id, testPackage2.Version.ToNormalizedString()); using (var packageStream = GetDownloadResult(packageFileInfo)) { // Act await projectA.InstallPackageAsync(testPackage2, packageStream, testNuGetProjectContext, token); await projectC.InstallPackageAsync(testPackage2, packageStream, testNuGetProjectContext, token); } var packageRestoreManager = new PackageRestoreManager( sourceRepositoryProvider, testSettings, testSolutionManager); var restoredPackages = new List <PackageIdentity>(); packageRestoreManager.PackageRestoredEvent += delegate(object sender, PackageRestoredEventArgs args) { restoredPackages.Add(args.Package); }; var restoreFailedPackages = new ConcurrentDictionary <Packaging.PackageReference, IEnumerable <string> >(new PackageReferenceComparer()); packageRestoreManager.PackageRestoreFailedEvent += delegate(object sender, PackageRestoreFailedEventArgs args) { restoreFailedPackages.AddOrUpdate(args.RestoreFailedPackageReference, args.ProjectNames, (Packaging.PackageReference packageReference, IEnumerable <string> oldValue) => { return(oldValue); }); }; Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(jQueryValidation)); // Delete packages folder TestFileSystemUtility.DeleteRandomTestFolder(Path.Combine(testSolutionManager.SolutionDirectory, "packages")); Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(jQuery144)); Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(jQueryValidation)); Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(testPackage1)); Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(testPackage2)); // Act await packageRestoreManager.RestoreMissingPackagesInSolutionAsync(testSolutionManager.SolutionDirectory, testNuGetProjectContext, CancellationToken.None); // Assert Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(jQuery144)); Assert.True(nuGetPackageManager.PackageExistsInPackagesFolder(jQueryValidation)); Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(testPackage1)); Assert.False(nuGetPackageManager.PackageExistsInPackagesFolder(testPackage2)); Assert.Equal(4, restoredPackages.Count); // The ordering is not guaranteed and can vary. Do not assert based on that Assert.True(restoredPackages.Contains(jQuery144)); Assert.True(restoredPackages.Contains(jQueryValidation)); Assert.True(restoredPackages.Contains(testPackage1)); Assert.True(restoredPackages.Contains(testPackage2)); Assert.Equal(2, restoreFailedPackages.Count); // The ordering is not guaranteed and can vary. Do not assert based on that var restoreFailedPackageKeys = restoreFailedPackages.Keys; var testPackage1Key = restoreFailedPackageKeys.Where(r => r.PackageIdentity.Equals(testPackage1)).First(); var testPackage1ProjectNames = restoreFailedPackages[testPackage1Key].ToList(); Assert.Equal(2, testPackage1ProjectNames.Count); Assert.True(testPackage1ProjectNames.Contains("projectB", StringComparer.OrdinalIgnoreCase)); Assert.True(testPackage1ProjectNames.Contains("projectC", StringComparer.OrdinalIgnoreCase)); var testPackage2Key = restoreFailedPackageKeys.Where(r => r.PackageIdentity.Equals(testPackage2)).First(); var testPackage2ProjectNames = restoreFailedPackages[testPackage2Key].ToList(); Assert.Equal(2, testPackage2ProjectNames.Count); Assert.True(testPackage2ProjectNames.Contains("projectA", StringComparer.OrdinalIgnoreCase)); Assert.True(testPackage2ProjectNames.Contains("projectC", StringComparer.OrdinalIgnoreCase)); } }