Exemplo n.º 1
0
        private ProjectRootElement ValidateProject(LockFile lockFile, Action <GenerateLockedPackageReferencesFile> modifier, out MockBuildEngine buildEngine)
        {
            buildEngine = new MockBuildEngine();

            GenerateLockedPackageReferencesFile task = new GenerateLockedPackageReferencesFile
            {
                BuildEngine       = buildEngine,
                ProjectAssetsFile = GetAssetsFilePath(lockFile),
            };

            modifier?.Invoke(task);

            task.TryCreateProject(out ProjectRootElement project).ShouldBeTrue();

            ProjectPropertyGroupElement propertyGroupElement = project.PropertyGroups.ShouldHaveSingleItem();

            ProjectPropertyElement propertyElement = propertyGroupElement.Properties.FirstOrDefault(i => i.Name.Equals("NuGetDeterministicPropsWasImported"));

            propertyElement.ShouldNotBeNull();

            propertyElement.Value.ShouldBe("true");

            HashSet <string> excludes = new HashSet <string>(task.PackagesToExclude == null ? Enumerable.Empty <string>() : task.PackagesToExclude.Select(i => i.ItemSpec), StringComparer.OrdinalIgnoreCase);

            foreach (TargetFrameworkInformation targetFramework in lockFile.PackageSpec.TargetFrameworks)
            {
                if (!task.ExcludeImplicitReferences && targetFramework.Dependencies.Any(i => !excludes.Contains(i.Name) && i.AutoReferenced))
                {
                    ProjectPropertyElement disableImplicitFrameworkReferencesPropertyElement = lockFile.PackageSpec.TargetFrameworks.Count > 1 ? project.Properties.FirstOrDefault(i => i.Name.Equals("DisableImplicitFrameworkReferences") && i.Condition.Contains($"'{targetFramework.FrameworkName.GetShortFolderName()}'")) : project.Properties.FirstOrDefault(i => i.Name.Equals("DisableImplicitFrameworkReferences") && i.Condition.Equals(String.Empty));

                    disableImplicitFrameworkReferencesPropertyElement.ShouldNotBeNull();
                }
            }

            ProjectElement secondElement = project.Children.Skip(1).FirstOrDefault();

            ProjectImportElement beforeImportElement = secondElement.ShouldBeOfType <ProjectImportElement>();

            beforeImportElement.Project.ShouldBe("Before.$(MSBuildThisFile)");
            beforeImportElement.Condition.ShouldBe("Exists('Before.$(MSBuildThisFile)')");

            ProjectElement lastElement = project.Children.LastOrDefault();

            ProjectImportElement afterImportElement = lastElement.ShouldBeOfType <ProjectImportElement>();

            afterImportElement.Project.ShouldBe("After.$(MSBuildThisFile)");
            afterImportElement.Condition.ShouldBe("Exists('After.$(MSBuildThisFile)')");

            return(project);
        }