diff --git a/README.md b/README.md index 94a3c9cd..29f5afd5 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ item metadata. It can also be turned off by default for all items of a given typ The basic item metadata that drive pack inference are: 1. **Pack**: *true*/*false*, determines whether inference applies to the item at all. -2. **PackagePath**: final path within the package. +2. **PackagePath**: final path within the package. Can be a directory path ending in `\` and in that case the item's *RelativeDir*, *Filename* and *Extension* will be appended automatically. Linked files are also supported automatically. If the item does **not** provide a *PackagePath*, and *Pack* is not *false*, the inference targets wil try to determine the right value, based on the following additional metadata: @@ -136,7 +136,7 @@ This even works transitively, so if you use *PrivateAssets=all* on package refer As usual, you can change this default behavior by using `Pack=false` metadata. -### Project References +### ProjectReference Unlike SDK Pack that [considers project references as package references by default](https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#project-to-project-references), NuGetizer has an explicit contract between projects: the `GetPackageContents` target. This target is invoked when packing project references, and it returns whatever the referenced project exposes as package contents (including the inference rules above). If the project is *packable* (that is, it produces a package, denoted by the presence of a `PackageId` property), it will be packed as a dependency/package reference instead. @@ -181,6 +181,8 @@ Package: Sample.1.0.0.nupkg sample.pdb ``` +Finally, you can focedly turn a project reference build output into a private asset even if it defines a `PackageId` by adding `PrivateAssets=all`. This is very useful for build and analyzer packages, which typically reference the main library project too, but need its output as private, since neither can use dependencies at run-time. + ### dotnet-nugetize Carefully tweaking your packages until they look exactly the way you want them should not be a tedious and slow process. Even requiring your project to be built between changes can be costly and reduce the speed at which you can iterate on the packaging aspects of the project. Also, generating the final `.nupkg`, opening it in a tool and inspecting its content, is also not ideal for rapid iteration. diff --git a/src/Directory.Build.targets b/src/Directory.Build.targets index 3a723967..7860700c 100644 --- a/src/Directory.Build.targets +++ b/src/Directory.Build.targets @@ -32,7 +32,7 @@ - + $(Description) diff --git a/src/NuGetizer.Tasks/AssignPackagePath.cs b/src/NuGetizer.Tasks/AssignPackagePath.cs index 9ede7673..6295336a 100644 --- a/src/NuGetizer.Tasks/AssignPackagePath.cs +++ b/src/NuGetizer.Tasks/AssignPackagePath.cs @@ -1,11 +1,12 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; -using static ThisAssembly.Strings; using NuGet.Packaging; +using static ThisAssembly.Strings; namespace NuGetizer.Tasks { @@ -31,6 +32,9 @@ public class AssignPackagePath : Task public override bool Execute() { + if (Environment.GetEnvironmentVariable("DEBUG_NUGETIZER") == "1") + Debugger.Break(); + var kindMap = KnownFolders.ToDictionary( kind => kind.ItemSpec, StringComparer.OrdinalIgnoreCase); @@ -79,8 +83,25 @@ ITaskItem EnsurePackagePath(ITaskItem file, IDictionary kindM } // If PackagePath already specified, we're done. - if (!string.IsNullOrEmpty(file.GetMetadata("PackagePath"))) + if (file.TryGetMetadata("PackagePath", out var packagePath)) + { + // If PackagePath ends in directory separator, we assume + // the file/path needs to be appended too. + if (packagePath.EndsWith("\\")) + { + if (file.TryGetMetadata("Link", out var link)) + packagePath = Path.Combine(packagePath, link); + else + packagePath = Path.Combine(packagePath, + file.GetMetadata("RelativeDir"), + file.GetMetadata("FileName") + + file.GetMetadata("Extension")); + + output.SetMetadata("PackagePath", packagePath); + } + return output; + } // If a packaging project is requesting the package path assignment, // perform it regardless of whether there is a PackageId on the items, @@ -180,7 +201,7 @@ ITaskItem EnsurePackagePath(ITaskItem file, IDictionary kindM // If we have no known package folder, files go to their RelativeDir location. // This allows custom packaging paths such as "workbooks", "docs" or whatever, which aren't prohibited by // the format. - var packagePath = string.IsNullOrEmpty(packageFolder) ? + packagePath = string.IsNullOrEmpty(packageFolder) ? // File goes to the determined target path (or the root of the package), such as a readme.txt targetPath : frameworkSpecific ? diff --git a/src/NuGetizer.Tasks/CreatePackage.cs b/src/NuGetizer.Tasks/CreatePackage.cs index 1f1bb096..160afa86 100644 --- a/src/NuGetizer.Tasks/CreatePackage.cs +++ b/src/NuGetizer.Tasks/CreatePackage.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Security.Cryptography; @@ -41,6 +42,9 @@ public class CreatePackage : Task public override bool Execute() { + if (Environment.GetEnvironmentVariable("DEBUG_NUGETIZER") == "1") + Debugger.Break(); + try { if (bool.TryParse(EmitPackage, out var emitPkg) && emitPkg) @@ -78,8 +82,8 @@ public Manifest Execute(Stream output) public Manifest CreateManifest() { var metadata = new ManifestMetadata(); - - metadata.Id = Manifest.GetMetadata(nameof(MetadataName.PackageId)); + + metadata.Id = Manifest.GetNullableMetadata(MetadataName.PackageId) ?? Manifest.GetMetadata("Id"); if (Manifest.TryGetMetadata(nameof(ManifestMetadata.Version), out var version)) metadata.Version = NuGetVersion.Parse(Manifest.GetMetadata(MetadataName.Version)); @@ -102,7 +106,7 @@ public Manifest CreateManifest() if (Manifest.TryGetMetadata("Copyright", out var copyright)) metadata.Copyright = copyright; - if (Manifest.TryGetBoolMetadata("RequireLicenseAcceptance", out var requireLicenseAcceptance) && + if (Manifest.TryGetBoolMetadata("RequireLicenseAcceptance", out var requireLicenseAcceptance) && requireLicenseAcceptance) metadata.RequireLicenseAcceptance = requireLicenseAcceptance; @@ -185,6 +189,7 @@ void GeneratePackage(Stream output = null) if (output == null) { + Directory.CreateDirectory(Path.GetDirectoryName(TargetPath)); using var stream = File.Create(TargetPath); builder.Save(stream); } diff --git a/src/NuGetizer.Tasks/Extensions.cs b/src/NuGetizer.Tasks/Extensions.cs index 2006e5c3..269caf48 100644 --- a/src/NuGetizer.Tasks/Extensions.cs +++ b/src/NuGetizer.Tasks/Extensions.cs @@ -4,10 +4,10 @@ using System.Runtime.Versioning; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; -using NuGetizer.Tasks; using NuGet.Frameworks; using NuGet.Packaging; using NuGet.Packaging.Core; +using NuGetizer.Tasks; namespace NuGetizer { diff --git a/src/NuGetizer.Tasks/InferImplicitPackageReference.cs b/src/NuGetizer.Tasks/InferImplicitPackageReference.cs index f0f1ce54..f2a1ca22 100644 --- a/src/NuGetizer.Tasks/InferImplicitPackageReference.cs +++ b/src/NuGetizer.Tasks/InferImplicitPackageReference.cs @@ -41,7 +41,7 @@ public override bool Execute() var inferred = new HashSet(); - foreach (var reference in PackageReferences.Where(x => + foreach (var reference in PackageReferences.Where(x => "all".Equals(x.GetMetadata("PrivateAssets"), StringComparison.OrdinalIgnoreCase) && // Unless explicitly set to Pack=false (!x.TryGetBoolMetadata("Pack", out var pack) || pack != false) && @@ -58,8 +58,8 @@ public override bool Execute() ImplicitPackageReferences = inferred .Select(x => new TaskItem( x.Id, - new Dictionary - { + new Dictionary + { { "Version", x.Version } , { "PrivateAssets", "all" }, })) @@ -92,7 +92,7 @@ public PackageIdentity(string id, string version) public string Id { get; } public string Version { get; } - public override bool Equals(object obj) + public override bool Equals(object obj) => obj is PackageIdentity dependency && dependency.Id == Id && dependency.Version == Version; diff --git a/src/NuGetizer.Tasks/NuGetizer.Shared.targets b/src/NuGetizer.Tasks/NuGetizer.Shared.targets index c26a1419..f69a51c1 100644 --- a/src/NuGetizer.Tasks/NuGetizer.Shared.targets +++ b/src/NuGetizer.Tasks/NuGetizer.Shared.targets @@ -37,6 +37,9 @@ Copyright (c) .NET Foundation. All rights reserved. + + $(AssemblyName) + true false @@ -236,7 +239,6 @@ Copyright (c) .NET Foundation. All rights reserved. - diff --git a/src/NuGetizer.Tasks/NuGetizer.Tasks.csproj b/src/NuGetizer.Tasks/NuGetizer.Tasks.csproj index eba95674..4b63be6c 100644 --- a/src/NuGetizer.Tasks/NuGetizer.Tasks.csproj +++ b/src/NuGetizer.Tasks/NuGetizer.Tasks.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/NuGetizer.Tasks/NuGetizer.targets b/src/NuGetizer.Tasks/NuGetizer.targets index a5c315a1..5ec2b727 100644 --- a/src/NuGetizer.Tasks/NuGetizer.targets +++ b/src/NuGetizer.Tasks/NuGetizer.targets @@ -34,7 +34,7 @@ Copyright (c) .NET Foundation. All rights reserved. item containing the manifest information will also be returned, as Identity=$(PackageId) - %(PackFolder)=metadata + %(PackFolder)=Metadata ... all manifest values as metadata items ... All items returned from this target contain a %(PackageId) metadata @@ -81,7 +81,7 @@ Copyright (c) .NET Foundation. All rights reserved. - metadata + Metadata $(PackageId) $(Platform) $(TargetFrameworkMoniker) @@ -89,13 +89,14 @@ Copyright (c) .NET Foundation. All rights reserved. - + + + Code="NG0011" + Text="Some project references cannot be properly packaged. Please install the NuGetizer package on the following projects: @(_NonNuGetizedProjectReference)." /> $(PackageId) $(TargetFrameworkMoniker) @@ -123,14 +128,16 @@ Copyright (c) .NET Foundation. All rights reserved. <_PackageContentFromDependency Include="@(_ReferencedPackageContent)" - Condition="'%(_ReferencedPackageContent.PackageId)' != '' and '%(_ReferencedPackageContent.PackageId)' != '$(PackageId)'" /> + Condition="'%(_ReferencedPackageContent.PackageId)' != '' and + '%(_ReferencedPackageContent.PackageId)' != '$(PackageId)'" /> <_ReferencedPackageContent Remove="@(_PackageContentFromDependency)" /> + AdditionalMetadata="OriginalTargetFramework=%(_ReferencedPackageContent.TargetFramework); + OriginalTargetFrameworkMoniker=%(_ReferencedPackageContent.TargetFrameworkMoniker)"> @@ -192,6 +199,7 @@ Copyright (c) .NET Foundation. All rights reserved. <_ShouldPack>%(_MSBuildProjectReferenceExistent.Pack) <_IsNuGetized>%(_ReferencedProjectTargetPath.IsNuGetized) + <_PrivateAssets>%(_MSBuildProjectReferenceExistent.PrivateAssets) @@ -199,7 +207,14 @@ Copyright (c) .NET Foundation. All rights reserved. <_NonNuGetizedProjectReference Include="@(_MSBuildProjectReferenceExistent)" Condition="'$(_IsNuGetized)' != 'true'" /> - <_NuGetizedProjectReference Include="@(_MSBuildProjectReferenceExistent)" Condition="'$(_IsNuGetized)' == 'true'" /> + <_NuGetizedProjectReference Include="@(_MSBuildProjectReferenceExistent)" Condition="'$(_IsNuGetized)' == 'true'"> + + IsPackable=false;PackFolder=$(PackFolder) + diff --git a/src/NuGetizer.Tests/AssignPackagePathTests.cs b/src/NuGetizer.Tests/AssignPackagePathTests.cs index 2bd95a9e..83021477 100644 --- a/src/NuGetizer.Tests/AssignPackagePathTests.cs +++ b/src/NuGetizer.Tests/AssignPackagePathTests.cs @@ -1,788 +1,863 @@ +using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Microsoft.Build.Evaluation; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; +using NuGetizer.Tasks; using Xunit; using Xunit.Abstractions; -using System.Linq; -using NuGetizer.Tasks; using Metadata = System.Collections.Generic.Dictionary; -using System; namespace NuGetizer { - public class AssignPackagePathTests - { + public class AssignPackagePathTests + { static ITaskItem[] kinds; - static ITaskItem[] Kinds + static ITaskItem[] Kinds => kinds ??= new Project(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NuGetizer.props"), null, null, new ProjectCollection()) .GetItems("PackFolderKind") .Select(item => new TaskItem(item.EvaluatedInclude, item.Metadata.ToDictionary(meta => meta.Name, meta => meta.UnevaluatedValue))) .ToArray(); - ITestOutputHelper output; - MockBuildEngine engine; + ITestOutputHelper output; + MockBuildEngine engine; + + public AssignPackagePathTests(ITestOutputHelper output) + { + this.output = output; + engine = new MockBuildEngine(output); + } + + [Fact] + public void when_file_has_no_kind_then_logs_error_code() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" } + }) + } + }; + + Assert.False(task.Execute()); + Assert.Equal(nameof(ThisAssembly.Strings.ErrorCode.NG0010), engine.LoggedErrorEvents[0].Code); + } + + [Fact] + public void when_file_has_no_kind_and_no_framework_specific_then_it_is_not_assigned_target_framework() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackagePath", "workbooks\\library.dll" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = "workbooks\\library.dll", + TargetFramework = "" + })); + } + + [Fact] + public void when_file_has_no_kind_and_package_path_and_framework_specific_then_it_is_assigned_target_framework_only() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackagePath", "workbooks\\library.dll" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "FrameworkSpecific", "true" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = "workbooks\\library.dll", + TargetFramework = "net45" + })); + } + + [Fact] + public void assigned_files_contains_all_files() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("a.dll", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "lib" } + }), + new TaskItem("a.pdb", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "symbols" } + }) + } + }; + + Assert.True(task.Execute()); + + Assert.Equal(task.Files.Length, task.AssignedFiles.Length); + } + + [Fact] + public void when_file_has_no_package_id_then_package_path_is_not_specified() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "lib" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Equal("", task.AssignedFiles[0].GetMetadata(MetadataName.PackagePath)); + } + + [Fact] + public void when_file_has_no_package_id_but_is_packaging_true_then_package_path_is_specified() + { + var task = new AssignPackagePath + { + IsPackaging = "true", + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "lib" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Equal(@"lib\net45\library.dll", task.AssignedFiles[0].GetMetadata(MetadataName.PackagePath)); + } + + [Fact] + public void when_file_has_no_package_id_then_target_framework_is_calculated_anyway() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "lib" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + TargetFramework = "net45" + })); + } + + [Fact] + public void when_file_has_no_package_id_then_package_folder_is_calculated_anyway() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "lib" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackageFolder = "lib" + })); + } + + [Fact] + public void when_file_has_no_tfm_then_assigned_file_contains_no_target_framework() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackageId", "A" }, + { "PackFolder", "lib" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackageFolder = "lib", + PackagePath = "lib\\library.dll", + TargetFramework = "" + })); + } + + [Fact] + public void when_file_has_target_framework_and_tfm_then_existing_value_is_preserved() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackageId", "A" }, + { "PackFolder", "contentFiles" }, + { "TargetFramework", "any" }, + { "TargetFrameworkMoniker", "MonoAndroid,Version=v2.5" }, + }) + } + }; + + Assert.True(task.Execute()); + + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + TargetFramework = "any", + })); + } + + [Fact] + public void when_content_file_has_link_then_package_path_is_relative_to_link() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem(@"..\..\readme.txt", new Metadata + { + { "Link", @"docs\readme.txt" }, + { "PackageId", "A" }, + { "PackFolder", "content" }, + { "TargetFramework", "any" }, + }) + } + }; + + Assert.True(task.Execute()); + + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + TargetPath = @"docs\readme.txt", + PackagePath = @"contentFiles\any\any\docs\readme.txt", + })); + } + + [Fact] + public void when_none_file_has_link_then_package_path_is_relative_to_link() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem(@"..\..\readme.txt", new Metadata + { + { "Link", @"docs\readme.txt" }, + { "PackageId", "A" }, + { "PackFolder", "none" }, + { "TargetFramework", "any" }, + }) + } + }; + + Assert.True(task.Execute()); + + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + TargetPath = @"docs\readme.txt", + PackagePath = @"docs\readme.txt", + })); + } + + [Fact] + public void when_package_file_has_directory_package_path_then_appends_file_and_extension() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem(@"readme.txt", new Metadata + { + { "PackageId", "A" }, + { "PackFolder", "none" }, + { "PackagePath", @"build\" }, + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = @"build\readme.txt", + })); + } - public AssignPackagePathTests(ITestOutputHelper output) - { - this.output = output; - engine = new MockBuildEngine(output); + [Fact] + public void when_package_file_has_directory_package_path_and_relative_dir_then_appends_relative_path_file_and_extension() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem(@"quickstarts\readme.txt", new Metadata + { + { "PackageId", "A" }, + { "PackFolder", "none" }, + { "PackagePath", @"docs\" }, + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = @"docs\quickstarts\readme.txt", + })); } [Fact] - public void when_file_has_no_kind_then_logs_error_code() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" } - }) - } - }; - - Assert.False(task.Execute()); - Assert.Equal(nameof(ThisAssembly.Strings.ErrorCode.NG0010), engine.LoggedErrorEvents[0].Code); - } - - [Fact] - public void when_file_has_no_kind_and_no_framework_specific_then_it_is_not_assigned_target_framework() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackagePath", "workbooks\\library.dll" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackagePath = "workbooks\\library.dll", - TargetFramework = "" - })); - } - - [Fact] - public void when_file_has_no_kind_and_package_path_and_framework_specific_then_it_is_assigned_target_framework_only() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackagePath", "workbooks\\library.dll" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "FrameworkSpecific", "true" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackagePath = "workbooks\\library.dll", - TargetFramework = "net45" - })); - } - - [Fact] - public void assigned_files_contains_all_files() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("a.dll", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "lib" } - }), - new TaskItem("a.pdb", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "symbols" } - }) - } - }; - - Assert.True(task.Execute()); - - Assert.Equal(task.Files.Length, task.AssignedFiles.Length); - } - - [Fact] - public void when_file_has_no_package_id_then_package_path_is_not_specified() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "lib" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Equal("", task.AssignedFiles[0].GetMetadata(MetadataName.PackagePath)); - } - - [Fact] - public void when_file_has_no_package_id_but_is_packaging_true_then_package_path_is_specified() - { - var task = new AssignPackagePath - { - IsPackaging = "true", - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "lib" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Equal(@"lib\net45\library.dll", task.AssignedFiles[0].GetMetadata(MetadataName.PackagePath)); - } - - [Fact] - public void when_file_has_no_package_id_then_target_framework_is_calculated_anyway() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "lib" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - TargetFramework = "net45" - })); - } - - [Fact] - public void when_file_has_no_package_id_then_package_folder_is_calculated_anyway() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "lib" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackageFolder = "lib" - })); - } - - [Fact] - public void when_file_has_no_tfm_then_assigned_file_contains_no_target_framework() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackageId", "A" }, - { "PackFolder", "lib" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackageFolder = "lib", - PackagePath = "lib\\library.dll", - TargetFramework = "" - })); - } - - [Fact] - public void when_file_has_target_framework_and_tfm_then_existing_value_is_preserved() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackageId", "A" }, - { "PackFolder", "contentFiles" }, - { "TargetFramework", "any" }, - { "TargetFrameworkMoniker", "MonoAndroid,Version=v2.5" }, - }) - } - }; - - Assert.True(task.Execute()); - - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - TargetFramework = "any", - })); - } - - [Fact] - public void when_content_file_has_link_then_package_path_is_relative_to_link() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem(@"..\..\readme.txt", new Metadata - { - { "Link", @"docs\readme.txt" }, - { "PackageId", "A" }, - { "PackFolder", "content" }, - { "TargetFramework", "any" }, - }) - } - }; - - Assert.True(task.Execute()); - - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - TargetPath = @"docs\readme.txt", - PackagePath = @"contentFiles\any\any\docs\readme.txt", - })); - } - - [Fact] - public void when_none_file_has_link_then_package_path_is_relative_to_link() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem(@"..\..\readme.txt", new Metadata - { - { "Link", @"docs\readme.txt" }, - { "PackageId", "A" }, - { "PackFolder", "none" }, - { "TargetFramework", "any" }, - }) - } - }; - - Assert.True(task.Execute()); - - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - TargetPath = @"docs\readme.txt", - PackagePath = @"docs\readme.txt", - })); - } - - - [Fact] - public void when_content_is_not_framework_specific_then_has_any_lang_and_tfm() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("readme.txt", new Metadata - { - { "PackageId", "A" }, - { "PackFolder", "content" }, - { "FrameworkSpecific", "false" }, - { "TargetFrameworkMoniker", "MonoAndroid,Version=v2.5" }, - }) - } - }; - - Assert.True(task.Execute()); - - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - CodeLanguage = "any", - TargetFramework = "any", - PackagePath = @"contentFiles\any\any\readme.txt" - })); - } - - // TODO: these all end up in all lowercase, but MonoAndroid, Xamarin.iOS are usually properly - // cased in nupkgs out in the wild (i.e. Rx) - [InlineData(".NETFramework,Version=v4.5", "net45")] - [InlineData(".NETPortable,Version=v5.0", "portable50")] - [InlineData("Xamarin.iOS,Version=v1.0", "xamarinios10")] - // TODO: should somehow we allow targetting monoandroid without the version suffix? - [InlineData("MonoAndroid,Version=v2.5", "monoandroid25")] - [Theory] - public void when_file_has_tfm_then_assigned_file_contains_target_framework(string targetFrameworkMoniker, string expectedTargetFramework) - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", targetFrameworkMoniker }, - { "PackFolder", "lib" } - }) - } - }; - - Assert.True(task.Execute()); - - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackageFolder = "lib", - PackagePath = $"lib\\{ expectedTargetFramework}\\library.dll", - TargetFramework = expectedTargetFramework, - })); - } - - public static IEnumerable GetMappedKnownKinds => Kinds - // Skip unmapped kinds (i.e. None, Dependency, etc.) - .Where(kind => !string.IsNullOrEmpty(kind.GetMetadata(MetadataName.PackageFolder)) && - // Skip contentFiles from this test since they get a special map that includes the codelang - kind.GetMetadata(MetadataName.PackageFolder) != "contentFiles") - .Select(kind => new object[] { kind.ItemSpec, kind.GetMetadata(MetadataName.PackageFolder), kind.GetMetadata(MetadataName.FrameworkSpecific) }); - - [MemberData(nameof(GetMappedKnownKinds))] - [Theory] - public void when_file_has_known_kind_then_assigned_file_contains_mapped_package_folder(string packageFileKind, string mappedPackageFolder, string frameworkSpecific) - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", packageFileKind } - }) - } - }; - - var isFrameworkSpecific = true; - bool.TryParse(frameworkSpecific, out isFrameworkSpecific); - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackageFolder = mappedPackageFolder, - PackagePath = $"{mappedPackageFolder}{(isFrameworkSpecific ? "\\net45" : "")}\\library.dll", - })); - } - - public static IEnumerable GetUnmappedKnownKinds => Kinds - .Where(kind => string.IsNullOrEmpty(kind.GetMetadata(MetadataName.PackageFolder)) && - kind.GetMetadata(MetadataName.PackageFolder) != "contentFiles" && - kind.ItemSpec != PackFolderKind.None) - .Select(kind => new object[] { kind.ItemSpec }); - - [MemberData(nameof(GetUnmappedKnownKinds))] - [Theory] - public void when_file_has_known_kind_with_no_package_folder_then_package_path_is_empty(string packageFileKind) - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("Foo", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", packageFileKind } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackagePath = "", - })); - } - - [Fact] - public void when_file_has_explicit_package_path_then_calculated_package_folder_is_empty_and_preserves_package_path() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("readme.txt", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "none" }, - { "PackagePath", "docs\\readme.txt" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackageFolder = "", - PackagePath = "docs\\readme.txt" - })); - } - - [InlineData("", "vb", "contentFiles\\vb\\any\\")] - [InlineData("", "", "contentFiles\\any\\any\\")] - [InlineData(".NETFramework,Version=v4.5", "cs", "contentFiles\\cs\\net45\\")] - [InlineData(".NETFramework,Version=v4.5", "", "contentFiles\\any\\net45\\")] - [Theory] - public void when_assigning_content_file_then_applies_tfm_and_language(string tfm, string lang, string expectedPath) - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("Sample.cs", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", tfm }, - { "PackFolder", "content" }, - { "CodeLanguage", lang } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Equal("contentFiles", task.AssignedFiles[0].GetMetadata(MetadataName.PackageFolder)); - Assert.True(task.AssignedFiles[0].GetMetadata("PackagePath").StartsWith(expectedPath), - $"'{task.AssignedFiles[0].GetMetadata("PackagePath")}' does not start with expected '{expectedPath}'"); - } - - [Fact] - public void when_assigning_content_file_with_reserved_dir_then_fails() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem(@"contentFiles\cs\monodroid\content.cs", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "TargetPath", @"contentFiles\cs\monodroid\content.cs" }, - { "PackFolder", "content" }, - }) - } - }; - - Assert.False(task.Execute()); - Assert.Contains(engine.LoggedErrorEvents, e => e.Code == nameof(ThisAssembly.Strings.ErrorCode.NG0013)); - } + public void when_package_file_has_directory_package_path_and_link_then_appends_linked_file() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem(@"..\..\foo\bar\readme.txt", new Metadata + { + { "PackageId", "A" }, + { "PackFolder", "none" }, + { "Link", @"quickstarts\readme.txt" }, + { "PackagePath", @"docs\" }, + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = @"docs\quickstarts\readme.txt", + })); + } [Fact] - public void when_assigning_content_file_with_additional_metadata_then_preserves_metadata() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("Sample.cs", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "content" }, - { MetadataName.ContentFile.CodeLanguage, "cs" }, - { MetadataName.ContentFile.BuildAction, "EmbeddedResource" }, - { MetadataName.ContentFile.CopyToOutput, "true" }, - { MetadataName.ContentFile.Flatten, "true" }, - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - BuildAction = "EmbeddedResource", - CodeLanguage = "cs", - CopyToOutput = "true", - Flatten = "true", - })); - } - - [Fact] - public void when_file_has_none_kind_then_assigned_file_has_empty_package_folder_and_relative_package_path() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem(@"content\docs\readme.txt", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "none" }, - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackageFolder = "", - PackagePath = @"content\docs\readme.txt", - })); - } - - [Fact] - public void when_file_has_none_kind_with_target_path_then_assigned_file_has_empty_package_folder_with_relative_package_path() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "none" }, - { "TargetPath", "workbook\\library.dll"} - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackageFolder = "", - PackagePath = @"workbook\library.dll", - })); - } - - [Fact] - public void when_file_has_none_kind_then_assigned_file_has_no_target_framework_in_package_path() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "none" } - }) - } - }; - - Assert.True(task.Execute()); - Assert.NotEqual("net45", task.AssignedFiles[0].GetMetadata(MetadataName.PackagePath).Split(Path.DirectorySeparatorChar)[0]); - } - - [InlineData("Build", "build")] - [InlineData("Runtimes", "runtimes")] - [InlineData("Workbook", "workbook")] - [Theory] - public void when_file_has_inferred_folder_from_kind_then_assigned_file_contains_inferred_package_folder(string packageFileKind, string inferredPackageFolder) - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("library.dll", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", packageFileKind } - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackageFolder = inferredPackageFolder, - PackagePath = $"{inferredPackageFolder}\\library.dll", - })); - } - - [Fact] - public void when_file_has_relative_target_path_without_tfm_then_package_path_has_relative_path() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("sdk\\bin\\tool.exe", new Metadata - { - { "PackageId", "A" }, - { "PackFolder", "tool" }, - { "TargetPath", "sdk\\bin\\tool.exe"} - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackagePath = @"tools\sdk\bin\tool.exe", - })); - } - - [Fact] - public void when_tool_has_relative_target_path_with_framework_specific_true_then_package_path_has_relative_path_with_target_framework() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("sdk\\bin\\tool.exe", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "tool" }, - { "FrameworkSpecific", "true" }, - { "TargetPath", "sdk\\bin\\tool.exe"} - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackagePath = @"tools\net45\sdk\bin\tool.exe", - })); - } - - [Fact] - public void when_lib_has_framework_specific_false_then_package_path_does_not_have_target_framework() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("console.exe", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "lib" }, - { "FrameworkSpecific", "false" }, - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackagePath = @"lib\console.exe", - })); - } - - [Fact] - public void when_file_is_not_framework_specific_then_it_is_not_assigned_target_framework() - { - var task = new AssignPackagePath - { - BuildEngine = engine, - KnownFolders = Kinds, - Files = new ITaskItem[] - { - new TaskItem("tools\\foo.exe", new Metadata - { - { "PackageId", "A" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, - { "PackFolder", "tools" }, - }) - } - }; - - Assert.True(task.Execute()); - Assert.Contains(task.AssignedFiles, item => item.Matches(new - { - PackagePath = @"tools\foo.exe", - TargetFramework = "", - })); - } - } + public void when_content_is_not_framework_specific_then_has_any_lang_and_tfm() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("readme.txt", new Metadata + { + { "PackageId", "A" }, + { "PackFolder", "content" }, + { "FrameworkSpecific", "false" }, + { "TargetFrameworkMoniker", "MonoAndroid,Version=v2.5" }, + }) + } + }; + + Assert.True(task.Execute()); + + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + CodeLanguage = "any", + TargetFramework = "any", + PackagePath = @"contentFiles\any\any\readme.txt" + })); + } + + // TODO: these all end up in all lowercase, but MonoAndroid, Xamarin.iOS are usually properly + // cased in nupkgs out in the wild (i.e. Rx) + [InlineData(".NETFramework,Version=v4.5", "net45")] + [InlineData(".NETPortable,Version=v5.0", "portable50")] + [InlineData("Xamarin.iOS,Version=v1.0", "xamarinios10")] + // TODO: should somehow we allow targetting monoandroid without the version suffix? + [InlineData("MonoAndroid,Version=v2.5", "monoandroid25")] + [Theory] + public void when_file_has_tfm_then_assigned_file_contains_target_framework(string targetFrameworkMoniker, string expectedTargetFramework) + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", targetFrameworkMoniker }, + { "PackFolder", "lib" } + }) + } + }; + + Assert.True(task.Execute()); + + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackageFolder = "lib", + PackagePath = $"lib\\{ expectedTargetFramework}\\library.dll", + TargetFramework = expectedTargetFramework, + })); + } + + public static IEnumerable GetMappedKnownKinds => Kinds + // Skip unmapped kinds (i.e. None, Dependency, etc.) + .Where(kind => !string.IsNullOrEmpty(kind.GetMetadata(MetadataName.PackageFolder)) && + // Skip contentFiles from this test since they get a special map that includes the codelang + kind.GetMetadata(MetadataName.PackageFolder) != "contentFiles") + .Select(kind => new object[] { kind.ItemSpec, kind.GetMetadata(MetadataName.PackageFolder), kind.GetMetadata(MetadataName.FrameworkSpecific) }); + + [MemberData(nameof(GetMappedKnownKinds))] + [Theory] + public void when_file_has_known_kind_then_assigned_file_contains_mapped_package_folder(string packageFileKind, string mappedPackageFolder, string frameworkSpecific) + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", packageFileKind } + }) + } + }; + + var isFrameworkSpecific = true; + bool.TryParse(frameworkSpecific, out isFrameworkSpecific); + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackageFolder = mappedPackageFolder, + PackagePath = $"{mappedPackageFolder}{(isFrameworkSpecific ? "\\net45" : "")}\\library.dll", + })); + } + + public static IEnumerable GetUnmappedKnownKinds => Kinds + .Where(kind => string.IsNullOrEmpty(kind.GetMetadata(MetadataName.PackageFolder)) && + kind.GetMetadata(MetadataName.PackageFolder) != "contentFiles" && + kind.ItemSpec != PackFolderKind.None) + .Select(kind => new object[] { kind.ItemSpec }); + + [MemberData(nameof(GetUnmappedKnownKinds))] + [Theory] + public void when_file_has_known_kind_with_no_package_folder_then_package_path_is_empty(string packageFileKind) + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("Foo", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", packageFileKind } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = "", + })); + } + + [Fact] + public void when_file_has_explicit_package_path_then_calculated_package_folder_is_empty_and_preserves_package_path() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("readme.txt", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "none" }, + { "PackagePath", "docs\\readme.txt" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackageFolder = "", + PackagePath = "docs\\readme.txt" + })); + } + + [InlineData("", "vb", "contentFiles\\vb\\any\\")] + [InlineData("", "", "contentFiles\\any\\any\\")] + [InlineData(".NETFramework,Version=v4.5", "cs", "contentFiles\\cs\\net45\\")] + [InlineData(".NETFramework,Version=v4.5", "", "contentFiles\\any\\net45\\")] + [Theory] + public void when_assigning_content_file_then_applies_tfm_and_language(string tfm, string lang, string expectedPath) + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("Sample.cs", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", tfm }, + { "PackFolder", "content" }, + { "CodeLanguage", lang } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Equal("contentFiles", task.AssignedFiles[0].GetMetadata(MetadataName.PackageFolder)); + Assert.True(task.AssignedFiles[0].GetMetadata("PackagePath").StartsWith(expectedPath), + $"'{task.AssignedFiles[0].GetMetadata("PackagePath")}' does not start with expected '{expectedPath}'"); + } + + [Fact] + public void when_assigning_content_file_with_reserved_dir_then_fails() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem(@"contentFiles\cs\monodroid\content.cs", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "TargetPath", @"contentFiles\cs\monodroid\content.cs" }, + { "PackFolder", "content" }, + }) + } + }; + + Assert.False(task.Execute()); + Assert.Contains(engine.LoggedErrorEvents, e => e.Code == nameof(ThisAssembly.Strings.ErrorCode.NG0013)); + } + + [Fact] + public void when_assigning_content_file_with_additional_metadata_then_preserves_metadata() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("Sample.cs", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "content" }, + { MetadataName.ContentFile.CodeLanguage, "cs" }, + { MetadataName.ContentFile.BuildAction, "EmbeddedResource" }, + { MetadataName.ContentFile.CopyToOutput, "true" }, + { MetadataName.ContentFile.Flatten, "true" }, + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + BuildAction = "EmbeddedResource", + CodeLanguage = "cs", + CopyToOutput = "true", + Flatten = "true", + })); + } + + [Fact] + public void when_file_has_none_kind_then_assigned_file_has_empty_package_folder_and_relative_package_path() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem(@"content\docs\readme.txt", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "none" }, + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackageFolder = "", + PackagePath = @"content\docs\readme.txt", + })); + } + + [Fact] + public void when_file_has_none_kind_with_target_path_then_assigned_file_has_empty_package_folder_with_relative_package_path() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "none" }, + { "TargetPath", "workbook\\library.dll"} + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackageFolder = "", + PackagePath = @"workbook\library.dll", + })); + } + + [Fact] + public void when_file_has_none_kind_then_assigned_file_has_no_target_framework_in_package_path() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "none" } + }) + } + }; + + Assert.True(task.Execute()); + Assert.NotEqual("net45", task.AssignedFiles[0].GetMetadata(MetadataName.PackagePath).Split(Path.DirectorySeparatorChar)[0]); + } + + [InlineData("Build", "build")] + [InlineData("Runtimes", "runtimes")] + [InlineData("Workbook", "workbook")] + [Theory] + public void when_file_has_inferred_folder_from_kind_then_assigned_file_contains_inferred_package_folder(string packageFileKind, string inferredPackageFolder) + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("library.dll", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", packageFileKind } + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackageFolder = inferredPackageFolder, + PackagePath = $"{inferredPackageFolder}\\library.dll", + })); + } + + [Fact] + public void when_file_has_relative_target_path_without_tfm_then_package_path_has_relative_path() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("sdk\\bin\\tool.exe", new Metadata + { + { "PackageId", "A" }, + { "PackFolder", "tool" }, + { "TargetPath", "sdk\\bin\\tool.exe"} + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = @"tools\sdk\bin\tool.exe", + })); + } + + [Fact] + public void when_tool_has_relative_target_path_with_framework_specific_true_then_package_path_has_relative_path_with_target_framework() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("sdk\\bin\\tool.exe", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "tool" }, + { "FrameworkSpecific", "true" }, + { "TargetPath", "sdk\\bin\\tool.exe"} + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = @"tools\net45\sdk\bin\tool.exe", + })); + } + + [Fact] + public void when_lib_has_framework_specific_false_then_package_path_does_not_have_target_framework() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("console.exe", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "lib" }, + { "FrameworkSpecific", "false" }, + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = @"lib\console.exe", + })); + } + + [Fact] + public void when_file_is_not_framework_specific_then_it_is_not_assigned_target_framework() + { + var task = new AssignPackagePath + { + BuildEngine = engine, + KnownFolders = Kinds, + Files = new ITaskItem[] + { + new TaskItem("tools\\foo.exe", new Metadata + { + { "PackageId", "A" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" }, + { "PackFolder", "tools" }, + }) + } + }; + + Assert.True(task.Execute()); + Assert.Contains(task.AssignedFiles, item => item.Matches(new + { + PackagePath = @"tools\foo.exe", + TargetFramework = "", + })); + } + } } diff --git a/src/NuGetizer.Tests/Builder.NuGetizer.cs b/src/NuGetizer.Tests/Builder.NuGetizer.cs index 04d0f4e5..89204d6a 100644 --- a/src/NuGetizer.Tests/Builder.NuGetizer.cs +++ b/src/NuGetizer.Tests/Builder.NuGetizer.cs @@ -25,7 +25,7 @@ public static TargetResult BuildProject( string projectContent = null, string target = "GetPackageContents", ITestOutputHelper output = null, - LoggerVerbosity? verbosity = null, + LoggerVerbosity? verbosity = null, params (string name, string contents)[] files) { using var sha = new SHA1Managed(); @@ -38,19 +38,31 @@ public static TargetResult BuildProject( var doc = XDocument.Parse(projectContent); doc.Root.AddFirst(XElement .Parse("")); - - doc.Save(Path.Combine(scenarioDir, "library.csproj")); + + doc.Save(Path.Combine(scenarioDir, "scenario.csproj")); foreach (var file in files) { - File.WriteAllText(Path.Combine(scenarioDir, file.name), file.contents); + try + { + doc = XDocument.Parse(file.contents); + doc.Root.AddFirst(XElement + .Parse("")); + + doc.Save(Path.Combine(scenarioDir, file.name)); + } + catch (System.Xml.XmlException) + { + File.WriteAllText(Path.Combine(scenarioDir, file.name), file.contents); + } + } using (var disable = OpenBuildLogAttribute.Disable()) - BuildScenario(scenarioName, target: "Restore", output: output, verbosity: verbosity) + BuildScenario(scenarioName, projectName: "scenario", target: "Restore", output: output, verbosity: verbosity) .AssertSuccess(output); - return BuildScenario(scenarioName, target: target, output: output, verbosity: verbosity); + return BuildScenario(scenarioName, projectName: "scenario", target: target, output: output, verbosity: verbosity); } public static TargetResult BuildScenario( @@ -198,7 +210,7 @@ public static bool IsActive return Thread.GetData(data) != null; #else - return false; + return false; #endif } set @@ -220,35 +232,35 @@ public static bool IsActive if (data != null) Thread.SetData(data, null); #endif - } } } - public static IDisposable Disable() => new DisposableOpenBinlog(); +} +public static IDisposable Disable() => new DisposableOpenBinlog(); - public override void Before(MethodInfo methodUnderTest) - { - IsActive = true; - base.Before(methodUnderTest); - } +public override void Before(MethodInfo methodUnderTest) +{ + IsActive = true; + base.Before(methodUnderTest); +} - public override void After(MethodInfo methodUnderTest) +public override void After(MethodInfo methodUnderTest) +{ + IsActive = false; + base.After(methodUnderTest); +} + +class DisposableOpenBinlog : IDisposable +{ + bool isActive; + + public DisposableOpenBinlog() { + isActive = IsActive; IsActive = false; - base.After(methodUnderTest); } - class DisposableOpenBinlog : IDisposable - { - bool isActive; - - public DisposableOpenBinlog() - { - isActive = IsActive; - IsActive = false; - } - - public void Dispose() => IsActive = isActive; - } + public void Dispose() => IsActive = isActive; +} } /// diff --git a/src/NuGetizer.Tests/Builder.cs b/src/NuGetizer.Tests/Builder.cs index a895924d..4b992f6c 100644 --- a/src/NuGetizer.Tests/Builder.cs +++ b/src/NuGetizer.Tests/Builder.cs @@ -19,94 +19,94 @@ /// static partial class Builder { - const string ToolsVersion = "Current"; + const string ToolsVersion = "Current"; - public static BuildResult Build(ProjectInstance project, string targets, Dictionary properties = null, ILogger[] loggers = null) - { - properties = properties ?? new Dictionary(); + public static BuildResult Build(ProjectInstance project, string targets, Dictionary properties = null, ILogger[] loggers = null) + { + properties = properties ?? new Dictionary(); - var manager = BuildManager.DefaultBuildManager; - var request = new BuildRequestData(project, targets.Split(',')); - var parameters = new BuildParameters - { - GlobalProperties = properties, - LogInitialPropertiesAndItems = true, - LogTaskInputs = true, - }; + var manager = BuildManager.DefaultBuildManager; + var request = new BuildRequestData(project, targets.Split(',')); + var parameters = new BuildParameters + { + GlobalProperties = properties, + LogInitialPropertiesAndItems = true, + LogTaskInputs = true, + }; - if (loggers != null) - parameters.Loggers = loggers; + if (loggers != null) + parameters.Loggers = loggers; - return manager.Build(parameters, request); - } + return manager.Build(parameters, request); + } - public static BuildResult Build(string projectOrSolution, string targets, Dictionary properties = null, ILogger[] loggers = null) - { - if (!Path.IsPathRooted(projectOrSolution)) - projectOrSolution = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, projectOrSolution); + public static BuildResult Build(string projectOrSolution, string targets, Dictionary properties = null, ILogger[] loggers = null) + { + if (!Path.IsPathRooted(projectOrSolution)) + projectOrSolution = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, projectOrSolution); - if (!File.Exists(projectOrSolution)) - throw new FileNotFoundException($"Project or solution to build {projectOrSolution} was not found.", projectOrSolution); + if (!File.Exists(projectOrSolution)) + throw new FileNotFoundException($"Project or solution to build {projectOrSolution} was not found.", projectOrSolution); - properties = properties ?? new Dictionary(); + properties = properties ?? new Dictionary(); - // If file is not a solution, build up a fake solution configuration so P2P references just work - if (Path.GetExtension(projectOrSolution) != ".sln") - AddSolutionConfiguration(projectOrSolution, properties); + // If file is not a solution, build up a fake solution configuration so P2P references just work + if (Path.GetExtension(projectOrSolution) != ".sln") + AddSolutionConfiguration(projectOrSolution, properties); - var projectInstance = new ProjectInstance(projectOrSolution, properties, ToolsVersion); + var projectInstance = new ProjectInstance(projectOrSolution, properties, ToolsVersion); - return Build(projectInstance, targets, properties, loggers); - } + return Build(projectInstance, targets, properties, loggers); + } public static string TestOutputPath([CallerMemberName] string testName = null) => "bin\\" + testName + "\\"; - static void AddSolutionConfiguration(string projectFile, Dictionary properties) - { - var collection = new ProjectCollection(properties); - var toolsVersion = default(string); - var project = new Project(projectFile, properties, toolsVersion, collection); - var config = project.GetPropertyValue("Configuration"); - var platform = project.GetPropertyValue("Platform"); - - var xml = new XElement("SolutionConfiguration"); - xml.Add(new XElement("ProjectConfiguration", - new XAttribute("Project", project.GetPropertyValue("ProjectGuid")), - new XAttribute("AbsolutePath", project.FullPath), - new XAttribute("BuildProjectInSolution", "true")) - { - Value = $"{config}|{platform}" - }); - - foreach (var reference in GetProjectReferences(project)) - { - xml.Add(new XElement("ProjectConfiguration", - new XAttribute("Project", reference.GetPropertyValue("ProjectGuid")), - new XAttribute("AbsolutePath", reference.FullPath), - new XAttribute("BuildProjectInSolution", "true")) - { - Value = $"{config}|{platform}" - }); - } - - properties["CurrentSolutionConfigurationContents"] = xml.ToString(SaveOptions.None); - } - - static IEnumerable GetProjectReferences(Project project) - { - foreach (var reference in project.GetItems("ProjectReference")) - { - var referenced = new Project( - Path.Combine(Path.GetDirectoryName(project.FullPath), reference.EvaluatedInclude), - project.GlobalProperties, - project.ToolsVersion, - new ProjectCollection(project.GlobalProperties)); - - yield return referenced; - foreach (var transitive in GetProjectReferences(referenced)) - { - yield return transitive; - } - } - } + static void AddSolutionConfiguration(string projectFile, Dictionary properties) + { + var collection = new ProjectCollection(properties); + var toolsVersion = default(string); + var project = new Project(projectFile, properties, toolsVersion, collection); + var config = project.GetPropertyValue("Configuration"); + var platform = project.GetPropertyValue("Platform"); + + var xml = new XElement("SolutionConfiguration"); + xml.Add(new XElement("ProjectConfiguration", + new XAttribute("Project", project.GetPropertyValue("ProjectGuid")), + new XAttribute("AbsolutePath", project.FullPath), + new XAttribute("BuildProjectInSolution", "true")) + { + Value = $"{config}|{platform}" + }); + + foreach (var reference in GetProjectReferences(project)) + { + xml.Add(new XElement("ProjectConfiguration", + new XAttribute("Project", reference.GetPropertyValue("ProjectGuid")), + new XAttribute("AbsolutePath", reference.FullPath), + new XAttribute("BuildProjectInSolution", "true")) + { + Value = $"{config}|{platform}" + }); + } + + properties["CurrentSolutionConfigurationContents"] = xml.ToString(SaveOptions.None); + } + + static IEnumerable GetProjectReferences(Project project) + { + foreach (var reference in project.GetItems("ProjectReference")) + { + var referenced = new Project( + Path.Combine(Path.GetDirectoryName(project.FullPath), reference.EvaluatedInclude), + project.GlobalProperties, + project.ToolsVersion, + new ProjectCollection(project.GlobalProperties)); + + yield return referenced; + foreach (var transitive in GetProjectReferences(referenced)) + { + yield return transitive; + } + } + } } \ No newline at end of file diff --git a/src/NuGetizer.Tests/CreatePackageTests.cs b/src/NuGetizer.Tests/CreatePackageTests.cs index b15a4e8d..3d21b3b8 100644 --- a/src/NuGetizer.Tests/CreatePackageTests.cs +++ b/src/NuGetizer.Tests/CreatePackageTests.cs @@ -1,132 +1,155 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; +using System.Linq; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; -using Xunit; -using Xunit.Abstractions; -using System.Linq; -using System.Diagnostics; using NuGet.Frameworks; -using NuGet.Packaging.Core; -using Metadata = System.Collections.Generic.Dictionary; -using NuGetizer.Tasks; using NuGet.Packaging; +using NuGet.Packaging.Core; using NuGet.Packaging.Licenses; +using NuGetizer.Tasks; +using Xunit; +using Xunit.Abstractions; +using Metadata = System.Collections.Generic.Dictionary; namespace NuGetizer { public class CreatePackageTests - { - ITestOutputHelper output; - MockBuildEngine engine; - CreatePackage task; - bool createPackage = false; - - public CreatePackageTests(ITestOutputHelper output) - { - this.output = output; - engine = new MockBuildEngine(output); - task = new CreatePackage - { - BuildEngine = engine, - NuspecFile = Path.GetTempFileName(), - Manifest = new TaskItem("package", new Metadata - { - { "Id", "package" }, - { "Version", "1.0.0" }, - { "Title", "title" }, - { "Description", "description" }, - { "Summary", "summary" }, - { "Language", "en" }, - - { "Copyright", "copyright" }, - { "RequireLicenseAcceptance", "true" }, - - { "Authors", "author1, author2" }, - { "Owners", "owner1, owner2" }, - { "Tags", "nuget msbuild" }, - - { "LicenseUrl", "http://contoso.com/license.txt" }, - { "ProjectUrl", "http://contoso.com/" }, - { "IconUrl", "http://contoso.com/icon.png" }, - { "ReleaseNotes", "release notes" }, - { "MinClientVersion", "3.4.0" }, - { "PackageTypes", PackageType.Dependency.Name } - }) - }; + { + ITestOutputHelper output; + MockBuildEngine engine; + CreatePackage task; + bool createPackage = false; + + public CreatePackageTests(ITestOutputHelper output) + { + this.output = output; + engine = new MockBuildEngine(output); + task = new CreatePackage + { + BuildEngine = engine, + NuspecFile = Path.GetTempFileName(), + Manifest = new TaskItem("package", new Metadata + { + { MetadataName.PackageId, "package" }, + { MetadataName.Version, "1.0.0" }, + { "Title", "title" }, + { "Description", "description" }, + { "Summary", "summary" }, + { "Language", "en" }, + + { "Copyright", "copyright" }, + { "RequireLicenseAcceptance", "true" }, + + { "Authors", "author1, author2" }, + { "Owners", "owner1, owner2" }, + { "Tags", "nuget msbuild" }, + + { "LicenseUrl", "http://contoso.com/license.txt" }, + { "ProjectUrl", "http://contoso.com/" }, + { "IconUrl", "http://contoso.com/icon.png" }, + { "ReleaseNotes", "release notes" }, + { "MinClientVersion", "3.4.0" }, + { "PackageTypes", PackageType.Dependency.Name } + }) + }; #if RELEASE // Create the actual .nupkg to ensure everything is working // fine end to end. createPackage = true; #endif - } + } + + Manifest ExecuteTask() => createPackage ? + task.Execute(new MemoryStream()) : + task.CreateManifest(); + + [Fact] + public void when_output_path_not_exists_then_creates_it() + { + task.Contents = new[] + { + // Need at least one dependency or content file for the generation to succeed. + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, "package" }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; + + task.EmitPackage = "true"; + task.TargetPath = Path.Combine(Base62.Encode(DateTime.Now.Ticks), "output.nupkg"); + + Assert.True(task.Execute()); + Assert.True(File.Exists(task.TargetPath)); + } - Manifest ExecuteTask() => createPackage ? - task.Execute(new MemoryStream()) : - task.CreateManifest(); - [Fact] - public void when_creating_package_then_contains_all_metadata() - { - task.Contents = new[] - { + [Fact] + public void when_creating_package_then_contains_all_metadata() + { + task.Contents = new[] + { // Need at least one dependency or content file for the generation to succeed. new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, - { MetadataName.TargetFramework, "net45" } - }), - }; - - var metadata = ExecuteTask().Metadata; - - Assert.True(metadata.RequireLicenseAcceptance); - - Assert.Equal(task.Manifest.GetMetadata("PackageId"), metadata.Id); - Assert.Equal(task.Manifest.GetMetadata("Version"), metadata.Version.ToString()); - Assert.Equal(task.Manifest.GetMetadata("Title"), metadata.Title); - Assert.Equal(task.Manifest.GetMetadata("Description"), metadata.Description); - Assert.Equal(task.Manifest.GetMetadata("Summary"), metadata.Summary); - Assert.Equal(task.Manifest.GetMetadata("Language"), metadata.Language); - Assert.Equal(task.Manifest.GetMetadata("Copyright"), metadata.Copyright); - Assert.Equal(task.Manifest.GetMetadata("LicenseUrl"), metadata.LicenseUrl.ToString()); - Assert.Equal(task.Manifest.GetMetadata("ProjectUrl"), metadata.ProjectUrl.ToString()); - Assert.Equal(task.Manifest.GetMetadata("IconUrl"), metadata.IconUrl.ToString()); - Assert.Equal(task.Manifest.GetMetadata("ReleaseNotes"), metadata.ReleaseNotes); - Assert.Equal(task.Manifest.GetMetadata("MinClientVersion"), metadata.MinClientVersion.ToString()); - Assert.Collection(metadata.PackageTypes, - item => - { - Assert.Equal(PackageType.Dependency.Name, item.Name); - Assert.Equal(PackageType.EmptyVersion, item.Version); - }); - } - - [Fact] - public void when_creating_package_has_development_dependency_metadata_then_manifest_has_development_dependency() - { - task.Contents = new[] - { + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; + + var metadata = ExecuteTask().Metadata; + + Assert.True(metadata.RequireLicenseAcceptance); + + Assert.Equal(task.Manifest.GetMetadata("PackageId"), metadata.Id); + Assert.Equal(task.Manifest.GetMetadata("Version"), metadata.Version.ToString()); + Assert.Equal(task.Manifest.GetMetadata("Title"), metadata.Title); + Assert.Equal(task.Manifest.GetMetadata("Description"), metadata.Description); + Assert.Equal(task.Manifest.GetMetadata("Summary"), metadata.Summary); + Assert.Equal(task.Manifest.GetMetadata("Language"), metadata.Language); + Assert.Equal(task.Manifest.GetMetadata("Copyright"), metadata.Copyright); + Assert.Equal(task.Manifest.GetMetadata("LicenseUrl"), metadata.LicenseUrl.ToString()); + Assert.Equal(task.Manifest.GetMetadata("ProjectUrl"), metadata.ProjectUrl.ToString()); + Assert.Equal(task.Manifest.GetMetadata("IconUrl"), metadata.IconUrl.ToString()); + Assert.Equal(task.Manifest.GetMetadata("ReleaseNotes"), metadata.ReleaseNotes); + Assert.Equal(task.Manifest.GetMetadata("MinClientVersion"), metadata.MinClientVersion.ToString()); + Assert.Collection(metadata.PackageTypes, + item => + { + Assert.Equal(PackageType.Dependency.Name, item.Name); + Assert.Equal(PackageType.EmptyVersion, item.Version); + }); + } + + [Fact] + public void when_creating_package_has_development_dependency_metadata_then_manifest_has_development_dependency() + { + task.Contents = new[] + { // Need at least one dependency or content file for the generation to succeed. new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, - { MetadataName.TargetFramework, "net45" } - }), - }; + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; - task.Manifest.SetMetadata("DevelopmentDependency", "true"); + task.Manifest.SetMetadata("DevelopmentDependency", "true"); - var metadata = ExecuteTask().Metadata; + var metadata = ExecuteTask().Metadata; - Assert.True(metadata.DevelopmentDependency); - } + Assert.True(metadata.DevelopmentDependency); + } [Fact] public void when_creating_package_has_license_expression_then_manifest_has_license() @@ -143,709 +166,709 @@ public void when_creating_package_has_license_expression_then_manifest_has_licen } [Fact] - public void when_creating_package_with_simple_dependency_then_contains_dependency_group() - { - task.Contents = new[] - { - new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, + public void when_creating_package_with_simple_dependency_then_contains_dependency_group() + { + task.Contents = new[] + { + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, // NOTE: AssignPackagePath takes care of converting TFM > short name { MetadataName.TargetFramework, "net45" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Single(manifest.Metadata.DependencyGroups); - Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.First().TargetFramework); - Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); - Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); - - // We get a version range actually for the specified dependency, like [1.0.0,) - Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString()); - } - - [Fact] - public void when_creating_package_with_dependency_and_include_assets_then_contains_dependency_include_attribute() - { - task.Contents = new[] - { - new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, + }), + }; + + var manifest = ExecuteTask(); + + Assert.NotNull(manifest); + Assert.Single(manifest.Metadata.DependencyGroups); + Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.First().TargetFramework); + Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); + Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); + + // We get a version range actually for the specified dependency, like [1.0.0,) + Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString()); + } + + [Fact] + public void when_creating_package_with_dependency_and_include_assets_then_contains_dependency_include_attribute() + { + task.Contents = new[] + { + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, // NOTE: AssignPackagePath takes care of converting TFM > short name { MetadataName.TargetFramework, "net45" }, - { MetadataName.IncludeAssets, "all" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Single(manifest.Metadata.DependencyGroups); - Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); - Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); - Assert.Equal(1, manifest.Metadata.DependencyGroups.First().Packages.First().Include.Count); - Assert.Equal("all", manifest.Metadata.DependencyGroups.First().Packages.First().Include[0]); - } - - [Fact] - public void when_creating_package_with_dependency_and_exclude_assets_then_contains_dependency_exclude_attribute() - { - task.Contents = new[] - { - new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, + { MetadataName.IncludeAssets, "all" } + }), + }; + + var manifest = ExecuteTask(); + + Assert.NotNull(manifest); + Assert.Single(manifest.Metadata.DependencyGroups); + Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); + Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); + Assert.Equal(1, manifest.Metadata.DependencyGroups.First().Packages.First().Include.Count); + Assert.Equal("all", manifest.Metadata.DependencyGroups.First().Packages.First().Include[0]); + } + + [Fact] + public void when_creating_package_with_dependency_and_exclude_assets_then_contains_dependency_exclude_attribute() + { + task.Contents = new[] + { + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, // NOTE: AssignPackagePath takes care of converting TFM > short name { MetadataName.TargetFramework, "net45" }, - { MetadataName.ExcludeAssets, "all" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Single(manifest.Metadata.DependencyGroups); - Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); - Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); - Assert.Equal(1, manifest.Metadata.DependencyGroups.First().Packages.First().Exclude.Count); - Assert.Equal("all", manifest.Metadata.DependencyGroups.First().Packages.First().Exclude[0]); - } - - [Fact] - public void when_creating_package_with_dependency_and_without_include_assets_then_not_contains_dependency_include_attribute() - { - task.Contents = new[] - { - new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, + { MetadataName.ExcludeAssets, "all" } + }), + }; + + var manifest = ExecuteTask(); + + Assert.NotNull(manifest); + Assert.Single(manifest.Metadata.DependencyGroups); + Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); + Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); + Assert.Equal(1, manifest.Metadata.DependencyGroups.First().Packages.First().Exclude.Count); + Assert.Equal("all", manifest.Metadata.DependencyGroups.First().Packages.First().Exclude[0]); + } + + [Fact] + public void when_creating_package_with_dependency_and_without_include_assets_then_not_contains_dependency_include_attribute() + { + task.Contents = new[] + { + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, // NOTE: AssignPackagePath takes care of converting TFM > short name { MetadataName.TargetFramework, "net45" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Single(manifest.Metadata.DependencyGroups); - Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); - Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); - Assert.Equal(0, manifest.Metadata.DependencyGroups.First().Packages.First().Include.Count); - } - - [Fact] - public void when_creating_package_with_dependency_and_without_exclude_assets_then_not_contains_dependency_exclude_attribute() - { - task.Contents = new[] - { - new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, + }), + }; + + var manifest = ExecuteTask(); + + Assert.NotNull(manifest); + Assert.Single(manifest.Metadata.DependencyGroups); + Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); + Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); + Assert.Equal(0, manifest.Metadata.DependencyGroups.First().Packages.First().Include.Count); + } + + [Fact] + public void when_creating_package_with_dependency_and_without_exclude_assets_then_not_contains_dependency_exclude_attribute() + { + task.Contents = new[] + { + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, // NOTE: AssignPackagePath takes care of converting TFM > short name { MetadataName.TargetFramework, "net45" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Single(manifest.Metadata.DependencyGroups); - Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); - Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); - Assert.Equal(0, manifest.Metadata.DependencyGroups.First().Packages.First().Exclude.Count); - } - - [Fact] - public void when_creating_package_with_non_framework_secific_dependency_then_contains_generic_dependency_group() - { - task.Contents = new[] - { - new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, - { MetadataName.TargetFramework, "net45" }, - { MetadataName.FrameworkSpecific, "false" }, - }), - new TaskItem("Microsoft.Build", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "15.0.0" }, - { MetadataName.TargetFramework, "net45" } - }), - }; + }), + }; - var manifest = ExecuteTask(); + var manifest = ExecuteTask(); - //Process.Start("notepad.exe", task.NuspecFile); + Assert.NotNull(manifest); + Assert.Single(manifest.Metadata.DependencyGroups); + Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); + Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); + Assert.Equal(0, manifest.Metadata.DependencyGroups.First().Packages.First().Exclude.Count); + } - Assert.NotNull(manifest); - Assert.Equal(2, manifest.Metadata.DependencyGroups.Count()); - Assert.True( - manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.AnyFramework || - manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.UnsupportedFramework); + [Fact] + public void when_creating_package_with_non_framework_secific_dependency_then_contains_generic_dependency_group() + { + task.Contents = new[] + { + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "net45" }, + { MetadataName.FrameworkSpecific, "false" }, + }), + new TaskItem("Microsoft.Build", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "15.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; + + var manifest = ExecuteTask(); + + //Process.Start("notepad.exe", task.NuspecFile); + + Assert.NotNull(manifest); + Assert.Equal(2, manifest.Metadata.DependencyGroups.Count()); + Assert.True( + manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.AnyFramework || + manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.UnsupportedFramework); + + Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.Last().TargetFramework); + + Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); + Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); + Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString()); + + Assert.Single(manifest.Metadata.DependencyGroups.Last().Packages); + Assert.Equal("Microsoft.Build", manifest.Metadata.DependencyGroups.Last().Packages.First().Id); + Assert.Equal("15.0.0", manifest.Metadata.DependencyGroups.Last().Packages.First().VersionRange.MinVersion.ToString()); + } - Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.Last().TargetFramework); + [Fact] + public void when_creating_package_with_any_framework_secific_dependency_then_contains_generic_dependency_group() + { + task.Contents = new[] + { + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "any" }, + }), + new TaskItem("Microsoft.Build", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "15.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; + + var manifest = ExecuteTask(); + + Assert.NotNull(manifest); + Assert.Equal(2, manifest.Metadata.DependencyGroups.Count()); + Assert.True( + manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.AnyFramework || + manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.UnsupportedFramework); + + Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.Last().TargetFramework); + + Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); + Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); + Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString()); + + Assert.Single(manifest.Metadata.DependencyGroups.Last().Packages); + Assert.Equal("Microsoft.Build", manifest.Metadata.DependencyGroups.Last().Packages.First().Id); + Assert.Equal("15.0.0", manifest.Metadata.DependencyGroups.Last().Packages.First().VersionRange.MinVersion.ToString()); + } - Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); - Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); - Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString()); + [Fact] + public void when_creating_package_with_empty_dependency_groups_then_succeeds() + { + task.Contents = new[] + { + new TaskItem(Path.GetTempFileName(), new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.None }, + { MetadataName.PackagePath, "readme.txt" } + }), + new TaskItem("_._", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "*" }, + { MetadataName.TargetFramework, "net45" } + }), + new TaskItem("_._", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "*" }, + { MetadataName.TargetFramework, "win" } + }), + new TaskItem("_._", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "*" }, + { MetadataName.TargetFramework, "wpa" } + }), + new TaskItem("_._", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "*" }, + { MetadataName.TargetFramework, "MonoAndroid10" } + }), + }; + + task.NuspecFile = Path.GetTempFileName(); + createPackage = Debugger.IsAttached; + + var manifest = ExecuteTask(); + + Assert.NotNull(manifest); + Assert.Equal(4, manifest.Metadata.DependencyGroups.Count()); + Assert.All(manifest.Metadata.DependencyGroups, d => Assert.Empty(d.Packages)); + } - Assert.Single(manifest.Metadata.DependencyGroups.Last().Packages); - Assert.Equal("Microsoft.Build", manifest.Metadata.DependencyGroups.Last().Packages.First().Id); - Assert.Equal("15.0.0", manifest.Metadata.DependencyGroups.Last().Packages.First().VersionRange.MinVersion.ToString()); - } + [Fact] + public void when_creating_package_with_multiple_target_frameworks_generates_empty_dependency_groups() + { + task.Contents = new[] + { + new TaskItem(Path.GetTempFileName(), new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackagePath, "lib\\net35\\library.dll" }, + { MetadataName.PackFolder, PackFolderKind.Lib }, + { MetadataName.TargetFramework, "net35" } + }), + new TaskItem(Path.GetTempFileName(), new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackagePath, "lib\\net40\\library.dll" }, + { MetadataName.PackFolder, PackFolderKind.Lib }, + { MetadataName.TargetFramework, "net40" } + }), + new TaskItem(Path.GetTempFileName(), new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackagePath, "lib\\net45\\library.dll" }, + { MetadataName.PackFolder, PackFolderKind.Lib }, + { MetadataName.TargetFramework, "net45" } + }) + }; + + var manifest = ExecuteTask(); + + HashSet requiredFrameworks = new HashSet() + { + "net35", + "net40", + "net45" + }; + + Assert.NotNull(manifest); + Assert.Equal(3, manifest.Metadata.DependencyGroups.Count()); + } - [Fact] - public void when_creating_package_with_any_framework_secific_dependency_then_contains_generic_dependency_group() - { - task.Contents = new[] - { - new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, - { MetadataName.TargetFramework, "any" }, - }), - new TaskItem("Microsoft.Build", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "15.0.0" }, - { MetadataName.TargetFramework, "net45" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Equal(2, manifest.Metadata.DependencyGroups.Count()); - Assert.True( - manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.AnyFramework || - manifest.Metadata.DependencyGroups.First().TargetFramework == NuGetFramework.UnsupportedFramework); - - Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.Last().TargetFramework); - - Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); - Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); - Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString()); - - Assert.Single(manifest.Metadata.DependencyGroups.Last().Packages); - Assert.Equal("Microsoft.Build", manifest.Metadata.DependencyGroups.Last().Packages.First().Id); - Assert.Equal("15.0.0", manifest.Metadata.DependencyGroups.Last().Packages.First().VersionRange.MinVersion.ToString()); - } - - [Fact] - public void when_creating_package_with_empty_dependency_groups_then_succeeds() - { - task.Contents = new[] - { - new TaskItem(Path.GetTempFileName(), new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.None }, - { MetadataName.PackagePath, "readme.txt" } - }), - new TaskItem("_._", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "*" }, - { MetadataName.TargetFramework, "net45" } - }), - new TaskItem("_._", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "*" }, - { MetadataName.TargetFramework, "win" } - }), - new TaskItem("_._", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "*" }, - { MetadataName.TargetFramework, "wpa" } - }), - new TaskItem("_._", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "*" }, - { MetadataName.TargetFramework, "MonoAndroid10" } - }), - }; - - task.NuspecFile = Path.GetTempFileName(); - createPackage = Debugger.IsAttached; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Equal(4, manifest.Metadata.DependencyGroups.Count()); - Assert.All(manifest.Metadata.DependencyGroups, d => Assert.Empty(d.Packages)); - } - - [Fact] - public void when_creating_package_with_multiple_target_frameworks_generates_empty_dependency_groups() - { - task.Contents = new[] - { - new TaskItem(Path.GetTempFileName(), new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackagePath, "lib\\net35\\library.dll" }, - { MetadataName.PackFolder, PackFolderKind.Lib }, - { MetadataName.TargetFramework, "net35" } - }), - new TaskItem(Path.GetTempFileName(), new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackagePath, "lib\\net40\\library.dll" }, - { MetadataName.PackFolder, PackFolderKind.Lib }, - { MetadataName.TargetFramework, "net40" } - }), - new TaskItem(Path.GetTempFileName(), new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackagePath, "lib\\net45\\library.dll" }, - { MetadataName.PackFolder, PackFolderKind.Lib }, - { MetadataName.TargetFramework, "net45" } - }) - }; - - var manifest = ExecuteTask(); - - HashSet requiredFrameworks = new HashSet() - { - "net35", - "net40", - "net45" - }; - - Assert.NotNull(manifest); - Assert.Equal(3, manifest.Metadata.DependencyGroups.Count()); - } - - [Fact] - public void when_creating_package_with_development_dependency_then_does_not_generate_dependency_group() - { - var content = Path.GetTempFileName(); - task.Contents = new[] - { - new TaskItem(content, new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.None }, - { MetadataName.PackagePath, "readme.txt" } - }), - new TaskItem("Helpers", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.Version, "1.0.0" }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.PrivateAssets, "all" }, + [Fact] + public void when_creating_package_with_development_dependency_then_does_not_generate_dependency_group() + { + var content = Path.GetTempFileName(); + task.Contents = new[] + { + new TaskItem(content, new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.None }, + { MetadataName.PackagePath, "readme.txt" } + }), + new TaskItem("Helpers", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.Version, "1.0.0" }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.PrivateAssets, "all" }, // NOTE: AssignPackagePath takes care of converting TFM > short name { MetadataName.TargetFramework, "net45" } - }), - }; + }), + }; - var manifest = ExecuteTask(); + var manifest = ExecuteTask(); - Assert.Empty(manifest.Metadata.DependencyGroups); - } + Assert.Empty(manifest.Metadata.DependencyGroups); + } - [Fact] - public void when_creating_package_with_referenced_package_project_then_contains_package_dependency() - { - task.Contents = new[] - { - new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, + [Fact] + public void when_creating_package_with_referenced_package_project_then_contains_package_dependency() + { + task.Contents = new[] + { + new TaskItem("Newtonsoft.Json", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, // NOTE: AssignPackagePath takes care of converting TFM > short name { MetadataName.TargetFramework, "net45" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Single(manifest.Metadata.DependencyGroups); - Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.First().TargetFramework); - Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); - Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); - - // We get a version range actually for the specified dependency, like [1.0.0,) - Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString()); - } - - [Fact] - public void when_creating_package_with_file_then_contains_file() - { - var content = Path.GetTempFileName(); - task.Contents = new[] - { - new TaskItem(content, new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.None }, - { MetadataName.PackagePath, "readme.txt" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.NotNull(manifest); - Assert.Contains(manifest.Files, file => file.Target == "readme.txt"); - } - - [Fact] - public void when_creating_package_with_content_file_then_adds_as_content_file() - { - var content = Path.GetTempFileName(); - task.Contents = new[] - { - new TaskItem(content, new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.ContentFiles }, - { MetadataName.PackageFolder, PackagingConstants.Folders.ContentFiles }, - { MetadataName.PackagePath, @"contentFiles\any\any\readme.txt" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.Contains(manifest.Files, file => file.Target == @"contentFiles\any\any\readme.txt"); - Assert.Contains(manifest.Metadata.ContentFiles, file => file.Include == @"any\any\readme.txt"); - } - - [Fact] - public void when_creating_package_with_content_file_build_action_then_adds_as_content_file() - { - var content = Path.GetTempFileName(); - task.Contents = new[] - { - new TaskItem(content, new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.ContentFiles }, - { MetadataName.PackageFolder, PackagingConstants.Folders.ContentFiles }, - { MetadataName.ContentFile.BuildAction, "EmbeddedResource" }, - { MetadataName.PackagePath, @"contentFiles\any\any\readme.txt" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.Contains(manifest.Files, file => file.Target == @"contentFiles\any\any\readme.txt"); - Assert.Contains(manifest.Metadata.ContentFiles, file => file.Include == @"any\any\readme.txt" && file.BuildAction == "EmbeddedResource"); - } - - [Fact] - public void when_creating_package_with_content_file_copy_to_output_then_adds_as_content_file() - { - var none = Path.GetTempFileName(); - var content = Path.GetTempFileName(); - task.Contents = new[] - { - new TaskItem(content, new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.ContentFiles }, - { MetadataName.PackageFolder, PackagingConstants.Folders.ContentFiles }, - { MetadataName.ContentFile.CopyToOutput, "true" }, - { MetadataName.PackagePath, @"contentFiles\any\any\readme.txt" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.Equal(new[] - { - new ManifestFile { Target = @"contentFiles\any\any\readme.txt" } - }, - manifest.Files, ManifestFileComparer.Default); - - Assert.Equal(new[] - { - new ManifestContentFiles { Include = @"any\any\readme.txt", CopyToOutput = "true" } - }, - manifest.Metadata.ContentFiles, ManifestContentFilesComparer.Default); - } - - [Fact] - public void when_creating_package_with_content_file_flatten_then_adds_as_content_file() - { - var none = Path.GetTempFileName(); - var content = Path.GetTempFileName(); - task.Contents = new[] - { - new TaskItem(content, new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.ContentFiles }, - { MetadataName.PackageFolder, PackagingConstants.Folders.ContentFiles }, - { MetadataName.ContentFile.Flatten, "true" }, - { MetadataName.PackagePath, @"contentFiles\any\any\readme.txt" } - }), - }; - - var manifest = ExecuteTask(); - - Assert.Equal(new[] - { - new ManifestFile - { - Target = @"contentFiles\any\any\readme.txt" - } - }, - manifest.Files,ManifestFileComparer.Default); - - Assert.Equal(new[] - { - new ManifestContentFiles - { - Include = @"any\any\readme.txt", - Flatten = "true" - } - }, - manifest.Metadata.ContentFiles, ManifestContentFilesComparer.Default); - } - - [Fact] - public void when_creating_package_with_framework_reference_then_contains_references() - { - task.Contents = new[] - { - new TaskItem("System.Xml", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, - { MetadataName.TargetFrameworkMoniker, TargetFrameworks.NET45 } - }), - new TaskItem("System.Xml", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, - { MetadataName.TargetFrameworkMoniker, TargetFrameworks.PCL78 } - }), - }; - - var manifest = ExecuteTask(); - - Assert.Equal(manifest.Metadata.FrameworkReferences, - new[] - { - new FrameworkAssemblyReference("System.Xml", new [] { NuGetFramework.Parse(TargetFrameworks.NET45) }), - new FrameworkAssemblyReference("System.Xml", new [] { NuGetFramework.Parse(TargetFrameworks.PCL78) }), - }, - FrameworkAssemblyReferenceComparer.Default); - - Assert.NotNull(manifest); - } - - [Fact] - public void when_creating_package_with_duplicate_framework_references_then_contains_only_unique_references() - { - task.Contents = new[] - { - new TaskItem("System.Xml", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, - { MetadataName.TargetFrameworkMoniker, TargetFrameworks.NET45 } - }), - new TaskItem("System.Xml", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, - { MetadataName.TargetFrameworkMoniker, TargetFrameworks.NET45 } - }), - new TaskItem("System.Xml", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, - { MetadataName.TargetFrameworkMoniker, TargetFrameworks.PCL78 } - }), - new TaskItem("System.Xml", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, - { MetadataName.TargetFrameworkMoniker, TargetFrameworks.PCL78 } - }), - }; - - var manifest = ExecuteTask(); - - Assert.Equal(manifest.Metadata.FrameworkReferences, - new[] - { - new FrameworkAssemblyReference("System.Xml", new [] { NuGetFramework.Parse(TargetFrameworks.NET45) }), - new FrameworkAssemblyReference("System.Xml", new [] { NuGetFramework.Parse(TargetFrameworks.PCL78) }), - }, - FrameworkAssemblyReferenceComparer.Default); - - Assert.NotNull(manifest); - } - - [Fact] - public void when_creating_package_without_package_type() - { - task.Manifest = new TaskItem("package", - new Metadata - { - { "Id", "package" }, - { "Version", "1.0.0" }, - { "Title", "title" }, - { "Description", "description" }, - { "Authors", "author1, author2" }, - }); - - task.Contents = new[] - { + }), + }; + + var manifest = ExecuteTask(); + + Assert.NotNull(manifest); + Assert.Single(manifest.Metadata.DependencyGroups); + Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.First().TargetFramework); + Assert.Single(manifest.Metadata.DependencyGroups.First().Packages); + Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id); + + // We get a version range actually for the specified dependency, like [1.0.0,) + Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString()); + } + + [Fact] + public void when_creating_package_with_file_then_contains_file() + { + var content = Path.GetTempFileName(); + task.Contents = new[] + { + new TaskItem(content, new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.None }, + { MetadataName.PackagePath, "readme.txt" } + }), + }; + + var manifest = ExecuteTask(); + + Assert.NotNull(manifest); + Assert.Contains(manifest.Files, file => file.Target == "readme.txt"); + } + + [Fact] + public void when_creating_package_with_content_file_then_adds_as_content_file() + { + var content = Path.GetTempFileName(); + task.Contents = new[] + { + new TaskItem(content, new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.ContentFiles }, + { MetadataName.PackageFolder, PackagingConstants.Folders.ContentFiles }, + { MetadataName.PackagePath, @"contentFiles\any\any\readme.txt" } + }), + }; + + var manifest = ExecuteTask(); + + Assert.Contains(manifest.Files, file => file.Target == @"contentFiles\any\any\readme.txt"); + Assert.Contains(manifest.Metadata.ContentFiles, file => file.Include == @"any\any\readme.txt"); + } + + [Fact] + public void when_creating_package_with_content_file_build_action_then_adds_as_content_file() + { + var content = Path.GetTempFileName(); + task.Contents = new[] + { + new TaskItem(content, new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.ContentFiles }, + { MetadataName.PackageFolder, PackagingConstants.Folders.ContentFiles }, + { MetadataName.ContentFile.BuildAction, "EmbeddedResource" }, + { MetadataName.PackagePath, @"contentFiles\any\any\readme.txt" } + }), + }; + + var manifest = ExecuteTask(); + + Assert.Contains(manifest.Files, file => file.Target == @"contentFiles\any\any\readme.txt"); + Assert.Contains(manifest.Metadata.ContentFiles, file => file.Include == @"any\any\readme.txt" && file.BuildAction == "EmbeddedResource"); + } + + [Fact] + public void when_creating_package_with_content_file_copy_to_output_then_adds_as_content_file() + { + var none = Path.GetTempFileName(); + var content = Path.GetTempFileName(); + task.Contents = new[] + { + new TaskItem(content, new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.ContentFiles }, + { MetadataName.PackageFolder, PackagingConstants.Folders.ContentFiles }, + { MetadataName.ContentFile.CopyToOutput, "true" }, + { MetadataName.PackagePath, @"contentFiles\any\any\readme.txt" } + }), + }; + + var manifest = ExecuteTask(); + + Assert.Equal(new[] + { + new ManifestFile { Target = @"contentFiles\any\any\readme.txt" } + }, + manifest.Files, ManifestFileComparer.Default); + + Assert.Equal(new[] + { + new ManifestContentFiles { Include = @"any\any\readme.txt", CopyToOutput = "true" } + }, + manifest.Metadata.ContentFiles, ManifestContentFilesComparer.Default); + } + + [Fact] + public void when_creating_package_with_content_file_flatten_then_adds_as_content_file() + { + var none = Path.GetTempFileName(); + var content = Path.GetTempFileName(); + task.Contents = new[] + { + new TaskItem(content, new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.ContentFiles }, + { MetadataName.PackageFolder, PackagingConstants.Folders.ContentFiles }, + { MetadataName.ContentFile.Flatten, "true" }, + { MetadataName.PackagePath, @"contentFiles\any\any\readme.txt" } + }), + }; + + var manifest = ExecuteTask(); + + Assert.Equal(new[] + { + new ManifestFile + { + Target = @"contentFiles\any\any\readme.txt" + } + }, + manifest.Files, ManifestFileComparer.Default); + + Assert.Equal(new[] + { + new ManifestContentFiles + { + Include = @"any\any\readme.txt", + Flatten = "true" + } + }, + manifest.Metadata.ContentFiles, ManifestContentFilesComparer.Default); + } + + [Fact] + public void when_creating_package_with_framework_reference_then_contains_references() + { + task.Contents = new[] + { + new TaskItem("System.Xml", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, + { MetadataName.TargetFrameworkMoniker, TargetFrameworks.NET45 } + }), + new TaskItem("System.Xml", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, + { MetadataName.TargetFrameworkMoniker, TargetFrameworks.PCL78 } + }), + }; + + var manifest = ExecuteTask(); + + Assert.Equal(manifest.Metadata.FrameworkReferences, + new[] + { + new FrameworkAssemblyReference("System.Xml", new [] { NuGetFramework.Parse(TargetFrameworks.NET45) }), + new FrameworkAssemblyReference("System.Xml", new [] { NuGetFramework.Parse(TargetFrameworks.PCL78) }), + }, + FrameworkAssemblyReferenceComparer.Default); + + Assert.NotNull(manifest); + } + + [Fact] + public void when_creating_package_with_duplicate_framework_references_then_contains_only_unique_references() + { + task.Contents = new[] + { + new TaskItem("System.Xml", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, + { MetadataName.TargetFrameworkMoniker, TargetFrameworks.NET45 } + }), + new TaskItem("System.Xml", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, + { MetadataName.TargetFrameworkMoniker, TargetFrameworks.NET45 } + }), + new TaskItem("System.Xml", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, + { MetadataName.TargetFrameworkMoniker, TargetFrameworks.PCL78 } + }), + new TaskItem("System.Xml", new Metadata + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.FrameworkReference }, + { MetadataName.TargetFrameworkMoniker, TargetFrameworks.PCL78 } + }), + }; + + var manifest = ExecuteTask(); + + Assert.Equal(manifest.Metadata.FrameworkReferences, + new[] + { + new FrameworkAssemblyReference("System.Xml", new [] { NuGetFramework.Parse(TargetFrameworks.NET45) }), + new FrameworkAssemblyReference("System.Xml", new [] { NuGetFramework.Parse(TargetFrameworks.PCL78) }), + }, + FrameworkAssemblyReferenceComparer.Default); + + Assert.NotNull(manifest); + } + + [Fact] + public void when_creating_package_without_package_type() + { + task.Manifest = new TaskItem("package", + new Metadata + { + { "Id", "package" }, + { "Version", "1.0.0" }, + { "Title", "title" }, + { "Description", "description" }, + { "Authors", "author1, author2" }, + }); + + task.Contents = new[] + { // Need at least one dependency or content file for the generation to succeed. new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, - { MetadataName.TargetFramework, "net45" } - }), - }; - - var metadata = ExecuteTask().Metadata; - Assert.Empty(metadata.PackageTypes); - } - - [Fact] - public void when_creating_package_with_package_type_empty() - { - task.Manifest = new TaskItem("package", - new Metadata - { - { "Id", "package" }, - { "Version", "1.0.0" }, - { "Title", "title" }, - { "Description", "description" }, - { "Authors", "author1, author2" }, - { "PackageTypes", string.Empty } - }); - - task.Contents = new[] - { + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; + + var metadata = ExecuteTask().Metadata; + Assert.Empty(metadata.PackageTypes); + } + + [Fact] + public void when_creating_package_with_package_type_empty() + { + task.Manifest = new TaskItem("package", + new Metadata + { + { "Id", "package" }, + { "Version", "1.0.0" }, + { "Title", "title" }, + { "Description", "description" }, + { "Authors", "author1, author2" }, + { "PackageTypes", string.Empty } + }); + + task.Contents = new[] + { // Need at least one dependency or content file for the generation to succeed. new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, - { MetadataName.TargetFramework, "net45" } - }), - }; - - var metadata = ExecuteTask().Metadata; - Assert.Empty(metadata.PackageTypes); - } - - [Fact] - public void when_creating_package_with_package_type_version() - { - task.Manifest = new TaskItem("package", - new Metadata - { - { "Id", "package" }, - { "Version", "1.0.0" }, - { "Title", "title" }, - { "Description", "description" }, - { "Authors", "author1, author2" }, - { "PackageTypes", "SomeType, 2.0.0" } - }); - - task.Contents = new[] - { + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; + + var metadata = ExecuteTask().Metadata; + Assert.Empty(metadata.PackageTypes); + } + + [Fact] + public void when_creating_package_with_package_type_version() + { + task.Manifest = new TaskItem("package", + new Metadata + { + { "Id", "package" }, + { "Version", "1.0.0" }, + { "Title", "title" }, + { "Description", "description" }, + { "Authors", "author1, author2" }, + { "PackageTypes", "SomeType, 2.0.0" } + }); + + task.Contents = new[] + { // Need at least one dependency or content file for the generation to succeed. new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, - { MetadataName.TargetFramework, "net45" } - }), - }; - - var metadata = ExecuteTask().Metadata; - Assert.Collection(metadata.PackageTypes, - item => - { - Assert.Equal("SomeType", item.Name); - Assert.Equal(new Version(2, 0, 0), item.Version); - }); - } - - [Fact] - public void when_creating_package_with_multiple_package_types() - { - task.Manifest = new TaskItem("package", - new Metadata - { - { "Id", "package" }, - { "Version", "1.0.0" }, - { "Title", "title" }, - { "Description", "description" }, - { "Authors", "author1, author2" }, - { "PackageTypes", "SomeType, 2.0.0; AnotherType; ThirdTypeWithVersion, 1.2.3.4" } - }); - - task.Contents = new[] - { + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; + + var metadata = ExecuteTask().Metadata; + Assert.Collection(metadata.PackageTypes, + item => + { + Assert.Equal("SomeType", item.Name); + Assert.Equal(new Version(2, 0, 0), item.Version); + }); + } + + [Fact] + public void when_creating_package_with_multiple_package_types() + { + task.Manifest = new TaskItem("package", + new Metadata + { + { "Id", "package" }, + { "Version", "1.0.0" }, + { "Title", "title" }, + { "Description", "description" }, + { "Authors", "author1, author2" }, + { "PackageTypes", "SomeType, 2.0.0; AnotherType; ThirdTypeWithVersion, 1.2.3.4" } + }); + + task.Contents = new[] + { // Need at least one dependency or content file for the generation to succeed. new TaskItem("Newtonsoft.Json", new Metadata - { - { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, - { MetadataName.PackFolder, PackFolderKind.Dependency }, - { MetadataName.Version, "8.0.0" }, - { MetadataName.TargetFramework, "net45" } - }), - }; - - var metadata = ExecuteTask().Metadata; - Assert.Collection(metadata.PackageTypes, - item => - { - Assert.Equal("SomeType", item.Name); - Assert.Equal(new Version(2, 0, 0), item.Version); - }, - item => - { - Assert.Equal("AnotherType", item.Name); - Assert.Equal(PackageType.EmptyVersion, item.Version); - }, - item => - { - Assert.Equal("ThirdTypeWithVersion", item.Name); - Assert.Equal(new Version(1, 2, 3, 4), item.Version); - }); - } - } + { + { MetadataName.PackageId, task.Manifest.GetMetadata("Id") }, + { MetadataName.PackFolder, PackFolderKind.Dependency }, + { MetadataName.Version, "8.0.0" }, + { MetadataName.TargetFramework, "net45" } + }), + }; + + var metadata = ExecuteTask().Metadata; + Assert.Collection(metadata.PackageTypes, + item => + { + Assert.Equal("SomeType", item.Name); + Assert.Equal(new Version(2, 0, 0), item.Version); + }, + item => + { + Assert.Equal("AnotherType", item.Name); + Assert.Equal(PackageType.EmptyVersion, item.Version); + }, + item => + { + Assert.Equal("ThirdTypeWithVersion", item.Name); + Assert.Equal(new Version(1, 2, 3, 4), item.Version); + }); + } + } } diff --git a/src/NuGetizer.Tests/NuGetizer.Tests.csproj b/src/NuGetizer.Tests/NuGetizer.Tests.csproj index 83a53602..13d1096c 100644 --- a/src/NuGetizer.Tests/NuGetizer.Tests.csproj +++ b/src/NuGetizer.Tests/NuGetizer.Tests.csproj @@ -27,7 +27,7 @@ - + diff --git a/src/NuGetizer.Tests/Scenarios/given_an_empty_library/empty_library_project.csproj b/src/NuGetizer.Tests/Scenarios/given_an_empty_library/empty_library_project.csproj deleted file mode 100644 index ec537fe5..00000000 --- a/src/NuGetizer.Tests/Scenarios/given_an_empty_library/empty_library_project.csproj +++ /dev/null @@ -1,28 +0,0 @@ - - - - - netstandard2.0 - - - - - - - - - - ItemDescription - - - - - - - PropertyDescription - - - - \ No newline at end of file diff --git a/src/NuGetizer.Tests/TargetsTests.cs b/src/NuGetizer.Tests/TargetsTests.cs index 0f6a7f3f..aca285bc 100644 --- a/src/NuGetizer.Tests/TargetsTests.cs +++ b/src/NuGetizer.Tests/TargetsTests.cs @@ -10,112 +10,112 @@ namespace NuGetizer { public class TargetsTests - { - ITestOutputHelper output; - - public TargetsTests(ITestOutputHelper output) => this.output = output; - - [Fact] - public void PackFrameworkReferences_is_not_true_for_build_primary_output() - { - var project = new Project("NuGetizer.targets", new Dictionary - { - { "PackFolder", "build" } - }, null); - - Assert.NotEqual("true", project.GetPropertyValue("PackFrameworkReferences")); - } - - [Fact] - public void PackFrameworkReferences_is_not_true_for_tool_primary_output() - { - var project = new Project("NuGetizer.targets", new Dictionary - { - { "PackFolder", "tool" } - }, null); - - Assert.NotEqual("true", project.GetPropertyValue("PackFrameworkReferences")); - } - - [Fact] - public void PackFrameworkReferences_is_not_true_for_tools_primary_output() - { - var project = new Project("NuGetizer.targets", new Dictionary - { - { "PackFolder", "tools" } - }, null); - - Assert.NotEqual("true", project.GetPropertyValue("PackFrameworkReferences")); - } - - [Fact] - public void PackFrameworkReferences_is_true_for_primary_output_lib() - { - var project = new Project("NuGetizer.targets", new Dictionary - { - { "PackFolder", "lib" } - }, null); - - Assert.Equal("true", project.GetPropertyValue("PackFrameworkReferences")); - } - - [Fact] - public void PackFrameworkReferences_is_true_for_default_primary_output_kind() - { - var project = new Project("NuGetizer.targets", new Dictionary - { - }, null); - - Assert.Equal("true", project.GetPropertyValue("PackFrameworkReferences")); - } - - [Fact] - public void PackFrameworkReferences_is_not_true_for_compat_istool() - { - var project = new Project("NuGetizer.targets", new Dictionary - { - { "IsTool", "true" } - }, null); - - Assert.NotEqual("true", project.GetPropertyValue("PackFrameworkReferences")); - } - - [Fact] - public void PackOnBuild_defaults_to_true_for_compat_GeneratePackageOnBuild_true() - { - var project = new Project("NuGetizer.targets", new Dictionary - { - { "GeneratePackageOnBuild", "true" } - }, null); - - Assert.Equal("true", project.GetPropertyValue("PackOnBuild")); - } - - [Fact] - public void package_contents_never_includes_nugetizer_package_reference() - { - var xml = ProjectRootElement.Create(Path.Combine(Directory.GetCurrentDirectory(), MethodBase.GetCurrentMethod().Name)); - xml.AddImport(@"Scenarios\Scenario.props"); - xml.AddImport(@"Scenarios\Scenario.targets"); - xml.AddItemGroup() - .AddItem("PackageReference", "NuGetizer", new[] { new KeyValuePair("Version", "*") }); - - xml.Save(); - - var project = new ProjectInstance(xml); - - var result = Builder.Build(project, "GetPackageContents"); - - Assert.Equal(BuildResultCode.Success, result.OverallResult); - Assert.Equal(TargetResultCode.Success, result["GetPackageContents"].ResultCode); - - var items = result["GetPackageContents"].Items; - - Assert.DoesNotContain(items, item => item.Matches(new - { + { + ITestOutputHelper output; + + public TargetsTests(ITestOutputHelper output) => this.output = output; + + [Fact] + public void PackFrameworkReferences_is_not_true_for_build_primary_output() + { + var project = new Project("NuGetizer.targets", new Dictionary + { + { "PackFolder", "build" } + }, null); + + Assert.NotEqual("true", project.GetPropertyValue("PackFrameworkReferences")); + } + + [Fact] + public void PackFrameworkReferences_is_not_true_for_tool_primary_output() + { + var project = new Project("NuGetizer.targets", new Dictionary + { + { "PackFolder", "tool" } + }, null); + + Assert.NotEqual("true", project.GetPropertyValue("PackFrameworkReferences")); + } + + [Fact] + public void PackFrameworkReferences_is_not_true_for_tools_primary_output() + { + var project = new Project("NuGetizer.targets", new Dictionary + { + { "PackFolder", "tools" } + }, null); + + Assert.NotEqual("true", project.GetPropertyValue("PackFrameworkReferences")); + } + + [Fact] + public void PackFrameworkReferences_is_true_for_primary_output_lib() + { + var project = new Project("NuGetizer.targets", new Dictionary + { + { "PackFolder", "lib" } + }, null); + + Assert.Equal("true", project.GetPropertyValue("PackFrameworkReferences")); + } + + [Fact] + public void PackFrameworkReferences_is_true_for_default_primary_output_kind() + { + var project = new Project("NuGetizer.targets", new Dictionary + { + }, null); + + Assert.Equal("true", project.GetPropertyValue("PackFrameworkReferences")); + } + + [Fact] + public void PackFrameworkReferences_is_not_true_for_compat_istool() + { + var project = new Project("NuGetizer.targets", new Dictionary + { + { "IsTool", "true" } + }, null); + + Assert.NotEqual("true", project.GetPropertyValue("PackFrameworkReferences")); + } + + [Fact] + public void PackOnBuild_defaults_to_true_for_compat_GeneratePackageOnBuild_true() + { + var project = new Project("NuGetizer.targets", new Dictionary + { + { "GeneratePackageOnBuild", "true" } + }, null); + + Assert.Equal("true", project.GetPropertyValue("PackOnBuild")); + } + + [Fact] + public void package_contents_never_includes_nugetizer_package_reference() + { + var xml = ProjectRootElement.Create(Path.Combine(Directory.GetCurrentDirectory(), MethodBase.GetCurrentMethod().Name)); + xml.AddImport(@"Scenarios\Scenario.props"); + xml.AddImport(@"Scenarios\Scenario.targets"); + xml.AddItemGroup() + .AddItem("PackageReference", "NuGetizer", new[] { new KeyValuePair("Version", "*") }); + + xml.Save(); + + var project = new ProjectInstance(xml); + + var result = Builder.Build(project, "GetPackageContents"); + + Assert.Equal(BuildResultCode.Success, result.OverallResult); + Assert.Equal(TargetResultCode.Success, result["GetPackageContents"].ResultCode); + + var items = result["GetPackageContents"].Items; + + Assert.DoesNotContain(items, item => item.Matches(new + { PackFolder = PackFolderKind.Dependency, - Identity = "NuGetizer", - })); - } - } + Identity = "NuGetizer", + })); + } + } } diff --git a/src/NuGetizer.Tests/Utilities/ManifestContentFilesComparer.cs b/src/NuGetizer.Tests/Utilities/ManifestContentFilesComparer.cs index ddd6c857..b3c921ba 100644 --- a/src/NuGetizer.Tests/Utilities/ManifestContentFilesComparer.cs +++ b/src/NuGetizer.Tests/Utilities/ManifestContentFilesComparer.cs @@ -5,8 +5,8 @@ namespace NuGetizer { class ManifestContentFilesComparer : IEqualityComparer - { - public static ManifestContentFilesComparer Default { get; } = new ManifestContentFilesComparer(); + { + public static ManifestContentFilesComparer Default { get; } = new ManifestContentFilesComparer(); public bool Equals(ManifestContentFiles x, ManifestContentFiles y) { @@ -15,10 +15,10 @@ public bool Equals(ManifestContentFiles x, ManifestContentFiles y) if (x == null || y == null) return false; - return StringComparer.OrdinalIgnoreCase.Equals(x.Include, y.Include) - && StringComparer.OrdinalIgnoreCase.Equals(x.BuildAction, y.BuildAction) - && StringComparer.OrdinalIgnoreCase.Equals(x.CopyToOutput, x.CopyToOutput) - && StringComparer.OrdinalIgnoreCase.Equals(x.Flatten, x.Flatten); + return StringComparer.OrdinalIgnoreCase.Equals(x.Include, y.Include) + && StringComparer.OrdinalIgnoreCase.Equals(x.BuildAction, y.BuildAction) + && StringComparer.OrdinalIgnoreCase.Equals(x.CopyToOutput, x.CopyToOutput) + && StringComparer.OrdinalIgnoreCase.Equals(x.Flatten, x.Flatten); } public int GetHashCode(ManifestContentFiles obj) @@ -26,10 +26,10 @@ public int GetHashCode(ManifestContentFiles obj) if (obj == null) throw new ArgumentNullException("obj"); - return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Include ?? "") - + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.BuildAction ?? "") - + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.CopyToOutput ?? "") - + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Flatten ?? ""); - } - } + return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Include ?? "") + + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.BuildAction ?? "") + + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.CopyToOutput ?? "") + + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Flatten ?? ""); + } + } } diff --git a/src/NuGetizer.Tests/Utilities/ManifestFileComparer.cs b/src/NuGetizer.Tests/Utilities/ManifestFileComparer.cs index aef9209f..bfcc6ced 100644 --- a/src/NuGetizer.Tests/Utilities/ManifestFileComparer.cs +++ b/src/NuGetizer.Tests/Utilities/ManifestFileComparer.cs @@ -6,7 +6,7 @@ namespace NuGetizer { class ManifestFileComparer : IEqualityComparer { - public static ManifestFileComparer Default { get; } = new ManifestFileComparer(); + public static ManifestFileComparer Default { get; } = new ManifestFileComparer(); public bool Equals(ManifestFile x, ManifestFile y) { @@ -15,9 +15,9 @@ public bool Equals(ManifestFile x, ManifestFile y) if (x == null || y == null) return false; - // We don't compare the Source since it's different for packaged manifests - return StringComparer.OrdinalIgnoreCase.Equals(x.Target, y.Target) - && StringComparer.OrdinalIgnoreCase.Equals(x.Exclude, x.Exclude); + // We don't compare the Source since it's different for packaged manifests + return StringComparer.OrdinalIgnoreCase.Equals(x.Target, y.Target) + && StringComparer.OrdinalIgnoreCase.Equals(x.Exclude, x.Exclude); } public int GetHashCode(ManifestFile obj) @@ -25,8 +25,8 @@ public int GetHashCode(ManifestFile obj) if (obj == null) throw new ArgumentNullException("obj"); - return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Target ?? "") - + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Exclude ?? ""); - } - } + return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Target ?? "") + + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Exclude ?? ""); + } + } } diff --git a/src/NuGetizer.Tests/Utilities/MockBuildEngine.cs b/src/NuGetizer.Tests/Utilities/MockBuildEngine.cs index 9e90bf9a..2995db65 100644 --- a/src/NuGetizer.Tests/Utilities/MockBuildEngine.cs +++ b/src/NuGetizer.Tests/Utilities/MockBuildEngine.cs @@ -6,88 +6,88 @@ namespace Microsoft.Build.Framework { - /// - /// Fake build engine for unit testing Tasks without spinning up - /// MSBuild. - /// - class MockBuildEngine : IBuildEngine - { - bool trace = false; - ITestOutputHelper output; - - public MockBuildEngine(bool trace = true) - { - this.trace = trace; - LoggedCustomEvents = new List(); - LoggedErrorEvents = new List(); - LoggedMessageEvents = new List(); - LoggedWarningEvents = new List(); - } - - public MockBuildEngine(ITestOutputHelper output, bool trace = false) - : this(trace) - { - this.output = output; - } - - public bool BuildProjectFile(string projectFileName, string[] targetNames, IDictionary globalProperties, IDictionary targetOutputs) - { - throw new NotSupportedException(); - } - - public int ColumnNumberOfTaskNode { get; set; } - - public bool ContinueOnError { get; set; } - - public int LineNumberOfTaskNode { get; set; } - - public string ProjectFileOfTaskNode { get; set; } - - public List LoggedCustomEvents { get; private set; } - public List LoggedErrorEvents { get; private set; } - public List LoggedMessageEvents { get; private set; } - public List LoggedWarningEvents { get; private set; } - - public void LogCustomEvent(CustomBuildEventArgs e) - { - if (trace) - TraceMessage(e.Message); - - LoggedCustomEvents.Add(e); - } - - public void LogErrorEvent(BuildErrorEventArgs e) - { - if (trace) - TraceMessage(e.Message); - - LoggedErrorEvents.Add(e); - } - - public void LogMessageEvent(BuildMessageEventArgs e) - { - if (trace) - TraceMessage(e.Message); - - LoggedMessageEvents.Add(e); - } - - public void LogWarningEvent(BuildWarningEventArgs e) - { - if (trace) - TraceMessage(e.Message); - - LoggedWarningEvents.Add(e); - } - - void TraceMessage(string message) - { - Console.WriteLine(message); - Trace.WriteLine(message); - Debug.WriteLine(message); - Debugger.Log(0, "", message); - if (output != null) - output.WriteLine(message); - } - } + /// + /// Fake build engine for unit testing Tasks without spinning up + /// MSBuild. + /// + class MockBuildEngine : IBuildEngine + { + bool trace = false; + ITestOutputHelper output; + + public MockBuildEngine(bool trace = true) + { + this.trace = trace; + LoggedCustomEvents = new List(); + LoggedErrorEvents = new List(); + LoggedMessageEvents = new List(); + LoggedWarningEvents = new List(); + } + + public MockBuildEngine(ITestOutputHelper output, bool trace = false) + : this(trace) + { + this.output = output; + } + + public bool BuildProjectFile(string projectFileName, string[] targetNames, IDictionary globalProperties, IDictionary targetOutputs) + { + throw new NotSupportedException(); + } + + public int ColumnNumberOfTaskNode { get; set; } + + public bool ContinueOnError { get; set; } + + public int LineNumberOfTaskNode { get; set; } + + public string ProjectFileOfTaskNode { get; set; } + + public List LoggedCustomEvents { get; private set; } + public List LoggedErrorEvents { get; private set; } + public List LoggedMessageEvents { get; private set; } + public List LoggedWarningEvents { get; private set; } + + public void LogCustomEvent(CustomBuildEventArgs e) + { + if (trace) + TraceMessage(e.Message); + + LoggedCustomEvents.Add(e); + } + + public void LogErrorEvent(BuildErrorEventArgs e) + { + if (trace) + TraceMessage(e.Message); + + LoggedErrorEvents.Add(e); + } + + public void LogMessageEvent(BuildMessageEventArgs e) + { + if (trace) + TraceMessage(e.Message); + + LoggedMessageEvents.Add(e); + } + + public void LogWarningEvent(BuildWarningEventArgs e) + { + if (trace) + TraceMessage(e.Message); + + LoggedWarningEvents.Add(e); + } + + void TraceMessage(string message) + { + Console.WriteLine(message); + Trace.WriteLine(message); + Debug.WriteLine(message); + Debugger.Log(0, "", message); + if (output != null) + output.WriteLine(message); + } + } } \ No newline at end of file diff --git a/src/NuGetizer.Tests/Utilities/PackageDependencyComparer.cs b/src/NuGetizer.Tests/Utilities/PackageDependencyComparer.cs index b0d816c2..a9d23ea1 100644 --- a/src/NuGetizer.Tests/Utilities/PackageDependencyComparer.cs +++ b/src/NuGetizer.Tests/Utilities/PackageDependencyComparer.cs @@ -4,9 +4,9 @@ namespace NuGetizer { - class PackageDependencyComparer : IEqualityComparer + class PackageDependencyComparer : IEqualityComparer { - public static PackageDependencyComparer Default { get; } = new PackageDependencyComparer(); + public static PackageDependencyComparer Default { get; } = new PackageDependencyComparer(); public bool Equals(PackageDependency x, PackageDependency y) { @@ -24,7 +24,7 @@ public int GetHashCode(PackageDependency obj) throw new ArgumentNullException("obj"); return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Id ?? "") + - StringComparer.OrdinalIgnoreCase.GetHashCode(obj.VersionRange.ToString() ?? ""); + StringComparer.OrdinalIgnoreCase.GetHashCode(obj.VersionRange.ToString() ?? ""); } } } diff --git a/src/NuGetizer.Tests/Utilities/PackageDependencyGroupComparer.cs b/src/NuGetizer.Tests/Utilities/PackageDependencyGroupComparer.cs index ebfff96d..39f9b76e 100644 --- a/src/NuGetizer.Tests/Utilities/PackageDependencyGroupComparer.cs +++ b/src/NuGetizer.Tests/Utilities/PackageDependencyGroupComparer.cs @@ -5,9 +5,9 @@ namespace NuGetizer { - class PackageDependencyGroupComparer : IEqualityComparer + class PackageDependencyGroupComparer : IEqualityComparer { - public static PackageDependencyGroupComparer Default { get; } = new PackageDependencyGroupComparer(); + public static PackageDependencyGroupComparer Default { get; } = new PackageDependencyGroupComparer(); public bool Equals(PackageDependencyGroup x, PackageDependencyGroup y) { diff --git a/src/NuGetizer.Tests/Utilities/TargetFrameworks.cs b/src/NuGetizer.Tests/Utilities/TargetFrameworks.cs index 695e4e3e..cc4ab712 100644 --- a/src/NuGetizer.Tests/Utilities/TargetFrameworks.cs +++ b/src/NuGetizer.Tests/Utilities/TargetFrameworks.cs @@ -1,11 +1,11 @@ namespace NuGetizer { static class TargetFrameworks - { - public const string NET45 = ".NETFramework,Version=v4.5"; - public const string NET46 = ".NETFramework,Version=v4.6"; - public const string PCL78 = ".NETPortable,Version=v4.0,Profile=Profile78"; - public const string XI10 = "Xamarin.iOS,Version=v1.0"; - public const string XA50 = "MonoAndroid,Version=v5.0"; - } + { + public const string NET45 = ".NETFramework,Version=v4.5"; + public const string NET46 = ".NETFramework,Version=v4.6"; + public const string PCL78 = ".NETPortable,Version=v4.0,Profile=Profile78"; + public const string XI10 = "Xamarin.iOS,Version=v1.0"; + public const string XA50 = "MonoAndroid,Version=v5.0"; + } } diff --git a/src/NuGetizer.Tests/Utilities/TaskItemExtensions.cs b/src/NuGetizer.Tests/Utilities/TaskItemExtensions.cs index f09e66b6..f3ef8c73 100644 --- a/src/NuGetizer.Tests/Utilities/TaskItemExtensions.cs +++ b/src/NuGetizer.Tests/Utilities/TaskItemExtensions.cs @@ -5,31 +5,31 @@ namespace NuGetizer { static class TaskItemExtensions - { - /// - /// Checks if the given item has metadata key/values matching the - /// anonymous object property/values. - /// - public static bool Matches(this ITaskItem item, object metadata) - { - foreach (var prop in metadata.GetType().GetProperties()) - { - var actual = item.GetMetadata(prop.Name); - var expected = prop.GetValue(metadata).ToString(); + { + /// + /// Checks if the given item has metadata key/values matching the + /// anonymous object property/values. + /// + public static bool Matches(this ITaskItem item, object metadata) + { + foreach (var prop in metadata.GetType().GetProperties()) + { + var actual = item.GetMetadata(prop.Name); + var expected = prop.GetValue(metadata).ToString(); - if (!actual.Equals(expected, StringComparison.OrdinalIgnoreCase)) - return false; - } + if (!actual.Equals(expected, StringComparison.OrdinalIgnoreCase)) + return false; + } - return true; - } + return true; + } - public static Manifest GetManifest(this ITaskItem package) - { - using (var reader = new PackageArchiveReader(package.GetMetadata("FullPath"))) - { - return reader.GetManifest(); - } - } - } + public static Manifest GetManifest(this ITaskItem package) + { + using (var reader = new PackageArchiveReader(package.GetMetadata("FullPath"))) + { + return reader.GetManifest(); + } + } + } } diff --git a/src/NuGetizer.Tests/Utilities/TestOutputLogger.cs b/src/NuGetizer.Tests/Utilities/TestOutputLogger.cs index 6fd4dad7..e97aadda 100644 --- a/src/NuGetizer.Tests/Utilities/TestOutputLogger.cs +++ b/src/NuGetizer.Tests/Utilities/TestOutputLogger.cs @@ -1,5 +1,5 @@ -using Microsoft.Build.Framework; -using System.Collections.Generic; +using System.Collections.Generic; +using Microsoft.Build.Framework; using Xunit.Abstractions; /// @@ -7,85 +7,85 @@ /// class TestOutputLogger : ILogger { - ITestOutputHelper output; - - public TestOutputLogger(ITestOutputHelper output, LoggerVerbosity? verbosity = LoggerVerbosity.Quiet) - { - this.output = output; - this.Verbosity = verbosity.GetValueOrDefault(); - } - - public void Reset() - { - Messages = new List(); - Warnings = new List(); - Errors = new List(); - FinishedTargets = new List(); - FinishedTasks = new List(); - } - - public LoggerVerbosity Verbosity { get; set; } - - public List Messages { get; private set; } = new List(); - - public List Warnings { get; private set; } = new List(); - - public List Errors { get; private set; } = new List(); - - public List FinishedTargets { get; private set; } = new List(); - - public List FinishedTasks { get; private set; } = new List(); - - public void Initialize(IEventSource eventSource) - { - eventSource.TargetFinished += (sender, e) => FinishedTargets.Add(e); - eventSource.TaskFinished += (sender, e) => FinishedTasks.Add(e); - - eventSource.AnyEventRaised += (sender, e) => - { - if (!(e is BuildMessageEventArgs) && Verbosity > LoggerVerbosity.Normal) - output?.WriteLine(e.Message); - }; - - eventSource.MessageRaised += (sender, e) => - { - var shouldLog = (e.Importance == MessageImportance.High); - switch (e.Importance) - { - case MessageImportance.Normal: - shouldLog = (Verbosity >= LoggerVerbosity.Normal); - break; - case MessageImportance.Low: - shouldLog = (Verbosity >= LoggerVerbosity.Detailed); - break; - default: - break; - } - - if (Verbosity != LoggerVerbosity.Quiet && shouldLog) - output?.WriteLine(e.Message); - - Messages.Add(e); - }; - - eventSource.ErrorRaised += (sender, e) => - { - output?.WriteLine(e.Message); - Errors.Add(e); - }; - - eventSource.WarningRaised += (sender, e) => - { - if (Verbosity != LoggerVerbosity.Quiet) - output?.WriteLine(e.Message); - - Warnings.Add(e); - }; - } - - public string Parameters { get; set; } - - public void Shutdown() - { - } + ITestOutputHelper output; + + public TestOutputLogger(ITestOutputHelper output, LoggerVerbosity? verbosity = LoggerVerbosity.Quiet) + { + this.output = output; + this.Verbosity = verbosity.GetValueOrDefault(); + } + + public void Reset() + { + Messages = new List(); + Warnings = new List(); + Errors = new List(); + FinishedTargets = new List(); + FinishedTasks = new List(); + } + + public LoggerVerbosity Verbosity { get; set; } + + public List Messages { get; private set; } = new List(); + + public List Warnings { get; private set; } = new List(); + + public List Errors { get; private set; } = new List(); + + public List FinishedTargets { get; private set; } = new List(); + + public List FinishedTasks { get; private set; } = new List(); + + public void Initialize(IEventSource eventSource) + { + eventSource.TargetFinished += (sender, e) => FinishedTargets.Add(e); + eventSource.TaskFinished += (sender, e) => FinishedTasks.Add(e); + + eventSource.AnyEventRaised += (sender, e) => + { + if (!(e is BuildMessageEventArgs) && Verbosity > LoggerVerbosity.Normal) + output?.WriteLine(e.Message); + }; + + eventSource.MessageRaised += (sender, e) => + { + var shouldLog = (e.Importance == MessageImportance.High); + switch (e.Importance) + { + case MessageImportance.Normal: + shouldLog = (Verbosity >= LoggerVerbosity.Normal); + break; + case MessageImportance.Low: + shouldLog = (Verbosity >= LoggerVerbosity.Detailed); + break; + default: + break; + } + + if (Verbosity != LoggerVerbosity.Quiet && shouldLog) + output?.WriteLine(e.Message); + + Messages.Add(e); + }; + + eventSource.ErrorRaised += (sender, e) => + { + output?.WriteLine(e.Message); + Errors.Add(e); + }; + + eventSource.WarningRaised += (sender, e) => + { + if (Verbosity != LoggerVerbosity.Quiet) + output?.WriteLine(e.Message); + + Warnings.Add(e); + }; + } + + public string Parameters { get; set; } + + public void Shutdown() + { + } } \ No newline at end of file diff --git a/src/NuGetizer.Tests/UtilitiesTests.cs b/src/NuGetizer.Tests/UtilitiesTests.cs index c7cf73c9..20f19195 100644 --- a/src/NuGetizer.Tests/UtilitiesTests.cs +++ b/src/NuGetizer.Tests/UtilitiesTests.cs @@ -6,46 +6,46 @@ namespace NuGetizer { public class UtilitiesTests - { - [Fact] - public void when_getting_target_framework_then_fallback_to_tfm() - { - var item = new TaskItem("Foo", new Metadata - { - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" } - }); - - var framework = item.GetTargetFramework(); - - Assert.Equal(new FrameworkName(".NETFramework,Version=v4.5"), framework); - } - - [Fact] - public void when_getting_target_framework_then_does_not_use_tfm() - { - var item = new TaskItem("Foo", new Metadata - { - { "TargetFramework", "net46" }, - { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" } - }); - - var framework = item.GetTargetFramework(); - - Assert.Equal(new FrameworkName(".NETFramework,Version=v4.6"), framework); - } - - [Fact] - public void when_getting_any_target_framework_then_suceeds() - { - var item = new TaskItem("Foo", new Metadata - { - { "TargetFramework", "any" }, - }); - - var framework = item.GetTargetFramework(); - - Assert.Equal(new FrameworkName("Any,Version=v0.0"), framework); - Assert.Equal("any", framework.GetShortFrameworkName()); - } - } + { + [Fact] + public void when_getting_target_framework_then_fallback_to_tfm() + { + var item = new TaskItem("Foo", new Metadata + { + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" } + }); + + var framework = item.GetTargetFramework(); + + Assert.Equal(new FrameworkName(".NETFramework,Version=v4.5"), framework); + } + + [Fact] + public void when_getting_target_framework_then_does_not_use_tfm() + { + var item = new TaskItem("Foo", new Metadata + { + { "TargetFramework", "net46" }, + { "TargetFrameworkMoniker", ".NETFramework,Version=v4.5" } + }); + + var framework = item.GetTargetFramework(); + + Assert.Equal(new FrameworkName(".NETFramework,Version=v4.6"), framework); + } + + [Fact] + public void when_getting_any_target_framework_then_suceeds() + { + var item = new TaskItem("Foo", new Metadata + { + { "TargetFramework", "any" }, + }); + + var framework = item.GetTargetFramework(); + + Assert.Equal(new FrameworkName("Any,Version=v0.0"), framework); + Assert.Equal("any", framework.GetShortFrameworkName()); + } + } } diff --git a/src/NuGetizer.Tests/given_a_complex_pack.cs b/src/NuGetizer.Tests/given_a_complex_pack.cs index 6d5aeb77..a89661b8 100644 --- a/src/NuGetizer.Tests/given_a_complex_pack.cs +++ b/src/NuGetizer.Tests/given_a_complex_pack.cs @@ -8,228 +8,228 @@ namespace NuGetizer { public class given_a_complex_pack - { - ITestOutputHelper output; + { + ITestOutputHelper output; - public given_a_complex_pack(ITestOutputHelper output) - { - this.output = output; + public given_a_complex_pack(ITestOutputHelper output) + { + this.output = output; Builder.BuildScenario(nameof(given_a_complex_pack), target: "Restore", output: output) .AssertSuccess(output); } [Fact] - public void when_getting_package_target_path_then_gets_package_metadata() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), new { Configuration = "Release" }, projectName: "a", target: "GetPackageTargetPath", output: output); - - result.AssertSuccess(output); - - var metadata = result.Items.FirstOrDefault(); - - Assert.NotNull(metadata); - Assert.Equal("A", metadata.GetMetadata("PackageId")); - Assert.Equal("1.0.0", metadata.GetMetadata("Version")); - Assert.Equal("NuGet", metadata.GetMetadata("Authors")); - } - - [Fact] - public void when_preparing_a_then_contains_assemblies_and_direct_dependency() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), new { Configuration = "Release" }, projectName: "a", target: "GetPackageContents", output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\a.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\a.xml", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\e.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\e.xml", - })); - Assert.Contains(result.Items, item => item.Matches(new - { + public void when_getting_package_target_path_then_gets_package_metadata() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), new { Configuration = "Release" }, projectName: "a", target: "GetPackageTargetPath", output: output); + + result.AssertSuccess(output); + + var metadata = result.Items.FirstOrDefault(); + + Assert.NotNull(metadata); + Assert.Equal("A", metadata.GetMetadata("PackageId")); + Assert.Equal("1.0.0", metadata.GetMetadata("Version")); + Assert.Equal("NuGet", metadata.GetMetadata("Authors")); + } + + [Fact] + public void when_preparing_a_then_contains_assemblies_and_direct_dependency() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), new { Configuration = "Release" }, projectName: "a", target: "GetPackageContents", output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\a.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\a.xml", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\e.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\e.xml", + })); + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Metadata, - Filename = "A", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - Identity = "B", + Filename = "A", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + Identity = "B", PackFolder = PackFolderKind.Dependency, - Version = "2.0.0", - })); - } - - [Fact] - public void when_preparing_b_then_contains_assemblies_and_direct_dependency() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), new { Configuration = "Release" }, projectName: "b", target: "GetPackageContents", output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\b.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\b.xml", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\d.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\d.xml", - })); - Assert.Contains(result.Items, item => item.Matches(new - { + Version = "2.0.0", + })); + } + + [Fact] + public void when_preparing_b_then_contains_assemblies_and_direct_dependency() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), new { Configuration = "Release" }, projectName: "b", target: "GetPackageContents", output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\b.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\b.xml", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\d.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\d.xml", + })); + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Metadata, - Filename = "B", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - Identity = "C", + Filename = "B", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + Identity = "C", PackFolder = PackFolderKind.Dependency, - Version = "3.0.0", - })); - } - - [Fact] - public void when_preparing_c_then_contains_external_dependency() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), new { Configuration = "Release" }, projectName: "c", target: "GetPackageContents", output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net45\c.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net45\c.xml", - })); - Assert.Contains(result.Items, item => item.Matches(new - { + Version = "3.0.0", + })); + } + + [Fact] + public void when_preparing_c_then_contains_external_dependency() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), new { Configuration = "Release" }, projectName: "c", target: "GetPackageContents", output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net45\c.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net45\c.xml", + })); + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Metadata, - Filename = "C", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - Identity = "Foo", + Filename = "C", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + Identity = "Foo", PackFolder = PackFolderKind.Dependency, - Version = "1.0.0", - })); - } + Version = "1.0.0", + })); + } - [Fact] - public void when_preparing_d_without_package_id_then_does_not_set_package_path() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "d", target: "GetPackageContents", output: output); + [Fact] + public void when_preparing_d_without_package_id_then_does_not_set_package_path() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "d", target: "GetPackageContents", output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - Filename = "d", - Extension = ".dll", + Assert.Contains(result.Items, item => item.Matches(new + { + Filename = "d", + Extension = ".dll", PackFolder = PackFolderKind.Lib, - PackagePath = "", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - Filename = "d", - Extension = ".xml", + PackagePath = "", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + Filename = "d", + Extension = ".xml", PackFolder = PackFolderKind.Lib, - PackagePath = "", - })); - } + PackagePath = "", + })); + } - [Fact] - public void when_packing_a_then_contains_assemblies_and_direct_dependency() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "a", target: "Pack", output: output); + [Fact] + public void when_packing_a_then_contains_assemblies_and_direct_dependency() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "a", target: "Pack", output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); + + var manifest = result.Items[0].GetManifest(); - var manifest = result.Items[0].GetManifest(); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\a.dll"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\a.xml"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\e.dll"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\e.xml"); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\a.dll"); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\a.xml"); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\e.dll"); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\e.xml"); + Assert.Contains(manifest.Metadata.DependencyGroups, group => + group.TargetFramework.Equals(NuGetFramework.Parse("net472")) && + group.Packages.Any(dep => dep.Id == "B" && dep.VersionRange.OriginalString == "2.0.0")); + } - Assert.Contains(manifest.Metadata.DependencyGroups, group => - group.TargetFramework.Equals(NuGetFramework.Parse("net472")) && - group.Packages.Any(dep => dep.Id == "B" && dep.VersionRange.OriginalString == "2.0.0")); - } - - [Fact] - public void when_packing_b_then_contains_assemblies_and_direct_dependency() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "b", target: "Pack", output: output); + [Fact] + public void when_packing_b_then_contains_assemblies_and_direct_dependency() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "b", target: "Pack", output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - var manifest = result.Items[0].GetManifest(); + var manifest = result.Items[0].GetManifest(); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\b.dll"); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\b.xml"); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\d.dll"); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\d.xml"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\b.dll"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\b.xml"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\d.dll"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net472\d.xml"); - Assert.Contains(manifest.Metadata.DependencyGroups, group => - group.TargetFramework.Equals(NuGetFramework.Parse("net472")) && - group.Packages.Any(dep => dep.Id == "C" && dep.VersionRange.OriginalString == "3.0.0")); - } + Assert.Contains(manifest.Metadata.DependencyGroups, group => + group.TargetFramework.Equals(NuGetFramework.Parse("net472")) && + group.Packages.Any(dep => dep.Id == "C" && dep.VersionRange.OriginalString == "3.0.0")); + } - [Fact] - public void when_packing_c_then_contains_external_dependency() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "c", target: "Pack", output: output); + [Fact] + public void when_packing_c_then_contains_external_dependency() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "c", target: "Pack", output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - var manifest = result.Items[0].GetManifest(); + var manifest = result.Items[0].GetManifest(); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net45\c.dll"); - Assert.Contains(manifest.Files, file => file.Target == @"lib\net45\c.xml"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net45\c.dll"); + Assert.Contains(manifest.Files, file => file.Target == @"lib\net45\c.xml"); - Assert.Contains(manifest.Metadata.DependencyGroups, group => - group.TargetFramework.Equals(NuGetFramework.Parse("net45")) && - group.Packages.Any(dep => dep.Id == "Foo" && dep.VersionRange.OriginalString == "1.0.0")); - } + Assert.Contains(manifest.Metadata.DependencyGroups, group => + group.TargetFramework.Equals(NuGetFramework.Parse("net45")) && + group.Packages.Any(dep => dep.Id == "Foo" && dep.VersionRange.OriginalString == "1.0.0")); + } - [Fact] - public void when_packing_d_without_package_id_then_target_is_skipped() - { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "d", target: "Pack", output: output); + [Fact] + public void when_packing_d_without_package_id_then_target_is_skipped() + { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), projectName: "d", target: "Pack", output: output); - Assert.Equal(TargetResultCode.Skipped, result.ResultCode); - } + Assert.Equal(TargetResultCode.Skipped, result.ResultCode); + } [Fact] public void when_pack_with_emit_nuspec_but_not_package_then_creates_nuspec_but_not_package() { - var result = Builder.BuildScenario(nameof(given_a_complex_pack), - new - { + var result = Builder.BuildScenario(nameof(given_a_complex_pack), + new + { EmitNuspec = "true", EmitPackage = "false", BaseOutputPath = Builder.TestOutputPath(), - }, - projectName: "a", + }, + projectName: "a", target: "Pack", output: output); result.AssertSuccess(output); diff --git a/src/NuGetizer.Tests/given_a_custom_build_project.cs b/src/NuGetizer.Tests/given_a_custom_build_project.cs index 258e315f..00394eb9 100644 --- a/src/NuGetizer.Tests/given_a_custom_build_project.cs +++ b/src/NuGetizer.Tests/given_a_custom_build_project.cs @@ -4,25 +4,25 @@ namespace NuGetizer { public class given_a_custom_build_project - { - ITestOutputHelper output; + { + ITestOutputHelper output; - public given_a_custom_build_project(ITestOutputHelper output) - { - this.output = output; - } + public given_a_custom_build_project(ITestOutputHelper output) + { + this.output = output; + } - [Fact] - public void when_packing_then_can_include_content() - { - var result = Builder.BuildScenario(nameof(given_a_custom_build_project), output: output); + [Fact] + public void when_packing_then_can_include_content() + { + var result = Builder.BuildScenario(nameof(given_a_custom_build_project), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = "Readme.txt", - })); - } - } + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = "Readme.txt", + })); + } + } } diff --git a/src/NuGetizer.Tests/given_a_library_with_content.cs b/src/NuGetizer.Tests/given_a_library_with_content.cs index 3bd1360a..18034365 100644 --- a/src/NuGetizer.Tests/given_a_library_with_content.cs +++ b/src/NuGetizer.Tests/given_a_library_with_content.cs @@ -6,348 +6,348 @@ namespace NuGetizer { public class given_a_library_with_content - { - ITestOutputHelper output; + { + ITestOutputHelper output; - public given_a_library_with_content(ITestOutputHelper output) - { - this.output = output; + public given_a_library_with_content(ITestOutputHelper output) + { + this.output = output; using var disable = OpenBuildLogAttribute.Disable(); Builder.BuildScenario(nameof(given_a_library_with_content), target: "Restore") .AssertSuccess(output); } - [Fact] - public void when_library_is_not_packable_then_still_contains_content_files() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content)); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - TargetPath = @"Resources\drawable-hdpi\Icon.png", - })); - } - - [Fact] - public void when_global_include_content_is_false_then_does_not_contain_content_files() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackContent = "false" - }); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - TargetPath = @"Resources\drawable-hdpi\Icon.png", - })); - } - - [Fact] - public void when_global_include_none_is_false_then_does_not_contain_none_copy_files() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackNone = "false" - }); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - TargetPath = @"none-copy.txt", - })); - } - - [Fact] - public void when_removing_inferred_package_file_from_content_then_content_is_not_included() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage", - RemoveContent = "true", - }); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.GetMetadata("PackagePath").StartsWith("contentFiles")); - } - - [Fact] - public void linked_package_file_has_relative_package_path() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"docs\Readme.txt", - })); - } - - #region Content scenarios - - [Fact] - public void content_no_copy_with_relativedir_can_specify_lang_tfm_metadata() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\any\any\quickstart\any-any.txt", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\cs\any\quickstart\cs-any.txt", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\fs\monoandroid51\quickstart\fs-tfm.txt", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\any\any\quickstart\any-non-tfm.txt", - })); - } - - [Fact] - public void content_no_copy_is_content_files_anylang_tfm_specific() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\any\monoandroid51\Resources\drawable-hdpi\Icon.png", - })); - } - - [Fact] - public void content_no_copy_is_included_from_source() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - var sourcePath = Path.Combine(Path.GetDirectoryName(result.ProjectOrSolutionFile), "content-with-kind.txt"); - - Assert.Contains(result.Items, item => item.Matches(new - { - FullPath = sourcePath, - })); - } - - [Fact] - public void content_no_copy_with_contentFiles_dir_fails() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage", - // Includes "contentFiles\cs\monoandroid\content.cs" which fails - IncludeContentWithReservedRelativeDir = "true" - }); - - Assert.Equal(TargetResultCode.Failure, result.ResultCode); - Assert.Contains(result.Logger.Errors, e => e.Code == nameof(ThisAssembly.Strings.ErrorCode.NG0013)); - } - - [Fact] - public void content_copy_include_false_is_not_included() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - TargetPath = "content-copy-with-include-false.txt", - })); - } - - [Fact] - public void content_copy_include_true_is_included_as_lib() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\monoandroid51\content-copy-with-include-true.txt", - })); - } - - [Fact] - public void content_copy_with_kind_is_included_as_kind() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"build\content-copy-with-kind.txt", - })); - } - - - [Fact] - public void content_copy_is_included_as_lib() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\monoandroid51\content-copy.txt", - })); - } - - [Fact] - public void content_include_false_is_not_included() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - TargetPath = "content-with-include-false.txt", - })); - } - - [Fact] - public void content_include_true_is_content_files_anylang_tfm_specific() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\any\monoandroid51\content-with-include-true.txt", - })); - } - - [Fact] - public void content_include_true_is_included_even_if_global_include_contents_is_false() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage", - PackContent = "false" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\any\monoandroid51\content-with-include-true.txt", - })); - } - - [Fact] - public void content_with_kind_is_included_as_kind() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"build\content-with-kind.txt", - })); - } - - - [Fact] - public void content_is_content_files_anylang_tfm_specific() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\any\monoandroid51\content.txt", - })); - } - - [Fact] - public void content_copy_relative_is_included_as_relative_lib() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\monoandroid51\relative\content-copy.txt", - })); - } - - [Fact] - public void content_copy_relative_kind_is_included_as_relative_kind() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"build\relative\content-copy-with-kind.txt", - })); - } + [Fact] + public void when_library_is_not_packable_then_still_contains_content_files() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content)); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + TargetPath = @"Resources\drawable-hdpi\Icon.png", + })); + } + + [Fact] + public void when_global_include_content_is_false_then_does_not_contain_content_files() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackContent = "false" + }); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + TargetPath = @"Resources\drawable-hdpi\Icon.png", + })); + } + + [Fact] + public void when_global_include_none_is_false_then_does_not_contain_none_copy_files() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackNone = "false" + }); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + TargetPath = @"none-copy.txt", + })); + } + + [Fact] + public void when_removing_inferred_package_file_from_content_then_content_is_not_included() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage", + RemoveContent = "true", + }); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.GetMetadata("PackagePath").StartsWith("contentFiles")); + } + + [Fact] + public void linked_package_file_has_relative_package_path() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"docs\Readme.txt", + })); + } + + #region Content scenarios + + [Fact] + public void content_no_copy_with_relativedir_can_specify_lang_tfm_metadata() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\any\any\quickstart\any-any.txt", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\cs\any\quickstart\cs-any.txt", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\fs\monoandroid51\quickstart\fs-tfm.txt", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\any\any\quickstart\any-non-tfm.txt", + })); + } + + [Fact] + public void content_no_copy_is_content_files_anylang_tfm_specific() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\any\monoandroid51\Resources\drawable-hdpi\Icon.png", + })); + } + + [Fact] + public void content_no_copy_is_included_from_source() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + var sourcePath = Path.Combine(Path.GetDirectoryName(result.ProjectOrSolutionFile), "content-with-kind.txt"); + + Assert.Contains(result.Items, item => item.Matches(new + { + FullPath = sourcePath, + })); + } + + [Fact] + public void content_no_copy_with_contentFiles_dir_fails() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage", + // Includes "contentFiles\cs\monoandroid\content.cs" which fails + IncludeContentWithReservedRelativeDir = "true" + }); + + Assert.Equal(TargetResultCode.Failure, result.ResultCode); + Assert.Contains(result.Logger.Errors, e => e.Code == nameof(ThisAssembly.Strings.ErrorCode.NG0013)); + } + + [Fact] + public void content_copy_include_false_is_not_included() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + TargetPath = "content-copy-with-include-false.txt", + })); + } + + [Fact] + public void content_copy_include_true_is_included_as_lib() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\monoandroid51\content-copy-with-include-true.txt", + })); + } + + [Fact] + public void content_copy_with_kind_is_included_as_kind() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"build\content-copy-with-kind.txt", + })); + } + + + [Fact] + public void content_copy_is_included_as_lib() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\monoandroid51\content-copy.txt", + })); + } + + [Fact] + public void content_include_false_is_not_included() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + TargetPath = "content-with-include-false.txt", + })); + } + + [Fact] + public void content_include_true_is_content_files_anylang_tfm_specific() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\any\monoandroid51\content-with-include-true.txt", + })); + } + + [Fact] + public void content_include_true_is_included_even_if_global_include_contents_is_false() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage", + PackContent = "false" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\any\monoandroid51\content-with-include-true.txt", + })); + } + + [Fact] + public void content_with_kind_is_included_as_kind() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"build\content-with-kind.txt", + })); + } + + + [Fact] + public void content_is_content_files_anylang_tfm_specific() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\any\monoandroid51\content.txt", + })); + } + + [Fact] + public void content_copy_relative_is_included_as_relative_lib() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\monoandroid51\relative\content-copy.txt", + })); + } + + [Fact] + public void content_copy_relative_kind_is_included_as_relative_kind() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"build\relative\content-copy-with-kind.txt", + })); + } [Fact] public void content_with_package_path_is_included_even_with_pack_content_false() @@ -371,233 +371,233 @@ public void content_with_package_path_is_included_even_with_pack_content_false() #region None scenarios [Fact] - public void none_no_copy_is_specified_relative_path() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\cs\monoandroid\none.cs", - })); - } - - [Fact] - public void none_no_copy_is_not_included() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - TargetPath = "none.txt", - })); - } - - [Fact] - public void none_copy_include_false_is_not_included() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - TargetPath = "none-copy-with-include-false.txt", - })); - } - - [Fact] - public void none_copy_include_true_is_included_as_lib() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\monoandroid51\none-copy-with-include-true.txt", - })); - } - - [Fact] - public void none_copy_with_kind_is_included_as_kind() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"build\none-copy-with-kind.txt", - })); - } - - [Fact] - public void none_copy_is_included_as_lib() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage", - PackNone = "true", - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\monoandroid51\none-copy.txt", - })); - } - - [Fact] - public void non_include_false_is_not_included() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - TargetPath = "none-with-include-false.txt", - })); - } - - [Fact] - public void none_include_true_is_included_as_none() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"none-with-include-true.txt", - })); - } - - [Fact] - public void none_include_true_is_included_even_if_global_include_none_is_false() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage", - PackNone = "false" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"none-with-include-true.txt", - })); - } - - [Fact] - public void none_with_kind_is_included_as_kind() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"build\none-with-kind.txt", - })); - } - - [Fact] - public void none_with_kind_is_included_from_source() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - var sourcePath = Path.Combine(Path.GetDirectoryName(result.ProjectOrSolutionFile), "none-with-kind.txt"); - - Assert.Contains(result.Items, item => item.Matches(new - { - FullPath = sourcePath, - })); - } - - [Fact] - public void none_no_kind_is_included__as_none() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage", - PackNone = "true" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - TargetPath = @"none.txt", - })); - } - - [Fact] - public void none_copy_relative_is_included_as_relative_lib() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\monoandroid51\relative\none-copy.txt", - })); - } - - [Fact] - public void none_copy_relative_kind_is_included_as_relative_kind() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_content), new - { - PackageId = "ContentPackage" - }); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"build\relative\none-copy-with-kind.txt", - })); - } + public void none_no_copy_is_specified_relative_path() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\cs\monoandroid\none.cs", + })); + } + + [Fact] + public void none_no_copy_is_not_included() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + TargetPath = "none.txt", + })); + } + + [Fact] + public void none_copy_include_false_is_not_included() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + TargetPath = "none-copy-with-include-false.txt", + })); + } + + [Fact] + public void none_copy_include_true_is_included_as_lib() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\monoandroid51\none-copy-with-include-true.txt", + })); + } + + [Fact] + public void none_copy_with_kind_is_included_as_kind() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"build\none-copy-with-kind.txt", + })); + } + + [Fact] + public void none_copy_is_included_as_lib() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage", + PackNone = "true", + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\monoandroid51\none-copy.txt", + })); + } + + [Fact] + public void non_include_false_is_not_included() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + TargetPath = "none-with-include-false.txt", + })); + } + + [Fact] + public void none_include_true_is_included_as_none() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"none-with-include-true.txt", + })); + } + + [Fact] + public void none_include_true_is_included_even_if_global_include_none_is_false() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage", + PackNone = "false" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"none-with-include-true.txt", + })); + } + + [Fact] + public void none_with_kind_is_included_as_kind() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"build\none-with-kind.txt", + })); + } + + [Fact] + public void none_with_kind_is_included_from_source() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + var sourcePath = Path.Combine(Path.GetDirectoryName(result.ProjectOrSolutionFile), "none-with-kind.txt"); + + Assert.Contains(result.Items, item => item.Matches(new + { + FullPath = sourcePath, + })); + } + + [Fact] + public void none_no_kind_is_included__as_none() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage", + PackNone = "true" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + TargetPath = @"none.txt", + })); + } + + [Fact] + public void none_copy_relative_is_included_as_relative_lib() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\monoandroid51\relative\none-copy.txt", + })); + } + + [Fact] + public void none_copy_relative_kind_is_included_as_relative_kind() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_content), new + { + PackageId = "ContentPackage" + }); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"build\relative\none-copy-with-kind.txt", + })); + } [Fact] public void none_with_package_path_is_included_by_default() diff --git a/src/NuGetizer.Tests/given_a_library_with_non_nugetized_reference.cs b/src/NuGetizer.Tests/given_a_library_with_non_nugetized_reference.cs index 4cdf871c..7e28569b 100644 --- a/src/NuGetizer.Tests/given_a_library_with_non_nugetized_reference.cs +++ b/src/NuGetizer.Tests/given_a_library_with_non_nugetized_reference.cs @@ -5,45 +5,45 @@ namespace NuGetizer { public class given_a_library_with_non_nugetized_reference - { - ITestOutputHelper output; - - public given_a_library_with_non_nugetized_reference(ITestOutputHelper output) => this.output = output; - - [Fact] - public void when_getting_contents_then_fails() - { - var properties = new - { - Configuration = "Release", - PackFrameworkReferences = "false", + { + ITestOutputHelper output; + + public given_a_library_with_non_nugetized_reference(ITestOutputHelper output) => this.output = output; + + [Fact] + public void when_getting_contents_then_fails() + { + var properties = new + { + Configuration = "Release", + PackFrameworkReferences = "false", }; - Builder.BuildScenario(nameof(given_a_library_with_non_nugetized_reference), properties, projectName: "a", target: "Restore") + Builder.BuildScenario(nameof(given_a_library_with_non_nugetized_reference), properties, projectName: "a", target: "Restore") .AssertSuccess(output); - var result = Builder.BuildScenario(nameof(given_a_library_with_non_nugetized_reference), properties, projectName: "a", target: "GetPackageContents"); + var result = Builder.BuildScenario(nameof(given_a_library_with_non_nugetized_reference), properties, projectName: "a", target: "GetPackageContents"); - Assert.Equal(TargetResultCode.Failure, result.ResultCode); - Assert.Contains(result.Logger.Errors, error => error.Code == "NG0011"); - } + Assert.Equal(TargetResultCode.Failure, result.ResultCode); + Assert.Contains(result.Logger.Errors, error => error.Code == "NG0011"); + } - [Fact] - public void when_include_in_package_false_then_does_not_fail() - { - var properties = new - { - Configuration = "Release", - PackFrameworkReferences = "false", - IncludeInPackage = "false", + [Fact] + public void when_include_in_package_false_then_does_not_fail() + { + var properties = new + { + Configuration = "Release", + PackFrameworkReferences = "false", + IncludeInPackage = "false", }; - + Builder.BuildScenario(nameof(given_a_library_with_non_nugetized_reference), properties, projectName: "a", target: "Restore") .AssertSuccess(output); - var result = Builder.BuildScenario(nameof(given_a_library_with_non_nugetized_reference), properties, projectName: "a", target: "GetPackageContents", output: output); + var result = Builder.BuildScenario(nameof(given_a_library_with_non_nugetized_reference), properties, projectName: "a", target: "GetPackageContents", output: output); - result.AssertSuccess(output); - } - } + result.AssertSuccess(output); + } + } } diff --git a/src/NuGetizer.Tests/given_a_library_with_private_assets_reference.cs b/src/NuGetizer.Tests/given_a_library_with_private_assets_reference.cs index b0cb78eb..53f72cac 100644 --- a/src/NuGetizer.Tests/given_a_library_with_private_assets_reference.cs +++ b/src/NuGetizer.Tests/given_a_library_with_private_assets_reference.cs @@ -4,82 +4,82 @@ namespace NuGetizer { public class given_a_library_with_private_assets_reference - { - ITestOutputHelper output; + { + ITestOutputHelper output; - public given_a_library_with_private_assets_reference(ITestOutputHelper output) => this.output = output; + public given_a_library_with_private_assets_reference(ITestOutputHelper output) => this.output = output; - [Fact] - public void when_getting_package_contents_then_contains_private_assets_as_primary_output() - { + [Fact] + public void when_getting_package_contents_then_contains_private_assets_as_primary_output() + { using (var disable = OpenBuildLogAttribute.Disable()) Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), target: "Restore", output: output) .AssertSuccess(output); - var result = Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), output: output); + var result = Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { + Assert.DoesNotContain(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Dependency, - Identity = "Mono.Options", - })); - Assert.Contains(result.Items, item => item.Matches(new - { + Identity = "Mono.Options", + })); + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Lib, - Filename = "Mono.Options", - NuGetSourceType = "Package", - NuGetPackageId = "Mono.Options", - })); - } + Filename = "Mono.Options", + NuGetSourceType = "Package", + NuGetPackageId = "Mono.Options", + })); + } - [Fact] - public void when_getting_package_contents_then_contains_private_lib_assets_as_primary_output_and_also_package_reference() - { - Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), target: "Restore", output: output) + [Fact] + public void when_getting_package_contents_then_contains_private_lib_assets_as_primary_output_and_also_package_reference() + { + Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), target: "Restore", output: output) .AssertSuccess(output); - var result = Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), output: output); + var result = Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Dependency, - Identity = "Newtonsoft.Json", - })); - Assert.Contains(result.Items, item => item.Matches(new - { + Identity = "Newtonsoft.Json", + })); + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Lib, - Filename = "Newtonsoft.Json", - NuGetSourceType = "Package", - NuGetPackageId = "Newtonsoft.Json", - })); - } + Filename = "Newtonsoft.Json", + NuGetSourceType = "Package", + NuGetPackageId = "Newtonsoft.Json", + })); + } - [Fact] - public void when_getting_package_contents_then_contains_dependency_for_non_private_assets_reference() - { - Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), target: "Restore", output: output) + [Fact] + public void when_getting_package_contents_then_contains_dependency_for_non_private_assets_reference() + { + Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), target: "Restore", output: output) .AssertSuccess(output); - var result = Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), output: output); + var result = Builder.BuildScenario(nameof(given_a_library_with_private_assets_reference), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Dependency, - Identity = "xunit", - })); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { + Identity = "xunit", + })); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Lib, - Filename = "xunit", - NuGetSourceType = "Package", - NuGetPackageId = "xunit", - })); - } - } + Filename = "xunit", + NuGetSourceType = "Package", + NuGetPackageId = "xunit", + })); + } + } } diff --git a/src/NuGetizer.Tests/given_a_library_with_project_reference.cs b/src/NuGetizer.Tests/given_a_library_with_project_reference.cs index 412524bc..2e22487e 100644 --- a/src/NuGetizer.Tests/given_a_library_with_project_reference.cs +++ b/src/NuGetizer.Tests/given_a_library_with_project_reference.cs @@ -4,77 +4,77 @@ namespace NuGetizer { - public class given_a_library_with_project_reference - { - ITestOutputHelper output; + public class given_a_library_with_project_reference + { + ITestOutputHelper output; - public given_a_library_with_project_reference(ITestOutputHelper output) - { - this.output = output; + public given_a_library_with_project_reference(ITestOutputHelper output) + { + this.output = output; Builder.BuildScenario(nameof(given_a_library_with_project_reference), output: output, target: "Restore") .AssertSuccess(output); } - [Fact] - public void when_getting_package_contents_then_retrieves_main_assembly_transitively() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_project_reference), output: output); + [Fact] + public void when_getting_package_contents_then_retrieves_main_assembly_transitively() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_project_reference), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - // TODO: build some helpers to make this easier to assert. - Assert.True(result.Items.Any(i => i.GetMetadata("FileName") == "a" && i.GetMetadata("Extension") == ".dll" && i.GetMetadata("PackFolder") == PackFolderKind.Lib), "Did not include main project output as Library"); - Assert.True(result.Items.Any(i => i.GetMetadata("FileName") == "b" && i.GetMetadata("Extension") == ".dll" && i.GetMetadata("PackFolder") == PackFolderKind.Lib), "Did not include referenced project output as Library"); - } + // TODO: build some helpers to make this easier to assert. + Assert.True(result.Items.Any(i => i.GetMetadata("FileName") == "a" && i.GetMetadata("Extension") == ".dll" && i.GetMetadata("PackFolder") == PackFolderKind.Lib), "Did not include main project output as Library"); + Assert.True(result.Items.Any(i => i.GetMetadata("FileName") == "b" && i.GetMetadata("Extension") == ".dll" && i.GetMetadata("PackFolder") == PackFolderKind.Lib), "Did not include referenced project output as Library"); + } - [Fact] - public void when_getting_package_contents_then_retrieves_symbols_transitively() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_project_reference), output: output); + [Fact] + public void when_getting_package_contents_then_retrieves_symbols_transitively() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_project_reference), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - // TODO: build some helpers to make this easier to assert. - Assert.True(result.Items.Any(i => i.GetMetadata("FileName") == "a" && i.GetMetadata("Extension") == ".pdb" && i.GetMetadata("PackFolder") == PackFolderKind.Lib), "Did not include main project symbols"); - Assert.True(result.Items.Any(i => i.GetMetadata("FileName") == "b" && i.GetMetadata("Extension") == ".pdb" && i.GetMetadata("PackFolder") == PackFolderKind.Lib), "Did not include referenced project symbols"); - } + // TODO: build some helpers to make this easier to assert. + Assert.True(result.Items.Any(i => i.GetMetadata("FileName") == "a" && i.GetMetadata("Extension") == ".pdb" && i.GetMetadata("PackFolder") == PackFolderKind.Lib), "Did not include main project symbols"); + Assert.True(result.Items.Any(i => i.GetMetadata("FileName") == "b" && i.GetMetadata("Extension") == ".pdb" && i.GetMetadata("PackFolder") == PackFolderKind.Lib), "Did not include referenced project symbols"); + } - [Fact] - public void when_include_in_package_false_then_does_not_include_referenced_project_outputs() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_project_reference), - properties: new { IncludeInPackage = "false" }, - output: output); + [Fact] + public void when_include_in_package_false_then_does_not_include_referenced_project_outputs() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_project_reference), + properties: new { IncludeInPackage = "false" }, + output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - Filename = "b", - Extension = ".dll", + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Filename = "b", + Extension = ".dll", PackFolder = "lib", - })); - } + })); + } - [Fact] - public void when_include_outputs_in_package_false_then_can_include_referenced_project_outputs() - { - var result = Builder.BuildScenario(nameof(given_a_library_with_project_reference), - properties: new - { - PackBuildOutput = "false", - PackProjectReference = "true" - }, - output: output); + [Fact] + public void when_include_outputs_in_package_false_then_can_include_referenced_project_outputs() + { + var result = Builder.BuildScenario(nameof(given_a_library_with_project_reference), + properties: new + { + PackBuildOutput = "false", + PackProjectReference = "true" + }, + output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - Filename = "b", - Extension = ".dll", + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Filename = "b", + Extension = ".dll", PackFolder = "lib", - })); - } - } + })); + } + } } diff --git a/src/NuGetizer.Tests/given_a_localized_library.cs b/src/NuGetizer.Tests/given_a_localized_library.cs index b968f741..91ee4689 100644 --- a/src/NuGetizer.Tests/given_a_localized_library.cs +++ b/src/NuGetizer.Tests/given_a_localized_library.cs @@ -5,24 +5,24 @@ namespace NuGetizer { public class given_a_localized_library - { - ITestOutputHelper output; + { + ITestOutputHelper output; - public given_a_localized_library(ITestOutputHelper output) - { - this.output = output; + public given_a_localized_library(ITestOutputHelper output) + { + this.output = output; Builder.BuildScenario(nameof(given_a_localized_library), output: output, target: "Restore") .AssertSuccess(output); } [Fact] - public void when_getting_package_contents_then_contains_localized_resources() - { - var result = Builder.BuildScenario(nameof(given_a_localized_library)); + public void when_getting_package_contents_then_contains_localized_resources() + { + var result = Builder.BuildScenario(nameof(given_a_localized_library)); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, i => i.GetMetadata("PackagePath") == "lib\\net45\\es-AR\\library.resources.dll"); - } - } + Assert.Contains(result.Items, i => i.GetMetadata("PackagePath") == "lib\\net45\\es-AR\\library.resources.dll"); + } + } } diff --git a/src/NuGetizer.Tests/given_a_multi_platform_solution.cs b/src/NuGetizer.Tests/given_a_multi_platform_solution.cs index 2881d925..178783aa 100644 --- a/src/NuGetizer.Tests/given_a_multi_platform_solution.cs +++ b/src/NuGetizer.Tests/given_a_multi_platform_solution.cs @@ -4,95 +4,95 @@ namespace NuGetizer { public class given_a_multi_platform_solution - { - ITestOutputHelper output; + { + ITestOutputHelper output; - public given_a_multi_platform_solution(ITestOutputHelper output) - { - this.output = output; + public given_a_multi_platform_solution(ITestOutputHelper output) + { + this.output = output; Builder.BuildScenario(nameof(given_a_multi_platform_solution), target: "Restore", output: output) .AssertSuccess(output); } [Fact] - public void then_includes_primary_output_from_platforms() - { - var result = Builder.BuildScenario(nameof(given_a_multi_platform_solution), output: output); + public void then_includes_primary_output_from_platforms() + { + var result = Builder.BuildScenario(nameof(given_a_multi_platform_solution), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\Forms.dll" - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\xamarinios10\Forms.dll" - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\monoandroid90\Forms.dll" - })); - } + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\Forms.dll" + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\xamarinios10\Forms.dll" + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\monoandroid90\Forms.dll" + })); + } - [Fact] - public void then_includes_direct_dependency_from_platforms() - { - var result = Builder.BuildScenario(nameof(given_a_multi_platform_solution), output: output); + [Fact] + public void then_includes_direct_dependency_from_platforms() + { + var result = Builder.BuildScenario(nameof(given_a_multi_platform_solution), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net472\Common.dll" + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net472\Common.dll" })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\xamarinios10\Common.dll" + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\xamarinios10\Common.dll" })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\monoandroid90\Common.dll" + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\monoandroid90\Common.dll" })); - } + } - [Fact] - public void then_includes_platform_and_language_quickstart_content() - { - var result = Builder.BuildScenario(nameof(given_a_multi_platform_solution), output: output); + [Fact] + public void then_includes_platform_and_language_quickstart_content() + { + var result = Builder.BuildScenario(nameof(given_a_multi_platform_solution), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\cs\monoandroid90\quickstart\sample.cs" + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\cs\monoandroid90\quickstart\sample.cs" })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\vb\monoandroid90\quickstart\sample.vb" + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\vb\monoandroid90\quickstart\sample.vb" })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"contentFiles\fs\monoandroid90\quickstart\sample.fs" + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"contentFiles\fs\monoandroid90\quickstart\sample.fs" })); - } + } - [Fact] - public void then_includes_platform_docs_from_before_get_package_contents() - { - var result = Builder.BuildScenario(nameof(given_a_multi_platform_solution), output: output); + [Fact] + public void then_includes_platform_docs_from_before_get_package_contents() + { + var result = Builder.BuildScenario(nameof(given_a_multi_platform_solution), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"docs\gettingstarted.html" - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"docs\overview\index.html" - })); - } + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"docs\gettingstarted.html" + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"docs\overview\index.html" + })); + } - } + } } diff --git a/src/NuGetizer.Tests/given_a_multitargeting_library.cs b/src/NuGetizer.Tests/given_a_multitargeting_library.cs index 94bd9a9a..c8846aff 100644 --- a/src/NuGetizer.Tests/given_a_multitargeting_library.cs +++ b/src/NuGetizer.Tests/given_a_multitargeting_library.cs @@ -5,20 +5,20 @@ namespace NuGetizer { public class given_a_multitargeting_library { - ITestOutputHelper output; + ITestOutputHelper output; - public given_a_multitargeting_library(ITestOutputHelper output) - { - this.output = output; + public given_a_multitargeting_library(ITestOutputHelper output) + { + this.output = output; Builder.BuildScenario(nameof(given_a_multitargeting_library), target: "Restore", output: output) .AssertSuccess(output); } [Fact] - public void when_gettingcontents_then_includes_content_from_all_frameworks() - { + public void when_gettingcontents_then_includes_content_from_all_frameworks() + { var result = Builder.BuildScenario(nameof(given_a_multitargeting_library), output: output); - result.AssertSuccess(output); + result.AssertSuccess(output); Assert.Contains(result.Items, item => item.Matches(new { diff --git a/src/NuGetizer.Tests/given_a_packaging_project.cs b/src/NuGetizer.Tests/given_a_packaging_project.cs index 48d05d29..3b23628e 100644 --- a/src/NuGetizer.Tests/given_a_packaging_project.cs +++ b/src/NuGetizer.Tests/given_a_packaging_project.cs @@ -4,136 +4,136 @@ namespace NuGetizer { public class given_a_packaging_project - { - ITestOutputHelper output; + { + ITestOutputHelper output; - public given_a_packaging_project(ITestOutputHelper output) - { - this.output = output; + public given_a_packaging_project(ITestOutputHelper output) + { + this.output = output; Builder.BuildScenario(nameof(given_a_packaging_project), target: "Restore") .AssertSuccess(output); } - - [Fact] - public void when_getting_contents_then_includes_referenced_project_outputs() - { - var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net46\b.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net46\b.xml", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net46\b.dll", - })); - } - - [Fact] - public void when_getting_contents_then_can_augment_package_metadata() - { - var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { + + [Fact] + public void when_getting_contents_then_includes_referenced_project_outputs() + { + var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net46\b.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net46\b.xml", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net46\b.dll", + })); + } + + [Fact] + public void when_getting_contents_then_can_augment_package_metadata() + { + var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Metadata, - Foo = "Bar", - })); - } - - [Fact] - public void when_getting_contents_then_includes_referenced_project_satellite_assembly() - { - var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net46\es-AR\b.resources.dll", - })); - } - - [Fact] - public void when_getting_contents_then_includes_referenced_project_dependencies() - { - var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net46\d.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net46\d.dll", - })); - } - - [Fact] - public void when_getting_contents_then_includes_referenced_project_dependency_satellite_assembly() - { - var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net46\es-AR\d.resources.dll", - })); - } - - [Fact] - public void when_getting_contents_then_includes_referenced_packagable_project_as_dependency() - { - var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { + Foo = "Bar", + })); + } + + [Fact] + public void when_getting_contents_then_includes_referenced_project_satellite_assembly() + { + var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net46\es-AR\b.resources.dll", + })); + } + + [Fact] + public void when_getting_contents_then_includes_referenced_project_dependencies() + { + var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net46\d.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net46\d.dll", + })); + } + + [Fact] + public void when_getting_contents_then_includes_referenced_project_dependency_satellite_assembly() + { + var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net46\es-AR\d.resources.dll", + })); + } + + [Fact] + public void when_getting_contents_then_includes_referenced_packagable_project_as_dependency() + { + var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.Dependency, - Identity = "E", - })); - } - - [Fact] - public void when_getting_contents_then_does_not_include_referenced_project_nuget_assembly_reference() - { - var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); - - result.AssertSuccess(output); - - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net46\Newtonsoft.Json.dll", - })); - } - - [Fact] - public void when_getting_contents_from_packaging_project_then_referenced_outputs_have_original_tfm_path() - { - var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net45\c.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net45\c.xml", - })); - } - } + Identity = "E", + })); + } + + [Fact] + public void when_getting_contents_then_does_not_include_referenced_project_nuget_assembly_reference() + { + var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); + + result.AssertSuccess(output); + + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net46\Newtonsoft.Json.dll", + })); + } + + [Fact] + public void when_getting_contents_from_packaging_project_then_referenced_outputs_have_original_tfm_path() + { + var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net45\c.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net45\c.xml", + })); + } + } } diff --git a/src/NuGetizer.Tests/given_a_packaging_project_with_netstandard.cs b/src/NuGetizer.Tests/given_a_packaging_project_with_netstandard.cs index 7b85a3e1..5521368d 100644 --- a/src/NuGetizer.Tests/given_a_packaging_project_with_netstandard.cs +++ b/src/NuGetizer.Tests/given_a_packaging_project_with_netstandard.cs @@ -6,52 +6,52 @@ namespace NuGetizer // NOTE: not added to the main project because it's built using MSBuild 14 and it's not working // by adding binding redirects, appdomain isolation, etc. :( public class given_a_packaging_project_with_netstandard - { - ITestOutputHelper output; - - public given_a_packaging_project_with_netstandard(ITestOutputHelper output) => this.output = output; - - [Fact] - public void can_get_content_from_referenced_single_targeting_netstandard() - { - Builder.BuildScenario(nameof(given_a_packaging_project_with_netstandard), - target: "Restore", - properties: new { SimulateCrossTargeting = "false" }); - var result = Builder.BuildScenario(nameof(given_a_packaging_project_with_netstandard), - target: "GetPackageContents", - properties: new { SimulateCrossTargeting = "false" }, - output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\netstandard1.6\b.dll", - })); - } - - [Fact] - public void can_get_content_from_referenced_cross_targeting_netstandard() - { - Builder.BuildScenario(nameof(given_a_packaging_project_with_netstandard), - target: "Restore", - properties: new { SimulateCrossTargeting = "true" }); - var result = Builder.BuildScenario(nameof(given_a_packaging_project_with_netstandard), - target: "GetPackageContents", - properties: new { SimulateCrossTargeting = "true" }, - output: output); - - result.AssertSuccess(output); - - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\net45\b.dll", - })); - Assert.Contains(result.Items, item => item.Matches(new - { - PackagePath = @"lib\netstandard1.6\b.dll", - })); - } - - } + { + ITestOutputHelper output; + + public given_a_packaging_project_with_netstandard(ITestOutputHelper output) => this.output = output; + + [Fact] + public void can_get_content_from_referenced_single_targeting_netstandard() + { + Builder.BuildScenario(nameof(given_a_packaging_project_with_netstandard), + target: "Restore", + properties: new { SimulateCrossTargeting = "false" }); + var result = Builder.BuildScenario(nameof(given_a_packaging_project_with_netstandard), + target: "GetPackageContents", + properties: new { SimulateCrossTargeting = "false" }, + output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard1.6\b.dll", + })); + } + + [Fact] + public void can_get_content_from_referenced_cross_targeting_netstandard() + { + Builder.BuildScenario(nameof(given_a_packaging_project_with_netstandard), + target: "Restore", + properties: new { SimulateCrossTargeting = "true" }); + var result = Builder.BuildScenario(nameof(given_a_packaging_project_with_netstandard), + target: "GetPackageContents", + properties: new { SimulateCrossTargeting = "true" }, + output: output); + + result.AssertSuccess(output); + + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\net45\b.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard1.6\b.dll", + })); + } + + } } diff --git a/src/NuGetizer.Tests/given_an_empty_library.cs b/src/NuGetizer.Tests/given_an_empty_library.cs index be26334b..9d835c90 100644 --- a/src/NuGetizer.Tests/given_an_empty_library.cs +++ b/src/NuGetizer.Tests/given_an_empty_library.cs @@ -5,174 +5,252 @@ namespace NuGetizer { public class given_an_empty_library - { - ITestOutputHelper output; - - public given_an_empty_library(ITestOutputHelper output) - { - this.output = output; - Builder.BuildScenario(nameof(given_an_empty_library), target: "Restore") - .AssertSuccess(output); + { + ITestOutputHelper output; + + public given_an_empty_library(ITestOutputHelper output) => this.output = output; + + [Fact] + public void when_is_packable_true_then_package_id_defaults_to_assembly_name() + { + var result = Builder.BuildProject(@" + + + true + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new + { + PackFolder = PackFolderKind.Metadata + })); + } + + [Fact] + public void when_no_is_packable_and_no_package_id_then_defaults_to_non_packable() + { + var result = Builder.BuildProject(@" + + + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + PackFolder = PackFolderKind.Metadata + })); } [Fact] - public void when_getting_package_contents_then_includes_output_assembly() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library)); - - result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - Extension = ".dll", + public void when_getting_package_contents_then_includes_output_assembly() + { + var result = Builder.BuildProject(@" + + + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new + { + Extension = ".dll", PackFolder = PackFolderKind.Lib })); - } - - [Fact] - public void when_include_outputs_in_package_is_false_then_does_not_include_main_assembly() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackBuildOutput = false, - }); - - result.AssertSuccess(output); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - Extension = ".dll", + } + + [Fact] + public void when_include_outputs_in_package_is_false_then_does_not_include_main_assembly() + { + var result = Builder.BuildProject(@" + + + false + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Extension = ".dll", PackFolder = PackFolderKind.Lib })); - } - - [Fact] - public void when_getting_package_contents_then_includes_symbols() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new { Configuration = "Debug" }); - - result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - Extension = ".pdb", - })); - } - - [Fact] - public void when_include_symbols_in_package_is_true_but_include_outputs_is_false_then_does_not_include_symbols() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackBuildOutput = false, - PackSymbols = true, - }); - - result.AssertSuccess(output); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - Extension = ".pdb", - })); - } - - [Fact] - public void when_include_symbols_in_package_is_false_then_does_not_include_symbols() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackSymbols = false, - }); - - result.AssertSuccess(output); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - Extension = ".pdb", - })); - } - - [Fact] - public void when_getting_package_contents_then_includes_xmldoc() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library)); - - Assert.Equal(TargetResultCode.Success, result.ResultCode); - Assert.Contains(result.Items, item => item.Matches(new - { - Extension = ".xml", + } + + [Fact] + public void when_getting_package_contents_then_includes_symbols() + { + var result = Builder.BuildProject(@" + + + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new + { + Extension = ".pdb", + })); + } + + [Fact] + public void when_include_symbols_in_package_is_true_but_include_outputs_is_false_then_does_not_include_symbols() + { + var result = Builder.BuildProject(@" + + + false + true + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Extension = ".pdb", + })); + } + + [Fact] + public void when_include_symbols_in_package_is_false_then_does_not_include_symbols() + { + var result = Builder.BuildProject(@" + + + false + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Extension = ".pdb", + })); + } + + [Fact] + public void when_getting_package_contents_then_includes_xmldoc() + { + var result = Builder.BuildProject(@" + + + false + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new + { + Extension = ".xml", PackFolder = PackFolderKind.Lib, - })); - } - - [Fact] - public void when_include_output_in_package_is_false_then_does_not_include_xmldoc() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackBuildOutput = false, - }); - - result.AssertSuccess(output); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - Extension = ".xml", + })); + } + + [Fact] + public void when_include_output_in_package_is_false_then_does_not_include_xmldoc() + { + var result = Builder.BuildProject(@" + + + false + netstandard2.0 + +", output: output); + + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Extension = ".xml", PackFolder = PackFolderKind.Lib, - })); - } - - [Fact] - public void when_getting_package_contents_then_annotates_items_with_package_id() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new { PackageId = "Foo" }, output: output); - - result.AssertSuccess(output); - Assert.All(result.Items, item => Assert.Equal("Foo", item.GetMetadata("PackageId"))); - } - - [Fact] - public void when_getting_package_contents_then_includes_framework_reference() - { - Builder.BuildScenario(nameof(given_an_empty_library), new + })); + } + + [Fact] + public void when_getting_package_contents_then_annotates_items_with_package_id() + { + var result = Builder.BuildProject(@" + + + Foo + netstandard2.0 + +", output: output); + + result.AssertSuccess(output); + Assert.All(result.Items, item => Assert.Equal("Foo", item.GetMetadata("PackageId"))); + } + + [Fact] + public void when_getting_package_contents_then_includes_framework_reference() + { + var result = Builder.BuildProject(@" + + + Foo + net472 + + + + +", output: output); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new { - PackageId = "Foo", - TargetFramework = "net472", - },target: "Restore").AssertSuccess(output); - - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackageId = "Foo", - TargetFramework = "net472", - }, output: output); - - result.AssertSuccess(output); - Assert.Contains(result.Items, item => item.Matches(new - { - Identity = "PresentationFramework", + Identity = "PresentationFramework", PackFolder = PackFolderKind.FrameworkReference, - })); - } - - [Fact] - public void when_include_framework_references_in_package_is_false_then_does_not_include_framework_reference() - { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackFrameworkReferences = false, - PackageId = "Foo", - }, output: output); - - result.AssertSuccess(output); - Assert.DoesNotContain(result.Items, item => item.Matches(new - { - Identity = "System.Core", + })); + } + + [Fact] + public void when_include_framework_references_in_package_is_false_then_does_not_include_framework_reference() + { + var result = Builder.BuildProject(@" + + + Foo + false + net472 + + + + +", output: output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Identity = "PresentationFramework", PackFolder = PackFolderKind.FrameworkReference, - })); - } + })); + } [Fact] public void when_updating_package_item_metadata_then_updates_metadata() { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackageId = "Foo", - AdvancedCustomization = "true", - }, target: "GetPackageMetadata", output: output); + var result = Builder.BuildProject(@" + + + Foo + false + netstandard2.0 + + + + ItemDescription + + +", output: output); result.AssertSuccess(output); @@ -183,12 +261,21 @@ public void when_updating_package_item_metadata_then_updates_metadata() } [Fact] - public void when_updating_package_metadata_property_in_target_then_updates_metadata() + public void when_updating_package_metadata_via_target_then_updates_metadata() { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackageId = "Foo", - }, target: "GetPackageMetadata", output: output); + var result = Builder.BuildProject(@" + + + Foo + false + netstandard2.0 + + + + PropertyDescription + + +", output: output); result.AssertSuccess(output); @@ -201,11 +288,20 @@ public void when_updating_package_metadata_property_in_target_then_updates_metad [Fact] public void when_setting_metadata_property_then_updates_metadata() { - var result = Builder.BuildScenario(nameof(given_an_empty_library), new - { - PackageId = "Foo", - Title = "MyPackage", - }, target: "GetPackageMetadata", output: output); + var result = Builder.BuildProject(@" + + + Foo + false + MyPackage + netstandard2.0 + + + + PropertyDescription + + +", output: output); result.AssertSuccess(output); diff --git a/src/NuGetizer.Tests/given_duplicate_package_files.cs b/src/NuGetizer.Tests/given_duplicate_package_files.cs index 8d244d0b..b9df480b 100644 --- a/src/NuGetizer.Tests/given_duplicate_package_files.cs +++ b/src/NuGetizer.Tests/given_duplicate_package_files.cs @@ -5,68 +5,68 @@ namespace NuGetizer { - public class given_duplicate_package_files - { - ITestOutputHelper output; + public class given_duplicate_package_files + { + ITestOutputHelper output; - public given_duplicate_package_files(ITestOutputHelper output) - { - this.output = output; + public given_duplicate_package_files(ITestOutputHelper output) + { + this.output = output; Builder.BuildScenario(nameof(given_duplicate_package_files), target: "Restore") .AssertSuccess(output); } [Fact] - public void exact_duplicates_are_removed() - { - var result = Builder.BuildScenario(nameof(given_duplicate_package_files), target: "Pack"); + public void exact_duplicates_are_removed() + { + var result = Builder.BuildScenario(nameof(given_duplicate_package_files), target: "Pack"); - Assert.Equal(TargetResultCode.Success, result.ResultCode); - } + Assert.Equal(TargetResultCode.Success, result.ResultCode); + } - [Fact] - public void same_source_different_target_are_preserved() - { - var result = Builder.BuildScenario(nameof(given_duplicate_package_files), target: "GetPackageContents"); - Assert.Equal(TargetResultCode.Success, result.ResultCode); + [Fact] + public void same_source_different_target_are_preserved() + { + var result = Builder.BuildScenario(nameof(given_duplicate_package_files), target: "GetPackageContents"); + Assert.Equal(TargetResultCode.Success, result.ResultCode); - Assert.Contains(result.Items, item => item.Matches(new - { + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.None, - PackagePath = @"content\a\1\content.txt" - })); - Assert.Contains(result.Items, item => item.Matches(new - { + PackagePath = @"content\a\1\content.txt" + })); + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.None, - PackagePath = @"content\a\2\content.txt" - })); - Assert.Contains(result.Items, item => item.Matches(new - { + PackagePath = @"content\a\2\content.txt" + })); + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.None, - PackagePath = @"content\b\1\content.txt" - })); - Assert.Contains(result.Items, item => item.Matches(new - { + PackagePath = @"content\b\1\content.txt" + })); + Assert.Contains(result.Items, item => item.Matches(new + { PackFolder = PackFolderKind.None, - PackagePath = @"content\b\2\content.txt" - })); + PackagePath = @"content\b\2\content.txt" + })); - result = Builder.BuildScenario(nameof(given_duplicate_package_files), target: "Pack"); - Assert.Equal(TargetResultCode.Success, result.ResultCode); - } + result = Builder.BuildScenario(nameof(given_duplicate_package_files), target: "Pack"); + Assert.Equal(TargetResultCode.Success, result.ResultCode); + } - [Fact] - public void real_duplicates_fail() - { - var result = Builder.BuildScenario(nameof(given_duplicate_package_files), - new { ReadmeA = "First Readme", ReadmeB = "Second Readme" }, - target: "Pack"); + [Fact] + public void real_duplicates_fail() + { + var result = Builder.BuildScenario(nameof(given_duplicate_package_files), + new { ReadmeA = "First Readme", ReadmeB = "Second Readme" }, + target: "Pack"); - Assert.Equal(TargetResultCode.Failure, result.ResultCode); + Assert.Equal(TargetResultCode.Failure, result.ResultCode); - // A distinct Readme.txt is generated by a.csproj and b.csproj but both have the - // same PackagePath so we can't disambiguate after hashing the contents. - Assert.Contains(result.Logger.Errors, e => e.Message.Contains("Readme.txt")); - } - } + // A distinct Readme.txt is generated by a.csproj and b.csproj but both have the + // same PackagePath so we can't disambiguate after hashing the contents. + Assert.Contains(result.Logger.Errors, e => e.Message.Contains("Readme.txt")); + } + } } \ No newline at end of file diff --git a/src/NuGetizer.Tests/given_projectreferences.cs b/src/NuGetizer.Tests/given_projectreferences.cs new file mode 100644 index 00000000..4526bb3f --- /dev/null +++ b/src/NuGetizer.Tests/given_projectreferences.cs @@ -0,0 +1,221 @@ +using Microsoft.Build.Execution; +using Xunit; +using Xunit.Abstractions; + +namespace NuGetizer +{ + public class given_projectreferences + { + ITestOutputHelper output; + + public given_projectreferences(ITestOutputHelper output) => this.output = output; + + [Fact] + public void when_privateassets_all_then_packs_library() + { + var result = Builder.BuildProject(@" + + + Analyzer + netstandard2.0 + analyzers\cs + + + + +", + "GetPackageContents", output, + files: ("Library.csproj", @" + + + Library + netstandard2.0 + +")); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"analyzers\cs\Library.dll", + })); + } + + [Fact] + public void when_privateassets_on_explicit_PackFolder_then_packs_library() + { + var result = Builder.BuildProject(@" + + + Analyzer + netstandard2.0 + analyzers\cs + + + + +", + "GetPackageContents", output, + files: ("Library.csproj", @" + + + Library + build + netstandard2.0 + +")); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"analyzers\cs\Library.dll", + })); + } + + [Fact] + public void when_privateassets_all_then_packs_transitive_libraries() + { + var result = Builder.BuildProject(@" + + + Library + netstandard2.0 + + + + +", + "GetPackageContents", output); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Prism.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Prism.Forms.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Xamarin.Forms.Core.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Xamarin.Forms.Platform.dll", + })); + Assert.Contains(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Xamarin.Forms.Xaml.dll", + })); + } + + [Fact] + public void when_privateassets_all_and_pack_false_then_does_not_pack_transitively() + { + var result = Builder.BuildProject(@" + + + Library + netstandard2.0 + + + + +", + "GetPackageContents", output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Prism.dll", + })); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Prism.Forms.dll", + })); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Xamarin.Forms.Core.dll", + })); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Xamarin.Forms.Platform.dll", + })); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Xamarin.Forms.Xaml.dll", + })); + } + + [Fact] + public void when_privateassets_pack_false_then_does_not_pack() + { + var result = Builder.BuildProject(@" + + + Library + netstandard2.0 + + + + +", + "GetPackageContents", output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + PackagePath = @"lib\netstandard2.0\Newtonsoft.Json.dll", + })); + } + + [Fact] + public void when_build_kind_then_does_not_pack_msbuild() + { + var result = Builder.BuildProject(@" + + + Library + netstandard2.0 + build + + + + +", + "GetPackageContents", output); + + result.AssertSuccess(output); + Assert.DoesNotContain(result.Items, item => item.Matches(new + { + Identity = "Microsoft.Build.Tasks.Core", + PackFolder = PackFolderKind.Dependency, + })); + } + + [Fact] + public void when_build_kind_and_explicit_pack_then_packs_msbuild() + { + var result = Builder.BuildProject(@" + + + Library + netstandard2.0 + build + + + + +", + "GetPackageContents", output); + + result.AssertSuccess(output); + Assert.Contains(result.Items, item => item.Matches(new + { + Identity = "Microsoft.Build.Tasks.Core", + PackFolder = PackFolderKind.Dependency, + })); + } + + } +} diff --git a/src/NuGetizer.Tests/given_sourcelink.cs b/src/NuGetizer.Tests/given_sourcelink.cs index 8d3ec070..75276eab 100644 --- a/src/NuGetizer.Tests/given_sourcelink.cs +++ b/src/NuGetizer.Tests/given_sourcelink.cs @@ -4,8 +4,8 @@ namespace NuGetizer { public class given_sourcelink - { - ITestOutputHelper output; + { + ITestOutputHelper output; public given_sourcelink(ITestOutputHelper output) => this.output = output; @@ -22,7 +22,7 @@ public void when_getting_metadata_then_adds_repository_info() -", +", "GetPackageMetadata", output); result.AssertSuccess(output); diff --git a/src/Sample/Properties/AssemblyInfo.cs b/src/Sample/Properties/AssemblyInfo.cs index 7dcc1177..1e02b4db 100644 --- a/src/Sample/Properties/AssemblyInfo.cs +++ b/src/Sample/Properties/AssemblyInfo.cs @@ -1,5 +1,5 @@ -using System.Resources; -using System.Reflection; +using System.Reflection; +using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; diff --git a/src/dotnet-nugetize/Program.cs b/src/dotnet-nugetize/Program.cs index ae5eaf27..89002896 100644 --- a/src/dotnet-nugetize/Program.cs +++ b/src/dotnet-nugetize/Program.cs @@ -46,7 +46,7 @@ static int Execute(bool binlog, bool debug) File.Delete(file); // Optimize the "build" so that it doesn't actually do a full compile if possible. - if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} -m:1 -p:dotnet-nugetize=\"{file}\" -nologo {bl} -t:\"GetPackageContents;Pack\" -p:EmitNuspec=true -p:EmitPackage=false -p:SkipCompilerExecution=true -p:DesignTimeBuild=true -p:DesignTimeSilentResolution=true -p:ResolveAssemblyReferencesSilent=true", debug)) + if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} -m:1 -p:dotnet-nugetize=\"{file}\" -nologo {bl} -t:\"GetPackageContents;Pack\" -p:EnableSourceLink=true -p:EnableSourceControlManagerQueries=true -p:EmitNuspec=true -p:EmitPackage=false -p:SkipCompilerExecution=true -p:DesignTimeBuild=true -p:DesignTimeSilentResolution=true -p:ResolveAssemblyReferencesSilent=true", debug)) return -1; var items = XDocument.Load(file); @@ -54,7 +54,7 @@ static int Execute(bool binlog, bool debug) foreach (var metadata in items.Root.Descendants("PackageMetadata") .Distinct(AnonymousComparer.Create( - (x, y) => x.Element("PackageId")?.Value == y.Element("PackageId")?.Value, + (x, y) => x.Element("PackageId")?.Value == y.Element("PackageId")?.Value, x => x.Element("PackageId")?.Value.GetHashCode() ?? 0))) { var packageId = metadata.Element("PackageId").Value; @@ -68,9 +68,9 @@ static int Execute(bool binlog, bool debug) .First(); foreach (var md in metadata.Elements() - .Where(x => - x.Name != "PackageId" && - x.Name != "Nuspec" && + .Where(x => + x.Name != "PackageId" && + x.Name != "Nuspec" && x.Name != "NuPkg") .OrderBy(x => x.Name.LocalName)) { @@ -81,6 +81,12 @@ static int Execute(bool binlog, bool debug) .Where(x => "Dependency".Equals(x.Element("PackFolder")?.Value, StringComparison.OrdinalIgnoreCase) && x.Element("PackageId")?.Value == packageId) + .Distinct(AnonymousComparer.Create(x => + x.Attribute("Include").Value + "|" + + x.Element("Version").Value + "|" + + x.Element("TargetFramework").Value)) + .OrderBy(x => x.Element("TargetFramework").Value) + .ThenBy(x => x.Attribute("Include").Value) .ToList(); if (dependencies.Count > 0) @@ -99,10 +105,9 @@ static int Execute(bool binlog, bool debug) ColorConsole.WriteLine($" Contents:".Yellow()); var contents = items.Root.Descendants("PackageContent") - .Where(x => - x.Element("PackagePath") != null && + .Where(x => + x.Element("PackagePath") != null && x.Element("PackageId")?.Value == packageId) - //.Select(x => x.Element("PackagePath").Value) .Distinct(AnonymousComparer.Create(x => x.Element("PackagePath").Value)) .OrderBy(x => Path.GetDirectoryName(x.Element("PackagePath").Value)) .ThenBy(x => x.Element("PackagePath").Value); @@ -120,6 +125,20 @@ static int Execute(bool binlog, bool debug) Console.WriteLine(); ColorConsole.WriteLine("Items: ", file.Yellow()); + if (binlog) + { + // Attempt to open binlog automatically if msbuildlog.com is installed + try + { + Process.Start(new ProcessStartInfo(Directory.GetCurrentDirectory() + "\\build.binlog") + { + UseShellExecute = true, + WindowStyle = ProcessWindowStyle.Maximized + }); + } + catch { } + } + return 0; } @@ -152,12 +171,19 @@ static int Render(IList files, int index, int level, string path) ColorConsole.Write("/".Gray()); var attributes = new List(); - if (element.Element("BuildAction")?.Value is string buildAction) - attributes.Add("buildAction=" + buildAction); - if (element.Element("CopyToOutput")?.Value is string copyToOutput) - attributes.Add("copyToOutput=" + copyToOutput); - if (element.Element("Flatten")?.Value is string flatten) - attributes.Add("flatten=" + flatten); + var packFolder = element.Element("PackFolder")?.Value; + + if (packFolder != null && + ("content".Equals(packFolder, StringComparison.OrdinalIgnoreCase) || + "contentFiles".Equals(packFolder, StringComparison.OrdinalIgnoreCase))) + { + if (element.Element("BuildAction")?.Value is string buildAction) + attributes.Add("buildAction=" + buildAction); + if (element.Element("CopyToOutput")?.Value is string copyToOutput) + attributes.Add("copyToOutput=" + copyToOutput); + if (element.Element("Flatten")?.Value is string flatten) + attributes.Add("flatten=" + flatten); + } ColorConsole.Write(Path.GetFileName(file).White()); if (attributes.Count > 0) @@ -176,21 +202,33 @@ static bool Execute(string program, string arguments, bool debug = false) { var info = new ProcessStartInfo(program, arguments) { - RedirectStandardOutput = debug, - RedirectStandardError = debug + RedirectStandardOutput = true, + RedirectStandardError = true }; var proc = Process.Start(info); + if (debug) { Console.Out.Write(proc.StandardOutput.ReadToEnd()); Console.Error.Write(proc.StandardError.ReadToEnd()); } - if (!proc.WaitForExit(5000)) + var timedout = false; + + // If process takes too long, start to automatically + // render the output. + while (!proc.WaitForExit(5000)) + { + timedout = true; + Console.Out.Write(proc.StandardOutput.ReadToEnd()); + Console.Error.Write(proc.StandardError.ReadToEnd()); + } + + if ((proc.ExitCode != 0 && !debug) || timedout) { - proc.Kill(); - return false; + Console.Out.Write(proc.StandardOutput.ReadToEnd()); + Console.Error.Write(proc.StandardError.ReadToEnd()); } return proc.ExitCode == 0; diff --git a/src/dotnet-nugetize/dotnet-nugetize.csproj b/src/dotnet-nugetize/dotnet-nugetize.csproj index d6bbf659..5034d422 100644 --- a/src/dotnet-nugetize/dotnet-nugetize.csproj +++ b/src/dotnet-nugetize/dotnet-nugetize.csproj @@ -18,7 +18,7 @@ - +