From 2d38aaa6e197f4328fb02afec9dd8ecdf6d66931 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Thu, 8 Dec 2016 14:56:31 -0800 Subject: [PATCH] fix dotnet-add-p2p tests --- .../DotnetAddP2PProjects/Lib/Lib.csproj | 2 +- .../ValidRef/ValidRef.csproj | 2 +- src/dotnet/MsbuildProject.cs | 40 ++++++++++------ .../dotnet-add/dotnet-add-p2p/Program.cs | 6 ++- .../dotnet-list/dotnet-list-p2ps/Program.cs | 3 +- .../dotnet-remove-p2p/Program.cs | 3 +- .../dotnet-add-p2p.Tests/GivenDotnetAddP2P.cs | 48 ++++++++++++------- 7 files changed, 67 insertions(+), 37 deletions(-) diff --git a/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Lib/Lib.csproj b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Lib/Lib.csproj index 7cd1371e1..f45f323b3 100644 --- a/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Lib/Lib.csproj +++ b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Lib/Lib.csproj @@ -4,7 +4,7 @@ Library - net451;netcoreapp1.0 + net451;netcoreapp1.0;netstandard1.4 diff --git a/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/ValidRef/ValidRef.csproj b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/ValidRef/ValidRef.csproj index 7cd1371e1..f45f323b3 100644 --- a/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/ValidRef/ValidRef.csproj +++ b/TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/ValidRef/ValidRef.csproj @@ -4,7 +4,7 @@ Library - net451;netcoreapp1.0 + net451;netcoreapp1.0;netstandard1.4 diff --git a/src/dotnet/MsbuildProject.cs b/src/dotnet/MsbuildProject.cs index 6fcf8525f..cdd14143a 100644 --- a/src/dotnet/MsbuildProject.cs +++ b/src/dotnet/MsbuildProject.cs @@ -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 _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 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; } diff --git a/src/dotnet/commands/dotnet-add/dotnet-add-p2p/Program.cs b/src/dotnet/commands/dotnet-add/dotnet-add-p2p/Program.cs index 02d1e1608..24a5b3085 100644 --- a/src/dotnet/commands/dotnet-add/dotnet-add-p2p/Program.cs +++ b/src/dotnet/commands/dotnet-add/dotnet-add-p2p/Program.cs @@ -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 refs = references.Select((r) => MsbuildProject.FromFile(r)); + IEnumerable refs = references.Select((r) => MsbuildProject.FromFile(collection, r)); if (frameworkString == null) { diff --git a/src/dotnet/commands/dotnet-list/dotnet-list-p2ps/Program.cs b/src/dotnet/commands/dotnet-list/dotnet-list-p2ps/Program.cs index 487959484..349c82bd4 100644 --- a/src/dotnet/commands/dotnet-list/dotnet-list-p2ps/Program.cs +++ b/src/dotnet/commands/dotnet-list/dotnet-list-p2ps/Program.cs @@ -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) diff --git a/src/dotnet/commands/dotnet-remove/dotnet-remove-p2p/Program.cs b/src/dotnet/commands/dotnet-remove/dotnet-remove-p2p/Program.cs index 6ef8033d1..ec646b6d2 100644 --- a/src/dotnet/commands/dotnet-remove/dotnet-remove-p2p/Program.cs +++ b/src/dotnet/commands/dotnet-remove/dotnet-remove-p2p/Program.cs @@ -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) { diff --git a/test/dotnet-add-p2p.Tests/GivenDotnetAddP2P.cs b/test/dotnet-add-p2p.Tests/GivenDotnetAddP2P.cs index 49bcceda7..6147c3415 100644 --- a/test/dotnet-add-p2p.Tests/GivenDotnetAddP2P.cs +++ b/test/dotnet-add-p2p.Tests/GivenDotnetAddP2P.cs @@ -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();