forked from coverlet-coverage/coverlet
/
InstrumentationHelper.cs
154 lines (131 loc) · 5.56 KB
/
InstrumentationHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
namespace Coverlet.Core.Helpers
{
internal static class InstrumentationHelper
{
public static string[] GetDependencies(string module)
{
IEnumerable<string> modules = Directory.GetFiles(Path.GetDirectoryName(module), "*.dll");
modules = modules.Where(a => IsAssembly(a) && Path.GetFileName(a) != Path.GetFileName(module));
return modules.ToArray();
}
public static bool HasPdb(string module)
{
using (var moduleStream = File.OpenRead(module))
using (var peReader = new PEReader(moduleStream))
{
foreach (var entry in peReader.ReadDebugDirectory())
{
if (entry.Type == DebugDirectoryEntryType.CodeView)
{
var codeViewData = peReader.ReadCodeViewDebugDirectoryData(entry);
var peDirectory = Path.GetDirectoryName(module);
return File.Exists(Path.Combine(peDirectory, Path.GetFileName(codeViewData.Path)));
}
}
return false;
}
}
public static void CopyCoverletDependency(string module)
{
var directory = Path.GetDirectoryName(module);
var moduleFileName = Path.GetFileName(module);
var assembly = typeof(Coverage).Assembly;
string name = Path.GetFileName(assembly.Location);
if (name == moduleFileName)
return;
File.Copy(assembly.Location, Path.Combine(directory, name), true);
}
public static void BackupOriginalModule(string module, string identifier)
{
var backupPath = GetBackupPath(module, identifier);
File.Copy(module, backupPath);
}
public static void RestoreOriginalModule(string module, string identifier)
{
var backupPath = GetBackupPath(module, identifier);
// Restore the original module - retry up to 10 times, since the destination file could be locked
// See: https://github.com/tonerdo/coverlet/issues/25
var retryStrategy = CreateRetryStrategy();
RetryHelper.Retry(() => {
File.Copy(backupPath, module, true);
File.Delete(backupPath);
}, retryStrategy, 10);
}
public static void DeleteHitsFile(string path)
{
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
// See: https://github.com/tonerdo/coverlet/issues/25
var retryStrategy = CreateRetryStrategy();
RetryHelper.Retry(() => File.Delete(path), retryStrategy, 10);
}
public static IEnumerable<string> GetExcludedFiles(IEnumerable<string> excludeRules,
string parentDir = null)
{
const string RELATIVE_KEY = nameof(RELATIVE_KEY);
parentDir = string.IsNullOrWhiteSpace(parentDir)? Directory.GetCurrentDirectory() : parentDir;
if (excludeRules == null || !excludeRules.Any()) return Enumerable.Empty<string>();
var matcherDict = new Dictionary<string, Matcher>(){ {RELATIVE_KEY, new Matcher()}};
foreach (var excludeRule in excludeRules)
{
if (Path.IsPathRooted(excludeRule)) {
var root = Path.GetPathRoot(excludeRule);
if (!matcherDict.ContainsKey(root)) {
matcherDict.Add(root, new Matcher());
}
matcherDict[root].AddInclude(excludeRule.Substring(root.Length));
} else {
matcherDict[RELATIVE_KEY].AddInclude(excludeRule);
}
}
var files = new List<string>();
foreach(var entry in matcherDict)
{
var root = entry.Key;
var matcher = entry.Value;
var directoryInfo = new DirectoryInfo(root.Equals(RELATIVE_KEY) ? parentDir : root);
var fileMatchResult = matcher.Execute(new DirectoryInfoWrapper(directoryInfo));
var currentFiles = fileMatchResult.Files
.Select(f => Path.GetFullPath(Path.Combine(directoryInfo.ToString(), f.Path)));
files.AddRange(currentFiles);
}
return files.Distinct();
}
private static bool IsAssembly(string filePath)
{
try
{
AssemblyName.GetAssemblyName(filePath);
return true;
}
catch
{
return false;
}
}
private static string GetBackupPath(string module, string identifier)
{
return Path.Combine(
Path.GetTempPath(),
Path.GetFileNameWithoutExtension(module) + "_" + identifier + ".dll"
);
}
private static Func<TimeSpan> CreateRetryStrategy(int initialSleepSeconds = 6)
{
TimeSpan retryStrategy()
{
var sleep = TimeSpan.FromMilliseconds(initialSleepSeconds);
initialSleepSeconds *= 2;
return sleep;
}
return retryStrategy;
}
}
}