Skip to content

Commit

Permalink
Merge pull request chocolatey#2944 from AdmiringWorm/source-logging
Browse files Browse the repository at this point in the history
(chocolatey#508) Change NuGet normal logging to be verbose logging
  • Loading branch information
gep13 committed Dec 21, 2022
2 parents 8ac5d78 + 4af6785 commit 0e6bca6
Show file tree
Hide file tree
Showing 8 changed files with 668 additions and 124 deletions.
5 changes: 5 additions & 0 deletions src/chocolatey.tests/MockLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public class MockLogger : Mock<ILog>, ILog, ILog<MockLogger>
{
public MockLogger()
{
LoggerNames = new HashSet<string>();
}

public void reset()
{
Messages.Clear();
this.ResetCalls();
LogMessagesToConsole = false;
LoggerNames.Clear();
}

public bool contains_message(string expectedMessage)
Expand Down Expand Up @@ -91,6 +93,8 @@ public int contains_message_count(string expectedMessage, LogLevel level)

private readonly Lazy<ConcurrentDictionary<string, IList<string>>> _messages = new Lazy<ConcurrentDictionary<string, IList<string>>>();

public HashSet<string> LoggerNames { get; private set; }

public ConcurrentDictionary<string, IList<string>> Messages
{
get { return _messages.Value; }
Expand All @@ -103,6 +107,7 @@ public IList<string> MessagesFor(LogLevel logLevel)

public void InitializeFor(string loggerName)
{
LoggerNames.Add(loggerName);
}

public void LogMessage(LogLevel logLevel, string message)
Expand Down
9 changes: 9 additions & 0 deletions src/chocolatey.tests/TinySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ public SourcePriorityAttribute()
{
}
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public sealed class LoggingAttribute : CategoryAttribute
{
public LoggingAttribute()
: base("Logging")
{
}
}
}

// ReSharper restore InconsistentNaming
Expand Down
25 changes: 25 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 @@ -153,6 +176,8 @@
<Compile Include="infrastructure.app\commands\ChocolateyUnpackSelfCommandSpecs.cs" />
<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();
}
}
}
}
Loading

0 comments on commit 0e6bca6

Please sign in to comment.