public void ItReturnsCorrectHierarchyOfDependenciesThatHaveChildren()
        {
            // Arrange
            // target definitions
            var mockTarget = new MockTaskItem(
                itemSpec: ".Net Framework,Version=v4.5",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.RuntimeIdentifier, "net45" },
                { MetadataKeys.TargetFrameworkMoniker, ".Net Framework,Version=v4.5" },
                { MetadataKeys.FrameworkName, ".Net Framework" },
                { MetadataKeys.FrameworkVersion, "4.5" },
                { MetadataKeys.Type, "Target" }
            });

            // package definitions
            var mockPackage = new MockTaskItem(
                itemSpec: "Package3/1.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "Package3" },
                { MetadataKeys.Version, "1.0.0" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "Package" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" },
                { PreprocessPackageDependenciesDesignTime.DependenciesMetadata, "ChildPackage1/1.0.0;ChildPackage2/2.0.0" },
                { MetadataKeys.IsTopLevelDependency, "True" }
            });

            var mockChildPackage1 = new MockTaskItem(
                itemSpec: "ChildPackage1/1.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "ChildPackage1" },
                { MetadataKeys.Version, "1.0.0" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "Package" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" },
                { PreprocessPackageDependenciesDesignTime.DependenciesMetadata, "ChildPackage11/1.0.0" },
                { MetadataKeys.IsTopLevelDependency, "False" }
            });

            var mockChildPackage11 = new MockTaskItem(
                itemSpec: "ChildPackage11/1.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "ChildPackage11" },
                { MetadataKeys.Version, "1.0.0" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "Package" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" },
                { MetadataKeys.IsTopLevelDependency, "False" }
            });

            var mockChildPackage2 = new MockTaskItem(
                itemSpec: "ChildPackage2/2.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "ChildPackage2" },
                { MetadataKeys.Version, "2.0.0" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "Package" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" },
                { MetadataKeys.IsTopLevelDependency, "False" }
            });

            // package dependencies
            var mockPackageDep = new MockTaskItem(
                itemSpec: "Package3/1.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" }
            });

            var mockChildPackageDep1 = new MockTaskItem(
                itemSpec: "ChildPackage1/1.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" },
                { MetadataKeys.ParentPackage, "Package3/1.0.0" }
            });

            var mockChildPackageDep11 = new MockTaskItem(
                itemSpec: "ChildPackage11/1.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" },
                { MetadataKeys.ParentPackage, "ChildPackage1/1.0.0" }
            });

            var mockChildPackageDep2 = new MockTaskItem(
                itemSpec: "ChildPackage2/2.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" },
                { MetadataKeys.ParentPackage, "Package3/1.0.0" }
            });

            var task = new PreprocessPackageDependenciesDesignTime();

            task.TargetDefinitions  = new[] { mockTarget };
            task.PackageDefinitions = new ITaskItem[] {
                mockPackage,
                mockChildPackage1,
                mockChildPackage11,
                mockChildPackage2
            };
            task.FileDefinitions     = new ITaskItem[] { };
            task.PackageDependencies = new ITaskItem[] {
                mockPackageDep,
                mockChildPackageDep1,
                mockChildPackageDep11,
                mockChildPackageDep2
            };
            task.FileDependencies  = new ITaskItem[] { };
            task.PackageReferences = new ITaskItem[] { };

            // Act
            var result = task.Execute();

            // Assert
            result.Should().BeTrue();
            task.DependenciesDesignTime.Count().Should().Be(5);

            var resultTargets = task.DependenciesDesignTime
                                .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5")).ToArray();

            resultTargets.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.Target, mockTarget, resultTargets[0]);

            mockPackage.SetMetadata(MetadataKeys.Path, mockPackage.GetMetadata(MetadataKeys.ResolvedPath));
            var resultPackage = task.DependenciesDesignTime
                                .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5/Package3/1.0.0")).ToArray();

            resultPackage.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.Package, mockPackage, resultPackage[0]);

            mockChildPackage1.SetMetadata(MetadataKeys.Path, mockChildPackage1.GetMetadata(MetadataKeys.ResolvedPath));
            var resultChildPackage1 = task.DependenciesDesignTime
                                      .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5/ChildPackage1/1.0.0")).ToArray();

            resultChildPackage1.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.Package, mockChildPackage1, resultChildPackage1[0]);

            mockChildPackage11.SetMetadata(MetadataKeys.Path, mockChildPackage11.GetMetadata(MetadataKeys.ResolvedPath));
            var resultChildPackage11 = task.DependenciesDesignTime
                                       .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5/ChildPackage11/1.0.0")).ToArray();

            resultChildPackage11.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.Package, mockChildPackage11, resultChildPackage11[0]);

            mockChildPackage2.SetMetadata(MetadataKeys.Path, mockChildPackage2.GetMetadata(MetadataKeys.ResolvedPath));
            var resultChildPackage2 = task.DependenciesDesignTime
                                      .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5/ChildPackage2/2.0.0")).ToArray();

            resultChildPackage2.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.Package, mockChildPackage2, resultChildPackage2[0]);
        }
        public void ItShouldReturnCorrectHierarchyWhenPackageHasChildAssemblyOrAnalyzerDependencies()
        {
            // Arrange
            // target definitions
            var mockTarget = new MockTaskItem(
                itemSpec: ".Net Framework,Version=v4.5",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.RuntimeIdentifier, "net45" },
                { MetadataKeys.TargetFrameworkMoniker, ".Net Framework,Version=v4.5" },
                { MetadataKeys.FrameworkName, ".Net Framework" },
                { MetadataKeys.FrameworkVersion, "4.5" },
                { MetadataKeys.Type, "Target" }
            });

            // package definitions
            var mockPackage = new MockTaskItem(
                itemSpec: "Package3/1.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "Package3" },
                { MetadataKeys.Version, "1.0.0" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "Package" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" },
                { PreprocessPackageDependenciesDesignTime.DependenciesMetadata,
                  @"mockChildAssembly1;somepath/mockChildAssembly2;somepath/mockChildAnalyzerAssembly" },
                { MetadataKeys.IsImplicitlyDefined, "True" }
            });

            var mockChildAssembly1 = new MockTaskItem(
                itemSpec: @"mockChildAssembly1",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "mockChildAssembly1" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "Assembly" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" }
            });

            var mockChildAssembly2 = new MockTaskItem(
                itemSpec: @"somepath/mockChildAssembly2",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "mockChildAssembly2" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "FrameworkAssembly" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" }
            });

            var mockChildAssemblyNoCompileMetadata = new MockTaskItem(
                itemSpec: @"somepath/mockChildAssemblyNoCompileMetadata",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "mockChildAssemblyNoCompileMetadata" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "Assembly" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" }
            });

            var mockChildAnalyzerAssembly = new MockTaskItem(
                itemSpec: @"somepath/mockChildAnalyzerAssembly",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Name, "mockChildAnalyzerAssembly" },
                { MetadataKeys.Path, "some path" },
                { MetadataKeys.ResolvedPath, "some resolved path" },
                { MetadataKeys.Type, "AnalyzerAssembly" },
                { PreprocessPackageDependenciesDesignTime.ResolvedMetadata, "True" }
            });

            // package dependencies
            var mockPackageDep = new MockTaskItem(
                itemSpec: "Package3/1.0.0",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" },
                { MetadataKeys.IsImplicitlyDefined, "True" }
            });

            var mockChildAssemblyDep1 = new MockTaskItem(
                itemSpec: "mockChildAssembly1",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" },
                { MetadataKeys.ParentPackage, "Package3/1.0.0" },
                { MetadataKeys.FileGroup, PreprocessPackageDependenciesDesignTime.CompileTimeAssemblyMetadata }
            });

            var mockChildAssemblyDep2 = new MockTaskItem(
                itemSpec: @"somepath/mockChildAssembly2",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" },
                { MetadataKeys.ParentPackage, "Package3/1.0.0" },
                { MetadataKeys.FileGroup, PreprocessPackageDependenciesDesignTime.CompileTimeAssemblyMetadata }
            });

            var mockChildAssemblyNoCompileMetadataDep = new MockTaskItem(
                itemSpec: "somepath/mockChildAssemblyNoCompileMetadata",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" },
                { MetadataKeys.ParentPackage, "Package3/1.0.0" }
            });

            var mockChildAnalyzerAssemblyDep = new MockTaskItem(
                itemSpec: "somepath/mockChildAnalyzerAssembly",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.ParentTarget, ".Net Framework,Version=v4.5" },
                { MetadataKeys.ParentPackage, "Package3/1.0.0" }
            });

            var task = new PreprocessPackageDependenciesDesignTime();

            task.TargetDefinitions  = new[] { mockTarget };
            task.PackageDefinitions = new ITaskItem[] {
                mockPackage
            };
            task.FileDefinitions = new ITaskItem[] {
                mockChildAssembly1,
                mockChildAssembly2,
                mockChildAssemblyNoCompileMetadata,
                mockChildAnalyzerAssembly
            };
            task.PackageDependencies = new ITaskItem[] {
                mockPackageDep
            };
            task.FileDependencies = new ITaskItem[] {
                mockChildAssemblyDep1,
                mockChildAssemblyDep2,
                mockChildAssemblyNoCompileMetadataDep,
                mockChildAnalyzerAssemblyDep
            };
            task.PackageReferences = new ITaskItem[] { new MockTaskItem(
                                                           itemSpec: "Package3",
                                                           metadata: new Dictionary <string, string>
                {
                    { MetadataKeys.IsImplicitlyDefined, "True" }
                }) };

            // Act
            var result = task.Execute();

            // Assert
            result.Should().BeTrue();
            task.DependenciesDesignTime.Count().Should().Be(5);

            var resultTargets = task.DependenciesDesignTime
                                .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5")).ToArray();

            resultTargets.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.Target, mockTarget, resultTargets[0]);

            mockPackage.SetMetadata(MetadataKeys.Path, mockPackage.GetMetadata(MetadataKeys.ResolvedPath));
            var resultPackage = task.DependenciesDesignTime
                                .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5/Package3/1.0.0")).ToArray();

            resultPackage.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.Package, mockPackage, resultPackage[0]);

            mockChildAssembly1.SetMetadata(MetadataKeys.Path, mockChildAssembly1.GetMetadata(MetadataKeys.ResolvedPath));
            var resultChildAssembly1 = task.DependenciesDesignTime
                                       .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5/mockChildAssembly1")).ToArray();

            resultChildAssembly1.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.Assembly, mockChildAssembly1, resultChildAssembly1[0]);

            mockChildAssembly2.SetMetadata(MetadataKeys.Path, mockChildAssembly2.GetMetadata(MetadataKeys.ResolvedPath));
            var resultChildAssembly2 = task.DependenciesDesignTime
                                       .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5/somepath/mockChildAssembly2")).ToArray();

            resultChildAssembly2.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.FrameworkAssembly, mockChildAssembly2, resultChildAssembly2[0]);

            mockChildAnalyzerAssembly.SetMetadata(MetadataKeys.Path, mockChildAnalyzerAssembly.GetMetadata(MetadataKeys.ResolvedPath));
            var resultChildAnalyzerAssembly = task.DependenciesDesignTime
                                              .Where(x => x.ItemSpec.Equals(".Net Framework,Version=v4.5/somepath/mockChildAnalyzerAssembly")).ToArray();

            resultChildAnalyzerAssembly.Length.Should().Be(1);
            VerifyTargetTaskItem(DependencyType.AnalyzerAssembly, mockChildAnalyzerAssembly, resultChildAnalyzerAssembly[0]);
        }
Beispiel #3
0
        public void ItShouldCombineSdkReferencesWithImplicitPackageReferences()
        {
            // Arrange
            var sdkReference1 = new MockTaskItem(
                itemSpec: "SdkReference1",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Version, "2.0.1" }
            });

            var sdkReference2 = new MockTaskItem(
                itemSpec: "SdkReference2",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Version, "1.0.1" }
            });

            var packageReference1 = new MockTaskItem(
                itemSpec: "tfm1/PackageReference1/3.0.1",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Type, "Package" },
                { MetadataKeys.IsTopLevelDependency, "True" },
                { MetadataKeys.IsImplicitlyDefined, "True" },
                { MetadataKeys.SDKPackageItemSpec, "tfm1/PackageReference1/3.0.1" },
                { MetadataKeys.Name, "PackageReference1" },
                { MetadataKeys.OriginalItemSpec, "PackageReference1" },
                { MetadataKeys.Path, @"x:\folder\subfolder" },
                { MetadataKeys.Version, @"3.0.1" }
            });

            var packageReference1_otherTFM = new MockTaskItem(
                itemSpec: "tfm2/PackageReference1/3.0.1",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Type, "Package" },
                { MetadataKeys.IsTopLevelDependency, "True" },
                { MetadataKeys.IsImplicitlyDefined, "True" },
                { MetadataKeys.SDKPackageItemSpec, "tfm2/PackageReference1/3.0.1" },
                { MetadataKeys.Name, "PackageReference1" },
                { MetadataKeys.OriginalItemSpec, "PackageReference1" },
                { MetadataKeys.Path, @"x:\folder\subfolder\tfm2" }
            });

            var packageReference_Target = new MockTaskItem(
                itemSpec: "packageReference_Target",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Type, "Target" },
                { MetadataKeys.IsTopLevelDependency, "True" },
                { MetadataKeys.IsImplicitlyDefined, "True" },
                { MetadataKeys.Name, "packageReference_Target" }
            });

            var packageReference_Unknown = new MockTaskItem(
                itemSpec: "packageReference_Unknown",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Type, "Xxx" },
                { MetadataKeys.IsTopLevelDependency, "True" },
                { MetadataKeys.IsImplicitlyDefined, "True" },
                { MetadataKeys.Name, "packageReference_Unknown" }
            });

            var packageReference_NotTopLevel = new MockTaskItem(
                itemSpec: "packageReference_NotTopLevel",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Type, "Package" },
                { MetadataKeys.IsTopLevelDependency, "False" },
                { MetadataKeys.IsImplicitlyDefined, "True" },
                { MetadataKeys.Name, "packageReference_NotTopLevel" }
            });

            var packageReference_NotTopLevel2 = new MockTaskItem(
                itemSpec: "packageReference_NotTopLevel2",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Type, "Package" },
                { MetadataKeys.IsTopLevelDependency, "xxxx" },
                { MetadataKeys.IsImplicitlyDefined, "True" },
                { MetadataKeys.Name, "packageReference_NotTopLevel2" }
            });

            var packageReference_NotImplicit = new MockTaskItem(
                itemSpec: "packageReference_NotImplicit",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Type, "Package" },
                { MetadataKeys.IsTopLevelDependency, "True" },
                { MetadataKeys.IsImplicitlyDefined, "False" },
                { MetadataKeys.Name, "packageReference_NotImplicit" }
            });

            var packageReference_NotImplicit2 = new MockTaskItem(
                itemSpec: "packageReference_NotImplicit2",
                metadata: new Dictionary <string, string>
            {
                { MetadataKeys.Type, "Package" },
                { MetadataKeys.IsTopLevelDependency, "True" },
                { MetadataKeys.IsImplicitlyDefined, "Xxxx" },
                { MetadataKeys.Name, "packageReference_NotImplicit2" }
            });

            var task = new CollectResolvedSDKReferencesDesignTime();

            task.ResolvedSdkReferences = new[] {
                sdkReference1,
                sdkReference2
            };
            task.DependenciesDesignTime = new ITaskItem[] {
                packageReference1,
                packageReference1_otherTFM,
                packageReference_Target,
                packageReference_Unknown,
                packageReference_NotTopLevel,
                packageReference_NotTopLevel2,
                packageReference_NotImplicit,
                packageReference_NotImplicit2
            };

            // Act
            var result = task.Execute();

            // Assert
            result.Should().BeTrue();
            task.ResolvedSDKReferencesDesignTime.Count().Should().Be(3);

            VerifyTaskItem(sdkReference1, task.ResolvedSDKReferencesDesignTime[0]);
            VerifyTaskItem(sdkReference2, task.ResolvedSDKReferencesDesignTime[1]);

            var path = packageReference1.GetMetadata(MetadataKeys.Path);

            packageReference1.RemoveMetadata(MetadataKeys.Path);
            packageReference1.SetMetadata(MetadataKeys.SDKRootFolder, path);
            VerifyTaskItem(packageReference1, task.ResolvedSDKReferencesDesignTime[2], "PackageReference1");
        }