dotnet-installer/test/dotnet-add-reference.Tests/GivenDotnetAddReference.cs

698 lines
33 KiB
C#
Raw Normal View History

// 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.
2016-11-22 22:41:56 +00:00
using FluentAssertions;
using Microsoft.Build.Construction;
using Microsoft.DotNet.Tools;
2016-11-22 22:41:56 +00:00
using Microsoft.DotNet.Tools.Test.Utilities;
using Msbuild.Tests.Utilities;
using System;
using System.IO;
2016-12-08 22:56:31 +00:00
using System.Linq;
using Xunit;
2017-01-06 22:08:50 +00:00
namespace Microsoft.DotNet.Cli.Add.Reference.Tests
{
2017-01-06 22:08:50 +00:00
public class GivenDotnetAddReference : TestBase
{
private const string HelpText = @"Usage: dotnet add <PROJECT> reference [options] <args>
2016-12-16 09:04:09 +00:00
Arguments:
<PROJECT> The project file to operate on. If a file is not specified, the command will search the current directory for one.
<args> Project to project references to add
2016-12-16 09:04:09 +00:00
Options:
-h, --help Show help information.
-f, --framework <FRAMEWORK> Add reference only when targeting a specific framework
2016-12-16 09:04:09 +00:00
";
private const string AddCommandHelpText = @"Usage: dotnet add [options] <PROJECT> [command]
Arguments:
<PROJECT> The project file to operate on. If a file is not specified, the command will search the current directory for one.
Options:
-h, --help Show help information.
Commands:
package <PACKAGE_NAME> .NET Add Package reference Command
reference <args> .NET Add Project to Project reference Command
";
const string FrameworkNet451Arg = "-f net451";
const string ConditionFrameworkNet451 = "== 'net451'";
const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0";
const string ConditionFrameworkNetCoreApp10 = "== 'netcoreapp1.0'";
static readonly string ProjectNotCompatibleErrorMessageRegEx = string.Format(CommonLocalizableStrings.ProjectNotCompatibleWithFrameworks, "[^`]*");
static readonly string ProjectDoesNotTargetFrameworkErrorMessageRegEx = string.Format(CommonLocalizableStrings.ProjectDoesNotTargetFramework, "[^`]*", "[^`]*");
2016-12-08 22:56:31 +00:00
static readonly string[] DefaultFrameworks = new string[] { "netcoreapp1.0", "net451" };
private TestSetup Setup([System.Runtime.CompilerServices.CallerMemberName] string callingMethod = nameof(Setup), string identifier = "")
{
return new TestSetup(
2016-11-22 22:41:56 +00:00
TestAssets.Get(TestSetup.TestGroup, TestSetup.ProjectName)
2016-11-23 21:46:44 +00:00
.CreateInstance(callingMethod: callingMethod, identifier: identifier)
2016-11-22 22:41:56 +00:00
.WithSourceFiles()
.Root
.FullName);
}
private ProjDir NewDir([System.Runtime.CompilerServices.CallerMemberName] string callingMethod = nameof(NewDir), string identifier = "")
{
return new ProjDir(TestAssets.CreateTestDirectory(callingMethod: callingMethod, identifier: identifier).FullName);
}
private ProjDir NewLib(string dir = null, [System.Runtime.CompilerServices.CallerMemberName] string callingMethod = nameof(NewDir), string identifier = "")
{
var projDir = dir == null ? NewDir(callingMethod: callingMethod, identifier: identifier) : new ProjDir(dir);
try
{
string args = $"classlib -o \"{projDir.Path}\" --debug:ephemeral-hive --no-restore";
new NewCommandShim()
.WithWorkingDirectory(projDir.Path)
.ExecuteWithCapturedOutput(args)
.Should().Pass();
}
catch (System.ComponentModel.Win32Exception e)
{
throw new Exception($"Intermittent error in `dotnet new` occurred when running it in dir `{projDir.Path}`\nException:\n{e}");
}
return projDir;
}
2016-12-08 22:56:31 +00:00
private static void SetTargetFrameworks(ProjDir proj, string[] frameworks)
{
var csproj = proj.CsProj();
csproj.AddProperty("TargetFrameworks", string.Join(";", frameworks));
csproj.Save();
}
private ProjDir NewLibWithFrameworks(string dir = null, [System.Runtime.CompilerServices.CallerMemberName] string callingMethod = nameof(NewDir), string identifier = "")
2016-12-08 22:56:31 +00:00
{
var ret = NewLib(dir, callingMethod: callingMethod, identifier: identifier);
2016-12-08 22:56:31 +00:00
SetTargetFrameworks(ret, DefaultFrameworks);
return ret;
}
[Theory]
[InlineData("--help")]
[InlineData("-h")]
public void WhenHelpOptionIsPassedItPrintsUsage(string helpArg)
{
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand().Execute(helpArg);
cmd.Should().Pass();
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
}
[Theory]
[InlineData("")]
[InlineData("unknownCommandName")]
public void WhenNoCommandIsPassedItPrintsError(string commandName)
{
var cmd = new DotnetCommand()
.ExecuteWithCapturedOutput($"add {commandName}");
cmd.Should().Fail();
cmd.StdErr.Should().Be(CommonLocalizableStrings.RequiredCommandNotPassed);
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(AddCommandHelpText);
}
[Fact]
public void WhenTooManyArgumentsArePassedItPrintsError()
{
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithProject("one two three")
.Execute("proj.csproj");
cmd.ExitCode.Should().NotBe(0);
cmd.StdErr.Should().BeVisuallyEquivalentTo(
"Unrecognized command or argument 'two'\r\nUnrecognized command or argument 'three'");
}
[Theory]
[InlineData("idontexist.csproj")]
[InlineData("ihave?inv@lid/char\\acters")]
public void WhenNonExistingProjectIsPassedItPrintsErrorAndUsage(string projName)
{
var setup = Setup();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(projName)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.ExitCode.Should().NotBe(0);
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindProjectOrDirectory, projName));
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
}
[Fact]
public void WhenBrokenProjectIsPassedItPrintsErrorAndUsage()
{
string projName = "Broken/Broken.csproj";
var setup = Setup();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(projName)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.ExitCode.Should().NotBe(0);
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ProjectIsInvalid, "Broken/Broken.csproj"));
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
}
[Fact]
public void WhenMoreThanOneProjectExistsInTheDirectoryItPrintsErrorAndUsage()
{
var setup = Setup();
2016-12-16 09:04:09 +00:00
var workingDir = Path.Combine(setup.TestRoot, "MoreThanOne");
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
2016-12-16 09:04:09 +00:00
.WithWorkingDirectory(workingDir)
.Execute($"\"{setup.ValidRefCsprojRelToOtherProjPath}\"");
cmd.ExitCode.Should().NotBe(0);
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.MoreThanOneProjectInDirectory, workingDir + Path.DirectorySeparatorChar));
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
}
[Fact]
public void WhenNoProjectsExistsInTheDirectoryItPrintsErrorAndUsage()
{
var setup = Setup();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.ExitCode.Should().NotBe(0);
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory, setup.TestRoot + Path.DirectorySeparatorChar));
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
}
[Fact]
public void ItAddsRefWithoutCondAndPrintsStatus()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
2016-12-16 09:04:09 +00:00
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
cmd.StdErr.Should().BeEmpty();
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojName).Should().Be(1);
}
[Fact]
public void ItAddsRefWithCondAndPrintsStatus()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
int condBefore = lib.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451);
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
cmd.StdErr.Should().BeEmpty();
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(setup.ValidRefCsprojName, ConditionFrameworkNet451).Should().Be(1);
}
[Fact]
public void WhenRefWithoutCondIsPresentItAddsDifferentRefWithoutCond()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
2017-01-06 22:08:50 +00:00
new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{setup.LibCsprojPath}\"")
.Should().Pass();
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(lib.Path)
.WithProject(lib.CsProjName)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojName).Should().Be(1);
}
[Fact]
public void WhenRefWithCondIsPresentItAddsDifferentRefWithCond()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
2017-01-06 22:08:50 +00:00
new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.LibCsprojPath}\"")
.Should().Pass();
int condBefore = lib.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451);
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj")); ;
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condBefore);
csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(setup.ValidRefCsprojName, ConditionFrameworkNet451).Should().Be(1);
}
[Fact]
public void WhenRefWithCondIsPresentItAddsRefWithDifferentCond()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
2017-01-06 22:08:50 +00:00
new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNetCoreApp10Arg} \"{setup.ValidRefCsprojPath}\"")
.Should().Pass();
int condBefore = lib.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451);
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(setup.ValidRefCsprojName, ConditionFrameworkNet451).Should().Be(1);
}
[Fact]
public void WhenRefWithConditionIsPresentItAddsDifferentRefWithoutCond()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
2017-01-06 22:08:50 +00:00
new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.LibCsprojPath}\"")
.Should().Pass();
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojName).Should().Be(1);
}
[Fact]
public void WhenRefWithNoCondAlreadyExistsItDoesntDuplicate()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
2017-01-06 22:08:50 +00:00
new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{setup.ValidRefCsprojPath}\"")
.Should().Pass();
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(lib.Path)
.WithProject(lib.CsProjName)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectAlreadyHasAreference, @"ValidRef\ValidRef.csproj"));
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojName).Should().Be(1);
}
[Fact]
public void WhenRefWithCondOnItemAlreadyExistsItDoesntDuplicate()
{
var setup = Setup();
var proj = new ProjDir(Path.Combine(setup.TestRoot, "WithExistingRefCondOnItem"));
string contentBefore = proj.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(proj.Path)
.WithProject(proj.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.LibCsprojRelPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectAlreadyHasAreference, @"..\Lib\Lib.csproj"));
proj.CsProjContent().Should().BeEquivalentTo(contentBefore);
}
[Fact]
public void WhenRefWithCondOnItemGroupAlreadyExistsItDoesntDuplicate()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
2017-01-06 22:08:50 +00:00
new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.ValidRefCsprojPath}\"")
.Should().Pass();
var csprojContentBefore = lib.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectAlreadyHasAreference, @"ValidRef\ValidRef.csproj"));
lib.CsProjContent().Should().BeEquivalentTo(csprojContentBefore);
}
[Fact]
public void WhenRefWithCondWithWhitespaceOnItemGroupExistsItDoesntDuplicate()
{
var setup = Setup();
var proj = new ProjDir(Path.Combine(setup.TestRoot, "WithExistingRefCondWhitespaces"));
string contentBefore = proj.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(proj.Path)
.WithProject(proj.CsProjName)
.Execute($"{FrameworkNet451Arg} \"{setup.LibCsprojRelPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectAlreadyHasAreference, @"..\Lib\Lib.csproj"));
proj.CsProjContent().Should().BeEquivalentTo(contentBefore);
}
[Fact]
public void WhenRefWithoutCondAlreadyExistsInNonUniformItemGroupItDoesntDuplicate()
{
var setup = Setup();
var proj = new ProjDir(Path.Combine(setup.TestRoot, "WithRefNoCondNonUniform"));
string contentBefore = proj.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(proj.Path)
.WithProject(proj.CsProjName)
.Execute($"\"{setup.LibCsprojRelPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectAlreadyHasAreference, @"..\Lib\Lib.csproj"));
proj.CsProjContent().Should().BeEquivalentTo(contentBefore);
}
[Fact]
public void WhenRefWithoutCondAlreadyExistsInNonUniformItemGroupItAddsDifferentRefInDifferentGroup()
{
var setup = Setup();
var proj = new ProjDir(Path.Combine(setup.TestRoot, "WithRefNoCondNonUniform"));
int noCondBefore = proj.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(proj.CsProjPath)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"..\ValidRef\ValidRef.csproj"));
var csproj = proj.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojName).Should().Be(1);
}
[Fact]
public void WhenRefWithCondAlreadyExistsInNonUniformItemGroupItDoesntDuplicate()
{
var setup = Setup();
var proj = new ProjDir(Path.Combine(setup.TestRoot, "WithRefCondNonUniform"));
string contentBefore = proj.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(proj.Path)
.WithProject(proj.CsProjName)
.Execute($"{FrameworkNet451Arg} \"{setup.LibCsprojRelPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectAlreadyHasAreference, @"..\Lib\Lib.csproj"));
proj.CsProjContent().Should().BeEquivalentTo(contentBefore);
}
[Fact]
public void WhenRefWithCondAlreadyExistsInNonUniformItemGroupItAddsDifferentRefInDifferentGroup()
{
var setup = Setup();
var proj = new ProjDir(Path.Combine(setup.TestRoot, "WithRefCondNonUniform"));
int condBefore = proj.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451);
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(proj.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, "..\\ValidRef\\ValidRef.csproj"));
var csproj = proj.CsProj();
csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(setup.ValidRefCsprojName, ConditionFrameworkNet451).Should().Be(1);
}
[Fact]
public void WhenEmptyItemGroupPresentItAddsRefInIt()
{
var setup = Setup();
var proj = new ProjDir(Path.Combine(setup.TestRoot, "EmptyItemGroup"));
int noCondBefore = proj.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(proj.CsProjPath)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"..\ValidRef\ValidRef.csproj"));
var csproj = proj.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojName).Should().Be(1);
}
[Fact]
public void ItAddsMultipleRefsNoCondToTheSameItemGroup()
{
string OutputText = $@"{string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"Lib\Lib.csproj")}
{string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj")}";
2016-12-16 09:04:09 +00:00
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{setup.LibCsprojPath}\" \"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().BeVisuallyEquivalentTo(OutputText);
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojName).Should().Be(1);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.LibCsprojName).Should().Be(1);
}
[Fact]
public void ItAddsMultipleRefsWithCondToTheSameItemGroup()
{
string OutputText = $@"{string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"Lib\Lib.csproj")}
{string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj")}";
2016-12-16 09:04:09 +00:00
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451);
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{setup.LibCsprojPath}\" \"{setup.ValidRefCsprojPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().BeVisuallyEquivalentTo(OutputText);
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(noCondBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(setup.ValidRefCsprojName, ConditionFrameworkNet451).Should().Be(1);
csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(setup.LibCsprojName, ConditionFrameworkNet451).Should().Be(1);
}
2016-11-21 19:38:25 +00:00
[Fact]
public void WhenProjectNameIsNotPassedItFindsItAndAddsReference()
2016-11-21 19:38:25 +00:00
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
2016-11-21 19:38:25 +00:00
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
2016-11-21 19:38:25 +00:00
.WithWorkingDirectory(lib.Path)
.Execute($"\"{setup.ValidRefCsprojPath}\"");
2016-11-21 19:38:25 +00:00
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
2016-11-21 19:38:25 +00:00
cmd.StdErr.Should().BeEmpty();
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojName).Should().Be(1);
2016-11-21 19:38:25 +00:00
}
[Fact]
2016-11-21 19:38:25 +00:00
public void WhenPassedReferenceDoesNotExistItShowsAnError()
{
2016-12-08 22:56:31 +00:00
var lib = NewLibWithFrameworks();
var contentBefore = lib.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(lib.Path)
.WithProject(lib.CsProjName)
.Execute("\"IDoNotExist.csproj\"");
cmd.Should().Fail();
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ReferenceDoesNotExist, "IDoNotExist.csproj"));
lib.CsProjContent().Should().BeEquivalentTo(contentBefore);
2016-11-21 19:38:25 +00:00
}
[Fact]
2016-11-21 19:38:25 +00:00
public void WhenPassedMultipleRefsAndOneOfthemDoesNotExistItCancelsWholeOperation()
{
2016-12-08 22:56:31 +00:00
var lib = NewLibWithFrameworks();
var setup = Setup();
var contentBefore = lib.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{setup.ValidRefCsprojPath}\" \"IDoNotExist.csproj\"");
cmd.Should().Fail();
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ReferenceDoesNotExist, "IDoNotExist.csproj"));
lib.CsProjContent().Should().BeEquivalentTo(contentBefore);
2016-11-21 19:38:25 +00:00
}
[Fact]
2016-11-21 19:38:25 +00:00
public void WhenPassedReferenceIsUsingSlashesItNormalizesItToBackslashes()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(lib.Path)
.WithProject(lib.CsProjName)
.Execute($"\"{setup.ValidRefCsprojPath.Replace('\\', '/')}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
cmd.StdErr.Should().BeEmpty();
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojRelPath.Replace('/', '\\')).Should().Be(1);
2016-11-21 19:38:25 +00:00
}
[Fact]
public void WhenReferenceIsRelativeAndProjectIsNotInCurrentDirectoryReferencePathIsFixed()
{
var setup = Setup();
var proj = new ProjDir(setup.LibDir);
int noCondBefore = proj.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(setup.LibCsprojPath)
.Execute($"\"{setup.ValidRefCsprojRelPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"..\ValidRef\ValidRef.csproj"));
cmd.StdErr.Should().BeEmpty();
var csproj = proj.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore + 1);
2016-11-23 22:02:19 +00:00
csproj.NumberOfProjectReferencesWithIncludeContaining(setup.ValidRefCsprojRelToOtherProjPath.Replace('/', '\\')).Should().Be(1);
}
2016-12-09 00:23:21 +00:00
[Fact]
2016-12-09 20:11:11 +00:00
public void ItCanAddReferenceWithConditionOnCompatibleFramework()
2016-12-09 00:23:21 +00:00
{
var setup = Setup();
var lib = new ProjDir(setup.LibDir);
var net45lib = new ProjDir(Path.Combine(setup.TestRoot, "Net45Lib"));
int condBefore = lib.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451);
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
2016-12-09 00:23:21 +00:00
.WithProject(lib.CsProjPath)
.Execute($"{FrameworkNet451Arg} \"{net45lib.CsProjPath}\"");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"..\Net45Lib\Net45Lib.csproj"));
2016-12-09 00:23:21 +00:00
var csproj = lib.CsProj();
csproj.NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451).Should().Be(condBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeAndConditionContaining(net45lib.CsProjName, ConditionFrameworkNet451).Should().Be(1);
}
[Fact]
public void ItCanAddRefWithoutCondAndTargetingSupersetOfFrameworksAndOneOfReferencesCompatible()
{
var setup = Setup();
var lib = new ProjDir(setup.LibDir);
var net452netcoreapp10lib = new ProjDir(Path.Combine(setup.TestRoot, "Net452AndNetCoreApp10Lib"));
int noCondBefore = net452netcoreapp10lib.CsProj().NumberOfItemGroupsWithoutCondition();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
2016-12-09 00:23:21 +00:00
.WithProject(net452netcoreapp10lib.CsProjPath)
.Execute($"\"{lib.CsProjPath}\"");
cmd.Should().Pass();
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"..\Lib\Lib.csproj"));
2016-12-09 00:23:21 +00:00
var csproj = net452netcoreapp10lib.CsProj();
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore + 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(lib.CsProjName).Should().Be(1);
}
[Theory]
[InlineData("net45")]
[InlineData("net40")]
[InlineData("netcoreapp1.1")]
[InlineData("nonexistingframeworkname")]
public void WhenFrameworkSwitchIsNotMatchingAnyOfTargetedFrameworksItPrintsError(string framework)
{
var setup = Setup();
var lib = new ProjDir(setup.LibDir);
var net45lib = new ProjDir(Path.Combine(setup.TestRoot, "Net45Lib"));
var csProjContent = lib.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
2016-12-09 00:23:21 +00:00
.WithProject(lib.CsProjPath)
.Execute($"-f {framework} \"{net45lib.CsProjPath}\"");
cmd.Should().Fail();
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ProjectDoesNotTargetFramework, setup.LibCsprojPath, framework));
2016-12-16 09:04:09 +00:00
2016-12-09 00:23:21 +00:00
lib.CsProjContent().Should().BeEquivalentTo(csProjContent);
}
[Theory]
[InlineData("")]
[InlineData("-f net45")]
public void WhenIncompatibleFrameworkDetectedItPrintsError(string frameworkArg)
{
var setup = Setup();
var lib = new ProjDir(setup.LibDir);
var net45lib = new ProjDir(Path.Combine(setup.TestRoot, "Net45Lib"));
var csProjContent = net45lib.CsProjContent();
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand()
2016-12-09 00:23:21 +00:00
.WithProject(net45lib.CsProjPath)
.Execute($"{frameworkArg} \"{lib.CsProjPath}\"");
cmd.Should().Fail();
2016-12-09 20:11:11 +00:00
cmd.StdErr.Should().MatchRegex(ProjectNotCompatibleErrorMessageRegEx);
cmd.StdErr.Should().MatchRegex(" - net45");
2016-12-09 00:23:21 +00:00
net45lib.CsProjContent().Should().BeEquivalentTo(csProjContent);
}
}
2016-11-23 22:02:19 +00:00
}