Skip to content

Commit e973b79

Browse files
committed
Add option for additional assembly probe dirs
1 parent 3454f21 commit e973b79

File tree

7 files changed

+36
-11
lines changed

7 files changed

+36
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Arguments:
5858
Options:
5959
-h|--help Show help information
6060
-v|--version Show version information
61+
-m|--moduleDir Path to the directory to probe for additional modules.
6162
-t|--target Path to the test runner application.
6263
-a|--targetargs Arguments to be passed to the test runner.
6364
-o|--output Output of the generated coverage report

src/coverlet.console/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static int Main(string[] args)
2424
app.VersionOption("-v|--version", GetAssemblyVersion());
2525

2626
CommandArgument module = app.Argument("<ASSEMBLY>", "Path to the test assembly.");
27+
CommandOption moduleDirs = app.Option("-m|--moduleDir", "Path to the directory to probe for additional modules.", CommandOptionType.MultipleValue);
2728
CommandOption target = app.Option("-t|--target", "Path to the test runner application.", CommandOptionType.SingleValue);
2829
CommandOption targs = app.Option("-a|--targetargs", "Arguments to be passed to the test runner.", CommandOptionType.SingleValue);
2930
CommandOption output = app.Option("-o|--output", "Output of the generated coverage report", CommandOptionType.SingleValue);
@@ -43,7 +44,7 @@ static int Main(string[] args)
4344
if (!target.HasValue())
4445
throw new CommandParsingException(app, "Target must be specified.");
4546

46-
Coverage coverage = new Coverage(module.Value, excludeFilters.Values.ToArray(), includeFilters.Values.ToArray(), excludedSourceFiles.Values.ToArray(), mergeWith.Value());
47+
Coverage coverage = new Coverage(module.Value, moduleDirs.Values.ToArray(), excludeFilters.Values.ToArray(), includeFilters.Values.ToArray(), excludedSourceFiles.Values.ToArray(), mergeWith.Value());
4748
coverage.PrepareModules();
4849

4950
Process process = new Process();

src/coverlet.core/Coverage.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Coverlet.Core
1414
public class Coverage
1515
{
1616
private string _module;
17+
private string[] _moduleDirs;
1718
private string _identifier;
1819
private string[] _excludeFilters;
1920
private string[] _includeFilters;
@@ -26,9 +27,10 @@ public string Identifier
2627
get { return _identifier; }
2728
}
2829

29-
public Coverage(string module, string[] excludeFilters, string[] includeFilters, string[] excludedSourceFiles, string mergeWith)
30+
public Coverage(string module, string[] moduleDirs, string[] excludeFilters, string[] includeFilters, string[] excludedSourceFiles, string mergeWith)
3031
{
3132
_module = module;
33+
_moduleDirs = moduleDirs ?? throw new ArgumentNullException(nameof(moduleDirs));
3234
_excludeFilters = excludeFilters;
3335
_includeFilters = includeFilters;
3436
_excludedSourceFiles = excludedSourceFiles;
@@ -40,7 +42,7 @@ public Coverage(string module, string[] excludeFilters, string[] includeFilters,
4042

4143
public void PrepareModules()
4244
{
43-
string[] modules = InstrumentationHelper.GetCoverableModules(_module);
45+
string[] modules = InstrumentationHelper.GetCoverableModules(_module, _moduleDirs);
4446
string[] excludes = InstrumentationHelper.GetExcludedFiles(_excludedSourceFiles);
4547
_excludeFilters = _excludeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();
4648
_includeFilters = _includeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,24 @@ namespace Coverlet.Core.Helpers
1414
{
1515
internal static class InstrumentationHelper
1616
{
17-
public static string[] GetCoverableModules(string module)
17+
public static string[] GetCoverableModules(string module, string[] moduleDirs)
1818
{
19-
IEnumerable<string> modules = Directory.EnumerateFiles(Path.GetDirectoryName(module)).Where(f => f.EndsWith(".exe") || f.EndsWith(".dll"));
20-
modules = modules.Where(m => IsAssembly(m) && Path.GetFileName(m) != Path.GetFileName(module));
21-
return modules.ToArray();
19+
// Prepare all the directories in which we probe for modules.
20+
string[] dirs = new string[moduleDirs.Length + 1];
21+
moduleDirs.CopyTo(dirs, 0);
22+
// Add the test assembly's directory.
23+
dirs[dirs.Length - 1] = Path.GetDirectoryName(module);
24+
25+
// The test module's name must be unique.
26+
var uniqueModules = new HashSet<string>
27+
{
28+
Path.GetFileName(module)
29+
};
30+
31+
return dirs.SelectMany(d => Directory.EnumerateFiles(d))
32+
.Where(f => f.EndsWith(".exe") || f.EndsWith(".dll"))
33+
.Where(m => IsAssembly(m) && uniqueModules.Add(Path.GetFileName(m)))
34+
.ToArray();
2235
}
2336

2437
public static bool HasPdb(string module)
@@ -43,7 +56,7 @@ public static bool HasPdb(string module)
4356
public static void BackupOriginalModule(string module, string identifier)
4457
{
4558
var backupPath = GetBackupPath(module, identifier);
46-
File.Copy(module, backupPath);
59+
File.Copy(module, backupPath, true);
4760
}
4861

4962
public static void RestoreOriginalModule(string module, string identifier)

src/coverlet.msbuild.tasks/InstrumentationTask.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class InstrumentationTask : Task
99
{
1010
private static Coverage _coverage;
1111
private string _path;
12+
private string _moduleDirs;
1213
private string _exclude;
1314
private string _include;
1415
private string _excludeByFile;
@@ -25,6 +26,12 @@ public string Path
2526
get { return _path; }
2627
set { _path = value; }
2728
}
29+
30+
public string ModuleDirs
31+
{
32+
get { return _moduleDirs; }
33+
set { _moduleDirs = value; }
34+
}
2835

2936
public string Exclude
3037
{
@@ -54,11 +61,12 @@ public override bool Execute()
5461
{
5562
try
5663
{
64+
var moduleDirs = _moduleDirs?.Split(',') ?? Array.Empty<string>();
5765
var excludedSourceFiles = _excludeByFile?.Split(',');
5866
var excludeFilters = _exclude?.Split(',');
5967
var includeFilters = _include?.Split(',');
6068

61-
_coverage = new Coverage(_path, excludeFilters, includeFilters, excludedSourceFiles, _mergeWith);
69+
_coverage = new Coverage(_path, moduleDirs, excludeFilters, includeFilters, excludedSourceFiles, _mergeWith);
6270
_coverage.PrepareModules();
6371
}
6472
catch (Exception ex)

test/coverlet.core.tests/CoverageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void TestCoverage()
2727
// Since Coverage only instruments dependancies, we need a fake module here
2828
var testModule = Path.Combine(directory.FullName, "test.module.dll");
2929

30-
var coverage = new Coverage(testModule, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), string.Empty);
30+
var coverage = new Coverage(testModule, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), string.Empty);
3131
coverage.PrepareModules();
3232

3333
var result = coverage.GetCoverageResult();

test/coverlet.core.tests/Helpers/InstrumentationHelperTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class InstrumentationHelperTests
1212
public void TestGetDependencies()
1313
{
1414
string module = typeof(InstrumentationHelperTests).Assembly.Location;
15-
var modules = InstrumentationHelper.GetCoverableModules(module);
15+
var modules = InstrumentationHelper.GetCoverableModules(module, Array.Empty<string>());
1616
Assert.False(Array.Exists(modules, m => m == module));
1717
}
1818

0 commit comments

Comments
 (0)