fix dotnet-add-p2p tests

This commit is contained in:
Krzysztof Wicher 2016-12-08 14:56:31 -08:00
parent 84e4da57cc
commit 2d38aaa6e1
7 changed files with 67 additions and 37 deletions

View file

@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net451;netcoreapp1.0</TargetFrameworks>
<TargetFrameworks>net451;netcoreapp1.0;netstandard1.4</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />

View file

@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net451;netcoreapp1.0</TargetFrameworks>
<TargetFrameworks>net451;netcoreapp1.0;netstandard1.4</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />

View file

@ -8,6 +8,7 @@ using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using NuGet.Frameworks;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@ -20,45 +21,47 @@ namespace Microsoft.DotNet.Tools
public ProjectRootElement ProjectRoot { get; private set; }
public string ProjectDirectory { get; private set; }
private ProjectCollection _collection;
private List<NuGetFramework> _cachedTfms = null;
private Project _cachedEvaluatedProject = null;
private MsbuildProject(ProjectRootElement project)
private MsbuildProject(ProjectCollection collection, ProjectRootElement project)
{
_collection = collection;
ProjectRoot = project;
ProjectDirectory = PathUtility.EnsureTrailingSlash(ProjectRoot.DirectoryPath);
}
public static MsbuildProject FromFileOrDirectory(string fileOrDirectory)
public static MsbuildProject FromFileOrDirectory(ProjectCollection collection, string fileOrDirectory)
{
if (File.Exists(fileOrDirectory))
{
return FromFile(fileOrDirectory);
return FromFile(collection, fileOrDirectory);
}
else
{
return FromDirectory(fileOrDirectory);
return FromDirectory(collection, fileOrDirectory);
}
}
public static MsbuildProject FromFile(string projectPath)
public static MsbuildProject FromFile(ProjectCollection collection, string projectPath)
{
if (!File.Exists(projectPath))
{
throw new GracefulException(CommonLocalizableStrings.ProjectDoesNotExist, projectPath);
}
var project = TryOpenProject(projectPath);
var project = TryOpenProject(collection, projectPath);
if (project == null)
{
throw new GracefulException(CommonLocalizableStrings.ProjectIsInvalid, projectPath);
}
return new MsbuildProject(project);
return new MsbuildProject(collection, project);
}
public static MsbuildProject FromDirectory(string projectDirectory)
public static MsbuildProject FromDirectory(ProjectCollection collection, string projectDirectory)
{
DirectoryInfo dir;
try
@ -93,13 +96,13 @@ namespace Microsoft.DotNet.Tools
throw new GracefulException(CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory, projectDirectory);
}
var project = TryOpenProject(projectFile.FullName);
var project = TryOpenProject(collection, projectFile.FullName);
if (project == null)
{
throw new GracefulException(CommonLocalizableStrings.FoundInvalidProject, projectFile.FullName);
}
return new MsbuildProject(project);
return new MsbuildProject(collection, project);
}
public int AddProjectToProjectReferences(string framework, IEnumerable<string> refs)
@ -237,9 +240,16 @@ namespace Microsoft.DotNet.Tools
return _cachedEvaluatedProject;
}
var loadedProjects = _collection.GetLoadedProjects(ProjectRoot.FullPath);
if (loadedProjects.Count >= 1)
{
_cachedEvaluatedProject = loadedProjects.First();
return _cachedEvaluatedProject;
}
try
{
_cachedEvaluatedProject = new Project(ProjectRoot);
_cachedEvaluatedProject = new Project(ProjectRoot, null, null, _collection);
}
catch (InvalidProjectFileException e)
{
@ -302,13 +312,13 @@ namespace Microsoft.DotNet.Tools
// There is ProjectRootElement.TryOpen but it does not work as expected
// I.e. it returns null for some valid projects
private static ProjectRootElement TryOpenProject(string filename)
private static ProjectRootElement TryOpenProject(ProjectCollection collection, string filename)
{
try
{
return ProjectRootElement.Open(filename, new ProjectCollection(), preserveFormatting: true);
return ProjectRootElement.Open(filename, collection, preserveFormatting: true);
}
catch (Microsoft.Build.Exceptions.InvalidProjectFileException)
catch (InvalidProjectFileException)
{
return null;
}

View file

@ -1,6 +1,7 @@
// 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 Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using NuGet.Frameworks;
@ -46,7 +47,8 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, $"<{LocalizableStrings.ProjectException}>");
}
var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);
ProjectCollection collection = new ProjectCollection();
var msbuildProj = MsbuildProject.FromFileOrDirectory(collection, projectArgument.Value);
if (app.RemainingArguments.Count == 0)
{
@ -58,7 +60,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
if (!forceOption.HasValue())
{
MsbuildProject.EnsureAllReferencesExist(references);
IEnumerable<MsbuildProject> refs = references.Select((r) => MsbuildProject.FromFile(r));
IEnumerable<MsbuildProject> refs = references.Select((r) => MsbuildProject.FromFile(collection, r));
if (frameworkString == null)
{

View file

@ -1,6 +1,7 @@
// 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 Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using System.Collections.Generic;
@ -31,7 +32,7 @@ namespace Microsoft.DotNet.Tools.List.ProjectToProjectReferences
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, $"<{LocalizableStrings.ProjectArgumentValueName}>");
}
var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);
var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), projectArgument.Value);
var p2ps = msbuildProj.GetProjectToProjectReferences();
if (p2ps.Count() == 0)

View file

@ -1,6 +1,7 @@
// 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 Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using System.Collections.Generic;
@ -39,7 +40,7 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, $"<{LocalizableStrings.ProjectException}>");
}
var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);
var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), projectArgument.Value);
if (app.RemainingArguments.Count == 0)
{

View file

@ -7,6 +7,7 @@ using Microsoft.DotNet.Tools.Test.Utilities;
using Msbuild.Tests.Utilities;
using System;
using System.IO;
using System.Linq;
using Xunit;
namespace Microsoft.DotNet.Cli.Add.P2P.Tests
@ -17,6 +18,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
const string ConditionFrameworkNet451 = "== 'net451'";
const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0";
const string ConditionFrameworkNetCoreApp10 = "== 'netcoreapp1.0'";
static readonly string[] DefaultFrameworks = new string[] { "netcoreapp1.0", "net451" };
private TestSetup Setup([System.Runtime.CompilerServices.CallerMemberName] string callingMethod = nameof(Setup), string identifier = "")
{
@ -52,6 +54,20 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
return dir;
}
private static void SetTargetFrameworks(ProjDir proj, string[] frameworks)
{
var csproj = proj.CsProj();
csproj.AddProperty("TargetFrameworks", string.Join(";", frameworks));
csproj.Save();
}
private ProjDir NewLibWithFrameworks([System.Runtime.CompilerServices.CallerMemberName] string callingMethod = nameof(NewDir), string identifier = "")
{
var ret = NewLib(callingMethod: callingMethod, identifier: identifier);
SetTargetFrameworks(ret, DefaultFrameworks);
return ret;
}
[Theory]
[InlineData("--help")]
[InlineData("-h")]
@ -122,7 +138,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void ItAddsRefWithoutCondAndPrintsStatus()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
@ -141,7 +157,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void ItAddsRefWithCondAndPrintsStatus()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
int condBefore = lib.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451);
@ -160,7 +176,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenRefWithoutCondIsPresentItAddsDifferentRefWithoutCond()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
new AddP2PCommand()
@ -184,7 +200,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenRefWithCondIsPresentItAddsDifferentRefWithCond()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
new AddP2PCommand()
@ -208,7 +224,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenRefWithCondIsPresentItAddsRefWithDifferentCond()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
new AddP2PCommand()
@ -232,7 +248,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenRefWithConditionIsPresentItAddsDifferentRefWithoutCond()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
new AddP2PCommand()
@ -256,7 +272,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenRefWithNoCondAlreadyExistsItDoesntDuplicate()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
new AddP2PCommand()
@ -297,7 +313,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenRefWithCondOnItemGroupAlreadyExistsItDoesntDuplicate()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
new AddP2PCommand()
@ -421,7 +437,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void ItAddsMultipleRefsNoCondToTheSameItemGroup()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
@ -440,7 +456,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void ItAddsMultipleRefsWithCondToTheSameItemGroup()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithConditionContaining(ConditionFrameworkNet451);
@ -459,7 +475,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenProjectNameIsNotPassedItFindsItAndAddsReference()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
@ -477,7 +493,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void ItAddsRefBetweenImports()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
var cmd = new AddP2PCommand()
@ -522,7 +538,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenPassedReferenceDoesNotExistItShowsAnError()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var contentBefore = lib.CsProjContent();
var cmd = new AddP2PCommand()
@ -537,7 +553,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenPassedMultipleRefsAndOneOfthemDoesNotExistItCancelsWholeOperation()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
var contentBefore = lib.CsProjContent();
@ -554,7 +570,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenPassedReferenceDoesNotExistAndForceSwitchIsPassedItAddsIt()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
const string nonExisting = "IDoNotExist.csproj";
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();
@ -573,7 +589,7 @@ namespace Microsoft.DotNet.Cli.Add.P2P.Tests
[Fact]
public void WhenPassedReferenceIsUsingSlashesItNormalizesItToBackslashes()
{
var lib = NewLib();
var lib = NewLibWithFrameworks();
var setup = Setup();
int noCondBefore = lib.CsProj().NumberOfItemGroupsWithoutCondition();