Skip to content

Commit

Permalink
(#508) Change project context to use nuget logger
Browse files Browse the repository at this point in the history
This commit updates the custom nuget project context to
reuse our custom logging implementation to get rid of some
duplication, and to prevent the need to change the logic in
multiple places if there is a need in the future.
  • Loading branch information
AdmiringWorm committed Dec 21, 2022
1 parent 104804b commit 08174d5
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 48 deletions.
24 changes: 24 additions & 0 deletions src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,49 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Chocolatey.NuGet.Commands, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Commands.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Commands.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.Common, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Common.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Common.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.Configuration, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Configuration.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Configuration.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.Credentials, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Credentials.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Credentials.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.DependencyResolver.Core, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.DependencyResolver.Core.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.DependencyResolver.Core.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.Frameworks, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Frameworks.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Frameworks.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.LibraryModel, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.LibraryModel.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.LibraryModel.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.PackageManagement, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.PackageManagement.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.PackageManagement.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.Packaging, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Packaging.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Packaging.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.ProjectModel, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.ProjectModel.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.ProjectModel.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.Protocol, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Protocol.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Protocol.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.Resolver, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Resolver.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Resolver.dll</HintPath>
</Reference>
<Reference Include="Chocolatey.NuGet.Versioning, Version=3.0.0.80, Culture=neutral, PublicKeyToken=fd112f53c3ab578c, processorArchitecture=MSIL">
<HintPath>..\packages\Chocolatey.NuGet.Versioning.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Versioning.dll</HintPath>
</Reference>
<Reference Include="log4net, Version=2.0.12.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.12\lib\net45\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Build" />
<Reference Include="Microsoft.Web.XmlTransform, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.Xdt.3.1.0\lib\net40\Microsoft.Web.XmlTransform.dll</HintPath>
</Reference>
Expand All @@ -117,6 +139,7 @@
<HintPath>..\packages\SimpleInjector.2.8.3\lib\net45\SimpleInjector.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IdentityModel" />
Expand Down Expand Up @@ -154,6 +177,7 @@
<Compile Include="infrastructure.app\commands\ChocolateyUpgradeCommandSpecs.cs" />
<Compile Include="infrastructure.app\configuration\ConfigurationOptionsSpec.cs" />
<Compile Include="infrastructure.app\nuget\ChocolateyNugetLoggerSpecs.cs" />
<Compile Include="infrastructure.app\nuget\ChocolateyNuGetProjectContextSpecs.cs" />
<Compile Include="infrastructure.app\nuget\NugetCommonSpecs.cs" />
<Compile Include="infrastructure.app\services\AutomaticUninstallerServiceSpecs.cs" />
<Compile Include="infrastructure.app\services\ChocolateyConfigSettingsServiceSpecs.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright © 2022-Present Chocolatey Software, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.tests.infrastructure.app.nuget
{
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.nuget;
using Moq;
using NuGet.Common;
using NuGet.ProjectManagement;
using NUnit.Framework;
using Should;

public class ChocolateyNuGetProjectContextSpecs
{
public abstract class ChocolateyNuGetProjectContextSpecsBase : TinySpec
{
protected ChocolateyConfiguration Configuration;
protected Mock<ILogger> Logger = new Mock<ILogger>();
protected ChocolateyNuGetProjectContext Service;

public override void Context()
{
Configuration = new ChocolateyConfiguration();
Service = new ChocolateyNuGetProjectContext(Configuration, Logger.Object);
}
}

[Categories.Logging, Parallelizable(ParallelScope.Self)]
public class when_calling_logging_methods_the_passed_in_logger_is_used : ChocolateyNuGetProjectContextSpecsBase
{
public override void Because()
{ }

public override void BeforeEachSpec()
{
base.BeforeEachSpec();

Logger.ResetCalls();
}

[Fact]
public void should_log_debug_information_in_child_logger()
{
Service.Log(MessageLevel.Debug, "Some {0} message", "DEBUG");

Logger.Verify(l => l.LogDebug("Some DEBUG message"), Times.Once);

// TODO: Uncomment once Moq is upgrade to v4.8 or later.
//Logger.VerifyNoOtherCalls();
}

[Fact]
public void should_log_error_information_in_child_logger()
{
Service.Log(MessageLevel.Error, "Some {0} message", "ERROR");

Logger.Verify(l => l.LogError("Some ERROR message"), Times.Once);

// TODO: Uncomment once Moq is upgrade to v4.8 or later.
//Logger.VerifyNoOtherCalls();
}

[Fact]
public void should_log_info_information_in_child_logger()
{
Service.Log(MessageLevel.Info, "Some {0} message", "INFO");

Logger.Verify(l => l.LogInformation("Some INFO message"), Times.Once);

// TODO: Uncomment once Moq is upgrade to v4.8 or later.
//Logger.VerifyNoOtherCalls();
}

[TestCase(LogLevel.Debug)]
public void should_log_to_child_logger_and_pass_along_original_message(LogLevel logLevel)
{
var logMessage = new LogMessage(logLevel, "My awesome message");

Service.Log(logMessage);

Logger.Verify(l => l.Log(logMessage), Times.Once);

// TODO: Uncomment once Moq is upgrade to v4.8 or later.
//Logger.VerifyNoOtherCalls();
}

[Fact]
public void should_log_warning_information_in_child_logger()
{
Service.Log(MessageLevel.Warning, "Some {0} message", "WARNING");

Logger.Verify(l => l.LogWarning("Some WARNING message"), Times.Once);

// TODO: Uncomment once Moq is upgrade to v4.8 or later.
//Logger.VerifyNoOtherCalls();
}

[Fact]
public void should_report_errors_to_child_logger()
{
Service.ReportError("Some kind of error!");

Logger.Verify(l => l.LogError("Some kind of error!"), Times.Once);

// TODO: Uncomment once Moq is upgrade to v4.8 or later.
//Logger.VerifyNoOtherCalls();
}

[Fact]
public void should_report_errors_with_message_to_child_logger()
{
var logMessage = new LogMessage(LogLevel.Debug, "Some message");

Service.ReportError(logMessage);

Logger.Verify(l => l.Log(logMessage), Times.Once);

// TODO: Uncomment once Moq is upgrade to v4.8 or later.
//Logger.VerifyNoOtherCalls();
}

[Fact]
public void should_report_warning_when_resolving_file_conflicts()
{
var message = "Some kind of message";

var result = Service.ResolveFileConflict(message);

result.ShouldEqual(FileConflictAction.OverwriteAll);

Logger.Verify(l => l.LogWarning("File conflict, overwriting all: Some kind of message"), Times.Once);

// TODO: Uncomment once Moq is upgrade to v4.8 or later.
//Logger.VerifyNoOtherCalls();
}
}
}
}
8 changes: 8 additions & 0 deletions src/chocolatey.tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Chocolatey.NuGet.Commands" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.Common" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.Configuration" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.Credentials" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.DependencyResolver.Core" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.Frameworks" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.LibraryModel" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.PackageManagement" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.Packaging" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.ProjectModel" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.Protocol" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.Resolver" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="Chocolatey.NuGet.Versioning" version="3.0.0-alpha-20221213-80" targetFramework="net48" />
<package id="log4net" version="2.0.12" targetFramework="net48" />
<package id="Microsoft.CSharp" version="4.3.0" targetFramework="net48" />
<package id="Microsoft.Web.Xdt" version="3.1.0" targetFramework="net48" />
<package id="Moq" version="4.2.1402.2112" targetFramework="net40" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace chocolatey.infrastructure.app.nuget

public class ChocolateyNuGetProjectContext : INuGetProjectContext
{
private readonly ILogger _logger;

public ChocolateyNuGetProjectContext(ChocolateyConfiguration config, ILogger logger)
{
//TODO, set client policy correctly here with settings, fix in chocolatey implementation of ISettings for this purpose
Expand All @@ -28,6 +30,7 @@ public ChocolateyNuGetProjectContext(ChocolateyConfiguration config, ILogger log
clientPolicyContext,
logger
);
_logger = logger;
}

private PackageExtractionContext _extractionContext;
Expand All @@ -37,78 +40,38 @@ public void Log(MessageLevel level, string message, params object[] args)
switch (level)
{
case MessageLevel.Debug:
this.Log().Debug("[NuGet] " + message, args);
_logger.LogDebug(message.format_with(args));
break;
case MessageLevel.Info:
this.Log().Info("[NuGet] " + message, args);
_logger.LogInformation(message.format_with(args));
break;
case MessageLevel.Warning:
this.Log().Warn("[NuGet] " + message, args);
_logger.LogWarning(message.format_with(args));
break;
case MessageLevel.Error:
this.Log().Error("[NuGet] " + message, args);
_logger.LogError(message.format_with(args));
break;
}
}

public void Log(ILogMessage message)
{
switch (message.Level)
{
case LogLevel.Debug:
this.Log().Debug("[NuGet] " + message.Message);
break;
case LogLevel.Warning:
this.Log().Warn("[NuGet] " + message.Message);
break;
case LogLevel.Error:
this.Log().Error("[NuGet] " + message.Message);
break;
case LogLevel.Verbose:
this.Log().Info(ChocolateyLoggers.Verbose, "[NuGet] " + message.Message);
break;
case LogLevel.Information:
this.Log().Info(ChocolateyLoggers.Verbose, "[NuGet] " + message.Message);
break;
case LogLevel.Minimal:
this.Log().Info("[NuGet] " + message.Message);
break;
}
_logger.Log(message);
}

public void ReportError(string message)
{
this.Log().Error("[NuGet] " + message);
_logger.LogError(message);
}

public void ReportError(ILogMessage message)
{
switch (message.Level)
{
case LogLevel.Debug:
this.Log().Debug("[NuGet] " + message.Message);
break;
case LogLevel.Warning:
this.Log().Warn("[NuGet] " + message.Message);
break;
case LogLevel.Error:
this.Log().Error("[NuGet] " + message.Message);
break;
case LogLevel.Verbose:
this.Log().Info(ChocolateyLoggers.Verbose, "[NuGet] " + message.Message);
break;
case LogLevel.Information:
this.Log().Info(ChocolateyLoggers.Verbose, "[NuGet] " + message.Message);
break;
case LogLevel.Minimal:
this.Log().Info("[NuGet] " + message.Message);
break;
}
_logger.Log(message);
}

public FileConflictAction ResolveFileConflict(string message)
{
this.Log().Warn("[NuGet] File conflict, overwriting all: " + message);
_logger.LogWarning("File conflict, overwriting all: {0}".format_with(message));
return FileConflictAction.OverwriteAll;
}

Expand Down

0 comments on commit 08174d5

Please sign in to comment.