Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkl committed Apr 16, 2021
1 parent 35e1d8e commit 656ccd2
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 399 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,27 @@ NuGet and MSBuild are very tightly coupled and a lot of times you need packages
Create a package repository with a package that supports two target frameworks:

```C#
PackageRepository.Create(rootPath)
.Package("MyPackage", "1.2.3", out PackageIdentify package)
using(PackageRepository.Create(rootPath)
.Package("MyPackage", "1.2.3", out PackageIdentity package)
.Library("net472")
.Library("netstandard2.0");
.Library("netstandard2.0"))
{
// Create projects that reference packages
}
```

The resulting package would have a `lib\net472\MyPackage.dll` and `lib\netstandard2.0\MyPackage.dll` class library. This allows you to restore and build projects that consume the packages

```C#
PackageRepository.Create(rootPath)
.Package("MyPackage", "1.0.0", out PackageIdentify package)
.Library("netstandard2.0");

ProjectCreator.Templates.SdkCsproj()
.ItemPackageReference(package)
.Save(Path.Combine(rootPath, "ClassLibraryA", "ClassLibraryA.csproj"))
.TryBuild(restore: true, out bool result, out BuildOutput buildOutput);
using(PackageRepository.Create(rootPath)
.Package("MyPackage", "1.0.0", out PackageIdentity package)
.Library("netstandard2.0"))
{
ProjectCreator.Templates.SdkCsproj()
.ItemPackageReference(package)
.Save(Path.Combine(rootPath, "ClassLibraryA", "ClassLibraryA.csproj"))
.TryBuild(restore: true, out bool result, out BuildOutput buildOutput);
}
```

The result would be a project that references the `MyPackage` package and would restore and build accordingly.
21 changes: 0 additions & 21 deletions src/MSBuildProjectCreator.UnitTests/BuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using NuGet.Packaging.Core;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -18,25 +16,6 @@ namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
{
public class BuildTests : TestBase
{
[Fact]
public void BuildCanConsumePackage()
{
PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageB", "1.0", out PackageIdentity packageB)
.Library("net45")
.Package("PackageA", "1.0.0", out PackageIdentity packageA)
.Dependency(packageB, "net45")
.Library("net45");

ProjectCreator.Templates.SdkCsproj(
targetFramework: "net45")
.ItemPackageReference(packageA)
.Save(Path.Combine(TestRootPath, "ClassLibraryA", "ClassLibraryA.csproj"))
.TryBuild(restore: true, out bool result, out BuildOutput buildOutput);

result.ShouldBeTrue(buildOutput.GetConsoleLog());
}

[Fact]
public void BuildTargetOutputsTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,68 @@ public class DependencyTests : TestBase
[Fact]
public void CanAddDependenciesToMultipleGroups()
{
PackageRepository.Create(TestRootPath)
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageA", "1.0.0", out PackageIdentity package)
.Dependency("PackageB", "1.0.0", "net45")
.Dependency("PackageB", "1.0.0", "net46")
.Dependency("PackageB", "1.0.0", "netstandard2.0");

ValidatePackageDependencies(
package,
new List<PackageDependencyGroup>
{
new PackageDependencyGroup(
FrameworkConstants.CommonFrameworks.Net45,
new List<PackageDependency>
{
new PackageDependency("PackageB", VersionRange.Parse("1.0.0")),
}),
new PackageDependencyGroup(
FrameworkConstants.CommonFrameworks.Net46,
new List<PackageDependency>
{
new PackageDependency("PackageB", VersionRange.Parse("1.0.0")),
}),
new PackageDependencyGroup(
FrameworkConstants.CommonFrameworks.NetStandard20,
new List<PackageDependency>
{
new PackageDependency("PackageB", VersionRange.Parse("1.0.0")),
}),
});
.Dependency("PackageB", "1.0.0", "net45")
.Dependency("PackageB", "1.0.0", "net46")
.Dependency("PackageB", "1.0.0", "netstandard2.0"))
{
ValidatePackageDependencies(
packageRepository,
package,
new List<PackageDependencyGroup>
{
new PackageDependencyGroup(
FrameworkConstants.CommonFrameworks.Net45,
new List<PackageDependency>
{
new PackageDependency("PackageB", VersionRange.Parse("1.0.0")),
}),
new PackageDependencyGroup(
FrameworkConstants.CommonFrameworks.Net46,
new List<PackageDependency>
{
new PackageDependency("PackageB", VersionRange.Parse("1.0.0")),
}),
new PackageDependencyGroup(
FrameworkConstants.CommonFrameworks.NetStandard20,
new List<PackageDependency>
{
new PackageDependency("PackageB", VersionRange.Parse("1.0.0")),
}),
});
}
}

[Fact]
public void CanAddMultipleDependenciesToSameGroup()
{
PackageRepository.Create(TestRootPath)
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageA", "1.0.0", out PackageIdentity package)
.Dependency("PackageB", "1.0.0", "net45")
.Dependency("PackageC", "1.1.0", "net45")
.Dependency("PackageD", "1.2.0", "net45");

ValidatePackageDependencies(
package,
new List<PackageDependencyGroup>
{
new PackageDependencyGroup(
FrameworkConstants.CommonFrameworks.Net45,
new List<PackageDependency>
{
new PackageDependency("PackageB", VersionRange.Parse("1.0.0")),
new PackageDependency("PackageC", VersionRange.Parse("1.1.0")),
new PackageDependency("PackageD", VersionRange.Parse("1.2.0")),
}),
});
.Dependency("PackageB", "1.0.0", "net45")
.Dependency("PackageC", "1.1.0", "net45")
.Dependency("PackageD", "1.2.0", "net45"))
{
ValidatePackageDependencies(
packageRepository,
package,
new List<PackageDependencyGroup>
{
new PackageDependencyGroup(
FrameworkConstants.CommonFrameworks.Net45,
new List<PackageDependency>
{
new PackageDependency("PackageB", VersionRange.Parse("1.0.0")),
new PackageDependency("PackageC", VersionRange.Parse("1.1.0")),
new PackageDependency("PackageD", VersionRange.Parse("1.2.0")),
}),
});
}
}

private void ValidatePackageDependencies(PackageIdentity package, IEnumerable<PackageDependencyGroup> expectedDependencyGroups)
private void ValidatePackageDependencies(PackageRepository packageRepository, PackageIdentity package, IEnumerable<PackageDependencyGroup> expectedDependencyGroups)
{
FileInfo nuspecFile = new FileInfo(((VersionFolderPathResolver)VersionFolderPathResolver).GetManifestFilePath(package.Id, package.Version));
FileInfo nuspecFile = new FileInfo(packageRepository.GetManifestFilePath(package.Id, package.Version));

nuspecFile.ShouldExist();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// Licensed under the MIT license.

using NuGet.Packaging;
using NuGet.Packaging.Core;
using Shouldly;
using System.IO;
Expand All @@ -22,11 +21,12 @@ public void CustomFileTest()

File.WriteAllText(sourceFileInfo.FullName, contents);

PackageRepository.Create(TestRootPath)
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageA", "1.0.0", out PackageIdentity packageA)
.FileCustom(relativePath, sourceFileInfo);

VerifyFileContents(packageA, relativePath, contents);
.FileCustom(relativePath, sourceFileInfo))
{
VerifyFileContents(packageRepository, packageA, relativePath, contents);
}
}

[Fact]
Expand All @@ -35,16 +35,17 @@ public void TextFileTest()
string relativePath = Path.Combine("test", "foo.txt");
const string contents = "FF6B25B727E04D9980DE3B5D7AE0FB6E";

PackageRepository.Create(TestRootPath)
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageA", "1.0.0", out PackageIdentity packageA)
.FileText(relativePath, contents);

VerifyFileContents(packageA, relativePath, contents);
.FileText(relativePath, contents))
{
VerifyFileContents(packageRepository, packageA, relativePath, contents);
}
}

private void VerifyFileContents(PackageIdentity package, string relativePath, string contents)
private void VerifyFileContents(PackageRepository packageRepository, PackageIdentity package, string relativePath, string contents)
{
DirectoryInfo packageDirectory = new DirectoryInfo(((VersionFolderPathResolver)VersionFolderPathResolver).GetInstallPath(package.Id, package.Version))
DirectoryInfo packageDirectory = new DirectoryInfo(packageRepository.GetInstallPath(package.Id, package.Version))
.ShouldExist();

FileInfo file = new FileInfo(Path.Combine(packageDirectory.FullName, relativePath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Licensed under the MIT license.

using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using Shouldly;
using System;
Expand All @@ -18,52 +17,56 @@ public class LibraryTests : TestBase
[Fact]
public void BasicLibrary()
{
PackageRepository.Create(TestRootPath)
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageA", "1.0.0", out PackageIdentity packageA)
.Library(FrameworkConstants.CommonFrameworks.Net45);

VerifyAssembly(packageA, FrameworkConstants.CommonFrameworks.Net45);
.Library(FrameworkConstants.CommonFrameworks.Net45))
{
VerifyAssembly(packageRepository, packageA, FrameworkConstants.CommonFrameworks.Net45);
}
}

[Fact]
public void LibraryWithVersion()
{
const string assemblyVersion = "2.3.4.5";

PackageRepository.Create(TestRootPath)
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageA", "1.0.0", out PackageIdentity packageA)
.Library(FrameworkConstants.CommonFrameworks.Net45, assemblyVersion: assemblyVersion);

VerifyAssembly(packageA, FrameworkConstants.CommonFrameworks.Net45, version: "2.3.4.5");
.Library(FrameworkConstants.CommonFrameworks.Net45, assemblyVersion: assemblyVersion))
{
VerifyAssembly(packageRepository, packageA, FrameworkConstants.CommonFrameworks.Net45, version: "2.3.4.5");
}
}

[Fact]
public void MultipleLibrariesMultipleTargetFrameworks()
{
PackageRepository.Create(TestRootPath)
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageA", "1.0.0", out PackageIdentity packageA)
.Library(FrameworkConstants.CommonFrameworks.Net45)
.Library(FrameworkConstants.CommonFrameworks.NetStandard20);

VerifyAssembly(packageA, FrameworkConstants.CommonFrameworks.Net45);
VerifyAssembly(packageA, FrameworkConstants.CommonFrameworks.NetStandard20);
.Library(FrameworkConstants.CommonFrameworks.NetStandard20))
{
VerifyAssembly(packageRepository, packageA, FrameworkConstants.CommonFrameworks.Net45);
VerifyAssembly(packageRepository, packageA, FrameworkConstants.CommonFrameworks.NetStandard20);
}
}

[Fact]
public void MultipleLibrariesSameTargetFramework()
{
PackageRepository.Create(TestRootPath)
using (PackageRepository packageRepository = PackageRepository.Create(TestRootPath)
.Package("PackageA", "1.0.0", out PackageIdentity packageA)
.Library(FrameworkConstants.CommonFrameworks.Net45, filename: null)
.Library(FrameworkConstants.CommonFrameworks.Net45, filename: "CustomAssembly.dll");

VerifyAssembly(packageA, FrameworkConstants.CommonFrameworks.Net45);
VerifyAssembly(packageA, FrameworkConstants.CommonFrameworks.Net45, assemblyFileName: "CustomAssembly.dll");
.Library(FrameworkConstants.CommonFrameworks.Net45, filename: null)
.Library(FrameworkConstants.CommonFrameworks.Net45, filename: "CustomAssembly.dll"))
{
VerifyAssembly(packageRepository, packageA, FrameworkConstants.CommonFrameworks.Net45);
VerifyAssembly(packageRepository, packageA, FrameworkConstants.CommonFrameworks.Net45, assemblyFileName: "CustomAssembly.dll");
}
}

private void VerifyAssembly(PackageIdentity packageIdentity, NuGetFramework targetFramework, string assemblyFileName = null, string version = null)
private void VerifyAssembly(PackageRepository packageRepository, PackageIdentity packageIdentity, NuGetFramework targetFramework, string assemblyFileName = null, string version = null)
{
DirectoryInfo packageDirectory = new DirectoryInfo(((VersionFolderPathResolver)VersionFolderPathResolver).GetInstallPath(packageIdentity.Id, packageIdentity.Version))
DirectoryInfo packageDirectory = new DirectoryInfo(packageRepository.GetInstallPath(packageIdentity.Id, packageIdentity.Version))
.ShouldExist();

DirectoryInfo libDirectory = new DirectoryInfo(Path.Combine(packageDirectory.FullName, "lib", targetFramework.GetShortFolderName()))
Expand Down
Loading

0 comments on commit 656ccd2

Please sign in to comment.