From c3cee71489ed0e6af7764502871faf2a102858ba Mon Sep 17 00:00:00 2001 From: deepakaravindr Date: Fri, 23 Oct 2015 14:47:35 -0700 Subject: [PATCH] Updated the tests to use the Util helper methods and use XElement to construct the test msbuild project files. 1. Tested that the added tests passed 2. Tested manually that if a project reference has the element false, it is ignored during 'nuget.exe pack -IncludeReferencedProjects' --- .../ProjectFactoryTest.cs | 119 ++++++------------ .../NuGet.CommandLine.Test/Util.cs | 12 +- 2 files changed, 51 insertions(+), 80 deletions(-) diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/ProjectFactoryTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/ProjectFactoryTest.cs index 33a60a80e1c..1a45ec5888d 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/ProjectFactoryTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/ProjectFactoryTest.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; +using System.Xml.Linq; using Microsoft.Build.Evaluation; using Moq; using NuGet.CommandLine.Test; @@ -9,8 +11,6 @@ namespace NuGet.CommandLine { - using System.Text; - public class ProjectFactoryTest { [Fact] @@ -168,10 +168,13 @@ public void EnsureProjectFactoryWorksAsExpectedWithReferenceOutputAssemblyValues workingDirectory, "pack Link\\Link.csproj -build -IncludeReferencedProjects -Version 1.0.0", waitForExit: true); + + // Assert + Util.VerifyResultSuccess(r); + var package = new OptimizedZipPackage(Path.Combine(workingDirectory, "Link.1.0.0.nupkg")); var files = package.GetFiles().Select(f => f.Path).ToArray(); - // Assert Assert.Equal(0, r.Item1); Array.Sort(files); Assert.Equal(files, new[] { @@ -227,10 +230,12 @@ public void EnsureProjectFactoryWorksAsExpectedWithReferenceOutputAssemblyValues workingDirectory, "pack Link\\Link.csproj -build -IncludeReferencedProjects -Version 1.0.0", waitForExit: true); + + // Assert + Util.VerifyResultSuccess(r); var package = new OptimizedZipPackage(Path.Combine(workingDirectory, "Link.1.0.0.nupkg")); var files = package.GetFiles().Select(f => f.Path).ToArray(); - // Assert Assert.Equal(0, r.Item1); Array.Sort(files); Assert.Equal(files, new[] { @@ -341,106 +346,62 @@ public class Source #region Helper private class DummyProject { - private readonly StringBuilder projectReferenceBuilder; - + private static XNamespace MSBuildNS = "http://schemas.microsoft.com/developer/msbuild/2003"; public DummyProject(string name, string path) { - this.Id = Guid.NewGuid(); - this.Location = path; - this.Name = name; - this.projectReferenceBuilder = new StringBuilder(); + Id = Guid.NewGuid(); + Location = path; + Name = name; + ProjectReferences = new List(); } - public string Name { get; private set; } - public Guid Id { get; private set; } - public string Location { get; private set; } + public string Name { get; } + public Guid Id { get; } + public string Location { get; } + private List ProjectReferences { get; } public void AddProjectReference(DummyProject project, bool exclude) { - this.projectReferenceBuilder.AppendLine(GenerateProjectReference(project, exclude)); + var projectReferenceElement = GenerateProjectReference(project, exclude); + ProjectReferences.Add(projectReferenceElement); } - private static string GenerateProjectReference(DummyProject project, bool exclude) + private static XElement GenerateProjectReference(DummyProject project, bool exclude) { - StringBuilder b = new StringBuilder(); - b.AppendLine(string.Format("\t", project.Location)); - b.AppendLine(string.Format("\t\t{0}", project.Id)); - b.AppendLine(string.Format("\t\t{0}", project.Name)); + var projectReferenceXElement = new XElement(MSBuildNS + "ProjectReference", + new XAttribute("Include", project.Location), + new XElement(MSBuildNS + "Project", project.Id), + new XElement(MSBuildNS + "Name", project.Name)); + if (exclude) { - b.AppendLine("\t\tfalse"); + projectReferenceXElement.Add(new XElement(MSBuildNS + "ReferenceOutputAssembly", "false")); } - b.AppendLine("\t"); - return b.ToString(); + return projectReferenceXElement; } public void WriteToFile() { - FileInfo file = new System.IO.FileInfo(this.Location); + FileInfo file = new FileInfo(Location); file.Directory.Create(); - File.WriteAllText(this.Location, this.ToString()); + File.WriteAllText(Location, ToString()); } public override string ToString() { - string projectFormat = @" - - - - Debug - AnyCPU - {0} - Library - Properties - {1} - {2} - v4.5 - 512 - - - true - full - false - bin\Debug\ - bin\Debug\Assembly.XML - DEBUG;TRACE - prompt - 4 - v4.5 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - -{3} - - -"; + var itemGroup = new XElement(MSBuildNS + "ItemGroup"); + foreach(var projectReferenceXElement in ProjectReferences) + { + itemGroup.Add(projectReferenceXElement); + } + + var projectXElement = Util.CreateProjFileXmlContent(Name); + projectXElement.Add(itemGroup); - return string.Format( - projectFormat, - this.Id, - this.Name, - this.Name, - this.projectReferenceBuilder); + return projectXElement.ToString(); } } - #endregion + #endregion } } diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs index 6fe3352d2bf..4e5b056fcab 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs @@ -513,6 +513,16 @@ public static JObject CreateSinglePackageRegistrationBlob(MockServer server, str string targetFrameworkVersion = "v4.5", string[] references = null, string[] contentFiles = null) + { + var project = CreateProjFileXmlContent(projectName, targetFrameworkVersion, references, contentFiles); + return project.ToString(); + } + + public static XElement CreateProjFileXmlContent( + string projectName = "proj1", + string targetFrameworkVersion = "v4.5", + string[] references = null, + string[] contentFiles = null) { XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003"; @@ -539,7 +549,7 @@ public static JObject CreateSinglePackageRegistrationBlob(MockServer server, str project.Add(new XElement(msbuild + "Import", new XAttribute("Project", @"$(MSBuildToolsPath)\Microsoft.CSharp.targets"))); - return project.ToString(); + return project; } public static string CreateSolutionFileContent()