Skip to content

Commit

Permalink
fix: fix package download when CPM is enabled (#119)
Browse files Browse the repository at this point in the history
* fix: fix problem when downloading packages and CPM is enabled

Closes #118

* test: add tests for new CheckCpmPreprocessor

* feat: redo download packages when CPM enabled
  • Loading branch information
wgnf committed Nov 18, 2022
1 parent 4f47d0e commit 61edea0
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Liz.Core.PackageReferences;
using Liz.Core.PackageReferences.Contracts.DotnetCli;
using Liz.Core.PackageReferences.Contracts.Models;
using Liz.Core.Preparation.Contracts.Models;
using Liz.Core.Utils.Contracts;
using Moq;
using System.IO.Abstractions;
Expand All @@ -16,7 +17,7 @@ public class DownloadPackageReferencesFacadeTests
[Fact]
public async Task DownloadAndEnrich_Fails_On_Invalid_Parameters()
{
var context = ArrangeContext<DownloadPackageReferencesFacade>.Create();
var context = CreateContext();
var sut = context.Build();

await Assert.ThrowsAsync<ArgumentNullException>(() => sut.DownloadAndEnrichAsync(null!));
Expand All @@ -27,7 +28,7 @@ public async Task DownloadAndEnrich_Does_Nothing_When_There_Are_No_Packages()
{
var mockFileSystem = new MockFileSystem();

var context = ArrangeContext<DownloadPackageReferencesFacade>.Create();
var context = CreateContext();
context.Use<IFileSystem>(mockFileSystem);

var sut = context.Build();
Expand Down Expand Up @@ -57,7 +58,7 @@ public async Task DownloadAndEnrich_Creates_Download_Directory()
{
var mockFileSystem = new MockFileSystem();

var context = ArrangeContext<DownloadPackageReferencesFacade>.Create();
var context = CreateContext();
context.Use<IFileSystem>(mockFileSystem);

var sut = context.Build();
Expand All @@ -82,12 +83,15 @@ public async Task DownloadAndEnrich_Creates_Download_Directory()
.NotBeEmpty();
}

[Fact]
public async Task DownloadAndEnrich()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task DownloadAndEnrich(bool isCpmEnabled)
{
var mockFileSystem = new MockFileSystem();

var context = ArrangeContext<DownloadPackageReferencesFacade>.Create();
context.Use(new SourceInfo { IsCpmEnabled = isCpmEnabled });
context.Use<IFileSystem>(mockFileSystem);

const string downloadDirectory = "C:/temp/download";
Expand Down Expand Up @@ -129,4 +133,13 @@ public async Task DownloadAndEnrich()
.Should()
.Contain(packageReference => packageReference.ArtifactDirectory != null);
}

private static ArrangeContext<DownloadPackageReferencesFacade> CreateContext()
{
var context = ArrangeContext<DownloadPackageReferencesFacade>.Create();

context.Use(new SourceInfo());

return context;
}
}
210 changes: 210 additions & 0 deletions src/Core/Liz.Core.Tests/Preparation/CheckCpmPreprocessorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using ArrangeContext.Moq;
using FluentAssertions;
using Liz.Core.Preparation;
using Liz.Core.Preparation.Contracts.Models;
using Liz.Core.Settings;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using Xunit;

namespace Liz.Core.Tests.Preparation;

public sealed class CheckCpmPreprocessorTests
{
[Fact]
public async Task Gets_Not_Enabled_When_No_Props_File()
{
const string targetFile = "C:/some/directory/target.file";
var sourceInfo = new SourceInfo();
var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ targetFile, new MockFileData("") }
});

var context = ArrangeContext<CheckCpmPreprocessor>.Create();
context.Use(sourceInfo);
context.Use<IFileSystem>(mockFileSystem);

context
.For<ExtractLicensesSettingsBase>()
.Setup(settings => settings.GetTargetFile())
.Returns(targetFile);

var sut = context.Build();

await sut.PreprocessAsync();

sourceInfo
.IsCpmEnabled
.Should()
.BeFalse();
}

[Fact]
public async Task Gets_Not_Enabled_When_No_Enabling_Element()
{
const string propsFileContent = @"
<Project>
<ItemGroup>
<PackageVersion Include=""NewtonSoft.Json"" Version=""1.3.3"" />
</ItemGroup>
</Project>";

const string targetFile = "C:/some/directory/target.file";
var sourceInfo = new SourceInfo();
var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ targetFile, new MockFileData("") },
{ "C:/some/Directory.Packages.props", new MockFileData(propsFileContent) }
});

var context = ArrangeContext<CheckCpmPreprocessor>.Create();
context.Use(sourceInfo);
context.Use<IFileSystem>(mockFileSystem);

context
.For<ExtractLicensesSettingsBase>()
.Setup(settings => settings.GetTargetFile())
.Returns(targetFile);

var sut = context.Build();

await sut.PreprocessAsync();

sourceInfo
.IsCpmEnabled
.Should()
.BeFalse();
}

[Fact]
public async Task Gets_Not_Enabled_When_Enabling_Elements_Value_Is_False()
{
const string propsFileContent = @"
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include=""NewtonSoft.Json"" Version=""1.3.3"" />
</ItemGroup>
</Project>";

const string targetFile = "C:/some/directory/target.file";
var sourceInfo = new SourceInfo();
var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ targetFile, new MockFileData("") },
{ "C:/some/Directory.Packages.props", new MockFileData(propsFileContent) }
});

var context = ArrangeContext<CheckCpmPreprocessor>.Create();
context.Use(sourceInfo);
context.Use<IFileSystem>(mockFileSystem);

context
.For<ExtractLicensesSettingsBase>()
.Setup(settings => settings.GetTargetFile())
.Returns(targetFile);

var sut = context.Build();

await sut.PreprocessAsync();

sourceInfo
.IsCpmEnabled
.Should()
.BeFalse();
}

[Fact]
public async Task Gets_Enabled_When_Enabling_Elements_Value_Is_True()
{
const string propsFileContent = @"
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include=""NewtonSoft.Json"" Version=""1.3.3"" />
</ItemGroup>
</Project>";

const string targetFile = "C:/some/directory/target.file";
var sourceInfo = new SourceInfo();
var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ targetFile, new MockFileData("") },
{ "C:/some/Directory.Packages.props", new MockFileData(propsFileContent) }
});

var context = ArrangeContext<CheckCpmPreprocessor>.Create();
context.Use(sourceInfo);
context.Use<IFileSystem>(mockFileSystem);

context
.For<ExtractLicensesSettingsBase>()
.Setup(settings => settings.GetTargetFile())
.Returns(targetFile);

var sut = context.Build();

await sut.PreprocessAsync();

sourceInfo
.IsCpmEnabled
.Should()
.BeTrue();
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Gets_Enabled_Status_From_Imported_File(bool enabledInImportedFile)
{
const string propsFileContent = @"
<Project>
<Import Project=""../Directory.Packages.props""/>
<ItemGroup>
<PackageVersion Include=""NewtonSoft.Json"" Version=""1.3.3"" />
</ItemGroup>
</Project>";

var importedPropsFileContent = $@"
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>{enabledInImportedFile}</ManagePackageVersionsCentrally>
</PropertyGroup>
</Project>";

const string targetFile = "C:/some/directory/target.file";
var sourceInfo = new SourceInfo();
var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ targetFile, new MockFileData("") },
{ "C:/some/Directory.Packages.props", new MockFileData(propsFileContent) },
{ "C:/Directory.Packages.props", new MockFileData(importedPropsFileContent) }
});

var context = ArrangeContext<CheckCpmPreprocessor>.Create();
context.Use(sourceInfo);
context.Use<IFileSystem>(mockFileSystem);

context
.For<ExtractLicensesSettingsBase>()
.Setup(settings => settings.GetTargetFile())
.Returns(targetFile);

var sut = context.Build();

await sut.PreprocessAsync();

sourceInfo
.IsCpmEnabled
.Should()
.Be(enabledInImportedFile);
}
}
5 changes: 5 additions & 0 deletions src/Core/Liz.Core/ExtractLicensesFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Liz.Core.PackageReferences.NuGetCli;
using Liz.Core.Preparation;
using Liz.Core.Preparation.Contracts;
using Liz.Core.Preparation.Contracts.Models;
using Liz.Core.Progress;
using Liz.Core.Projects;
using Liz.Core.Result;
Expand All @@ -38,6 +39,8 @@ public IExtractLicenses Create(
IProgressHandler? progressHandler = null)
{
settings.EnsureValidity();

var sourceInfo = new SourceInfo();

var logger = GetLogger(logLevel, loggerProvider);
var fileSystem = new FileSystem();
Expand Down Expand Up @@ -120,6 +123,7 @@ public IExtractLicenses Create(
var downloadPackageReferencesViaDotnetCli = new DownloadPackageReferencesViaDotnetCli(cliToolExecutor);

var downloadPackageReferencesFacade = new DownloadPackageReferencesFacade(
sourceInfo,
provideTemporaryDirectories,
logger,
downloadPackageReferencesViaDotnetCli,
Expand All @@ -137,6 +141,7 @@ public IExtractLicenses Create(

var preprocessors = new IPreprocessor[]
{
new CheckCpmPreprocessor(sourceInfo, settings, logger, fileSystem),
new DeserializeLicenseTypeDefinitionsPreprocessor(settings, logger, fileContentProvider),
new DeserializeUrlToLicenseTypeMappingPreprocessor(settings, logger, fileContentProvider),
new DeserializeLicenseTypeWhitelistPreprocessor(settings, logger, fileContentProvider),
Expand Down
Loading

0 comments on commit 61edea0

Please sign in to comment.