protected override Task <int> Execute()
        {
            var consoleReport = new ConsoleReport();
            var result        = consoleReport.Execute(_coverageLoadedFileOption.Result, _thresholdOption.Value);

            return(Task.FromResult(result));
        }
Exemple #2
0
        static int Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            var commandLineApplication = new CommandLineApplication();

            commandLineApplication.Name        = "MiniCover";
            commandLineApplication.Description = "MiniCover - Code coverage for .NET Core via assembly instrumentation";

            commandLineApplication.Command("instrument", command =>
            {
                command.Description = "Instrument assemblies";

                var workDirOption           = CreateWorkdirOption(command);
                var includeAssembliesOption = command.Option("--assemblies", "Pattern to include assemblies [default: **/*.dll]", CommandOptionType.MultipleValue);
                var excludeAssembliesOption = command.Option("--exclude-assemblies", "Pattern to exclude assemblies", CommandOptionType.MultipleValue);
                var includeSourceOption     = command.Option("--sources", "Pattern to include source files [default: **/*]", CommandOptionType.MultipleValue);
                var excludeSourceOption     = command.Option("--exclude-sources", "Pattern to exclude source files", CommandOptionType.MultipleValue);
                var hitsFileOption          = command.Option("--hits-file", "Hits file name [default: coverage-hits.txt]", CommandOptionType.SingleValue);
                var coverageFileOption      = CreateCoverageFileOption(command);

                command.HelpOption("-h | --help");

                command.OnExecute(() =>
                {
                    var workdir = UpdateWorkingDirectory(workDirOption);

                    var assemblies = GetFiles(includeAssembliesOption, excludeAssembliesOption, "**/*.dll");
                    if (assemblies.Length == 0)
                    {
                        throw new Exception("No assemblies found");
                    }

                    var sourceFiles = GetFiles(includeSourceOption, excludeSourceOption, "**/*.cs");
                    if (sourceFiles.Length == 0)
                    {
                        throw new Exception("No source files found");
                    }

                    var hitsFile     = GetHitsFile(hitsFileOption);
                    var coverageFile = GetCoverageFile(coverageFileOption);
                    var instrumenter = new Instrumenter(assemblies, hitsFile, sourceFiles, workdir);
                    var result       = instrumenter.Execute();
                    SaveCoverageFile(coverageFile, result);
                    return(0);
                });
            });

            commandLineApplication.Command("uninstrument", command =>
            {
                command.Description = "Uninstrument assemblies";

                var workDirOption      = CreateWorkdirOption(command);
                var coverageFileOption = CreateCoverageFileOption(command);
                command.HelpOption("-h | --help");

                command.OnExecute(() =>
                {
                    UpdateWorkingDirectory(workDirOption);

                    var coverageFile = GetCoverageFile(coverageFileOption);
                    var result       = LoadCoverageFile(coverageFile);
                    Uninstrumenter.Execute(result);
                    return(0);
                });
            });

            commandLineApplication.Command("report", command =>
            {
                command.Description = "Outputs coverage report";

                var workDirOption      = CreateWorkdirOption(command);
                var coverageFileOption = CreateCoverageFileOption(command);
                var thresholdOption    = CreateThresholdOption(command);
                command.HelpOption("-h | --help");

                command.OnExecute(() =>
                {
                    UpdateWorkingDirectory(workDirOption);

                    var coverageFile  = GetCoverageFile(coverageFileOption);
                    var threshold     = GetThreshold(thresholdOption);
                    var result        = LoadCoverageFile(coverageFile);
                    var consoleReport = new ConsoleReport();
                    return(consoleReport.Execute(result, threshold));
                });
            });

            commandLineApplication.Command("htmlreport", command =>
            {
                command.Description = "Write html report to folder";

                var workDirOption      = CreateWorkdirOption(command);
                var coverageFileOption = CreateCoverageFileOption(command);
                var thresholdOption    = CreateThresholdOption(command);
                var outputOption       = command.Option("--output", "Output folder for html report [default: coverage-html]", CommandOptionType.SingleValue);
                command.HelpOption("-h | --help");

                command.OnExecute(() =>
                {
                    UpdateWorkingDirectory(workDirOption);

                    var coverageFile = GetCoverageFile(coverageFileOption);
                    var threshold    = GetThreshold(thresholdOption);
                    var result       = LoadCoverageFile(coverageFile);
                    var output       = GetHtmlReportOutput(outputOption);
                    var htmlReport   = new HtmlReport(output);
                    return(htmlReport.Execute(result, threshold));
                });
            });

            commandLineApplication.Command("xmlreport", command =>
            {
                command.Description = "Write an NCover-formatted XML report to folder";

                var workDirOption      = CreateWorkdirOption(command);
                var coverageFileOption = CreateCoverageFileOption(command);
                var thresholdOption    = CreateThresholdOption(command);
                var outputOption       = command.Option("--output", "Output file for NCover report [default: coverage.xml]", CommandOptionType.SingleValue);
                command.HelpOption("-h | --help");

                command.OnExecute(() =>
                {
                    UpdateWorkingDirectory(workDirOption);

                    var coverageFile = GetCoverageFile(coverageFileOption);
                    var threshold    = GetThreshold(thresholdOption);
                    var result       = LoadCoverageFile(coverageFile);
                    var output       = GetXmlReportOutput(outputOption);
                    XmlReport.Execute(result, output, threshold);
                    return(0);
                });
            });

            commandLineApplication.Command("opencoverreport", command =>
            {
                command.Description = "Write an OpenCover-formatted XML report to folder";

                var workDirOption      = CreateWorkdirOption(command);
                var coverageFileOption = CreateCoverageFileOption(command);
                var thresholdOption    = CreateThresholdOption(command);
                var outputOption       = command.Option("--output", "Output file for OpenCover report [default: opencovercoverage.xml]", CommandOptionType.SingleValue);
                command.HelpOption("-h | --help");

                command.OnExecute(() =>
                {
                    UpdateWorkingDirectory(workDirOption);

                    var coverageFile = GetCoverageFile(coverageFileOption);
                    var threshold    = GetThreshold(thresholdOption);
                    var result       = LoadCoverageFile(coverageFile);
                    var output       = GetOpenCoverXmlReportOutput(outputOption);
                    OpenCoverReport.Execute(result, output, threshold);
                    return(0);
                });
            });

            commandLineApplication.Command("reset", command =>
            {
                command.Description = "Reset hits count";

                var workDirOption      = CreateWorkdirOption(command);
                var coverageFileOption = CreateCoverageFileOption(command);
                command.HelpOption("-h | --help");

                command.OnExecute(() =>
                {
                    UpdateWorkingDirectory(workDirOption);

                    var coverageFile = GetCoverageFile(coverageFileOption);

                    if (File.Exists(coverageFile))
                    {
                        var result = LoadCoverageFile(coverageFile);

                        if (File.Exists(result.HitsFile))
                        {
                            File.Delete(result.HitsFile);
                        }
                    }

                    return(0);
                });
            });

            commandLineApplication.HelpOption("-h | --help");

            commandLineApplication.OnExecute(() =>
            {
                commandLineApplication.ShowHelp();
                return(0);
            });

            return(commandLineApplication.Execute(args));
        }