Ejemplo n.º 1
0
        public void TestOwinSelfHostSample(string version)
        {
            TestSolutionAnalysis results = AnalyzeSolution("OwinSelfhostSample.sln", tempDir, downloadLocation, version);

            string projectDir    = results.ProjectResults.FirstOrDefault().ProjectDirectory;
            var    csProjContent = results.ProjectResults.FirstOrDefault().CsProjectContent;
            var    startupText   = File.ReadAllText(Path.Combine(projectDir, "Startup.cs"));
            var    programText   = File.ReadAllText(Path.Combine(projectDir, "Program.cs"));

            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.OwinSelfHostProgram, programText);
            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.OwinSelfHostStartup, startupText);

            //Check that package has been added:
            StringAssert.Contains(@"Microsoft.AspNetCore.Diagnostics", csProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore.Server.Kestrel", csProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore.Hosting", csProjContent);

            //Check that correct version is used
            Assert.True(csProjContent.IndexOf(string.Concat(">", version, "<")) > 0);
        }
Ejemplo n.º 2
0
Archivo: OwinTests.cs Proyecto: aws/cta
        public void TestCustomServer(string version)
        {
            TestSolutionAnalysis results = AnalyzeSolution("CustomServer.sln", tempDir, downloadLocation, version);

            var myApp = results.ProjectResults.Where(p => p.CsProjectPath.EndsWith("MyApp.csproj")).FirstOrDefault();
            FileAssert.Exists(myApp.CsProjectPath);
            var customServer = results.ProjectResults.Where(p => p.CsProjectPath.EndsWith("MyCustomServer.csproj")).FirstOrDefault();
            FileAssert.Exists(customServer.CsProjectPath);

            //MyApp
            var myappProjContent = myApp.CsProjectContent;
            var startupText = File.ReadAllText(Path.Combine(myApp.ProjectDirectory, "Startup.cs"));
            var programText = File.ReadAllText(Path.Combine(myApp.ProjectDirectory, "Program.cs"));

            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.CustomServerProgram.NormalizeNewLineChars(), programText.NormalizeNewLineChars());
            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.CustomServerStartup.NormalizeNewLineChars(), startupText.NormalizeNewLineChars());

            //Check for comment on how to implement a custom server here in program

            //Check that package has been added:
            StringAssert.Contains(@"Microsoft.AspNetCore.Owin", myappProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore.Diagnostics", myappProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore.Server.Kestrel", myappProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore.Hosting", myappProjContent);

            //Check that correct version is used
            Assert.True(myappProjContent.IndexOf(string.Concat(">", version, "<")) > 0);

            //CustomServer
            var customProjContent = customServer.CsProjectContent;
            var customerServerText = File.ReadAllText(Path.Combine(customServer.ProjectDirectory, "CustomServer.cs"));
            var serverFactoryText = File.ReadAllText(Path.Combine(customServer.ProjectDirectory, "ServerFactory.cs"));

            //MyCustomerServer very difficult
            //Keep their server intact but must implement IServer instead of just IDisposable
            //Change their Start class to implement StartAsync instead and change reference to it to startAsync also

            //Check that correct version is used
            Assert.True(customProjContent.IndexOf(string.Concat(">", version, "<")) > 0);
        }
Ejemplo n.º 3
0
        private void TestWebSocketSample(string version)
        {
            TestSolutionAnalysis results = AnalyzeSolution("WebSocketSample.sln", tempDir, downloadLocation, version);

            var sampleClient = results.ProjectResults.Where(p => p.CsProjectPath.EndsWith("SampleClient.csproj")).FirstOrDefault();

            FileAssert.Exists(sampleClient.CsProjectPath);
            var webSocketServer = results.ProjectResults.Where(p => p.CsProjectPath.EndsWith("WebSocketServer.csproj")).FirstOrDefault();

            FileAssert.Exists(webSocketServer.CsProjectPath);

            var clientProgramText = File.ReadAllText(Path.Combine(sampleClient.ProjectDirectory, "Program.cs"));

            var serverStartupText = File.ReadAllText(Path.Combine(webSocketServer.ProjectDirectory, "Startup.cs"));
            var serverProgramText = File.ReadAllText(Path.Combine(webSocketServer.ProjectDirectory, "Program.cs"));

            StringAssert.Contains(@"Microsoft.AspNetCore.Builder", serverStartupText);
            StringAssert.Contains(@"IApplicationBuilder", serverStartupText);
            StringAssert.Contains(@"UseOwin", serverStartupText);

            StringAssert.Contains("WebHostBuilder", serverProgramText);
        }
Ejemplo n.º 4
0
Archivo: OwinTests.cs Proyecto: aws/cta
        public void TestEmbedded(string version)
        {
            TestSolutionAnalysis results = AnalyzeSolution("Embedded.sln", tempDir, downloadLocation, version);

            string projectDir = results.ProjectResults.FirstOrDefault().ProjectDirectory;
            var csProjContent = results.ProjectResults.FirstOrDefault().CsProjectContent;

            //Host program.cs rule and dependencies
            var startupText = File.ReadAllText(Path.Combine(projectDir, "Startup.cs"));
            var programText = File.ReadAllText(Path.Combine(projectDir, "Program.cs"));

            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.EmbeddedStartup.NormalizeNewLineChars(), startupText.NormalizeNewLineChars());
            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.EmbeddedProgram.NormalizeNewLineChars(), programText.NormalizeNewLineChars());

            //Check that package has been added:
            StringAssert.Contains(@"Microsoft.AspNetCore.Owin", csProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore.Diagnostics", csProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore.Server.Kestrel", csProjContent);

            //Check that correct version is used
            Assert.True(csProjContent.IndexOf(string.Concat(">", version, "<")) > 0);
        }
Ejemplo n.º 5
0
        private void TestAspNetRoutes(string version)
        {
            TestSolutionAnalysis results = AnalyzeSolution("AspNetRoutes.sln", tempDir, downloadLocation, version);

            var    analysisResult = results.SolutionAnalysisResult;
            var    csProjContent  = results.ProjectResults.FirstOrDefault().CsProjectContent;
            string projectDir     = results.ProjectResults.FirstOrDefault().ProjectDirectory;

            StringAssert.Contains("Microsoft.AspNetCore.Owin", analysisResult);
            StringAssert.Contains("Replace IOwinContext with HttpContext", analysisResult);

            var startupText = File.ReadAllText(Path.Combine(projectDir, "Startup.cs"));
            //var programText = File.ReadAllText(Path.Combine(projectDir, "Program.cs"));
            var owinapp2Text = File.ReadAllText(Path.Combine(projectDir, "OwinApp2.cs"));

            //Check that namespace was added
            StringAssert.Contains(@"using Microsoft.AspNetCore.Http", owinapp2Text);

            //Check that httpcontext is added (with a space) to make sure it's not httpcontextbase
            StringAssert.Contains(@"HttpContext ", owinapp2Text);

            //Check that namespaces were added
            //StringAssert.Contains(@"Microsoft.AspNetCore.Hosting", programText);
            //StringAssert.Contains(@"Microsoft.Extensions.Hosting;", programText);

            //Check that namespace was added
            StringAssert.Contains(@"Microsoft.AspNetCore.Http", startupText);

            //Check that httpcontext is added (with a space) to make sure it's not httpcontextbase
            StringAssert.Contains(@"HttpContext ", startupText);

            //Check that files have been created
            FileAssert.Exists(Path.Combine(projectDir, "Startup.cs"));
            //FileAssert.Exists(Path.Combine(projectDir, "Program.cs")); // This should be added but class library does not do this

            //Check that package has been added:
            //Assert.True(csProjContent.IndexOf(@"Microsoft.AspNetCore.Owin") > 0);
        }
Ejemplo n.º 6
0
Archivo: WCFTests.cs Proyecto: aws/cta
        public void TestWCFServiceLibrary(string version)
        {
            var solutionPath             = CopySolutionFolderToTemp("WCFTCPSelfHost.sln", tempDir);
            TestSolutionAnalysis results = AnalyzeSolution(solutionPath, version);

            var testCaseName = "WcfServiceLibrary1";
            var project      = results.ProjectResults.Where(prop => prop.CsProjectPath.EndsWith(testCaseName + ".csproj")).FirstOrDefault();
            var projectDir   = Directory.GetParent(project.CsProjectPath).FullName;

            var csProjContent = project.CsProjectContent;

            var startupFile = Path.Combine(projectDir, "Startup.cs");
            var programFile = Path.Combine(projectDir, "Program.cs");

            FileAssert.DoesNotExist(startupFile);
            FileAssert.DoesNotExist(programFile);

            StringAssert.Contains(@"CoreWCF.Primitives", csProjContent);
            StringAssert.Contains(@"CoreWCF.Http", csProjContent);
            StringAssert.Contains(@"CoreWCF.NetTcp", csProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore", csProjContent);
            StringAssert.DoesNotContain(@"CoreWCF.ConfigurationManager", csProjContent);
        }
Ejemplo n.º 7
0
        private void TestEmbedded(string version)
        {
            TestSolutionAnalysis results = AnalyzeSolution("Embedded.sln", tempDir, downloadLocation, version);

            var analysisResult = results.SolutionAnalysisResult;

            string projectDir = results.ProjectResults.FirstOrDefault().ProjectDirectory;

            StringAssert.Contains("Microsoft.AspNetCore.Owin", analysisResult);
            StringAssert.Contains("Microsoft.AspNetCore.Hosting", analysisResult);
            StringAssert.Contains("Microsoft.AspNetCore.Server.Kestrel", analysisResult);

            //Host program.cs rule and dependencies

            var startupText = File.ReadAllText(Path.Combine(projectDir, "Startup.cs"));
            var programText = File.ReadAllText(Path.Combine(projectDir, "Program.cs"));

            StringAssert.Contains(@"Microsoft.AspNetCore.Builder", startupText);
            StringAssert.Contains(@"IApplicationBuilder", startupText);

            StringAssert.Contains("Microsoft.AspNetCore.Hosting", programText);
            StringAssert.Contains("Microsoft.AspNetCore.Server.Kestrel", programText);
            StringAssert.Contains("WebHostBuilder", programText);
        }
Ejemplo n.º 8
0
Archivo: OwinTests.cs Proyecto: aws/cta
        public void TestBranchingPipelines(string version)
        {
            TestSolutionAnalysis results = AnalyzeSolution("BranchingPipelines.sln", tempDir, downloadLocation, version);

            string projectDir = results.ProjectResults.FirstOrDefault().ProjectDirectory;
            var csProjContent = results.ProjectResults.FirstOrDefault().CsProjectContent;
            var startupText = File.ReadAllText(Path.Combine(projectDir, "Startup.cs"));
            //var programText = File.ReadAllText(Path.Combine(projectDir, "Program.cs"));
            var displayText = File.ReadAllText(Path.Combine(projectDir, "DisplayBreadCrumbs.cs"));
            var addText = File.ReadAllText(Path.Combine(projectDir, "AddBreadCrumbMiddleware.cs"));

            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.BranchingPipelinesDisplayBreadCrumbs.NormalizeNewLineChars(), displayText.NormalizeNewLineChars());
            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.BranchingPipelinesAddBreadCrumbMiddleware.NormalizeNewLineChars(), addText.NormalizeNewLineChars());
            StringAssert.AreEqualIgnoringCase(ExpectedOutputConstants.BranchingPipelinesStartup.NormalizeNewLineChars(), startupText.NormalizeNewLineChars());

            //Check that package has been added:
            StringAssert.Contains(@"Microsoft.AspNetCore.Owin", csProjContent);
            StringAssert.Contains(@"Microsoft.AspNetCore.Diagnostics", csProjContent);

            //Check that correct version is used
            Assert.True(csProjContent.IndexOf(string.Concat(">", version, "<")) > 0);

            //FileAssert.Exists(Path.Combine(projectDir, "Program.cs")); // This should be added but class library does not do this
        }
Ejemplo n.º 9
0
        public TestSolutionAnalysis AnalyzeSolution(
            string solutionPath,
            string version,
            Dictionary <string, List <string> > metaReferences = null,
            bool skipCopy    = false,
            bool portCode    = true,
            bool portProject = true)
        {
            TestSolutionAnalysis result = new TestSolutionAnalysis();

            if (solutionPath != null && solutionPath.Length > 0)
            {
                List <PortCoreConfiguration> solutionPortConfiguration = new List <PortCoreConfiguration>();
                IEnumerable <string>         projectFiles = Utils.GetProjectPaths(solutionPath);

                if (projectFiles != null && projectFiles.Count() > 0)
                {
                    foreach (string projectFile in projectFiles)
                    {
                        Dictionary <string, Tuple <string, string> > packages = new Dictionary <string, Tuple <string, string> >
                        {
                            { "Newtonsoft.Json", new Tuple <string, string>("9.0.0", "*") }
                        };
                        PortCoreConfiguration projectConfiguration = new PortCoreConfiguration()
                        {
                            SolutionPath    = solutionPath,
                            ProjectPath     = projectFile,
                            UseDefaultRules = true,
                            TargetVersions  = new List <string> {
                                version
                            },
                            PackageReferences = packages,
                            PortCode          = portCode,
                            PortProject       = portProject
                        };

                        if (metaReferences != null)
                        {
                            projectConfiguration.MetaReferences = metaReferences.ContainsKey(projectFile) ? metaReferences[projectFile] : null;
                        }

                        solutionPortConfiguration.Add(projectConfiguration);
                        result.ProjectResults.Add(new ProjectResult()
                        {
                            CsProjectPath    = projectFile,
                            ProjectDirectory = Directory.GetParent(projectFile).FullName
                        });
                    }

                    // SolutionPort should remove this extra config because it does not have a matching analyzer result.
                    // Otherwise will hit KeyNotFoundException
                    solutionPortConfiguration.Add(new PortCoreConfiguration
                    {
                        SolutionPath    = solutionPath,
                        ProjectPath     = "fakeproject.csproj",
                        UseDefaultRules = true,
                    });

                    SolutionPort solutionPort = new SolutionPort(solutionPath, solutionPortConfiguration);
                    CopyTestRules();
                    var analysisRunResult = solutionPort.AnalysisRun();

                    StringBuilder str = new StringBuilder();
                    foreach (var projectResult in analysisRunResult.ProjectResults)
                    {
                        Assert.IsTrue(projectResult.ProjectActions.ToSummaryString()?.Length > 0);
                        StringBuilder projectResults = new StringBuilder();
                        projectResults.AppendLine(projectResult.ProjectFile);
                        projectResults.AppendLine(projectResult.ProjectActions.ToString());
                        result.ProjectResults.Where(p => p.CsProjectPath == projectResult.ProjectFile).FirstOrDefault().ProjectAnalysisResult = projectResults.ToString();

                        str.Append(projectResults);
                    }
                    result.SolutionAnalysisResult = str.ToString();


                    var runResult = solutionPort.Run();

                    foreach (var projectFile in result.ProjectResults)
                    {
                        projectFile.CsProjectContent = File.ReadAllText(projectFile.CsProjectPath);
                    }

                    result.SolutionRunResult = runResult;
                }
            }
            return(result);
        }
Ejemplo n.º 10
0
        private void ValidateMvcMusicStore(TestSolutionAnalysis results, string version)
        {
            var    analysisResult = results.ProjectResults.FirstOrDefault().ProjectAnalysisResult;
            var    csProjContent  = results.ProjectResults.FirstOrDefault().CsProjectContent;
            string projectDir     = results.ProjectResults.FirstOrDefault().ProjectDirectory;

            StringAssert.Contains("HtmlEncoder", analysisResult);
            StringAssert.Contains("EntityFrameworkCore", analysisResult);

            var accountControllerText      = File.ReadAllText(Path.Combine(projectDir, "Controllers", "AccountController.cs"));
            var checkoutControllerText     = File.ReadAllText(Path.Combine(projectDir, "Controllers", "CheckoutController.cs"));
            var shoppingCartControllerText = File.ReadAllText(Path.Combine(projectDir, "Controllers", "ShoppingCartController.cs"));
            var storeManagerControllerText = File.ReadAllText(Path.Combine(projectDir, "Controllers", "StoreManagerController.cs"));
            var musicStoreEntitiesText     = File.ReadAllText(Path.Combine(projectDir, "Models", "MusicStoreEntities.cs"));
            var shoppingCartText           = File.ReadAllText(Path.Combine(projectDir, "Models", "ShoppingCart.cs"));


            //Check that namespace was added
            StringAssert.Contains(@"Microsoft.AspNetCore.Mvc", accountControllerText);

            //Check that namespace was added
            StringAssert.Contains(@"Microsoft.AspNetCore.Mvc", checkoutControllerText);

            //Check that method was replaced
            StringAssert.Contains(@"HtmlEncoder.Default.Encode", shoppingCartControllerText);

            //Check that namespaces were added
            StringAssert.Contains(@"Microsoft.AspNetCore.Mvc", storeManagerControllerText);
            StringAssert.Contains(@"Microsoft.EntityFrameworkCore", storeManagerControllerText);

            //Check that namespace was added
            StringAssert.Contains(@"OnConfiguring", musicStoreEntitiesText);
            StringAssert.Contains(@"Microsoft.EntityFrameworkCore", musicStoreEntitiesText);

            //Check that httpcontext is added (with a space) to make sure it's not httpcontextbase
            StringAssert.Contains(@"HttpContext ", shoppingCartText);

            //Check that wwwroot dir was created
            DirectoryAssert.Exists(Path.Combine(projectDir, "wwwroot"));

            //Check that files have been created
            FileAssert.Exists(Path.Combine(projectDir, "Startup.cs"));
            FileAssert.Exists(Path.Combine(projectDir, "Program.cs"));
            FileAssert.Exists(Path.Combine(projectDir, "appsettings.json"));

            //Check that package has been added:
            Assert.True(csProjContent.IndexOf(@"Microsoft.EntityFrameworkCore") > 0);

            //Check that correct version is used
            Assert.True(csProjContent.IndexOf(string.Concat(@">", version, "<")) > 0);


            Assert.True(csProjContent.IndexOf("Newtonsoft.Json") > 0);


            //Check project actions
            var mvcProjectActions = results.SolutionRunResult.ProjectResults.First(p => p.ProjectFile.EndsWith("MvcMusicStore.csproj"))
                                    .ExecutedActions.First(a => a.Key == "Project").Value;

            Assert.AreEqual(mvcProjectActions.Count, 4);
        }
Ejemplo n.º 11
0
        private void ValidateWebApiWithReferences(TestSolutionAnalysis results)
        {
            StringAssert.Contains("IActionResult", results.SolutionAnalysisResult);
            StringAssert.Contains("Startup", results.SolutionAnalysisResult);


            var webProjectFile = results.ProjectResults.Where(p => p.CsProjectPath.EndsWith("WebApiWithReferences.csproj")).FirstOrDefault();

            FileAssert.Exists(webProjectFile.CsProjectPath);
            var testClassLibraryProjectFile = results.ProjectResults.Where(p => p.CsProjectPath.EndsWith("TestReference.csproj")).FirstOrDefault();

            FileAssert.Exists(testClassLibraryProjectFile.CsProjectPath);
            var testClassLibrary2ProjectFile = results.ProjectResults.Where(p => p.CsProjectPath.EndsWith("TestReference2.csproj")).FirstOrDefault();

            FileAssert.Exists(testClassLibrary2ProjectFile.CsProjectPath);

            var webCsProjContent           = webProjectFile.CsProjectContent;
            var classLibraryCsProjContent  = testClassLibraryProjectFile.CsProjectContent;
            var classLibrary2CsProjContent = testClassLibrary2ProjectFile.CsProjectContent;

            string webProjectDir = Directory.GetParent(webProjectFile.CsProjectPath).FullName;

            var homeControllerText = File.ReadAllText(Path.Combine(webProjectDir, "Controllers", "HouseController.cs"));

            //Check that namespace has been added
            StringAssert.Contains(@"Microsoft.AspNetCore.Mvc", homeControllerText);

            //Check that attribute has been added to class, and base class changed:
            StringAssert.Contains(@"[ApiController]", homeControllerText);
            StringAssert.Contains(@"ControllerBase", homeControllerText);

            //Check that invocation expression was replaced:
            StringAssert.Contains(@"this.Response.Headers.Add", homeControllerText);

            //Check that identifier as replaced:
            StringAssert.Contains(@"IActionResult", homeControllerText);
            //Check that identifier as replaced:
            StringAssert.DoesNotContain(@"IHttpActionResult", homeControllerText);

            //Check that files have been created
            FileAssert.Exists(Path.Combine(webProjectDir, "Startup.cs"));
            FileAssert.Exists(Path.Combine(webProjectDir, "Program.cs"));

            //Check that package has been added:
            StringAssert.Contains(@"Microsoft.AspNetCore.OData", webCsProjContent);

            //Check the framework references used
            Assert.True(webCsProjContent.IndexOf(@"Microsoft.NET.Sdk.Web") > 0);
            Assert.True(classLibraryCsProjContent.IndexOf("Microsoft.NET.Sdk") > 0);
            Assert.True(classLibrary2CsProjContent.IndexOf("Microsoft.NET.Sdk") > 0);

            //Check that projects are still referenced
            Assert.True(webCsProjContent.IndexOf(@"ProjectReference Include=") > 0);
            Assert.True(webCsProjContent.IndexOf(@"..\TestReference\TestReference.csproj") > 0);
            Assert.True(webCsProjContent.IndexOf(@"..\TestReference2\TestReference2.csproj""") > 0);

            Assert.True(webCsProjContent.IndexOf("Newtonsoft.Json") > 0);

            //Check project actions
            var classlibrary1Actions = results.SolutionRunResult.ProjectResults.First(p => p.ProjectFile.EndsWith("TestReference.csproj"))
                                       .ExecutedActions.First(a => a.Key == "Project").Value;
            var classlibrary2Actions = results.SolutionRunResult.ProjectResults.First(p => p.ProjectFile.EndsWith("TestReference2.csproj"))
                                       .ExecutedActions.First(a => a.Key == "Project").Value;
            var webApiProjectActions = results.SolutionRunResult.ProjectResults.First(p => p.ProjectFile.EndsWith("WebApiWithReferences.csproj"))
                                       .ExecutedActions.First(a => a.Key == "Project").Value;

            Assert.AreEqual(classlibrary1Actions.Count, 2);
            Assert.AreEqual(classlibrary2Actions.Count, 2);
            Assert.AreEqual(webApiProjectActions.Count, 4);
        }
Ejemplo n.º 12
0
        public TestSolutionAnalysis AnalyzeSolution(
            string solutionPath,
            string version,
            Dictionary <string, List <string> > metaReferences = null,
            bool skipCopy    = false,
            bool portCode    = true,
            bool portProject = true)
        {
            TestSolutionAnalysis result = new TestSolutionAnalysis();
            var solutionDir             = Directory.GetParent(solutionPath).FullName;

            if (solutionPath != null && solutionPath.Length > 0)
            {
                List <PortCoreConfiguration> solutionPortConfiguration = new List <PortCoreConfiguration>();
                IEnumerable <string>         projectFiles = Directory.EnumerateFiles(solutionDir, "*.csproj", SearchOption.AllDirectories);

                if (projectFiles != null && projectFiles.Count() > 0)
                {
                    foreach (string projectFile in projectFiles)
                    {
                        Dictionary <string, Tuple <string, string> > packages = new Dictionary <string, Tuple <string, string> >
                        {
                            { "Newtonsoft.Json", new Tuple <string, string>("9.0.0", "*") }
                        };
                        PortCoreConfiguration projectConfiguration = new PortCoreConfiguration()
                        {
                            ProjectPath     = projectFile,
                            UseDefaultRules = true,
                            TargetVersions  = new List <string> {
                                version
                            },
                            PackageReferences = packages,
                            PortCode          = portCode,
                            PortProject       = portProject
                        };

                        if (metaReferences != null)
                        {
                            projectConfiguration.MetaReferences = metaReferences.ContainsKey(projectFile) ? metaReferences[projectFile] : null;
                        }

                        solutionPortConfiguration.Add(projectConfiguration);
                    }

                    // SolutionPort should remove this extra config because it does not have a matching analyzer result.
                    // Otherwise will hit KeyNotFoundException
                    solutionPortConfiguration.Add(new PortCoreConfiguration
                    {
                        ProjectPath     = "fakeproject.csproj",
                        UseDefaultRules = true,
                    });

                    SolutionPort solutionPort = new SolutionPort(solutionPath, solutionPortConfiguration);
                    CopyTestRules();
                    var analysisRunResult = solutionPort.AnalysisRun();

                    foreach (var projectResult in analysisRunResult.ProjectResults)
                    {
                        Assert.IsTrue(projectResult.ProjectActions.ToSummaryString()?.Length > 0);
                    }
                    var runResult = solutionPort.Run();
                    result = GenerateSolutionResult(solutionDir, analysisRunResult, runResult);
                }
            }
            return(result);
        }
Ejemplo n.º 13
0
        public TestSolutionAnalysis AnalyzeSolution(string solutionName, string tempDir, string downloadLocation, string version, bool skipCopy = false)
        {
            TestSolutionAnalysis result = new TestSolutionAnalysis();

            var sourceDir   = Directory.GetParent(Directory.EnumerateFiles(downloadLocation, solutionName, SearchOption.AllDirectories).FirstOrDefault());
            var solutionDir = Path.Combine(tempDir, version);

            if (!skipCopy)
            {
                CopyDirectory(sourceDir, new DirectoryInfo(solutionDir));
            }
            else
            {
                solutionDir = tempDir;
            }
            string solutionPath = Directory.EnumerateFiles(solutionDir, solutionName, SearchOption.AllDirectories).FirstOrDefault();

            if (solutionPath != null && solutionPath.Length > 0)
            {
                List <PortCoreConfiguration> solutionPortConfiguration = new List <PortCoreConfiguration>();
                IEnumerable <string>         projectFiles = Directory.EnumerateFiles(solutionDir, "*.csproj", SearchOption.AllDirectories);

                if (projectFiles != null && projectFiles.Count() > 0)
                {
                    foreach (string projectFile in projectFiles)
                    {
                        ProjectResult project = new ProjectResult();

                        Dictionary <string, string> packages = new Dictionary <string, string>();
                        packages.Add("Newtonsoft.Json", "*");
                        PortCoreConfiguration projectConfiguration = new PortCoreConfiguration()
                        {
                            ProjectPath     = projectFile,
                            UseDefaultRules = true,
                            TargetVersions  = new List <string> {
                                version
                            },
                            PackageReferences = packages
                        };

                        //project.CsProjectContent = File.ReadAllText(projectFile);
                        project.ProjectDirectory = Directory.GetParent(projectFile).FullName;
                        project.CsProjectPath    = projectFile;

                        result.ProjectResults.Add(project);

                        solutionPortConfiguration.Add(projectConfiguration);
                    }

                    SolutionPort solutionPort      = new SolutionPort(solutionPath, solutionPortConfiguration);
                    var          analysisRunResult = solutionPort.AnalysisRun();

                    foreach (var projectAction in analysisRunResult.Values)
                    {
                        Assert.IsTrue(projectAction.ToSummaryString()?.Length > 0);
                    }

                    StringBuilder str = new StringBuilder();
                    foreach (var k in analysisRunResult.Keys)
                    {
                        StringBuilder projectResults = new StringBuilder();
                        projectResults.AppendLine(k);
                        projectResults.AppendLine(analysisRunResult[k].ToString());
                        result.ProjectResults.Where(p => p.CsProjectPath == k).FirstOrDefault().ProjectAnalysisResult = projectResults.ToString();

                        str.Append(projectResults);
                    }

                    result.SolutionAnalysisResult = str.ToString();

                    var runResult = solutionPort.Run();
                    result.SolutionRunResult = runResult;

                    foreach (var project in result.ProjectResults)
                    {
                        project.CsProjectContent = File.ReadAllText(project.CsProjectPath);
                    }
                }
            }
            return(result);
        }