/// <inheritdoc/> public IAssemblyTarget LoadAssembly(LoadMethod loadMethod, string assemblyPath, string pdbPath = null) { IAssemblyTarget target = null; var assembly = this.loader.LoadAssembly(loadMethod, assemblyPath, pdbPath); if (loadMethod == LoadMethod.LoadBits) { // Assemlies loaded by bits will have the codebase set to the assembly that loaded it. Set it to the correct path here. var codebaseUri = new Uri(assemblyPath); target = AssemblyTarget.FromPath(codebaseUri, assembly.Location, assembly.FullName); } else { target = AssemblyTarget.FromAssembly(assembly); } return(target); }
/// <inheritdoc /> public ITestRunResult RunTests(IAssemblyTarget target, IAssemblyTarget tests) { ITestRunResult result; try { // Load tests and target var loader = new AssemblyLoader(); loader.LoadAssemblyWithReferences(LoadMethod.LoadFrom, target.Location); var testAssembly = loader.LoadAssembly(LoadMethod.LoadFrom, tests.Location); // Grab the reflection wrapper. var testUtilReflectionWrapper = new TestUtilReflectionWrapper(loader); // If we have ignore targets, set up the appropriate test state recorder. if (this.ignoreTargets != null && this.ignoreTargets.Any()) { testUtilReflectionWrapper.InvokeStateRecorderPropertySet(this.BuildTestStateRecorder()); } // Execute the tests. foreach (var type in testAssembly.GetTypes()) { var testObj = Activator.CreateInstance(type); var method = type.GetMethods().First(x => x.Name.Equals(this.testMethodName)); method.Invoke(testObj, null); } // Execute Save by finding the loaded winbert assembly and calling TestUtil.SaveResults() var analysisLogPath = CreateAnalysisFilePath(target, tests); testUtilReflectionWrapper.InvokeSaveResults(analysisLogPath); result = TestRunResult.Successful(analysisLogPath, target); } catch (Exception) { result = TestRunResult.Failure(target); } return result; }
/// <inheritdoc /> public IEnumerable<IAssemblyTarget> LoadTargetWithReferences(LoadMethod loadMethod, IAssemblyTarget target) { return this.LoadAssemblyWithReferences(loadMethod, target.CodeBase.LocalPath); }
/// <inheritdoc /> public IAssemblyTarget LoadTarget(LoadMethod loadMethod, IAssemblyTarget target) { return this.LoadAssembly(loadMethod, target.CodeBase.LocalPath); }
/// <summary> /// Loads the target module. /// </summary> /// <param name="target"> /// The target to load. /// </param> /// <param name="host"> /// The host to use when loading the module. /// </param> /// <returns> /// The target module. /// </returns> private static IAssembly LoadModule(IAssemblyTarget target, IMetadataHost host) { var location = string.IsNullOrEmpty(target.Location) ? target.CodeBase.LocalPath : target.Location; var module = host.LoadUnitFrom(location) as IAssembly; if (module == null || module is Dummy) { throw new FileNotFoundException("Unable to load assembly at location: " + target.Location); } return module; }
/// <summary> /// Gets a mutable copy of the target assembly. /// </summary> /// <param name="target"> /// The assembly to retrieve the mutable copy for. /// </param> /// <param name="host"> /// The host to use when loading the assembly. /// </param> /// <returns> /// A mutable copy of the target assembly. /// </returns> private static Assembly GetMutableAssembly(IAssemblyTarget target, IMetadataHost host) { var copier = new MetadataDeepCopier(host); return copier.Copy(LoadModule(target, host)); }
/// <summary> /// Factory method for creating instrumentation targets. /// </summary> /// <param name="target"> /// The target assembly to prepare for instrumentation. /// </param> /// <returns> /// An instrumentation target. /// </returns> public static InstrumentationTarget Create(IAssemblyTarget target) { if(target == null) { throw new ArgumentNullException("target"); } var host = new PeReader.DefaultHost(); var mutableAssembly = GetMutableAssembly(target, host); var pdbReader = GetPdbReader(mutableAssembly, host); return new InstrumentationTarget() { Host = host, MutableAssembly = mutableAssembly, PdbReader = pdbReader, Target = target, LocalScopeProvider = pdbReader == null ? null : new ILGenerator.LocalScopeProvider(pdbReader), SourceLocationProvider = pdbReader }; }
/// <summary> /// Creates a file path for the analysis file from the assembly target. /// </summary> /// <param name="target"> /// The assembly target to create the analysis file path for. /// </param> /// <param name="tests"> /// The tests. /// </param> /// <returns> /// A string path to an XML file that should contain analysis information. /// </returns> private static string CreateAnalysisFilePath(IAssemblyTarget target, IAssemblyTarget tests) { var endingIdx = tests.Location.LastIndexOf(".instrumented.dll"); var uniqueId = tests.Location.Substring(endingIdx - 7, 7); var analysisFileName = Path.GetFileName(target.Location); var extension = Path.GetExtension(analysisFileName); analysisFileName = Path.ChangeExtension(analysisFileName, extension + "." + uniqueId + ".analysis.xml"); return Path.Combine(Path.GetDirectoryName(target.Location), analysisFileName); }
/// <inheritdoc /> public IEnumerable <IAssemblyTarget> LoadTargetWithReferences(LoadMethod loadMethod, IAssemblyTarget target) { return(this.LoadAssemblyWithReferences(loadMethod, target.CodeBase.LocalPath)); }
/// <inheritdoc /> public IAssemblyTarget LoadTarget(LoadMethod loadMethod, IAssemblyTarget target) { return(this.LoadAssembly(loadMethod, target.CodeBase.LocalPath)); }