Skip to content

Commit 88a5782

Browse files
Joseph SunJosephSun2003
authored andcommitted
Add new method to manually call the unloading of modules
1 parent 3e78d22 commit 88a5782

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/coverlet.collector/DataCollection/CoverageWrapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,10 @@ public void PrepareModules(Coverage coverage)
7070
{
7171
coverage.PrepareModules();
7272
}
73+
74+
public void UnloadModule(Coverage coverage, string modulePath)
75+
{
76+
coverage.UnloadModule(modulePath);
77+
}
7378
}
7479
}

src/coverlet.collector/Utilities/Interfaces/ICoverageWrapper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,11 @@ internal interface ICoverageWrapper
3939
/// <param name="coverage"></param>
4040
void PrepareModules(Coverage coverage);
4141

42+
/// <summary>
43+
/// Unload module in the specified path
44+
/// </summary>
45+
/// <param name="modulePath"></param>
46+
void UnloadModule(Coverage coverage, string modulePath);
47+
4248
}
4349
}

src/coverlet.core/Coverage.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ internal class Coverage
5959
private readonly CoverageParameters _parameters;
6060

6161
public string Identifier { get; }
62+
private List<string> unloadedModules { get; set; }
6263

6364
public Coverage(string moduleOrDirectory,
6465
CoverageParameters parameters,
@@ -78,6 +79,7 @@ public Coverage(string moduleOrDirectory,
7879
_cecilSymbolHelper = cecilSymbolHelper;
7980
Identifier = Guid.NewGuid().ToString();
8081
_results = new List<InstrumenterResult>();
82+
unloadedModules = new List<string>();
8183
}
8284

8385
public Coverage(CoveragePrepareResult prepareResult,
@@ -241,7 +243,10 @@ public CoverageResult GetCoverageResult()
241243
}
242244

243245
modules.Add(Path.GetFileName(result.ModulePath), documents);
244-
_instrumentationHelper.RestoreOriginalModule(result.ModulePath, Identifier);
246+
if (!unloadedModules.Contains(result.ModulePath))
247+
{
248+
UnloadModule(result.ModulePath);
249+
}
245250
}
246251

247252
// In case of anonymous delegate compiler generate a custom class and passes it as type.method delegate.
@@ -326,6 +331,16 @@ public CoverageResult GetCoverageResult()
326331
return coverageResult;
327332
}
328333

334+
/// <summary>
335+
/// Manually invoke the unloading of modules and restoration of the original assembly files
336+
/// </summary>
337+
/// <param name="modulePath"></param>
338+
public void UnloadModule(string modulePath)
339+
{
340+
unloadedModules.Add(modulePath);
341+
_instrumentationHelper.RestoreOriginalModule(modulePath, Identifier);
342+
}
343+
329344
private bool BranchInCompilerGeneratedClass(string methodName)
330345
{
331346
foreach (InstrumenterResult instrumentedResult in _results)

0 commit comments

Comments
 (0)