Ejemplo n.º 1
0
        public TestAssembly(string assemblyName, string testSourceCodeFile)
        {
            var outputFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            outputFolder.ShouldNotBeNull();

            m_TestAssemblyPath = Path.Combine(outputFolder, assemblyName + ".dll");

            var compiler     = new Microsoft.CSharp.CSharpCodeProvider();
            var compilerArgs = new CompilerParameters
            {
                OutputAssembly          = m_TestAssemblyPath,
                IncludeDebugInformation = true,
                CompilerOptions         = "/o- /debug+ /warn:0"
            };

            compilerArgs.ReferencedAssemblies.Add(typeof(Enumerable).Assembly.Location);

            var compilerResult = compiler.CompileAssemblyFromSource(compilerArgs, testSourceCodeFile);

            if (compilerResult.Errors.Count > 0)
            {
                var errorText = compilerResult.Errors
                                .OfType <CompilerError>()
                                .Select(e => $"({e.Line},{e.Column}): error {e.ErrorNumber}: {e.ErrorText}")
                                .Prepend("Compiler errors:")
                                .StringJoin("\n");
                throw new Exception(errorText);
            }

            m_TestAssemblyPath = compilerResult.PathToAssembly;

            PeVerify.Verify(m_TestAssemblyPath); // pre-check..sometimes we can compile code that doesn't verify

            var results = ElevatedWeaver.PatchAllDependentAssemblies(m_TestAssemblyPath, PatchTestAssembly.Yes);

            results.Count.ShouldBe(2);
            results.ShouldContain(new PatchResult("mscorlib", null, PatchState.IgnoredOutsideAllowedPaths));
            results.ShouldContain(new PatchResult(m_TestAssemblyPath, ElevatedWeaver.GetPatchBackupPathFor(m_TestAssemblyPath), PatchState.Patched));

            m_TestAssembly = AssemblyDefinition.ReadAssembly(m_TestAssemblyPath);
            MockInjector.IsPatched(m_TestAssembly).ShouldBeTrue();

            PeVerify.Verify(m_TestAssemblyPath);
        }
Ejemplo n.º 2
0
        public NPath Compile(string testAssemblyName, string sourceCode, params string[] dependentAssemblyNames)
        {
            var testAssemblyPath = BaseDir.Combine(testAssemblyName + ".dll");

            // set up to compile

            var compiler     = new Microsoft.CSharp.CSharpCodeProvider();
            var compilerArgs = new CompilerParameters
            {
                OutputAssembly          = testAssemblyPath,
                IncludeDebugInformation = true,
                CompilerOptions         = "/o- /debug+ /warn:0"
            };

            compilerArgs.ReferencedAssemblies.Add(typeof(int).Assembly.Location); // mscorlib
            compilerArgs.ReferencedAssemblies.AddRange(
                dependentAssemblyNames.Select(n => BaseDir.Combine(n + ".dll").ToString()));

            // compile and handle errors

            var compilerResult = compiler.CompileAssemblyFromSource(compilerArgs, sourceCode);

            if (compilerResult.Errors.Count > 0)
            {
                var errorText = compilerResult.Errors
                                .OfType <CompilerError>()
                                .Select(e => $"({e.Line},{e.Column}): error {e.ErrorNumber}: {e.ErrorText}")
                                .Prepend("Compiler errors:")
                                .StringJoin("\n");
                throw new Exception(errorText);
            }

            testAssemblyPath.ShouldBe(new NPath(compilerResult.PathToAssembly));

            PeVerify.Verify(testAssemblyPath); // sanity check on what the compiler generated

            return(testAssemblyPath);
        }