Skip to content

Commit e67ba69

Browse files
Joseph SunJosephSun2003
Joseph Sun
authored andcommitted
Refactor explicit and implicit variable usage to comply with dotnet code style rules
1 parent da1257e commit e67ba69

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

src/coverlet.console/Program.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static int Main(string[] args)
3434
var verbosity = new Option<LogLevel>(new[] { "--verbosity", "-v" }, () => LogLevel.Normal, "Sets the verbosity level of the command. Allowed values are quiet, minimal, normal, detailed.") { Arity = ArgumentArity.ZeroOrOne };
3535
var formats = new Option<string[]>(new[] { "--format", "-f" }, () => new[] { "json" }, "Format of the generated coverage report.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
3636
var threshold = new Option<string>("--threshold", "Exits with error if the coverage % is below value.") { Arity = ArgumentArity.ZeroOrOne };
37-
var thresholdTypes = new Option<List<string>>("--threshold-type", () => new List<string>(new string[] { "line", "branch", "method" }), "Coverage type to apply the threshold to.").FromAmong("line", "branch", "method");
37+
Option<List<string>> thresholdTypes = new Option<List<string>>("--threshold-type", () => new List<string>(new string[] { "line", "branch", "method" }), "Coverage type to apply the threshold to.").FromAmong("line", "branch", "method");
3838
var thresholdStat = new Option<ThresholdStatistic>("--threshold-stat", () => ThresholdStatistic.Minimum, "Coverage statistic used to enforce the threshold value.") { Arity = ArgumentArity.ZeroOrOne };
3939
var excludeFilters = new Option<string[]>("--exclude", "Filter expressions to exclude specific modules and types.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
4040
var includeFilters = new Option<string[]>("--include", "Filter expressions to include only specific modules and types.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
@@ -49,6 +49,7 @@ static int Main(string[] args)
4949
var doesNotReturnAttributes = new Option<string[]>("--does-not-return-attribute", "Attributes that mark methods that do not return") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
5050
var excludeAssembliesWithoutSources = new Option<string>("--exclude-assemblies-without-sources", "Specifies behaviour of heuristic to ignore assemblies with missing source documents.") { Arity = ArgumentArity.ZeroOrOne };
5151
var sourceMappingFile = new Option<string>("--source-mapping-file", "Specifies the path to a SourceRootsMappings file.") { Arity = ArgumentArity.ZeroOrOne };
52+
var unloadCoverletModuleOnly = new Option<bool>("--unload-coverlet-module-only", "Specifies Whether or not coverlet will generate a report"){ Arity = ArgumentArity.ZeroOrOne };
5253

5354
RootCommand rootCommand = new()
5455
{
@@ -73,7 +74,8 @@ static int Main(string[] args)
7374
useSourceLink,
7475
doesNotReturnAttributes,
7576
excludeAssembliesWithoutSources,
76-
sourceMappingFile
77+
sourceMappingFile,
78+
unloadCoverletModuleOnly
7779
};
7880

7981
rootCommand.Description = "Cross platform .NET Core code coverage tool";
@@ -102,11 +104,12 @@ static int Main(string[] args)
102104
string[] doesNotReturnAttributesValue = context.ParseResult.GetValueForOption(doesNotReturnAttributes);
103105
string excludeAssembliesWithoutSourcesValue = context.ParseResult.GetValueForOption(excludeAssembliesWithoutSources);
104106
string sourceMappingFileValue = context.ParseResult.GetValueForOption(sourceMappingFile);
107+
bool unloadCoverletModuleOnlyBool = context.ParseResult.GetValueForOption(unloadCoverletModuleOnly);
105108

106109
if (string.IsNullOrEmpty(moduleOrAppDirectoryValue) || string.IsNullOrWhiteSpace(moduleOrAppDirectoryValue))
107110
throw new ArgumentException("No test assembly or application directory specified.");
108111

109-
var taskStatus = await HandleCommand(moduleOrAppDirectoryValue,
112+
int taskStatus = await HandleCommand(moduleOrAppDirectoryValue,
110113
targetValue,
111114
targsValue,
112115
outputValue,
@@ -127,7 +130,8 @@ static int Main(string[] args)
127130
useSourceLinkValue,
128131
doesNotReturnAttributesValue,
129132
excludeAssembliesWithoutSourcesValue,
130-
sourceMappingFileValue);
133+
sourceMappingFileValue,
134+
unloadCoverletModuleOnlyBool);
131135
context.ExitCode = taskStatus;
132136

133137
});
@@ -154,7 +158,8 @@ private static Task<int> HandleCommand(string moduleOrAppDirectory,
154158
bool useSourceLink,
155159
string[] doesNotReturnAttributes,
156160
string excludeAssembliesWithoutSources,
157-
string sourceMappingFile
161+
string sourceMappingFile,
162+
bool unloadCoverletModuleOnly
158163
)
159164
{
160165

@@ -232,6 +237,12 @@ string sourceMappingFile
232237

233238
string dOutput = output != null ? output : Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar.ToString();
234239

240+
if (unloadCoverletModuleOnly)
241+
{
242+
int unloadModuleExitCode = coverage.UnloadModule(moduleOrAppDirectory);
243+
return Task.FromResult(unloadModuleExitCode);
244+
}
245+
235246
logger.LogInformation("\nCalculating coverage result...");
236247

237248
CoverageResult result = coverage.GetCoverageResult();

src/coverlet.core/Coverage.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public CoveragePrepareResult PrepareModules()
111111
_parameters.IncludeFilters = _parameters.IncludeFilters?.Where(f => _instrumentationHelper.IsValidFilterExpression(f)).ToArray();
112112

113113
IReadOnlyList<string> validModules = _instrumentationHelper.SelectModules(modules, _parameters.IncludeFilters, _parameters.ExcludeFilters).ToList();
114-
foreach (var excludedModule in modules.Except(validModules))
114+
foreach (string excludedModule in modules.Except(validModules))
115115
{
116116
_logger.LogVerbose($"Excluded module: '{excludedModule}'");
117117
}
@@ -337,10 +337,20 @@ public CoverageResult GetCoverageResult()
337337
/// of instrumentation in large scale testing utilising parallelization
338338
/// </summary>
339339
/// <param name="modulePath"></param>
340-
public void UnloadModule(string modulePath)
340+
public int UnloadModule(string modulePath)
341341
{
342-
_unloadedModules.Add(modulePath);
343-
_instrumentationHelper.RestoreOriginalModule(modulePath, Identifier);
342+
try
343+
{
344+
_instrumentationHelper.RestoreOriginalModule(modulePath, Identifier);
345+
_unloadedModules.Add(modulePath);
346+
}
347+
catch (Exception e)
348+
{
349+
_logger.LogVerbose($"{e.InnerException} occured, module unloading aborted.");
350+
return -1;
351+
}
352+
353+
return 0;
344354
}
345355

346356
private bool BranchInCompilerGeneratedClass(string methodName)

test/coverlet.integration.tests/DeterministicBuild.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private static void DeleteTestIntermediateFiles(string testResultsPath)
300300
{
301301
if (Directory.Exists(testResultsPath))
302302
{
303-
DirectoryInfo hdDirectory = new DirectoryInfo(testResultsPath);
303+
var hdDirectory = new DirectoryInfo(testResultsPath);
304304

305305
// search for directory "In" which has second copy e.g. '_fv-az365-374_2023-10-10_14_26_42\In\fv-az365-374\coverage.json'
306306
DirectoryInfo[] intermediateFolder = hdDirectory.GetDirectories("In", SearchOption.AllDirectories);
@@ -316,7 +316,7 @@ private static void DeleteLogFiles(string directory)
316316
{
317317
if (Directory.Exists(directory))
318318
{
319-
DirectoryInfo hdDirectory = new DirectoryInfo(directory);
319+
var hdDirectory = new DirectoryInfo(directory);
320320
FileInfo[] filesInDir = hdDirectory.GetFiles("log.*.txt");
321321

322322
foreach (FileInfo foundFile in filesInDir)
@@ -344,7 +344,7 @@ private static void DeleteCoverageFiles(string directory)
344344
{
345345
if (Directory.Exists(directory))
346346
{
347-
DirectoryInfo hdDirectory = new DirectoryInfo(directory);
347+
var hdDirectory = new DirectoryInfo(directory);
348348
FileInfo[] filesInDir = hdDirectory.GetFiles("coverage.cobertura.xml");
349349

350350
foreach (FileInfo foundFile in filesInDir)

test/coverlet.tests.projectsample.aspmvcrazor/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright (c) Toni Solarin-Sodara
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
var builder = WebApplication.CreateBuilder(args);
4+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
55

66
// Add services to the container.
77
builder.Services.AddRazorPages();
88

9-
var app = builder.Build();
9+
WebApplication app = builder.Build();
1010

1111
// Configure the HTTP request pipeline.
1212
if (!app.Environment.IsDevelopment())

0 commit comments

Comments
 (0)