From 0349aed9a740838da1695091f948b8af8500c540 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Wed, 30 Nov 2016 12:07:13 -0800 Subject: [PATCH] complete tests --- .../WithDoubledRef/WithDoubledRef.cs | 3 + .../WithDoubledRef/WithDoubledRef.csproj | 27 ++ .../ProjectRootElementExtensions.cs | 3 +- test/Msbuild.Tests.Utilities/TestSetup.cs | 1 + .../GivenDotnetRemoveP2P.cs | 292 +++++++++++++++++- 5 files changed, 313 insertions(+), 13 deletions(-) create mode 100644 TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.cs create mode 100644 TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.csproj diff --git a/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.cs b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.cs new file mode 100644 index 000000000..0ba94d2f9 --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.cs @@ -0,0 +1,3 @@ +public class WithDoubledRef +{ +} \ No newline at end of file diff --git a/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.csproj b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.csproj new file mode 100644 index 000000000..a60b1e748 --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.csproj @@ -0,0 +1,27 @@ + + + + Library + net451;netcoreapp1.0 + + + + + + + + 1.0.0-alpha-20161104-2 + All + + + + + 1.0.1 + + + + + + + + diff --git a/test/Msbuild.Tests.Utilities/ProjectRootElementExtensions.cs b/test/Msbuild.Tests.Utilities/ProjectRootElementExtensions.cs index ddf278f4c..bd960bb92 100644 --- a/test/Msbuild.Tests.Utilities/ProjectRootElementExtensions.cs +++ b/test/Msbuild.Tests.Utilities/ProjectRootElementExtensions.cs @@ -57,7 +57,8 @@ namespace Msbuild.Tests.Utilities string itemType, string includePattern) { - return root.Items.Where((it) => it.ItemType == itemType && it.Include.Contains(includePattern)); + return root.Items.Where((it) => it.ItemType == itemType && it.Include.Contains(includePattern) + && it.ConditionChain().Count() == 0); } public static int NumberOfProjectReferencesWithIncludeContaining( diff --git a/test/Msbuild.Tests.Utilities/TestSetup.cs b/test/Msbuild.Tests.Utilities/TestSetup.cs index 091e91176..eaf2c7d79 100644 --- a/test/Msbuild.Tests.Utilities/TestSetup.cs +++ b/test/Msbuild.Tests.Utilities/TestSetup.cs @@ -13,6 +13,7 @@ namespace Msbuild.Tests.Utilities public string TestRoot { get; private set; } private const string ValidRef = "ValidRef"; + public string ValidRefDir => Path.Combine(TestRoot, ValidRef); public string ValidRefCsprojName => $"{ValidRef}.csproj"; public string ValidRefCsprojRelPath => Path.Combine(ValidRef, ValidRefCsprojName); public string ValidRefCsprojPath => Path.Combine(TestRoot, ValidRefCsprojRelPath); diff --git a/test/dotnet-remove-p2p.Tests/GivenDotnetRemoveP2P.cs b/test/dotnet-remove-p2p.Tests/GivenDotnetRemoveP2P.cs index 807895233..7a3086cdd 100644 --- a/test/dotnet-remove-p2p.Tests/GivenDotnetRemoveP2P.cs +++ b/test/dotnet-remove-p2p.Tests/GivenDotnetRemoveP2P.cs @@ -52,6 +52,35 @@ namespace Microsoft.DotNet.Cli.Remove.P2P.Tests return dir; } + private ProjDir GetLibRef(TestSetup setup) + { + return new ProjDir(setup.LibDir); + } + + private ProjDir AddLibRef(TestSetup setup, ProjDir proj, string additionalArgs = "") + { + var ret = GetLibRef(setup); + new AddP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(proj.CsProjPath) + .Execute($"{additionalArgs} \"{ret.CsProjPath}\"") + .Should().Pass(); + + return ret; + } + + private ProjDir AddValidRef(TestSetup setup, ProjDir proj, string frameworkArg = "") + { + var ret = new ProjDir(setup.ValidRefDir); + new AddP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(proj.CsProjPath) + .Execute($"{frameworkArg} \"{ret.CsProjPath}\"") + .Should().Pass(); + + return ret; + } + [Theory] [InlineData("--help")] [InlineData("-h")] @@ -122,49 +151,288 @@ namespace Microsoft.DotNet.Cli.Remove.P2P.Tests [Fact] public void ItRemovesRefWithoutCondAndPrintsStatus() { - throw new NotImplementedException(); + var lib = NewLib(); + var setup = Setup(); + var libref = AddLibRef(setup, lib); + + int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{libref.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeContaining(libref.Name).Should().Be(0); } [Fact] - public void ItRemovesRefWithConAndPrintsStatus() + public void ItRemovesRefWithCondAndPrintsStatus() { - throw new NotImplementedException(); + var lib = NewLib(); + var setup = Setup(); + var libref = AddLibRef(setup, lib, FrameworkNet451Arg); + + int condBefore = lib.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"{FrameworkNet451Arg} \"{libref.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(libref.Name, ConditionFrameworkNet451).Should().Be(0); } [Fact] - public void WhenTwoRefsArePresentItDoesNotRemoveBoth() + public void WhenTwoDifferentRefsArePresentItDoesNotRemoveBoth() { - throw new NotImplementedException(); + var lib = NewLib(); + var setup = Setup(); + var libref = AddLibRef(setup, lib); + var validref = AddValidRef(setup, lib); + + int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{libref.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + cmd.StdOut.Should().NotContain(validref.Name); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore); + csproj.NumberOfProjectReferencesWithIncludeContaining(libref.Name).Should().Be(0); } [Fact] public void WhenRefWithoutCondIsNotThereItPrintsMessage() { - throw new NotImplementedException(); + var lib = NewLib(); + var setup = Setup(); + var libref = GetLibRef(setup); + + string csprojContetntBefore = lib.CsProjContent(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{libref.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().Contain("could not be found"); + lib.CsProjContent().Should().BeEquivalentTo(csprojContetntBefore); } [Fact] public void WhenRefWithCondIsNotThereItPrintsMessage() { - throw new NotImplementedException(); + var lib = NewLib(); + var setup = Setup(); + var libref = GetLibRef(setup); + + string csprojContetntBefore = lib.CsProjContent(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"{FrameworkNet451Arg} \"{libref.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().Contain("could not be found"); + lib.CsProjContent().Should().BeEquivalentTo(csprojContetntBefore); } [Fact] - public void WhenRefWithCondIsPresentAndRemovingNoCondItDoesNotRemoveIt() + public void WhenRefWithAndWithoutCondArePresentAndRemovingNoCondItDoesNotRemoveOther() { - throw new NotImplementedException(); + var lib = NewLib(); + var setup = Setup(); + var librefCond = AddLibRef(setup, lib, FrameworkNet451Arg); + var librefNoCond = AddLibRef(setup, lib); + + var csprojBefore = lib.CsProj(); + int noCondBefore = csprojBefore.NumberOfItemGroupsWithoutCondition(); + int condBefore = csprojBefore.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{librefNoCond.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeContaining(librefNoCond.Name).Should().Be(0); + + csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condBefore); + csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(librefCond.Name, ConditionFrameworkNet451).Should().Be(1); } [Fact] - public void WhenRefWithoutCondIsPresentAndRemovingRefWithCondItDoesNotRemoveIt() + public void WhenRefWithAndWithoutCondArePresentAndRemovingCondItDoesNotRemoveOther() { - throw new NotImplementedException(); + var lib = NewLib(); + var setup = Setup(); + var librefCond = AddLibRef(setup, lib, FrameworkNet451Arg); + var librefNoCond = AddLibRef(setup, lib); + + var csprojBefore = lib.CsProj(); + int noCondBefore = csprojBefore.NumberOfItemGroupsWithoutCondition(); + int condBefore = csprojBefore.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"{FrameworkNet451Arg} \"{librefCond.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore); + csproj.NumberOfProjectReferencesWithIncludeContaining(librefNoCond.Name).Should().Be(1); + + csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(librefCond.Name, ConditionFrameworkNet451).Should().Be(0); } [Fact] public void WhenRefWithDifferentCondIsPresentItDoesNotRemoveIt() { - throw new NotImplementedException(); + var lib = NewLib(); + var setup = Setup(); + var librefCondNet451 = AddLibRef(setup, lib, FrameworkNet451Arg); + var librefCondNetCoreApp10 = AddLibRef(setup, lib, FrameworkNetCoreApp10Arg); + + var csprojBefore = lib.CsProj(); + int condNet451Before = csprojBefore.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451); + int condNetCoreApp10Before = csprojBefore.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNetCoreApp10); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"{FrameworkNet451Arg} \"{librefCondNet451.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condNet451Before - 1); + csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(librefCondNet451.Name, ConditionFrameworkNet451).Should().Be(0); + + csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNetCoreApp10).Should().Be(condNetCoreApp10Before); + csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(librefCondNetCoreApp10.Name, ConditionFrameworkNetCoreApp10).Should().Be(1); + } + + [Fact] + public void WhenDuplicateReferencesArePresentItRemovesThemAll() + { + var setup = Setup(); + var proj = new ProjDir(Path.Combine(setup.TestRoot, "WithDoubledRef")); + var libref = GetLibRef(setup); + + int noCondBefore = proj.CsProj().NumberOfItemGroupsWithoutCondition(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(proj.CsProjPath) + .Execute($"\"{libref.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + + var csproj = proj.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeContaining(libref.Name).Should().Be(0); + } + + [Fact] + public void WhenPassingRefWithRelPathItRemovesRefWithAbsolutePath() + { + var setup = Setup(); + var lib = GetLibRef(setup); + var libref = AddValidRef(setup, lib, "--force"); + + int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(lib.Path) + .WithProject(lib.CsProjPath) + .Execute($"\"{setup.ValidRefCsprojRelToOtherProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeContaining(libref.Name).Should().Be(0); + } + + [Fact] + public void WhenPassingRefWithRelPathToProjectItRemovesRefWithPathRelToProject() + { + var setup = Setup(); + var lib = GetLibRef(setup); + var libref = AddValidRef(setup, lib); + + int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{setup.ValidRefCsprojRelToOtherProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeContaining(libref.Name).Should().Be(0); + } + + [Fact] + public void WhenPassingRefWithAbsolutePathItRemovesRefWithRelPath() + { + var setup = Setup(); + var lib = GetLibRef(setup); + var libref = AddValidRef(setup, lib); + + int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{setup.ValidRefCsprojPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeContaining(libref.Name).Should().Be(0); + } + + [Fact] + public void WhenPassingMultipleReferencesItRemovesThemAll() + { + var lib = NewLib(); + var setup = Setup(); + var libref = AddLibRef(setup, lib); + var validref = AddValidRef(setup, lib); + + int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{libref.CsProjPath}\" \"{validref.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeContaining(libref.Name).Should().Be(0); + csproj.NumberOfProjectReferencesWithIncludeContaining(validref.Name).Should().Be(0); + } + + [Fact] + public void WhenPassingMultipleReferencesAndOneOfThemDoesNotExistItRemovesOne() + { + var lib = NewLib(); + var setup = Setup(); + var libref = GetLibRef(setup); + var validref = AddValidRef(setup, lib); + + int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition(); + var cmd = new RemoveP2PCommand() + .WithWorkingDirectory(setup.TestRoot) + .WithProject(lib.CsProjPath) + .Execute($"\"{libref.CsProjPath}\" \"{validref.CsProjPath}\""); + cmd.Should().Pass(); + cmd.StdOut.Should().MatchRegex("(^|[\r\n])Project reference[^\r\n]*removed.($|[\r\n])"); + cmd.StdOut.Should().Contain("could not be found"); + var csproj = lib.CsProj(); + csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1); + csproj.NumberOfProjectReferencesWithIncludeContaining(validref.Name).Should().Be(0); } } }