Skip to content

Commit 98f1f75

Browse files
Joseph SunJosephSun2003
Joseph Sun
authored andcommitted
add unload modules unique specifically for use in coverlet.console
1 parent 6d2dd57 commit 98f1f75

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/coverlet.console/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +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 };
52+
var unloadCoverletFromModulesOnly = new Option<bool>("--unload-coverlet-from-modules-only", "Specifies Whether or not coverlet will only unload after unit tests are finished"){ Arity = ArgumentArity.ZeroOrOne };
5353

5454
RootCommand rootCommand = new()
5555
{
@@ -75,7 +75,7 @@ static int Main(string[] args)
7575
doesNotReturnAttributes,
7676
excludeAssembliesWithoutSources,
7777
sourceMappingFile,
78-
unloadCoverletModuleOnly
78+
unloadCoverletFromModulesOnly
7979
};
8080

8181
rootCommand.Description = "Cross platform .NET Core code coverage tool";
@@ -104,7 +104,7 @@ static int Main(string[] args)
104104
string[] doesNotReturnAttributesValue = context.ParseResult.GetValueForOption(doesNotReturnAttributes);
105105
string excludeAssembliesWithoutSourcesValue = context.ParseResult.GetValueForOption(excludeAssembliesWithoutSources);
106106
string sourceMappingFileValue = context.ParseResult.GetValueForOption(sourceMappingFile);
107-
bool unloadCoverletModuleOnlyBool = context.ParseResult.GetValueForOption(unloadCoverletModuleOnly);
107+
bool unloadCoverletFromModulesOnlyBool = context.ParseResult.GetValueForOption(unloadCoverletFromModulesOnly);
108108

109109
if (string.IsNullOrEmpty(moduleOrAppDirectoryValue) || string.IsNullOrWhiteSpace(moduleOrAppDirectoryValue))
110110
throw new ArgumentException("No test assembly or application directory specified.");
@@ -131,7 +131,7 @@ static int Main(string[] args)
131131
doesNotReturnAttributesValue,
132132
excludeAssembliesWithoutSourcesValue,
133133
sourceMappingFileValue,
134-
unloadCoverletModuleOnlyBool);
134+
unloadCoverletFromModulesOnlyBool);
135135
context.ExitCode = taskStatus;
136136

137137
});
@@ -159,7 +159,7 @@ private static Task<int> HandleCommand(string moduleOrAppDirectory,
159159
string[] doesNotReturnAttributes,
160160
string excludeAssembliesWithoutSources,
161161
string sourceMappingFile,
162-
bool unloadCoverletModuleOnly
162+
bool unloadCoverletFromModulesOnly
163163
)
164164
{
165165

@@ -237,9 +237,9 @@ bool unloadCoverletModuleOnly
237237

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

240-
if (unloadCoverletModuleOnly)
240+
if (unloadCoverletFromModulesOnly)
241241
{
242-
int unloadModuleExitCode = coverage.UnloadModule(moduleOrAppDirectory);
242+
int unloadModuleExitCode = coverage.UnloadModule();
243243
return Task.FromResult(unloadModuleExitCode);
244244
}
245245

src/coverlet.core/Coverage.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,39 @@ public CoverageResult GetCoverageResult()
326326
return coverageResult;
327327
}
328328

329+
/// <summary>
330+
/// unloads all modules that were instrumented
331+
/// </summary>
332+
/// <returns> exit code of module unloading </returns>
333+
public int UnloadModule()
334+
{
335+
string[] modules = _instrumentationHelper.GetCoverableModules(_moduleOrAppDirectory,
336+
_parameters.IncludeDirectories, _parameters.IncludeTestAssembly);
337+
338+
IReadOnlyList<string> validModules = _instrumentationHelper
339+
.SelectModules(modules, _parameters.IncludeFilters, _parameters.ExcludeFilters).ToList();
340+
foreach (string modulePath in validModules) {
341+
try
342+
{
343+
_instrumentationHelper.RestoreOriginalModule(modulePath, Identifier);
344+
}
345+
catch (Exception e)
346+
{
347+
_logger.LogVerbose($"{e.InnerException} occured, module unloading aborted.");
348+
return -1;
349+
}
350+
}
351+
352+
return 0;
353+
}
354+
329355
/// <summary>
330356
/// Invoke the unloading of modules and restoration of the original assembly files, made public to allow unloading
331357
/// of instrumentation in large scale testing utilising parallelization
332358
/// </summary>
333359
/// <param name="modulePath"></param>
334-
public int UnloadModule(string modulePath)
360+
/// <returns> exist code of unloading modules </returns>
361+
public void UnloadModule(string modulePath)
335362
{
336363
try
337364
{
@@ -340,10 +367,7 @@ public int UnloadModule(string modulePath)
340367
catch (Exception e)
341368
{
342369
_logger.LogVerbose($"{e.InnerException} occured, module unloading aborted.");
343-
return -1;
344370
}
345-
346-
return 0;
347371
}
348372

349373
private bool BranchInCompilerGeneratedClass(string methodName)

0 commit comments

Comments
 (0)