public async Task RestoreCommand_VerifyRuntimeSpecificAssetsAreNotIncludedForCompile_RuntimeAndRef() { // Arrange var sources = new List <PackageSource>(); var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""netstandard1.5"": { ""dependencies"": { ""packageA"": ""1.0.0"" } } }, ""runtimes"": { ""win7-x64"": {} } }"; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); packageSource.Create(); project1.Create(); sources.Add(new PackageSource(packageSource.FullName)); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); var specPath1 = Path.Combine(project1.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1); var logger = new TestLogger(); var request = new TestRestoreRequest(spec1, sources, packagesDir.FullName, logger); request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json"); var packageAContext = new SimpleTestPackageContext("packageA"); packageAContext.AddFile("ref/netstandard1.5/a.dll"); packageAContext.AddFile("runtimes/win7-x64/lib/netstandard1.5/a.dll"); SimpleTestPackageUtility.CreateFullPackage(packageSource.FullName, packageAContext); // Act var command = new RestoreCommand(request); var result = await command.ExecuteAsync(); var lockFile = result.LockFile; await result.CommitAsync(logger, CancellationToken.None); var targetLib = lockFile.GetTarget(NuGetFramework.Parse("netstandard1.5"), "win7-x64") .Libraries .Single(library => library.Name == "packageA"); // Assert Assert.True(result.Success); Assert.Equal("ref/netstandard1.5/a.dll", targetLib.CompileTimeAssemblies.Single()); Assert.Equal("runtimes/win7-x64/lib/netstandard1.5/a.dll", targetLib.RuntimeAssemblies.Single()); } }
public async Task RestoreCommand_WhenSwitchingBetweenLowercaseSettings_LockFileAlwaysRespectsLatestSetting(bool isLowercase) { // Arrange using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var sourceDir = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var projectDir = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); sourceDir.Create(); projectDir.Create(); var resolverA = new VersionFolderPathResolver(packagesDir.FullName, !isLowercase); var resolverB = new VersionFolderPathResolver(packagesDir.FullName, isLowercase); var sources = new List <string>(); sources.Add(sourceDir.FullName); var projectJson = @" { ""frameworks"": { ""netstandard1.0"": { ""dependencies"": { ""PackageA"": ""1.0.0-Beta"" } } } }"; File.WriteAllText(Path.Combine(projectDir.FullName, "project.json"), projectJson); var specPath = Path.Combine(projectDir.FullName, "project.json"); var spec = JsonPackageSpecReader.GetPackageSpec(projectJson, "project1", specPath); var logger = new TestLogger(); var lockFilePath = Path.Combine(projectDir.FullName, "project.lock.json"); var lockFileFormat = new LockFileFormat(); var packageId = "PackageA"; var packageVersion = "1.0.0-Beta"; var packageAContext = new SimpleTestPackageContext(packageId, packageVersion); packageAContext.AddFile("lib/netstandard1.0/a.dll"); SimpleTestPackageUtility.CreateFullPackage(sourceDir.FullName, packageAContext); // Act // Execute the first restore with the opposite lowercase setting. var requestA = new TestRestoreRequest( spec, sources.Select(x => Repository.Factory.GetCoreV3(x)), packagesDir.FullName, Enumerable.Empty <string>(), logger) { LockFilePath = lockFilePath, IsLowercasePackagesDirectory = !isLowercase }; var commandA = new RestoreCommand(requestA); var resultA = await commandA.ExecuteAsync(); await resultA.CommitAsync(logger, CancellationToken.None); // Execute the second restore with the request lowercase setting. var requestB = new TestRestoreRequest( spec, sources.Select(x => Repository.Factory.GetCoreV3(x)), packagesDir.FullName, Enumerable.Empty <string>(), logger) { LockFilePath = lockFilePath, IsLowercasePackagesDirectory = isLowercase, ExistingLockFile = lockFileFormat.Read(lockFilePath) }; var commandB = new RestoreCommand(requestB); var resultB = await commandB.ExecuteAsync(); await resultB.CommitAsync(logger, CancellationToken.None); // Assert // Commands should have succeeded. Assert.True(resultA.Success); Assert.True(resultB.Success); // The lock file library path should match the requested case. var libraryA = resultA .LockFile .Libraries .FirstOrDefault(l => l.Name == packageId && l.Version.ToNormalizedString() == packageVersion); Assert.NotNull(libraryA); Assert.Equal( PathUtility.GetPathWithForwardSlashes(resolverA.GetPackageDirectory(packageId, libraryA.Version)), libraryA.Path); Assert.True(File.Exists(resolverA.GetPackageFilePath(packageId, libraryA.Version))); var libraryB = resultB .LockFile .Libraries .FirstOrDefault(l => l.Name == packageId && l.Version.ToNormalizedString() == packageVersion); Assert.NotNull(libraryB); Assert.Equal( PathUtility.GetPathWithForwardSlashes(resolverB.GetPackageDirectory(packageId, libraryB.Version)), libraryB.Path); Assert.True(File.Exists(resolverB.GetPackageFilePath(packageId, libraryB.Version))); // The lock file on disk should match the second restore's library. var diskLockFile = lockFileFormat.Read(lockFilePath); var lockFileLibrary = diskLockFile .Libraries .FirstOrDefault(l => l.Name == packageId && l.Version.ToNormalizedString() == packageVersion); Assert.NotNull(lockFileLibrary); Assert.Equal( PathUtility.GetPathWithForwardSlashes(resolverB.GetPackageDirectory(packageId, libraryB.Version)), libraryB.Path); Assert.Equal(libraryB, lockFileLibrary); } }
public async Task RestoreCommand_ReferenceWithSameNameDifferentCasing() { // Arrange var sources = new List <PackageSource>(); // Both TxMs reference packageA, but they are different types. // Verify that the reference does not show up under libraries. var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""netstandard1.3"": { ""dependencies"": { ""packageA"": ""4.0.0"" } } } }"; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "PROJECT1")); packagesDir.Create(); packageSource.Create(); project1.Create(); sources.Add(new PackageSource(packageSource.FullName)); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); var specPath1 = Path.Combine(project1.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "PROJECT1", specPath1); var logger = new TestLogger(); var request = new TestRestoreRequest(spec1, sources, packagesDir.FullName, logger); request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json"); var aContext = new SimpleTestPackageContext() { Id = "packageA", Version = "4.0.0" }; aContext.Dependencies.Add(new SimpleTestPackageContext("proJect1")); SimpleTestPackageUtility.CreateFullPackage(packageSource.FullName, "projeCt1", "4.0.0"); // Act var command = new RestoreCommand(request); var result = await command.ExecuteAsync(); var lockFile = result.LockFile; await result.CommitAsync(logger, CancellationToken.None); // Assert // Verify no stack overflows from circular dependencies Assert.False(result.Success); } }
public async Task RestoreCommand_ObservesLowercaseFlag(bool isLowercase) { // Arrange using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var sourceDir = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var projectDir = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); sourceDir.Create(); projectDir.Create(); var resolver = new VersionFolderPathResolver(packagesDir.FullName, isLowercase); var sources = new List <string>(); sources.Add(sourceDir.FullName); var projectJson = @" { ""frameworks"": { ""netstandard1.0"": { ""dependencies"": { ""PackageA"": ""1.0.0-Beta"" } } } }"; File.WriteAllText(Path.Combine(projectDir.FullName, "project.json"), projectJson); var specPath = Path.Combine(projectDir.FullName, "project.json"); var spec = JsonPackageSpecReader.GetPackageSpec(projectJson, "project1", specPath); var logger = new TestLogger(); var request = new TestRestoreRequest( spec, sources.Select(x => Repository.Factory.GetCoreV3(x)), packagesDir.FullName, Enumerable.Empty <string>(), logger) { IsLowercasePackagesDirectory = isLowercase }; request.LockFilePath = Path.Combine(projectDir.FullName, "project.lock.json"); var packageId = "PackageA"; var packageVersion = "1.0.0-Beta"; var packageAContext = new SimpleTestPackageContext(packageId, packageVersion); packageAContext.AddFile("lib/netstandard1.0/a.dll"); SimpleTestPackageUtility.CreateFullPackage(sourceDir.FullName, packageAContext); // Act var command = new RestoreCommand(request); var result = await command.ExecuteAsync(); var lockFile = result.LockFile; // Assert Assert.True(result.Success); var library = lockFile .Libraries .FirstOrDefault(l => l.Name == packageId && l.Version.ToNormalizedString() == packageVersion); Assert.NotNull(library); Assert.Equal( PathUtility.GetPathWithForwardSlashes(resolver.GetPackageDirectory(packageId, library.Version)), library.Path); Assert.True(File.Exists(resolver.GetPackageFilePath(packageId, library.Version))); } }
public async Task GetInstallActionsAsync_WithPackagesConfigProject_WhenUpdatingPackage_ReturnsCorrectActions() { const string projectName = "a"; string projectId = Guid.NewGuid().ToString(); using (TestDirectory testDirectory = TestDirectory.Create()) { var packageV1 = new SimpleTestPackageContext(packageId: "b", version: "1.0.0"); var packageV2 = new SimpleTestPackageContext(packageV1.Id, version: "2.0.0"); string packageSourceDirectoryPath = Path.Combine(testDirectory, "packageSource"); await SimpleTestPackageUtility.CreateFolderFeedV3Async( packageSourceDirectoryPath, PackageSaveMode.Defaultv3, packageV1, packageV2); var packageSource = new PackageSource(packageSourceDirectoryPath); var packageSources = new List <PackageSource>() { packageSource }; Initialize(packageSources); string projectFullPath = Path.Combine(testDirectory.Path, $"{projectName}.csproj"); NuGetFramework targetFramework = NuGetFramework.Parse("net46"); var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(targetFramework, new TestNuGetProjectContext()); var project = new TestMSBuildNuGetProject(msBuildNuGetProjectSystem, testDirectory.Path, projectFullPath, projectId); _solutionManager.NuGetProjects.Add(project); string[] projectIds = new[] { projectId }; string[] packageSourceNames = new[] { packageSource.Name }; await PerformOperationAsync(async (projectManager) => { IReadOnlyList <ProjectAction> actions = await projectManager.GetInstallActionsAsync( projectIds, packageV1.Identity, VersionConstraints.None, includePrelease: true, DependencyBehavior.Lowest, packageSourceNames, CancellationToken.None); Assert.NotEmpty(actions); Assert.Equal(1, actions.Count); ProjectAction action = actions[0]; Assert.Equal(packageV1.Identity, action.PackageIdentity); Assert.Equal(NuGetProjectActionType.Install, action.ProjectActionType); Assert.Equal(projectId, action.ProjectId); Assert.Empty(action.ImplicitActions); await projectManager.ExecuteActionsAsync(actions, CancellationToken.None); }); await PerformOperationAsync(async (projectManager) => { IReadOnlyList <ProjectAction> actions = await projectManager.GetInstallActionsAsync( projectIds, packageV2.Identity, VersionConstraints.None, includePrelease: true, DependencyBehavior.Lowest, packageSourceNames, CancellationToken.None); Assert.NotEmpty(actions); Assert.Equal(2, actions.Count); ProjectAction action = actions[0]; Assert.Equal(packageV1.Identity, action.PackageIdentity); Assert.Equal(NuGetProjectActionType.Uninstall, action.ProjectActionType); Assert.Equal(projectId, action.ProjectId); action = actions[1]; Assert.Equal(packageV2.Identity, action.PackageIdentity); Assert.Equal(NuGetProjectActionType.Install, action.ProjectActionType); Assert.Equal(projectId, action.ProjectId); }); } }
public async Task GetInstallActionsAsync_WithProjectReferenceProject_WhenUpdatingPackage_ReturnsCorrectActions() { const string projectName = "a"; string projectId = Guid.NewGuid().ToString(); var projectSystemCache = new ProjectSystemCache(); using (TestDirectory testDirectory = TestDirectory.Create()) { var packageV1 = new SimpleTestPackageContext(packageId: "b", version: "1.0.0"); var packageV2 = new SimpleTestPackageContext(packageV1.Id, version: "2.0.0"); string packageSourceDirectoryPath = Path.Combine(testDirectory, "packageSource"); await SimpleTestPackageUtility.CreateFolderFeedV3Async( packageSourceDirectoryPath, PackageSaveMode.Defaultv3, packageV1, packageV2); var packageSource = new PackageSource(packageSourceDirectoryPath); var packageSources = new List <PackageSource>() { packageSource }; Initialize(packageSources); string projectFullPath = Path.Combine(testDirectory.Path, $"{projectName}.csproj"); var unconfiguredProject = new Mock <UnconfiguredProject>(); var configuredProject = new Mock <ConfiguredProject>(); var projectServices = new Mock <ConfiguredProjectServices>(); var packageReferencesService = new Mock <IPackageReferencesService>(); var result = new Mock <IUnresolvedPackageReference>(); unconfiguredProject.Setup(x => x.GetSuggestedConfiguredProjectAsync()) .ReturnsAsync(configuredProject.Object); configuredProject.SetupGet(x => x.Services) .Returns(projectServices.Object); projectServices.SetupGet(x => x.PackageReferences) .Returns(packageReferencesService.Object); packageReferencesService.Setup(x => x.AddAsync(It.IsNotNull <string>(), It.IsNotNull <string>())) .ReturnsAsync(new AddReferenceResult <IUnresolvedPackageReference>(result.Object, added: true)); var nuGetProjectServices = new Mock <INuGetProjectServices>(); nuGetProjectServices.SetupGet(x => x.ScriptService) .Returns(Mock.Of <IProjectScriptHostService>()); var project = new CpsPackageReferenceProject( projectName: projectName, projectUniqueName: projectFullPath, projectFullPath: projectFullPath, projectSystemCache, unconfiguredProject.Object, nuGetProjectServices.Object, projectId); PackageSpec packageSpec = CreatePackageSpec( project.ProjectName, Path.Combine(testDirectory, "package.spec")); DependencyGraphSpec projectRestoreInfo = ProjectJsonTestHelpers.GetDGSpecFromPackageSpecs(packageSpec); projectRestoreInfo.AddProject(packageSpec); var projectNames = new ProjectNames( fullName: projectFullPath, uniqueName: projectFullPath, shortName: projectName, customUniqueName: projectName, projectId: projectId); projectSystemCache.AddProjectRestoreInfo(projectNames, projectRestoreInfo, Array.Empty <IAssetsLogMessage>()); _solutionManager.NuGetProjects.Add(project); string[] projectIds = new[] { projectId }; string[] packageSourceNames = new[] { packageSource.Name }; await PerformOperationAsync(async (projectManager) => { IReadOnlyList <ProjectAction> actions = await projectManager.GetInstallActionsAsync( projectIds, packageV1.Identity, VersionConstraints.None, includePrelease: true, DependencyBehavior.Lowest, packageSourceNames, CancellationToken.None); Assert.NotEmpty(actions); Assert.Equal(1, actions.Count); ProjectAction action = actions[0]; Assert.Equal(packageV1.Identity, action.PackageIdentity); Assert.Equal(NuGetProjectActionType.Install, action.ProjectActionType); Assert.Equal(projectId, action.ProjectId); Assert.Equal(1, action.ImplicitActions.Count); ImplicitProjectAction implicitAction = action.ImplicitActions[0]; Assert.Equal(packageV1.Identity, implicitAction.PackageIdentity); Assert.Equal(NuGetProjectActionType.Install, implicitAction.ProjectActionType); await projectManager.ExecuteActionsAsync(actions, CancellationToken.None); }); await PerformOperationAsync(async (projectManager) => { IReadOnlyList <ProjectAction> actions = await projectManager.GetInstallActionsAsync( projectIds, packageV2.Identity, VersionConstraints.None, includePrelease: true, DependencyBehavior.Lowest, packageSourceNames, CancellationToken.None); Assert.NotEmpty(actions); Assert.Equal(1, actions.Count); ProjectAction action = actions[0]; Assert.Equal(packageV2.Identity, action.PackageIdentity); Assert.Equal(NuGetProjectActionType.Install, action.ProjectActionType); Assert.Equal(projectId, action.ProjectId); Assert.Equal(2, action.ImplicitActions.Count); ImplicitProjectAction implicitAction = action.ImplicitActions[0]; Assert.Equal(packageV1.Identity, implicitAction.PackageIdentity); Assert.Equal(NuGetProjectActionType.Uninstall, implicitAction.ProjectActionType); implicitAction = action.ImplicitActions[1]; Assert.Equal(packageV2.Identity, implicitAction.PackageIdentity); Assert.Equal(NuGetProjectActionType.Install, implicitAction.ProjectActionType); }); } }
public async Task RestoreTargets_RestoreWithRuntimesAsync() { // Arrange var sources = new List <PackageSource>(); var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""netstandardapp1.5"": { ""dependencies"": { ""packageA"": ""1.0.0"" } } } }"; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); packageSource.Create(); project1.Create(); sources.Add(new PackageSource(packageSource.FullName)); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); var specPath1 = Path.Combine(project1.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1); var logger = new TestLogger(); var request = new TestRestoreRequest(spec1, sources, packagesDir.FullName, logger) { LockFilePath = Path.Combine(project1.FullName, "project.lock.json") }; request.RequestedRuntimes.Add("win7-x86"); var packageA = new SimpleTestPackageContext() { Id = "packageA" }; packageA.AddFile("lib/netstandard1.5/a.dll"); packageA.AddFile("native/a.dll"); packageA.AddFile("runtimes/unix/native/a.dll"); packageA.AddFile("runtimes/unix/lib/netstandard1.5/a.dll"); packageA.AddFile("runtimes/win7/lib/netstandard1.5/a.dll"); packageA.AddFile("runtimes/win7-x86/lib/netstandard1.5/a.dll"); packageA.AddFile("runtimes/win7-x86/lib/netstandard1.5/en-us/a.resources.dll"); await SimpleTestPackageUtility.CreatePackagesAsync(packageSource.FullName, packageA); // Act var command = new RestoreCommand(request); var result = await command.ExecuteAsync(); await result.CommitAsync(logger, CancellationToken.None); var format = new LockFileFormat(); var lockFile = format.Read(request.LockFilePath); var targetLib = lockFile.Targets.Single(graph => graph.RuntimeIdentifier == null).Libraries.Single(); var ridTargetLib = lockFile.Targets.Single(graph => graph.RuntimeIdentifier != null).Libraries.Single(); // Assert Assert.True(result.Success); Assert.Equal(5, targetLib.RuntimeTargets.Count); Assert.Equal("runtimes/unix/lib/netstandard1.5/a.dll", targetLib.RuntimeTargets[0].Path); Assert.Equal("runtime", targetLib.RuntimeTargets[0].Properties["assetType"]); Assert.Equal("unix", targetLib.RuntimeTargets[0].Properties["rid"]); Assert.Equal("runtimes/win7-x86/lib/netstandard1.5/en-us/a.resources.dll", targetLib.RuntimeTargets[3].Path); Assert.Equal("resource", targetLib.RuntimeTargets[3].Properties["assetType"]); Assert.Equal("win7-x86", targetLib.RuntimeTargets[3].Properties["rid"]); Assert.Equal("runtimes/unix/native/a.dll", targetLib.RuntimeTargets[1].Path); Assert.Equal("native", targetLib.RuntimeTargets[1].Properties["assetType"]); Assert.Equal("unix", targetLib.RuntimeTargets[1].Properties["rid"]); // This section does not exist for RID graphs Assert.Equal(0, ridTargetLib.RuntimeTargets.Count); } }
public async Task BuildIntegratedRestoreUtility_RestoreDeferredProjectLoad() { // Arrange var projectName = "testproj"; var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""net45"": { ""dependencies"": { ""packageA"": ""1.0.0"" } } } }"; using (var packageSource = TestDirectory.Create()) using (var rootFolder = TestDirectory.Create()) { var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider( new List <PackageSource>() { new PackageSource(packageSource.Path) }); var packageContext = new SimpleTestPackageContext("packageA"); packageContext.AddFile("lib/net45/a.dll"); SimpleTestPackageUtility.CreateOPCPackage(packageContext, packageSource); var projectFolder = new DirectoryInfo(Path.Combine(rootFolder, projectName)); projectFolder.Create(); var projectConfig = Path.Combine(projectFolder.FullName, "project.json"); var projectPath = Path.Combine(projectFolder.FullName, $"{projectName}.csproj"); var packageSpec = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", projectConfig); var metadata = new ProjectRestoreMetadata(); packageSpec.RestoreMetadata = metadata; metadata.ProjectStyle = ProjectStyle.ProjectJson; metadata.ProjectPath = projectPath; metadata.ProjectJsonPath = packageSpec.FilePath; metadata.ProjectName = packageSpec.Name; metadata.ProjectUniqueName = projectPath; foreach (var framework in packageSpec.TargetFrameworks.Select(e => e.FrameworkName)) { metadata.TargetFrameworks.Add(new ProjectRestoreMetadataFrameworkInfo(framework)); } var solutionManager = new TestSolutionManager(false); var testLogger = new TestLogger(); var restoreContext = new DependencyGraphCacheContext(testLogger); restoreContext.DeferredPackageSpecs.Add(packageSpec); // Act await DependencyGraphRestoreUtility.RestoreAsync( solutionManager, restoreContext, new RestoreCommandProvidersCache(), (c) => { }, sourceRepositoryProvider.GetRepositories(), NullSettings.Instance, testLogger, CancellationToken.None); // Assert Assert.True(File.Exists(Path.Combine(projectFolder.FullName, "project.lock.json"))); Assert.True(testLogger.Errors == 0); } }