public static TestResult ExecuteTestRun(this BaseModuleWeaver weaver, string assemblyPath) { assemblyPath = Path.Combine(CodeBaseLocation.CurrentDirectory, assemblyPath); var fodyTempDir = Path.Combine(Path.GetDirectoryName(assemblyPath), "fodytemp"); Directory.CreateDirectory(fodyTempDir); var targetAssemblyPath = Path.Combine(fodyTempDir, Path.GetFileName(assemblyPath)); var targetSymbolsPath = Path.ChangeExtension(targetAssemblyPath, "pdb"); var symbolsPath = Path.ChangeExtension(assemblyPath, "pdb"); File.Copy(assemblyPath, targetAssemblyPath, true); File.Copy(symbolsPath, targetSymbolsPath, true); using (var assemblyResolver = new MockAssemblyResolver()) { var typeCache = CacheTypes(weaver, assemblyResolver); var testStatus = new TestResult(); weaver.LogDebug = text => testStatus.AddMessage(text, MessageImportanceDefaults.Debug); weaver.LogInfo = text => testStatus.AddMessage(text, MessageImportanceDefaults.Info); weaver.LogMessage = (text, messageImportance) => testStatus.AddMessage(text, messageImportance); weaver.LogWarning = text => testStatus.AddWarning(text, null); weaver.LogWarningPoint = (text, sequencePoint) => testStatus.AddWarning(text, sequencePoint); weaver.LogError = text => testStatus.AddError(text, null); weaver.LogErrorPoint = (text, sequencePoint) => testStatus.AddError(text, sequencePoint); weaver.FindType = typeCache.FindType; weaver.ResolveAssembly = assemblyResolver.Resolve; var readerParameters = new ReaderParameters(ReadingMode.Immediate) { AssemblyResolver = assemblyResolver, SymbolReaderProvider = new SymbolReaderProvider(), ReadWrite = true, ReadSymbols = true }; using (var moduleDefinition = ModuleDefinition.ReadModule(targetAssemblyPath, readerParameters)) { weaver.ModuleDefinition = moduleDefinition; weaver.Execute(); moduleDefinition.Write(); } PeVerifier.ThrowIfDifferent(assemblyPath, targetAssemblyPath, new[] { "0x80070002" }); testStatus.Assembly = Assembly.Load(File.ReadAllBytes(targetAssemblyPath)); return(testStatus); } }
public static TestResult ExecuteTestRun( this BaseModuleWeaver weaver, string assemblyPath, bool runPeVerify = true, Action <ModuleDefinition> afterExecuteCallback = null, Action <ModuleDefinition> beforeExecuteCallback = null, string assemblyName = null, IEnumerable <string> ignoreCodes = null) { assemblyPath = Path.Combine(CodeBaseLocation.CurrentDirectory, assemblyPath); var fodyTempDir = Path.Combine(Path.GetDirectoryName(assemblyPath), "fodytemp"); Directory.CreateDirectory(fodyTempDir); IoHelper.PurgeDirectory(fodyTempDir); string targetFileName; if (assemblyName == null) { assemblyName = Path.GetFileNameWithoutExtension(assemblyPath); targetFileName = Path.GetFileName(assemblyPath); } else { targetFileName = assemblyName + ".dll"; } var targetAssemblyPath = Path.Combine(fodyTempDir, targetFileName); using (var assemblyResolver = new MockAssemblyResolver()) { var typeCache = CacheTypes(weaver, assemblyResolver); var testStatus = new TestResult(); weaver.LogDebug = text => testStatus.AddMessage(text, MessageImportanceDefaults.Debug); weaver.LogInfo = text => testStatus.AddMessage(text, MessageImportanceDefaults.Info); weaver.LogMessage = (text, messageImportance) => testStatus.AddMessage(text, messageImportance); weaver.LogWarning = text => testStatus.AddWarning(text, null); weaver.LogWarningPoint = (text, sequencePoint) => testStatus.AddWarning(text, sequencePoint); weaver.LogError = text => testStatus.AddError(text, null); weaver.LogErrorPoint = (text, sequencePoint) => testStatus.AddError(text, sequencePoint); weaver.AssemblyFilePath = assemblyPath; weaver.FindType = typeCache.FindType; weaver.TryFindType = typeCache.TryFindType; weaver.ResolveAssembly = assemblyResolver.Resolve; var readerParameters = new ReaderParameters { AssemblyResolver = assemblyResolver, SymbolReaderProvider = new SymbolReaderProvider(), ReadWrite = false, ReadSymbols = true, }; using (var module = ModuleDefinition.ReadModule(assemblyPath, readerParameters)) { module.Assembly.Name.Name = assemblyName; weaver.ModuleDefinition = module; weaver.TypeSystem = new TypeSystem(typeCache.FindType, module); beforeExecuteCallback?.Invoke(module); weaver.Execute(); ReferenceCleaner.CleanReferences(module, weaver, weaver.LogDebug); afterExecuteCallback?.Invoke(module); var writerParameters = new WriterParameters { WriteSymbols = true }; module.Write(targetAssemblyPath, writerParameters); } if (runPeVerify && IsWindows()) { List <string> ignoreList; if (ignoreCodes == null) { ignoreList = new List <string>(); } else { ignoreList = ignoreCodes.ToList(); } PeVerifier.ThrowIfDifferent(assemblyPath, targetAssemblyPath, ignoreList, Path.GetDirectoryName(assemblyPath)); } testStatus.Assembly = Assembly.Load(File.ReadAllBytes(targetAssemblyPath)); testStatus.AssemblyPath = targetAssemblyPath; return(testStatus); } }