Merge pull request #4496 from livarcocc/issue_4486

Merging metadata with different values
This commit is contained in:
Livar 2016-10-25 22:26:21 -07:00 committed by GitHub
commit 7149c722b1
2 changed files with 66 additions and 2 deletions

View file

@ -273,8 +273,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
mergedItem.UnionExcludes(existingItem.Excludes()); mergedItem.UnionExcludes(existingItem.Excludes());
mergedItem.UnionExcludes(item.Excludes()); mergedItem.UnionExcludes(item.Excludes());
mergedItem.AddMetadata(existingItem.Metadata); mergedItem.AddMetadata(MergeMetadata(existingItem.Metadata, item.Metadata));
mergedItem.AddMetadata(item.Metadata);
item.RemoveIncludes(commonIncludes); item.RemoveIncludes(commonIncludes);
existingItem.RemoveIncludes(commonIncludes); existingItem.RemoveIncludes(commonIncludes);
@ -289,6 +288,37 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
return mergeResult; return mergeResult;
} }
private ICollection<ProjectMetadataElement> MergeMetadata(
ICollection<ProjectMetadataElement> existingMetadataElements,
ICollection<ProjectMetadataElement> newMetadataElements)
{
var mergedMetadata = new List<ProjectMetadataElement>(existingMetadataElements);
foreach (var newMetadata in newMetadataElements)
{
var existingMetadata = mergedMetadata.FirstOrDefault(m =>
m.Name.Equals(newMetadata.Name, StringComparison.OrdinalIgnoreCase));
if (existingMetadata == null)
{
mergedMetadata.Add(newMetadata);
}
else
{
MergeMetadata(existingMetadata, newMetadata);
}
}
return mergedMetadata;
}
public void MergeMetadata(ProjectMetadataElement existingMetadata, ProjectMetadataElement newMetadata)
{
if (existingMetadata.Value != newMetadata.Value)
{
existingMetadata.Value = string.Join(";", new [] { existingMetadata.Value, newMetadata.Value });
}
}
private IEnumerable<ProjectItemElement> FindExistingItemsWithSameCondition( private IEnumerable<ProjectItemElement> FindExistingItemsWithSameCondition(
ProjectItemElement item, ProjectItemElement item,
ProjectRootElement project, ProjectRootElement project,

View file

@ -260,6 +260,40 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
contentItems.First().GetMetadataWithName("PackagePath").Value.Should().BeEmpty(); contentItems.First().GetMetadataWithName("PackagePath").Value.Should().BeEmpty();
} }
[Fact]
public void Migrating_same_file_with_multiple_mappings_string_joins_the_mappings_in_PackagePath()
{
var mockProj = RunPackOptionsRuleOnPj(@"
{
""packOptions"": {
""files"": {
""include"": [""path/to/some/file.cs""],
""mappings"": {
""other/path/file.cs"": ""path/to/some/file.cs"",
""different/path/file1.cs"": ""path/to/some/file.cs""
}
}
}
}");
var expectedPackagePath = string.Join(
";",
new [] {
Path.Combine("different", "path"),
Path.Combine("other", "path")
});
var contentItems = mockProj.Items
.Where(item => item.ItemType.Equals("Content", StringComparison.Ordinal))
.Where(item =>
item.GetMetadataWithName("Pack").Value == "true" &&
item.GetMetadataWithName("PackagePath") != null);
contentItems.Count().Should().Be(1);
contentItems.First().Include.Should().Be(@"path\to\some\file.cs");
contentItems.First().GetMetadataWithName("PackagePath").Value.Should().Be(expectedPackagePath);
}
private ProjectRootElement RunPackOptionsRuleOnPj(string packOptions, string testDirectory = null) private ProjectRootElement RunPackOptionsRuleOnPj(string packOptions, string testDirectory = null)
{ {
testDirectory = testDirectory ?? Temp.CreateDirectory().Path; testDirectory = testDirectory ?? Temp.CreateDirectory().Path;