static int Main(string[] args) { if (args.Length == 1 && (args[0] == "-h" || args[0] == "--help" || args[0] == "/?")) { DisplayHelp(); return(0); } else { try { ParseArguments(args); } catch (Exception e) { Console.WriteLine(e.Message); DisplayHelp(); return(1); } } isVariantEdge = args[1] == "edge"; backupDirectory = FindBackupDirectory(); // input parameters: codePath = args[2]; string solPath = args[3]; string testProjPath = args[4]; string testDllPath = args[5]; string target = "--target dotnet"; string targetargs = $"--targetargs \"test {testProjPath} --no-build\""; string coveragePath = backupDirectory + @"\coverage.json"; string output = $"--output {coveragePath}"; string coverletCommand = String.Join(" ", new string[] { "coverlet", testDllPath, target, targetargs, output }); string buildCommand = String.Join(" ", new string[] { "dotnet", "build", solPath }); StaticCodeAnalysis codeAnalysis = new StaticCodeAnalysis(codePath); GraphCreator graphCreator = new GraphCreator(codeAnalysis); DirectedGraph graph; string coverageTargets; bool hasCoverageTargets; if (isVariantEdge) { graph = graphCreator.CreateYoYoGraph(); coverageTargets = "invocations"; hasCoverageTargets = graph.GetNodesOfType(Type.INVOCATION).Any(); } else // is variant 'type' { graph = graphCreator.CreateInheritanceGraph(); coverageTargets = "methods"; hasCoverageTargets = graph.GetNodesOfType(Type.METHOD).Any(); } // if the source code doesn't contain coverage targets then output 100% coverage and return if (!hasCoverageTargets) { Console.WriteLine(Environment.NewLine + $"The given source code does not contain any {coverageTargets}" + Environment.NewLine); OutputResult(1); return(0); } BackupFile(codePath); try { // manipulate the source code file to be able to gather information needed if (isVariantEdge) { graphCreator.PreCorrectLineNumbers(graph as YoYoGraph); // YoYo graphs contain line numbers which have to be corrected before code gets inserted } SyntaxRewriter rewriter = new SyntaxRewriter(codeAnalysis, graph); SyntaxNode newRoot = rewriter.Visit(codeAnalysis.GetRoot()); File.WriteAllText(codePath, newRoot.GetText().ToString(), Encoding.Default); Console.WriteLine(Environment.NewLine + $"Instrumented file '{codePath}'"); // re-build the solution RunCMDCommand(buildCommand, "Re-building solution ...", "build succeeded", "build failed"); // start coverlet RunCMDCommand(coverletCommand, "Starting coverlet ...", "coverlet calculation succeeded", "coverlet calculation failed"); // read the generated json file & calculate the coverage Console.WriteLine(Environment.NewLine + "Calculating coverage result ..."); Coverage coverage = new Coverage(coveragePath, codePath, graph); double result = coverage.Calculate(); OutputResultAndGraph(result, graph); } catch (Exception e) { Console.WriteLine(e.Message); return(1); } finally { RestoreFile(codePath); } return(0); }
public SyntaxRewriter(StaticCodeAnalysis codeAnalysis, DirectedGraph graph) { this.isVariantEdge = graph.GetType() == typeof(YoYoGraph); this.codeAnalysis = codeAnalysis; this.graph = graph; }