This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for PackageDownload and PackageVersion (#986)
* Added support for PackageDownload and PackageVersion * reacted to initial pr feedback
- Loading branch information
1 parent
89f08d8
commit 0281fda
Showing
10 changed files
with
856 additions
and
31 deletions.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
....Inspection.Tests/RepositoryInspection/DirectoryBuildTargetsReaderPackageDownloadTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using NSubstitute; | ||
using NuGet.Versioning; | ||
using NuKeeper.Abstractions.Logging; | ||
using NuKeeper.Abstractions.RepositoryInspection; | ||
using NuKeeper.Inspection.RepositoryInspection; | ||
using NUnit.Framework; | ||
|
||
namespace NuKeeper.Inspection.Tests.RepositoryInspection | ||
{ | ||
[TestFixture] | ||
public class DirectoryBuildTargetsReaderPackageDownloadTests | ||
{ | ||
const string PackagesFileWithSinglePackage = | ||
@"<Project><ItemGroup><PackageDownload Include=""foo"" Version=""[1.2.3.4]"" /></ItemGroup></Project>"; | ||
|
||
private const string PackagesFileWithTwoPackages = @"<Project><ItemGroup> | ||
<PackageDownload Include=""foo"" Version=""[1.2.3.4]"" /> | ||
<PackageDownload Update=""bar"" Version=""[2.3.4.5]"" /></ItemGroup></Project>"; | ||
|
||
[Test] | ||
public void EmptyPackagesListShouldBeParsed() | ||
{ | ||
const string emptyContents = | ||
@"<Project/>"; | ||
|
||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(emptyContents), TempPath()); | ||
|
||
Assert.That(packages, Is.Not.Null); | ||
Assert.That(packages, Is.Empty); | ||
} | ||
|
||
[Test] | ||
public void SinglePackageShouldBeRead() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithSinglePackage), TempPath()); | ||
|
||
Assert.That(packages, Is.Not.Null); | ||
Assert.That(packages, Is.Not.Empty); | ||
} | ||
|
||
[Test] | ||
public void SinglePackageShouldBePopulated() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithSinglePackage), TempPath()); | ||
|
||
var package = packages.FirstOrDefault(); | ||
PackageAssert.IsPopulated(package); | ||
} | ||
|
||
[Test] | ||
public void SinglePackageFromVerboseFormatShouldBePopulated() | ||
{ | ||
const string verboseFormatVersion = | ||
@"<Project><ItemGroup><PackageDownload Include=""foo""><PrivateAssets>all</PrivateAssets><Version>1.2.3.4</Version></PackageDownload></ItemGroup></Project>"; | ||
|
||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(verboseFormatVersion), TempPath()); | ||
|
||
var package = packages.FirstOrDefault(); | ||
PackageAssert.IsPopulated(package); | ||
} | ||
|
||
[Test] | ||
public void SinglePackageShouldBeCorrect() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithSinglePackage), TempPath()); | ||
|
||
var package = packages.FirstOrDefault(); | ||
|
||
Assert.That(package, Is.Not.Null); | ||
Assert.That(package.Id, Is.EqualTo("foo")); | ||
Assert.That(package.Version, Is.EqualTo(new NuGetVersion("1.2.3.4"))); | ||
Assert.That(package.Path.PackageReferenceType, Is.EqualTo(PackageReferenceType.DirectoryBuildTargets)); | ||
} | ||
|
||
[Test] | ||
public void TwoPackagesShouldBePopulated() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithTwoPackages), TempPath()) | ||
.ToList(); | ||
|
||
Assert.That(packages, Is.Not.Null); | ||
Assert.That(packages.Count, Is.EqualTo(2)); | ||
|
||
PackageAssert.IsPopulated(packages[0]); | ||
PackageAssert.IsPopulated(packages[1]); | ||
} | ||
|
||
[Test] | ||
public void TwoPackagesShouldBeRead() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithTwoPackages), TempPath()) | ||
.ToList(); | ||
|
||
Assert.That(packages.Count, Is.EqualTo(2)); | ||
|
||
Assert.That(packages[0].Id, Is.EqualTo("foo")); | ||
Assert.That(packages[0].Version, Is.EqualTo(new NuGetVersion("1.2.3.4"))); | ||
|
||
Assert.That(packages[1].Id, Is.EqualTo("bar")); | ||
Assert.That(packages[1].Version, Is.EqualTo(new NuGetVersion("2.3.4.5"))); | ||
} | ||
|
||
[Test] | ||
public void ResultIsReiterable() | ||
{ | ||
var path = TempPath(); | ||
|
||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithTwoPackages), path); | ||
|
||
foreach (var package in packages) | ||
{ | ||
PackageAssert.IsPopulated(package); | ||
} | ||
|
||
Assert.That(packages.Select(p => p.Path), Is.All.EqualTo(path)); | ||
} | ||
|
||
[Test] | ||
public void WhenOnePackageCannotBeRead_TheOthersAreStillRead() | ||
{ | ||
var badVersion = PackagesFileWithTwoPackages.Replace("1.2.3.4", "notaversion", StringComparison.OrdinalIgnoreCase); | ||
|
||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(badVersion), TempPath()) | ||
.ToList(); | ||
|
||
Assert.That(packages.Count, Is.EqualTo(1)); | ||
PackageAssert.IsPopulated(packages[0]); | ||
} | ||
|
||
private static PackagePath TempPath() | ||
{ | ||
return new PackagePath( | ||
OsSpecifics.GenerateBaseDirectory(), | ||
Path.Combine("src", "Directory.Build.Props"), | ||
PackageReferenceType.DirectoryBuildTargets); | ||
} | ||
|
||
private static DirectoryBuildTargetsReader MakeReader() | ||
{ | ||
return new DirectoryBuildTargetsReader(Substitute.For<INuKeeperLogger>()); | ||
} | ||
|
||
private static Stream StreamFromString(string contents) | ||
{ | ||
return new MemoryStream(Encoding.UTF8.GetBytes(contents)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...r.Inspection.Tests/RepositoryInspection/DirectoryBuildTargetsReaderPackageVersionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using NSubstitute; | ||
using NuGet.Versioning; | ||
using NuKeeper.Abstractions.Logging; | ||
using NuKeeper.Abstractions.RepositoryInspection; | ||
using NuKeeper.Inspection.RepositoryInspection; | ||
using NUnit.Framework; | ||
|
||
namespace NuKeeper.Inspection.Tests.RepositoryInspection | ||
{ | ||
[TestFixture] | ||
public class DirectoryBuildTargetsReaderPackageVersionTests | ||
{ | ||
const string PackagesFileWithSinglePackage = | ||
@"<Project><ItemGroup><PackageVersion Include=""foo"" Version=""1.2.3.4"" /></ItemGroup></Project>"; | ||
|
||
private const string PackagesFileWithTwoPackages = @"<Project><ItemGroup> | ||
<PackageVersion Include=""foo"" Version=""1.2.3.4"" /> | ||
<PackageVersion Update=""bar"" Version=""2.3.4.5"" /></ItemGroup></Project>"; | ||
|
||
[Test] | ||
public void EmptyPackagesListShouldBeParsed() | ||
{ | ||
const string emptyContents = | ||
@"<Project/>"; | ||
|
||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(emptyContents), TempPath()); | ||
|
||
Assert.That(packages, Is.Not.Null); | ||
Assert.That(packages, Is.Empty); | ||
} | ||
|
||
[Test] | ||
public void SinglePackageShouldBeRead() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithSinglePackage), TempPath()); | ||
|
||
Assert.That(packages, Is.Not.Null); | ||
Assert.That(packages, Is.Not.Empty); | ||
} | ||
|
||
[Test] | ||
public void SinglePackageShouldBePopulated() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithSinglePackage), TempPath()); | ||
|
||
var package = packages.FirstOrDefault(); | ||
PackageAssert.IsPopulated(package); | ||
} | ||
|
||
[Test] | ||
public void SinglePackageFromVerboseFormatShouldBePopulated() | ||
{ | ||
const string verboseFormatVersion = | ||
@"<Project><ItemGroup><PackageVersion Include=""foo""><PrivateAssets>all</PrivateAssets><Version>1.2.3.4</Version></PackageVersion></ItemGroup></Project>"; | ||
|
||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(verboseFormatVersion), TempPath()); | ||
|
||
var package = packages.FirstOrDefault(); | ||
PackageAssert.IsPopulated(package); | ||
} | ||
|
||
[Test] | ||
public void SinglePackageShouldBeCorrect() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithSinglePackage), TempPath()); | ||
|
||
var package = packages.FirstOrDefault(); | ||
|
||
Assert.That(package, Is.Not.Null); | ||
Assert.That(package.Id, Is.EqualTo("foo")); | ||
Assert.That(package.Version, Is.EqualTo(new NuGetVersion("1.2.3.4"))); | ||
Assert.That(package.Path.PackageReferenceType, Is.EqualTo(PackageReferenceType.DirectoryBuildTargets)); | ||
} | ||
|
||
[Test] | ||
public void TwoPackagesShouldBePopulated() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithTwoPackages), TempPath()) | ||
.ToList(); | ||
|
||
Assert.That(packages, Is.Not.Null); | ||
Assert.That(packages.Count, Is.EqualTo(2)); | ||
|
||
PackageAssert.IsPopulated(packages[0]); | ||
PackageAssert.IsPopulated(packages[1]); | ||
} | ||
|
||
[Test] | ||
public void TwoPackagesShouldBeRead() | ||
{ | ||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithTwoPackages), TempPath()) | ||
.ToList(); | ||
|
||
Assert.That(packages.Count, Is.EqualTo(2)); | ||
|
||
Assert.That(packages[0].Id, Is.EqualTo("foo")); | ||
Assert.That(packages[0].Version, Is.EqualTo(new NuGetVersion("1.2.3.4"))); | ||
|
||
Assert.That(packages[1].Id, Is.EqualTo("bar")); | ||
Assert.That(packages[1].Version, Is.EqualTo(new NuGetVersion("2.3.4.5"))); | ||
} | ||
|
||
[Test] | ||
public void ResultIsReiterable() | ||
{ | ||
var path = TempPath(); | ||
|
||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(PackagesFileWithTwoPackages), path); | ||
|
||
foreach (var package in packages) | ||
{ | ||
PackageAssert.IsPopulated(package); | ||
} | ||
|
||
Assert.That(packages.Select(p => p.Path), Is.All.EqualTo(path)); | ||
} | ||
|
||
[Test] | ||
public void WhenOnePackageCannotBeRead_TheOthersAreStillRead() | ||
{ | ||
var badVersion = PackagesFileWithTwoPackages.Replace("1.2.3.4", "notaversion", StringComparison.OrdinalIgnoreCase); | ||
|
||
var reader = MakeReader(); | ||
var packages = reader.Read(StreamFromString(badVersion), TempPath()) | ||
.ToList(); | ||
|
||
Assert.That(packages.Count, Is.EqualTo(1)); | ||
PackageAssert.IsPopulated(packages[0]); | ||
} | ||
|
||
private static PackagePath TempPath() | ||
{ | ||
return new PackagePath( | ||
OsSpecifics.GenerateBaseDirectory(), | ||
Path.Combine("src", "Directory.Packages.Props"), | ||
PackageReferenceType.DirectoryBuildTargets); | ||
} | ||
|
||
private static DirectoryBuildTargetsReader MakeReader() | ||
{ | ||
return new DirectoryBuildTargetsReader(Substitute.For<INuKeeperLogger>()); | ||
} | ||
|
||
private static Stream StreamFromString(string contents) | ||
{ | ||
return new MemoryStream(Encoding.UTF8.GetBytes(contents)); | ||
} | ||
} | ||
} |
Oops, something went wrong.