Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix system packages not being found #121

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class FindPackageReferenceArtifactTests
[InlineData("abc", "1.33.7", false)]
public async Task Finds_Package_Reference_Artifact(string packageName, string packageVersion, bool expectedOutcome)
{
var fileSystem = new FileSystem();
var logger = new Logging.Null.NullLogger();
var cliExecutor = new DefaultCliToolExecutor(logger);
var provideNugetCaches = new ProvideNugetCacheDirectories(cliExecutor);
var fileSystem = new FileSystem();
var provideNugetCaches = new ProvideNugetCacheDirectories(cliExecutor, fileSystem);

var findPackageReferenceArtifact = new FindPackageReferenceArtifact(provideNugetCaches, fileSystem, logger);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Liz.Core.CliTool.Contracts;
using Liz.Core.Utils;
using Moq;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using Xunit;

namespace Liz.Core.Tests.Utils;
Expand All @@ -13,6 +15,8 @@ public class ProvideNugetCacheDirectoriesTests
public async Task Provides_Cache_Directories()
{
var context = ArrangeContext<ProvideNugetCacheDirectories>.Create();
context.Use<IFileSystem>(new MockFileSystem());

var sut = context.Build();

const string expectedDirectory = @"C:\Users\some-user\.nuget\packages\";
Expand All @@ -26,7 +30,7 @@ public async Task Provides_Cache_Directories()

result
.Should()
.OnlyContain(directory => directory == expectedDirectory);
.Contain(directory => directory == expectedDirectory);

// getting the directory should be cached, so a second call should just return the cache
_ = await sut.GetAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Liz.Core/ExtractLicensesFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public IExtractLicenses Create(
new ExportLicenseTextsResultProcessor(settings, fileSystem)
};

var provideNugetCacheDirectories = new ProvideNugetCacheDirectories(cliToolExecutor);
var provideNugetCacheDirectories = new ProvideNugetCacheDirectories(cliToolExecutor, fileSystem);
var findPackageReferenceArtifact = new FindPackageReferenceArtifact(
provideNugetCacheDirectories,
fileSystem,
Expand Down
26 changes: 24 additions & 2 deletions src/Core/Liz.Core/Utils/ProvideNugetCacheDirectories.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using Liz.Core.CliTool.Contracts;
using Liz.Core.Utils.Contracts;
using System.IO.Abstractions;

namespace Liz.Core.Utils;

internal sealed class ProvideNugetCacheDirectories : IProvideNugetCacheDirectories
{
private readonly ICliToolExecutor _cliToolExecutor;
private readonly IFileSystem _fileSystem;

private IEnumerable<string>? _nugetCacheDirectories;

public ProvideNugetCacheDirectories(
ICliToolExecutor cliToolExecutor)
ICliToolExecutor cliToolExecutor,
IFileSystem fileSystem)
{
_cliToolExecutor = cliToolExecutor ?? throw new ArgumentNullException(nameof(cliToolExecutor));
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
}

public async Task<IEnumerable<string>> GetAsync()
Expand All @@ -22,6 +26,16 @@ public async Task<IEnumerable<string>> GetAsync()
}

private async Task<IEnumerable<string>> GetCacheDirectoriesAsync()
{
var cacheDirectories = await GetNugetLocalsGlobalPackagesAsync();

if (TryGetNugetFallbackFolder(out var nugetFallbackFolder))
cacheDirectories.Add(nugetFallbackFolder);

return cacheDirectories;
}

private async Task<IList<string>> GetNugetLocalsGlobalPackagesAsync()
{
// refer to https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-locals
var result = await _cliToolExecutor
Expand All @@ -32,7 +46,15 @@ private async Task<IEnumerable<string>> GetCacheDirectoriesAsync()
return cacheDirectories;
}

private static IEnumerable<string> ParseCliOutput(string cliOutput)
private bool TryGetNugetFallbackFolder(out string nugetFallbackFolder)
{
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
nugetFallbackFolder = _fileSystem.Path.Combine(programFiles, "dotnet", "sdk", "NuGetFallbackFolder");

return _fileSystem.Directory.Exists(nugetFallbackFolder);
}

private static IList<string> ParseCliOutput(string cliOutput)
{
const string searchString = "global-packages: ";

Expand Down