Exemplo n.º 1
0
        public void Run_RunsMethod()
        {
            // Arrange
            string solutionDir      = Path.GetFullPath(typeof(MethodRunnerIntegrationTests).GetTypeInfo().Assembly.Location + "../../../../../../..");
            string projectDir       = "StubProject.OlderFramework";
            string projectName      = projectDir;
            string projectAbsSrcDir = $"{solutionDir}/test/{projectDir}";
            string outputDir        = $"{projectAbsSrcDir}/bin/debug/netcoreapp1.0";
            string assemblyFilePath = $"{outputDir}/{projectName}.dll";
            string entryClassName   = $"{projectName}.EntryStubClass";
            int    testExitCode     = 10; // Arbitrary

            string[] stubArgs = new string[] { testExitCode.ToString() };

            DirectoryAssemblyLoadContext dalc = new DirectoryAssemblyLoadContext(outputDir);
            Assembly assembly = dalc.LoadFromAssemblyPath(assemblyFilePath);

            IServiceCollection services = new ServiceCollection();

            services.AddProjectHost();
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            IMethodRunner    runner          = serviceProvider.GetService <IMethodRunner>();

            // Act
            int result = runner.Run(assembly, entryClassName, args: stubArgs);

            // Assert
            Assert.Equal(testExitCode, result);
            (serviceProvider as IDisposable).Dispose();
        }
        public void Run_RunsEntryMethod(string projectDir)
        {
            // Arrange
            string tempDir = $"{_tempDir}{projectDir}";

            _directoryService.Empty(tempDir); // TODO for netstandard2.0 and earlier, AssemblyLoadContexts cannot be unloaded (can't reuse same directory within same process)
            string solutionDir        = Path.GetFullPath(typeof(ProjectHostEndToEndTests).GetTypeInfo().Assembly.Location + "../../../../../../..");
            string projectName        = projectDir;
            string projectAbsSrcDir   = $"{solutionDir}/test/{projectDir}";
            string projectAbsFilePath = $"{tempDir}/{projectName}.csproj";
            string assemblyName       = projectName;
            string className          = $"{projectName}.EntryStubClass";
            int    testExitCode       = 10; // Arbitrary

            string[] stubArgs = new string[] { testExitCode.ToString() };

            _directoryService.Copy(projectAbsSrcDir, tempDir, excludePatterns: new string[] { "^bin$", "^obj$" });

            IServiceCollection services = new ServiceCollection();

            services.AddProjectHost();
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            IProjectLoader   loader          = serviceProvider.GetService <IProjectLoader>();
            IMethodRunner    runner          = serviceProvider.GetService <IMethodRunner>();

            // Act
            Assembly assembly = loader.Load(projectAbsFilePath, assemblyName);
            int      result   = runner.Run(assembly, className, args: stubArgs);

            // Assert
            Assert.Equal(testExitCode, result);
            (serviceProvider as IDisposable).Dispose();
        }