diff --git a/src/chocolatey.tests/chocolatey.tests.csproj b/src/chocolatey.tests/chocolatey.tests.csproj index 259b700e35..54958b3368 100644 --- a/src/chocolatey.tests/chocolatey.tests.csproj +++ b/src/chocolatey.tests/chocolatey.tests.csproj @@ -77,27 +77,49 @@ MinimumRecommendedRules.ruleset + + ..\packages\Chocolatey.NuGet.Commands.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Commands.dll + ..\packages\Chocolatey.NuGet.Common.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Common.dll ..\packages\Chocolatey.NuGet.Configuration.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Configuration.dll + + ..\packages\Chocolatey.NuGet.Credentials.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Credentials.dll + + + ..\packages\Chocolatey.NuGet.DependencyResolver.Core.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.DependencyResolver.Core.dll + ..\packages\Chocolatey.NuGet.Frameworks.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Frameworks.dll + + ..\packages\Chocolatey.NuGet.LibraryModel.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.LibraryModel.dll + + + ..\packages\Chocolatey.NuGet.PackageManagement.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.PackageManagement.dll + ..\packages\Chocolatey.NuGet.Packaging.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Packaging.dll + + ..\packages\Chocolatey.NuGet.ProjectModel.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.ProjectModel.dll + ..\packages\Chocolatey.NuGet.Protocol.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Protocol.dll + + ..\packages\Chocolatey.NuGet.Resolver.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Resolver.dll + ..\packages\Chocolatey.NuGet.Versioning.3.0.0-alpha-20221213-80\lib\net472\Chocolatey.NuGet.Versioning.dll ..\packages\log4net.2.0.12\lib\net45\log4net.dll + ..\packages\Microsoft.Web.Xdt.3.1.0\lib\net40\Microsoft.Web.XmlTransform.dll @@ -117,6 +139,7 @@ ..\packages\SimpleInjector.2.8.3\lib\net45\SimpleInjector.dll + @@ -154,6 +177,7 @@ + diff --git a/src/chocolatey.tests/infrastructure.app/nuget/ChocolateyNuGetProjectContextSpecs.cs b/src/chocolatey.tests/infrastructure.app/nuget/ChocolateyNuGetProjectContextSpecs.cs new file mode 100644 index 0000000000..4b6b8d8432 --- /dev/null +++ b/src/chocolatey.tests/infrastructure.app/nuget/ChocolateyNuGetProjectContextSpecs.cs @@ -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 Logger = new Mock(); + 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(); + } + } + } +} diff --git a/src/chocolatey.tests/packages.config b/src/chocolatey.tests/packages.config index 7eda096783..30e7907527 100644 --- a/src/chocolatey.tests/packages.config +++ b/src/chocolatey.tests/packages.config @@ -1,12 +1,20 @@  + + + + + + + + diff --git a/src/chocolatey/infrastructure.app/nuget/ChocolateyNuGetProjectContext.cs b/src/chocolatey/infrastructure.app/nuget/ChocolateyNuGetProjectContext.cs index b69cdef48f..b50e293d6c 100644 --- a/src/chocolatey/infrastructure.app/nuget/ChocolateyNuGetProjectContext.cs +++ b/src/chocolatey/infrastructure.app/nuget/ChocolateyNuGetProjectContext.cs @@ -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 @@ -28,6 +30,7 @@ public ChocolateyNuGetProjectContext(ChocolateyConfiguration config, ILogger log clientPolicyContext, logger ); + _logger = logger; } private PackageExtractionContext _extractionContext; @@ -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; }