From d6d6fc612b0c9566e8e4e4cd003d9f722ba231ca Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Fri, 16 Dec 2016 12:23:07 -0800 Subject: [PATCH] Added a unit test for cleaning empty targets. Added a clean empty targets method to CleanOutputProjectRule that removes any targets without content inside of them from the csproj. --- .../Rules/CleanOutputProjectRule.cs | 25 +++++++----- .../GivenThatIWantToCleanTheOutputProject.cs | 38 +++++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToCleanTheOutputProject.cs diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/CleanOutputProjectRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/CleanOutputProjectRule.cs index 175809ca0..f31be9ba2 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/CleanOutputProjectRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/CleanOutputProjectRule.cs @@ -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 containers) + { + foreach (var container in containers) + { + container.RemoveIfEmpty(); + } + } } } diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToCleanTheOutputProject.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToCleanTheOutputProject.cs new file mode 100644 index 000000000..a5ee4c588 --- /dev/null +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToCleanTheOutputProject.cs @@ -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(), mockProj, null, null); + var cleanOutputProjectRule = new CleanOutputProjectRule(); + + cleanOutputProjectRule.Apply(null, migrationRuleInputs); + + mockProj.Targets.Should().HaveCount(c => c == 1); + } + } +} \ No newline at end of file