Merge pull request #5062 from livarcocc/clean_empty_targets_migration

Added a clean empty targets method to CleanOutputProjectRule
This commit is contained in:
Livar 2016-12-16 18:07:37 -08:00 committed by GitHub
commit a9ca6d8c5b
2 changed files with 54 additions and 9 deletions

View file

@ -22,19 +22,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
CleanEmptyPropertiesAndItems(outputProject);
CleanPropertiesThatDontChangeValue(outputProject);
CleanEmptyPropertyAndItemGroups(outputProject);
CleanEmptyTargets(outputProject);
}
private void CleanEmptyPropertyAndItemGroups(ProjectRootElement msbuildProject)
{
foreach (var propertyGroup in msbuildProject.PropertyGroups)
{
propertyGroup.RemoveIfEmpty();
}
foreach (var itemGroup in msbuildProject.ItemGroups)
{
itemGroup.RemoveIfEmpty();
}
CleanEmptyProjectElementContainers(msbuildProject.PropertyGroups);
CleanEmptyProjectElementContainers(msbuildProject.ItemGroups);
}
private void CleanEmptyPropertiesAndItems(ProjectRootElement msbuildProject)
@ -69,5 +63,18 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
}
}
}
private void CleanEmptyTargets(ProjectRootElement msbuildProject)
{
CleanEmptyProjectElementContainers(msbuildProject.Targets);
}
private void CleanEmptyProjectElementContainers(IEnumerable<ProjectElementContainer> containers)
{
foreach (var container in containers)
{
container.RemoveIfEmpty();
}
}
}
}

View file

@ -0,0 +1,38 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Linq;
using FluentAssertions;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
using Microsoft.DotNet.ProjectJsonMigration;
using System;
using System.IO;
using Microsoft.Build.Construction;
using Microsoft.DotNet.ProjectJsonMigration.Rules;
using Microsoft.DotNet.Internal.ProjectModel;
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
public class GivenThatIWantToCleanTheOutputProject : TestBase
{
[Fact]
public void ItRemovesEmptyTargetsFromTheProject()
{
var mockProj = ProjectRootElement.Create();
var target = mockProj.CreateTargetElement("Test");
mockProj.AppendChild(target);
target.AddTask("Exec");
var targetToRemove = mockProj.CreateTargetElement("ToRemove");
mockProj.AppendChild(targetToRemove);
var migrationRuleInputs = new MigrationRuleInputs(Enumerable.Empty<ProjectContext>(), mockProj, null, null);
var cleanOutputProjectRule = new CleanOutputProjectRule();
cleanOutputProjectRule.Apply(null, migrationRuleInputs);
mockProj.Targets.Should().HaveCount(c => c == 1);
}
}
}