public async Task ProjectResolution_HigherVersionForProjectReferences()
        {
            // Arrange
            var project1Json = @"
            {
              ""version"": ""1.0.0-*"",
              ""description"": """",
              ""authors"": [ ""author"" ],
              ""tags"": [ """" ],
              ""projectUrl"": """",
              ""licenseUrl"": """",
              ""frameworks"": {
                ""net45"": {
                }
              }
            }";

            var project2Json = @"
            {
              ""version"": ""2.0.0-*"",
              ""description"": """",
              ""authors"": [ ""author"" ],
              ""tags"": [ """" ],
              ""projectUrl"": """",
              ""licenseUrl"": """",
              ""frameworks"": {
                ""net45"": {
                }
              }
            }";

            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"));
                var project2      = new DirectoryInfo(Path.Combine(workingDir, "projects", "project2"));
                packagesDir.Create();
                packageSource.Create();
                project1.Create();
                project2.Create();

                File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json);
                File.WriteAllText(Path.Combine(project2.FullName, "project.json"), project2Json);

                var specPath1 = Path.Combine(project1.FullName, "project.json");
                var specPath2 = Path.Combine(project2.FullName, "project.json");
                var spec1     = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1);
                var spec2     = JsonPackageSpecReader.GetPackageSpec(project2Json, "project2", specPath2);

                var logger = new TestLogger();

                var restoreContext = new RestoreArgs()
                {
                    Sources = new List <string>()
                    {
                        packageSource.FullName
                    },
                    GlobalPackagesFolder = packagesDir.FullName,
                    Log          = logger,
                    CacheContext = new SourceCacheContext()
                };

                // Modify specs for netcore
                spec2 = spec2.WithTestRestoreMetadata();
                spec1 = spec1.WithTestRestoreMetadata().WithTestProjectReference(spec2);

                var request = await ProjectJsonTestHelpers.GetRequestAsync(restoreContext, spec1, spec2);

                request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json");

                // Act
                var command = new RestoreCommand(request);
                var result  = await command.ExecuteAsync();

                var lockFile = result.LockFile;
                await result.CommitAsync(logger, CancellationToken.None);

                // Assert
                Assert.True(result.Success);
                Assert.Equal(0, result.GetAllUnresolved().Count);
                Assert.Equal(0, logger.Messages.Where(s => s.IndexOf("Dependency specified was") > -1).Count());
            }
        }
        public async Task ProjectResolution_ProjectReferenceWithInCompatibleTFMNoRange()
        {
            // Arrange
            var sources = new List <PackageSource>();

            var project1Json = @"
            {
              ""version"": ""1.0.0-*"",
              ""description"": """",
              ""authors"": [ ""author"" ],
              ""tags"": [ """" ],
              ""projectUrl"": """",
              ""licenseUrl"": """",
              ""frameworks"": {
                ""net45"": {
                }
              }
            }";

            var project2Json = @"
            {
              ""version"": ""2.0.0-*"",
              ""description"": """",
              ""authors"": [ ""author"" ],
              ""tags"": [ """" ],
              ""projectUrl"": """",
              ""licenseUrl"": """",
              ""frameworks"": {
                ""net46"": {
                }
              }
            }";

            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"));
                var project2      = new DirectoryInfo(Path.Combine(workingDir, "projects", "project2"));
                packagesDir.Create();
                packageSource.Create();
                project1.Create();
                project2.Create();

                File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json);
                File.WriteAllText(Path.Combine(project2.FullName, "project.json"), project2Json);

                var specPath1 = Path.Combine(project1.FullName, "project.json");
                var specPath2 = Path.Combine(project2.FullName, "project.json");
                var spec1     = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1);
                var spec2     = JsonPackageSpecReader.GetPackageSpec(project2Json, "project2", specPath2);

                var logger = new TestLogger();

                var restoreContext = new RestoreArgs()
                {
                    Sources = new List <string>()
                    {
                        packageSource.FullName
                    },
                    GlobalPackagesFolder = packagesDir.FullName,
                    Log          = logger,
                    CacheContext = new SourceCacheContext()
                };

                // Modify specs for netcore
                spec2 = spec2.WithTestRestoreMetadata();
                spec1 = spec1.WithTestRestoreMetadata().WithTestProjectReference(spec2);

                var request = await ProjectJsonTestHelpers.GetRequestAsync(restoreContext, spec1, spec2);

                request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json");

                // Act
                var command = new RestoreCommand(request);
                var result  = await command.ExecuteAsync();

                var lockFile = result.LockFile;
                await result.CommitAsync(logger, CancellationToken.None);

                var issue = result.CompatibilityCheckResults.SelectMany(ccr => ccr.Issues).Single();

                // Assert
                Assert.False(result.Success);
                Assert.Equal(0, result.GetAllUnresolved().Count);
                Assert.Equal("Project project2 is not compatible with net45 (.NETFramework,Version=v4.5). Project project2 supports: net46 (.NETFramework,Version=v4.6)", issue.Format());
                Assert.Equal(1, logger.ErrorMessages.Count());

                Assert.Contains("Project project2 is not compatible with net45 (.NETFramework,Version=v4.5). Project project2 supports: net46 (.NETFramework,Version=v4.6)", logger.ErrorMessages.Last());
            }
        }
        public async Task Project2ProjectInLockFile_VerifyProjectsReferencesInLibAndTargets()
        {
            // Arrange
            var sources = new List <PackageSource>();

            var project1Json = @"
            {
                ""version"": ""1.0.0"",
                ""frameworks"": {
                    ""net45"": {}
                }
            }";

            var project2Json = @"
            {
                ""version"": ""1.0.0"",
                ""frameworks"": {
                    ""net45"": {}
                }
            }";

            var project3Json = @"
            {
                ""version"": ""1.0.0"",
                ""dependencies"": {
                },
                ""frameworks"": {
                    ""net45"": {}
                }
            }";

            using (var packagesDir = TestDirectory.Create())
                using (var workingDir = TestDirectory.Create())
                {
                    var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1"));
                    var project2 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project2"));
                    var project3 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project3"));
                    project1.Create();
                    project2.Create();
                    project3.Create();

                    File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json);
                    File.WriteAllText(Path.Combine(project2.FullName, "project.json"), project2Json);
                    File.WriteAllText(Path.Combine(project3.FullName, "project.json"), project3Json);

                    var project1Path = Path.Combine(project1.FullName, "project1.csproj");
                    var project2Path = Path.Combine(project2.FullName, "project2.csproj");
                    var project3Path = Path.Combine(project3.FullName, "project3.csproj");

                    File.WriteAllText(project1Path, string.Empty);
                    File.WriteAllText(project2Path, string.Empty);
                    File.WriteAllText(project3Path, string.Empty);

                    var specPath1 = Path.Combine(project1.FullName, "project.json");
                    var specPath2 = Path.Combine(project2.FullName, "project.json");
                    var specPath3 = Path.Combine(project3.FullName, "project.json");

                    var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1);
                    var spec2 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project2", specPath2);
                    var spec3 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project3", specPath3);

                    var logger = new TestLogger();

                    var restoreContext = new RestoreArgs()
                    {
                        GlobalPackagesFolder = packagesDir,
                        Log          = logger,
                        CacheContext = new SourceCacheContext()
                    };

                    // Modify specs for netcore
                    spec3          = spec3.WithTestRestoreMetadata();
                    spec3.FilePath = project3Path;
                    spec3.RestoreMetadata.ProjectPath       = project3Path;
                    spec3.RestoreMetadata.ProjectUniqueName = project3Path;

                    spec2          = spec2.WithTestRestoreMetadata().WithTestProjectReference(spec3);
                    spec2.FilePath = project2Path;
                    spec2.RestoreMetadata.ProjectPath       = project2Path;
                    spec2.RestoreMetadata.ProjectUniqueName = project2Path;

                    spec1          = spec1.WithTestRestoreMetadata().WithTestProjectReference(spec2);
                    spec1.FilePath = project1Path;
                    spec1.RestoreMetadata.ProjectPath       = project1Path;
                    spec1.RestoreMetadata.ProjectUniqueName = project1Path;

                    var request = await ProjectJsonTestHelpers.GetRequestAsync(restoreContext, spec1, spec2, spec3);

                    request.LockFilePath = Path.Combine(project1.FullName, "project.assets.json");
                    var format = new LockFileFormat();

                    // Act
                    var command = new RestoreCommand(request);
                    var result  = await command.ExecuteAsync();

                    await result.CommitAsync(logger, CancellationToken.None);

                    Assert.True(result.Success, logger.ShowMessages());

                    var lockFile = format.Read(request.LockFilePath, logger);

                    var project1Lib = lockFile.GetLibrary("project1", NuGetVersion.Parse("1.0.0"));
                    var project2Lib = lockFile.GetLibrary("project2", NuGetVersion.Parse("1.0.0"));
                    var project3Lib = lockFile.GetLibrary("project3", NuGetVersion.Parse("1.0.0"));

                    var project2Target = lockFile.GetTarget(FrameworkConstants.CommonFrameworks.Net45, runtimeIdentifier: null)
                                         .Libraries
                                         .Where(lib => lib.Name == "project2")
                                         .Single();

                    var project3Target = lockFile.GetTarget(FrameworkConstants.CommonFrameworks.Net45, runtimeIdentifier: null)
                                         .Libraries
                                         .Where(lib => lib.Name == "project3")
                                         .Single();

                    // Assert
                    Assert.True(result.Success);
                    Assert.Equal(2, lockFile.Libraries.Count);

                    Assert.Equal("project", project2Lib.Type);
                    Assert.Equal("project", project3Lib.Type);

                    Assert.Equal("../project2/project2.csproj", project2Lib.Path);
                    Assert.Equal("../project3/project3.csproj", project3Lib.Path);

                    Assert.Equal("../project2/project2.csproj", project2Lib.MSBuildProject);
                    Assert.Equal("../project3/project3.csproj", project3Lib.MSBuildProject);

                    Assert.Equal(1, project2Target.Dependencies.Count);
                    Assert.Equal("project3", project2Target.Dependencies.Single().Id);
                    Assert.Equal("1.0.0", project2Target.Dependencies.Single().VersionRange.ToLegacyShortString());

                    Assert.Equal(0, project3Target.Dependencies.Count);

                    Assert.Equal(".NETFramework,Version=v4.5", project2Target.Framework);
                    Assert.Equal(".NETFramework,Version=v4.5", project3Target.Framework);
                }
        }
        public async Task ProjectResolution_ProjectFrameworkAssemblyReferences()
        {
            // Arrange
            var project1Json = @"
            {
              ""version"": ""1.0.0-*"",
              ""description"": """",
              ""authors"": [ ""author"" ],
              ""tags"": [ """" ],
              ""projectUrl"": """",
              ""licenseUrl"": """",
              ""frameworks"": {
                ""net45"": {
                }
              }
            }";

            var project2Json = @"
            {
              ""version"": ""1.0.0"",
              ""description"": """",
              ""authors"": [ ""author"" ],
              ""tags"": [ """" ],
              ""projectUrl"": """",
              ""licenseUrl"": """",
              ""frameworks"": {
                ""net45"": {
                   ""frameworkAssemblies"": {
                     ""ReferenceA"": """",
                     ""ReferenceB"": """",
                   },
                }
              }
            }";

            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"));
                var project2      = new DirectoryInfo(Path.Combine(workingDir, "projects", "project2"));
                packagesDir.Create();
                packageSource.Create();
                project1.Create();
                project2.Create();

                File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json);
                File.WriteAllText(Path.Combine(project2.FullName, "project.json"), project2Json);

                var specPath1 = Path.Combine(project1.FullName, "project.json");
                var specPath2 = Path.Combine(project2.FullName, "project.json");
                var spec1     = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1);
                var spec2     = JsonPackageSpecReader.GetPackageSpec(project2Json, "project2", specPath2);

                var logger = new TestLogger();

                var restoreContext = new RestoreArgs()
                {
                    Sources = new List <string>()
                    {
                        packageSource.FullName
                    },
                    GlobalPackagesFolder = packagesDir.FullName,
                    Log          = logger,
                    CacheContext = new SourceCacheContext()
                };

                // Modify specs for netcore
                spec2 = spec2.WithTestRestoreMetadata();
                spec1 = spec1.WithTestRestoreMetadata().WithTestProjectReference(spec2);

                var request = await ProjectJsonTestHelpers.GetRequestAsync(restoreContext, spec1, spec2);

                request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json");

                // Act
                var command = new RestoreCommand(request);
                var result  = await command.ExecuteAsync();

                var lockFile = result.LockFile;
                await result.CommitAsync(logger, CancellationToken.None);

                var target              = result.LockFile.GetTarget(NuGetFramework.Parse("net45"), null);
                var project2Lib         = target.Libraries.Where(lib => lib.Name == "project2").Single();
                var frameworkReferences = project2Lib.FrameworkAssemblies;

                // Assert
                Assert.True(result.Success);
                Assert.Equal(2, frameworkReferences.Count);
                Assert.Equal("ReferenceA", frameworkReferences[0]);
                Assert.Equal("ReferenceB", frameworkReferences[1]);
            }
        }
        public async Task DependencyTypeConstraint_PackagesDependOnProject()
        {
            // Arrange
            var sources = new List <PackageSource>();

            var project1Json = @"
            {
              ""version"": ""1.0.0"",
              ""description"": """",
              ""authors"": [ ""author"" ],
              ""tags"": [ """" ],
              ""projectUrl"": """",
              ""licenseUrl"": """",
              ""dependencies"": {
                ""packageA"": ""1.0.0""
              },
              ""frameworks"": {
                ""net45"": {
                }
              }
            }";

            var packageBProjectJson = @"
            {
              ""version"": ""1.0.0"",
              ""description"": """",
              ""authors"": [ ""author"" ],
              ""tags"": [ """" ],
              ""projectUrl"": """",
              ""licenseUrl"": """",
              ""frameworks"": {
                ""net45"": {
                }
              }
            }";

            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"));
                var packageBProject = new DirectoryInfo(Path.Combine(workingDir, "projects", "packageB"));
                packagesDir.Create();
                packageSource.Create();
                project1.Create();
                packageBProject.Create();
                sources.Add(new PackageSource(packageSource.FullName));

                File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json);
                File.WriteAllText(Path.Combine(packageBProject.FullName, "project.json"), packageBProjectJson);

                var specPath1 = Path.Combine(project1.FullName, "project.json");
                var specPath2 = Path.Combine(packageBProject.FullName, "project.json");
                var spec1     = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1);
                var spec2     = JsonPackageSpecReader.GetPackageSpec(packageBProjectJson, "packageB", specPath2);

                var packageAPath = SimpleTestPackageUtility.CreateFullPackage(
                    packageSource.FullName,
                    "packageA",
                    "1.0.0",
                    new Packaging.Core.PackageDependency[]
                {
                    new Packaging.Core.PackageDependency("packageB", VersionRange.Parse("1.0.0"))
                });

                await GlobalFolderUtility.AddPackageToGlobalFolderAsync(packageAPath, packagesDir);

                var logger = new TestLogger();

                var restoreContext = new RestoreArgs()
                {
                    Sources = new List <string>()
                    {
                        packageSource.FullName
                    },
                    GlobalPackagesFolder = packagesDir.FullName,
                    Log          = logger,
                    CacheContext = new SourceCacheContext()
                };

                // Modify specs for netcore
                spec2 = spec2.WithTestRestoreMetadata();
                spec1 = spec1.WithTestRestoreMetadata().WithTestProjectReference(spec2);

                var request = await ProjectJsonTestHelpers.GetRequestAsync(restoreContext, spec1, spec2);

                request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json");

                // Act
                var command = new RestoreCommand(request);
                var result  = await command.ExecuteAsync();

                var lockFile = result.LockFile;
                await result.CommitAsync(logger, CancellationToken.None);

                // Assert
                Assert.True(result.Success);
                var packageBLib = lockFile.GetLibrary("packageB", NuGetVersion.Parse("1.0.0"));
                Assert.NotNull(packageBLib);
                Assert.Equal(LibraryType.Project, packageBLib.Type);
            }
        }