-
Notifications
You must be signed in to change notification settings - Fork 389
Inject InstrumentationHelper #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MarcoRossignoli
merged 7 commits into
coverlet-coverage:master
from
MarcoRossignoli:externalpdb
Sep 10, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d3c510
inject InstrumentationHelper
MarcoRossignoli cb01739
nit
MarcoRossignoli 2b3876f
updates
MarcoRossignoli 2ad5b6f
updates
MarcoRossignoli 0928845
merge master
MarcoRossignoli 238694a
merge master
MarcoRossignoli aa5f867
make test predictable
MarcoRossignoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Coverlet.Core.Abstracts | ||
{ | ||
public interface IInstrumentationHelper | ||
{ | ||
void BackupOriginalModule(string module, string identifier); | ||
void DeleteHitsFile(string path); | ||
string[] GetCoverableModules(string module, string[] directories, bool includeTestAssembly); | ||
bool HasPdb(string module, out bool embedded); | ||
bool IsModuleExcluded(string module, string[] excludeFilters); | ||
bool IsModuleIncluded(string module, string[] includeFilters); | ||
bool IsValidFilterExpression(string filter); | ||
bool IsTypeExcluded(string module, string type, string[] excludeFilters); | ||
bool IsTypeIncluded(string module, string type, string[] includeFilters); | ||
void RestoreOriginalModule(string module, string identifier); | ||
bool EmbeddedPortablePdbHasLocalSource(string module); | ||
bool IsLocalMethod(string method); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,22 +9,21 @@ | |
using System.Reflection.PortableExecutable; | ||
using System.Text.RegularExpressions; | ||
using Coverlet.Core.Abstracts; | ||
using Microsoft.Extensions.FileSystemGlobbing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.FileSystemGlobbing.Abstractions; | ||
|
||
namespace Coverlet.Core.Helpers | ||
{ | ||
internal static class InstrumentationHelper | ||
internal class InstrumentationHelper : IInstrumentationHelper | ||
{ | ||
private static readonly ConcurrentDictionary<string, string> _backupList = new ConcurrentDictionary<string, string>(); | ||
private readonly ConcurrentDictionary<string, string> _backupList = new ConcurrentDictionary<string, string>(); | ||
private readonly IRetryHelper _retryHelper; | ||
|
||
static InstrumentationHelper() | ||
public InstrumentationHelper(IProcessExitHandler processExitHandler, IRetryHelper retryHelper) | ||
{ | ||
DependencyInjection.Current.GetService<IProcessExitHandler>().Add((s, e) => RestoreOriginalModules()); | ||
processExitHandler.Add((s, e) => RestoreOriginalModules()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ViktorHofer you did this change, now a single instance of |
||
_retryHelper = retryHelper; | ||
} | ||
|
||
public static string[] GetCoverableModules(string module, string[] directories, bool includeTestAssembly) | ||
public string[] GetCoverableModules(string module, string[] directories, bool includeTestAssembly) | ||
{ | ||
Debug.Assert(directories != null); | ||
|
||
|
@@ -68,7 +67,7 @@ public static string[] GetCoverableModules(string module, string[] directories, | |
.ToArray(); | ||
} | ||
|
||
public static bool HasPdb(string module, out bool embedded) | ||
public bool HasPdb(string module, out bool embedded) | ||
{ | ||
embedded = false; | ||
using (var moduleStream = File.OpenRead(module)) | ||
|
@@ -94,7 +93,7 @@ public static bool HasPdb(string module, out bool embedded) | |
} | ||
} | ||
|
||
public static bool EmbeddedPortablePdbHasLocalSource(string module) | ||
public bool EmbeddedPortablePdbHasLocalSource(string module) | ||
{ | ||
using (FileStream moduleStream = File.OpenRead(module)) | ||
using (var peReader = new PEReader(moduleStream)) | ||
|
@@ -129,7 +128,7 @@ public static bool EmbeddedPortablePdbHasLocalSource(string module) | |
return true; | ||
} | ||
|
||
public static void BackupOriginalModule(string module, string identifier) | ||
public void BackupOriginalModule(string module, string identifier) | ||
{ | ||
var backupPath = GetBackupPath(module, identifier); | ||
var backupSymbolPath = Path.ChangeExtension(backupPath, ".pdb"); | ||
|
@@ -150,7 +149,7 @@ public static void BackupOriginalModule(string module, string identifier) | |
} | ||
} | ||
|
||
public static void RestoreOriginalModule(string module, string identifier) | ||
public void RestoreOriginalModule(string module, string identifier) | ||
{ | ||
var backupPath = GetBackupPath(module, identifier); | ||
var backupSymbolPath = Path.ChangeExtension(backupPath, ".pdb"); | ||
|
@@ -159,14 +158,14 @@ public static void RestoreOriginalModule(string module, string identifier) | |
// See: https://github.com/tonerdo/coverlet/issues/25 | ||
var retryStrategy = CreateRetryStrategy(); | ||
|
||
DependencyInjection.Current.GetService<IRetryHelper>().Retry(() => | ||
_retryHelper.Retry(() => | ||
{ | ||
File.Copy(backupPath, module, true); | ||
File.Delete(backupPath); | ||
_backupList.TryRemove(module, out string _); | ||
}, retryStrategy, 10); | ||
|
||
DependencyInjection.Current.GetService<IRetryHelper>().Retry(() => | ||
_retryHelper.Retry(() => | ||
{ | ||
if (File.Exists(backupSymbolPath)) | ||
{ | ||
|
@@ -178,7 +177,7 @@ public static void RestoreOriginalModule(string module, string identifier) | |
}, retryStrategy, 10); | ||
} | ||
|
||
public static void RestoreOriginalModules() | ||
public void RestoreOriginalModules() | ||
{ | ||
// Restore the original module - retry up to 10 times, since the destination file could be locked | ||
// See: https://github.com/tonerdo/coverlet/issues/25 | ||
|
@@ -187,7 +186,7 @@ public static void RestoreOriginalModules() | |
foreach (string key in _backupList.Keys.ToList()) | ||
{ | ||
string backupPath = _backupList[key]; | ||
DependencyInjection.Current.GetService<IRetryHelper>().Retry(() => | ||
_retryHelper.Retry(() => | ||
{ | ||
File.Copy(backupPath, key, true); | ||
File.Delete(backupPath); | ||
|
@@ -196,15 +195,15 @@ public static void RestoreOriginalModules() | |
} | ||
} | ||
|
||
public static void DeleteHitsFile(string path) | ||
public 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(); | ||
DependencyInjection.Current.GetService<IRetryHelper>().Retry(() => File.Delete(path), retryStrategy, 10); | ||
_retryHelper.Retry(() => File.Delete(path), retryStrategy, 10); | ||
} | ||
|
||
public static bool IsValidFilterExpression(string filter) | ||
public bool IsValidFilterExpression(string filter) | ||
{ | ||
if (filter == null) | ||
return false; | ||
|
@@ -236,7 +235,7 @@ public static bool IsValidFilterExpression(string filter) | |
return true; | ||
} | ||
|
||
public static bool IsModuleExcluded(string module, string[] excludeFilters) | ||
public bool IsModuleExcluded(string module, string[] excludeFilters) | ||
{ | ||
if (excludeFilters == null || excludeFilters.Length == 0) | ||
return false; | ||
|
@@ -264,7 +263,7 @@ public static bool IsModuleExcluded(string module, string[] excludeFilters) | |
return false; | ||
} | ||
|
||
public static bool IsModuleIncluded(string module, string[] includeFilters) | ||
public bool IsModuleIncluded(string module, string[] includeFilters) | ||
{ | ||
if (includeFilters == null || includeFilters.Length == 0) | ||
return true; | ||
|
@@ -291,7 +290,7 @@ public static bool IsModuleIncluded(string module, string[] includeFilters) | |
return false; | ||
} | ||
|
||
public static bool IsTypeExcluded(string module, string type, string[] excludeFilters) | ||
public bool IsTypeExcluded(string module, string type, string[] excludeFilters) | ||
{ | ||
if (excludeFilters == null || excludeFilters.Length == 0) | ||
return false; | ||
|
@@ -303,7 +302,7 @@ public static bool IsTypeExcluded(string module, string type, string[] excludeFi | |
return IsTypeFilterMatch(module, type, excludeFilters); | ||
} | ||
|
||
public static bool IsTypeIncluded(string module, string type, string[] includeFilters) | ||
public bool IsTypeIncluded(string module, string type, string[] includeFilters) | ||
{ | ||
if (includeFilters == null || includeFilters.Length == 0) | ||
return true; | ||
|
@@ -315,10 +314,10 @@ public static bool IsTypeIncluded(string module, string type, string[] includeFi | |
return IsTypeFilterMatch(module, type, includeFilters); | ||
} | ||
|
||
public static bool IsLocalMethod(string method) | ||
public bool IsLocalMethod(string method) | ||
=> new Regex(WildcardToRegex("<*>*__*|*")).IsMatch(method); | ||
|
||
private static bool IsTypeFilterMatch(string module, string type, string[] filters) | ||
private bool IsTypeFilterMatch(string module, string type, string[] filters) | ||
{ | ||
Debug.Assert(module != null); | ||
Debug.Assert(filters != null); | ||
|
@@ -338,15 +337,15 @@ private static bool IsTypeFilterMatch(string module, string type, string[] filte | |
return false; | ||
} | ||
|
||
private static string GetBackupPath(string module, string identifier) | ||
private string GetBackupPath(string module, string identifier) | ||
{ | ||
return Path.Combine( | ||
Path.GetTempPath(), | ||
Path.GetFileNameWithoutExtension(module) + "_" + identifier + ".dll" | ||
); | ||
} | ||
|
||
private static Func<TimeSpan> CreateRetryStrategy(int initialSleepSeconds = 6) | ||
private Func<TimeSpan> CreateRetryStrategy(int initialSleepSeconds = 6) | ||
{ | ||
TimeSpan retryStrategy() | ||
{ | ||
|
@@ -358,14 +357,14 @@ TimeSpan retryStrategy() | |
return retryStrategy; | ||
} | ||
|
||
private static string WildcardToRegex(string pattern) | ||
private string WildcardToRegex(string pattern) | ||
{ | ||
return "^" + Regex.Escape(pattern). | ||
Replace("\\*", ".*"). | ||
Replace("\\?", "?") + "$"; | ||
} | ||
|
||
private static bool IsAssembly(string filePath) | ||
private bool IsAssembly(string filePath) | ||
{ | ||
Debug.Assert(filePath != null); | ||
|
||
|
@@ -383,5 +382,4 @@ private static bool IsAssembly(string filePath) | |
} | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing I need to do is to expose container to be used by console/task/collector without expose internal implementation.