Skip to content

Commit

Permalink
Merge pull request #487 from tonyhallett/improve-logging
Browse files Browse the repository at this point in the history
Improve logging
  • Loading branch information
tonyhallett authored Mar 3, 2025
2 parents 0ad38f0 + 1aca674 commit 3673239
Show file tree
Hide file tree
Showing 48 changed files with 295 additions and 231 deletions.
2 changes: 2 additions & 0 deletions FineCodeCoverageTests/AppOptionsProvider_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using AutoMoq;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;
using Microsoft.VisualStudio.Settings;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -199,6 +200,7 @@ public void Should_Not_Default_Any_Other_AppOptions_Properties()
nameof(IAppOptions.ThresholdForCrapScore),
nameof(IAppOptions.ThresholdForNPathComplexity),
nameof(IAppOptions.ThresholdForCyclomaticComplexity),
nameof(IAppOptions.RunMsCodeCoverage),
nameof(IAppOptions.NamespacedClasses),
nameof(IAppOptions.ShowCoverageInOverviewMargin),
nameof(IAppOptions.ShowCoveredInOverviewMargin),
Expand Down
1 change: 1 addition & 0 deletions FineCodeCoverageTests/CoverageProject_Settings_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;
using FineCodeCoverageTests.TestHelpers;
using Moq;
using NUnit.Framework;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Engine;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Output;
using Moq;
using NUnit.Framework;
using SharedProject.Core.CoverageToolOutput;
Expand Down
1 change: 1 addition & 0 deletions FineCodeCoverageTests/CoverletConsole_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using FineCodeCoverage.Engine.Coverlet;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;
using Moq;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using FineCodeCoverage.Engine.Coverlet;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;
using Moq;
using NUnit.Framework;

Expand Down Expand Up @@ -279,7 +280,7 @@ public async Task Should_Use_The_ProcessResponseProcessor_Async()
mockProcesUtil.Setup(p => p.ExecuteAsync(It.IsAny<ExecuteRequest>(), ct).Result).Returns(executeResponse);
var mockProcessResponseProcessor = mocker.GetMock<IProcessResponseProcessor>();

var logTitle = "Coverlet Collector Run (TestProject)";
var logTitle = "Coverlet Collector Run (TestProject) - Output";
mockProcessResponseProcessor.Setup(rp => rp.Process(executeResponse, It.IsAny<Func<int, bool>>(), true, logTitle, It.IsAny<Action>()));

await coverletDataCollectorUtil.RunAsync(ct);
Expand Down
1 change: 1 addition & 0 deletions FineCodeCoverageTests/DotNetToolList_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AutoMoq;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Engine.Coverlet;
using FineCodeCoverage.Output;
using NUnit.Framework;

namespace FineCodeCoverageTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Impl;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FineCodeCoverage.Editor.DynamicCoverage;
using FineCodeCoverage.Editor.DynamicCoverage.TrackedLinesImpl.Construction;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Output;
using FineCodeCoverageTests.TestHelpers;
using Microsoft.VisualStudio.Text;
using Moq;
Expand Down
10 changes: 2 additions & 8 deletions FineCodeCoverageTests/FCCEngine_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Engine.MsTestPlatform;
using FineCodeCoverage.Engine.ReportGenerator;
using FineCodeCoverage.Impl;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;
using Moq;
Expand Down Expand Up @@ -105,13 +106,6 @@ public void SetUp()
mockAppOptionsProvider.Setup(x => x.Get()).Returns(mockedAppOptions.Object);
}

[Test]
public async Task Should_Log_Starting_When_Initialized_Async()
{
await ReloadInitializedCoverage_Async();
VerifyLogsReloadCoverageStatus(ReloadCoverageStatus.Start);
}

[Test]
public async Task Should_Prepare_For_Coverage_Suitable_CoverageProjects_Async()
{
Expand Down Expand Up @@ -319,7 +313,7 @@ public async Task Should_Cancel_Existing_ReloadCoverage_When_ReloadCoverage_Asyn

private void VerifyLogsReloadCoverageStatus(ReloadCoverageStatus reloadCoverageStatus)
{
mocker.Verify<ILogger>(l => l.Log(fccEngine.GetLogReloadCoverageStatusMessage(reloadCoverageStatus)));
mocker.Verify<ILogger>(l => l.Log(StatusMarkerProvider.Get(reloadCoverageStatus.ToString())));
}

private async Task StopCoverage_Async()
Expand Down
1 change: 1 addition & 0 deletions FineCodeCoverageTests/Initializer_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using FineCodeCoverage.Core.Initialization;
using FineCodeCoverage.Engine;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Output;
using Moq;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace FineCodeCoverageTests.MsCodeCoverage
{

internal class MsCodeCoverageRunSettingsService_Test_Execution_Not_Finished_Tests
{
[Test]
Expand Down Expand Up @@ -126,7 +125,7 @@ public async Task Should_Not_Throw_If_No_Results_Async()
public async Task Should_Combined_Log_When_No_Cobertura_Files_Async()
{
await RunAndProcessReportAsync(null, Array.Empty<string>());
autoMocker.Verify<ILogger>(logger => logger.Log("No cobertura files for ms code coverage."));
autoMocker.Verify<FineCodeCoverage.Output.ILogger>(logger => logger.Log("No cobertura files for ms code coverage."));
autoMocker.Verify<IReportGeneratorUtil>(
reportGenerator => reportGenerator.LogCoverageProcess("No cobertura files for ms code coverage.")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using FineCodeCoverage.Core.Utilities;
using System.IO;
using System;
using FineCodeCoverage.Output;

namespace FineCodeCoverageTests.MsCodeCoverage
{
Expand Down
1 change: 1 addition & 0 deletions FineCodeCoverageTests/ProcessResponseProcessor_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using AutoMoq;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Output;
using NUnit.Framework;

namespace Test
Expand Down
44 changes: 39 additions & 5 deletions FineCodeCoverageTests/TestContainerDiscovery_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
using FineCodeCoverage.Engine.MsTestPlatform.CodeCoverage;
using FineCodeCoverage.Impl;
using FineCodeCoverage.Options;
using Microsoft.Build.Evaluation;
using Microsoft.VisualStudio.TestWindow.Extensibility;
using Moq;
using NUnit.Framework;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Test
{

internal class TestOperationStateInvocationManager_Tests
{
private AutoMoqer mocker;
Expand Down Expand Up @@ -60,7 +57,7 @@ public void Should_Return_True_For_All_Other_States_If_Was_Initialized_When_Test
public void Should_Log_When_Cannot_Invoke(TestOperationStates testOperationState)
{
testOperationStateInvocationManager.CanInvoke(testOperationState);
mocker.Verify<ILogger>(logger => logger.Log($"Skipping {testOperationState} as FCC not initialized"));
mocker.Verify<FineCodeCoverage.Output.ILogger>(logger => logger.Log($"Skipping {testOperationState} as FCC not initialized"));
}

}
Expand Down Expand Up @@ -379,7 +376,7 @@ public void Should_Handle_Any_Exception_In_OperationState_Changed_Handler_Loggin
var exception = new Exception();
mocker.GetMock<IFCCEngine>().Setup(engine => engine.StopCoverage()).Throws(exception);
RaiseTestExecutionCancelling();
mocker.Verify<ILogger>(logger => logger.Log("Error processing unit test events", exception));
mocker.Verify<FineCodeCoverage.Output.ILogger>(logger => logger.Log("Error processing unit test events", exception));
}

[TestCase(true)]
Expand Down Expand Up @@ -429,5 +426,42 @@ public void Should_MsCodeCoverageRunSettingsService_TestExecutionNotFinishedAsyn
mockMsCodeCoverageRunSettingsService.Verify(msCodeCoverageRunSettingsService => msCodeCoverageRunSettingsService.TestExecutionNotFinishedAsync(mockTestOperation.Object));

}

[Test]
public void Should_Log_Coverage_Starting_With_Run_Number_When_TestExecutionStartingAsync_And_Coverage_Not_Disabled()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(true);
});

var operation = new Mock<IOperation>().Object;
RaiseTestExecutionStarting(operation);

mocker.Verify<FineCodeCoverage.Output.ILogger>(
logger => logger.Log("================================== COVERAGE STARTING - 1 =================================="));

RaiseTestExecutionStarting(operation);

mocker.Verify<FineCodeCoverage.Output.ILogger>(
logger => logger.Log("================================== COVERAGE STARTING - 2 =================================="));
}

[Test]
public void Should_Not_Log_Coverage_Starting_When_Coverage_Disabled()
{
SetUpOptions(mockAppOptions =>
{
mockAppOptions.Setup(o => o.Enabled).Returns(false);
mockAppOptions.Setup(o => o.DisabledNoCoverage).Returns(true);
});

var operation = new Mock<IOperation>().Object;
RaiseTestExecutionStarting(operation);

mocker.Verify<FineCodeCoverage.Output.ILogger>(
logger => logger.Log("================================== COVERAGE STARTING - 1 =================================="), Times.Never());

}
}
}
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ When not using Microsoft.TestingPlatform you have added test adapters through nu

Fine Code Coverage provides code coverage using one of 3 different coverage tools. In previous releases there were two coverage tools being utilised, OpenCover and Coverlet that will be referred to as 'old coverage'.
Microsoft now provides a free coverage solution that you can choose to use by setting the Visual Studio Fine Code Coverage enumeration option RunMsCodeCoverage. This will probably be the preferred coverage
tool for most developers. It is currently in Beta.
tool for most developers.

With the old coverage it was possible for FCC to provide an abstraction over each tool's exclusion / inclusion options. This abstraction does not work for MS code coverage.
Thus you will find that there are separate configuration options for Ms coverage vs old coverage and options that are common to the two.
Expand Down Expand Up @@ -109,8 +109,6 @@ The old coverage was based upon every test. Ms code coverage is coverage from th

## How to utilize MS Code Coverage with FCC ?

Firstly you need to change the RunMsCodeCoverage option from No.

Ms code coverage requires a [runsettings](https://docs.microsoft.com/en-us/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=vs-2022) file that is configured appropriately for
code coverage. This requires that you have the ms code coverage package and have pointed to it with the TestAdaptersPaths element as well as specifying the ms data collector. [Exclusions and inclusions](https://docs.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2022#include-or-exclude-assemblies-and-members)
are also specified in the runsettings. I don't think that the documentation is clear enough on how this works so you may want to look at [this issue](https://github.com/microsoft/vstest/issues/3462).
Expand Down Expand Up @@ -349,7 +347,7 @@ If you are using option 1) then project and global options will only be used whe
| DisabledNoCoverage | Set to false for VS Option Enabled=false to not disable coverage |
| RunWhenTestsFail | By default coverage runs when tests fail. Set to false to prevent this. **Cannot be used in conjunction with RunInParallel** |
| RunWhenTestsExceed | Specify a value to only run coverage based upon the number of executing tests. **Cannot be used in conjunction with RunInParallel** |
| RunMsCodeCoverage | Change to IfInRunSettings to only collect with configured runsettings. Yes for runsettings generation. |
| RunMsCodeCoverage | Change to IfInRunSettings to only collect with configured runsettings. Yes (default) for runsettings generation. No to use Coverlet or OpenCover. |
| IncludeTestAssembly | Specifies whether to report code coverage of the test assembly |
| IncludeReferencedProjects | Set to true to add all directly referenced projects to Include. |
| IncludeAssemblies | Provide a list of assemblies to include in coverage. The dll name without extension is used for matching. |
Expand Down
1 change: 1 addition & 0 deletions SharedProject/Core/AppDataFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Threading;
using FineCodeCoverage.Options;
using FineCodeCoverage.Output;

namespace FineCodeCoverage.Engine
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Output;
using SharedProject.Core.CoverageToolOutput;

namespace FineCodeCoverage.Engine
Expand Down
5 changes: 1 addition & 4 deletions SharedProject/Core/CoverageUtilManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition;
using System.Threading;
using System.Threading.Tasks;
using FineCodeCoverage.Engine.Coverlet;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Engine.OpenCover;
using FineCodeCoverage.Engine.ReportGenerator;

namespace FineCodeCoverage.Engine
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.Composition;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Output;

namespace FineCodeCoverage.Engine.Coverlet
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.Composition;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Output;

namespace FineCodeCoverage.Engine.Coverlet
{
Expand Down
1 change: 1 addition & 0 deletions SharedProject/Core/Coverlet/Console/CoverletConsoleUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Output;

namespace FineCodeCoverage.Engine.Coverlet
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel.Composition;
using System.Linq;
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Output;

namespace FineCodeCoverage.Engine.Coverlet
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using FineCodeCoverage.Core.Utilities;
using FineCodeCoverage.Core.Utilities.VsThreading;
using FineCodeCoverage.Engine.Model;
using FineCodeCoverage.Output;
using Microsoft.VisualStudio.Shell;
using Task = System.Threading.Tasks.Task;

Expand Down Expand Up @@ -259,7 +260,7 @@ public async Task RunAsync(CancellationToken cancellationToken)
// https://github.com/dotnet/sdk/blob/936935f18c3540ed77c97e392780a9dd82aca441/src/Cli/dotnet/commands/dotnet-test/Program.cs#L86

// test failure has exit code 1
processResponseProcessor.Process(result, code => code == 0 || code == 1, true, GetLogTitle(), () =>
processResponseProcessor.Process(result, code => code == 0 || code == 1, true, $"{GetLogTitle()} - Output", () =>
{
coverletDataCollectorGeneratedCobertura.CorrectPath(coverageProject.CoverageOutputFolder, coverageProject.CoverageOutputFile);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using FineCodeCoverage.Options;

namespace FineCodeCoverage.Engine.Coverlet
{
Expand All @@ -14,12 +11,7 @@ internal class DataCollectorSettingsBuilder : IDataCollectorSettingsBuilder
private string generatedRunSettingsPath;
private string existingRunSettings;
private bool runSettingsOnly;
private readonly ILogger logger;

public DataCollectorSettingsBuilder(ILogger logger)
{
this.logger = logger;
}
#region Arguments
internal string ProjectDll { get; set; }
internal string Blame { get; set; }
Expand Down Expand Up @@ -72,7 +64,6 @@ public string Build()
private void GenerateRunSettings()
{
var runSettingsDocument = existingRunSettings == null ? GenerateFullRunSettings() : GenerateRunSettingsFromExisting();
logger.Log($"Saving run settings to {generatedRunSettingsPath}");
runSettingsDocument.Save(generatedRunSettingsPath);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
using System.ComponentModel.Composition;
using FineCodeCoverage.Options;

namespace FineCodeCoverage.Engine.Coverlet
{
[Export(typeof(IDataCollectorSettingsBuilderFactory))]
internal class DataCollectorSettingsBuilderFactory : IDataCollectorSettingsBuilderFactory
{
private readonly ILogger logger;

[ImportingConstructor]
public DataCollectorSettingsBuilderFactory(ILogger logger)
{
this.logger = logger;
}
public IDataCollectorSettingsBuilder Create()
{
return new DataCollectorSettingsBuilder(logger);
return new DataCollectorSettingsBuilder();
}
}
}
Loading

0 comments on commit 3673239

Please sign in to comment.