示例#1
0
        public SonarDocument Parse(string solutionDirectory, bool useAbsolutePath)
        {
            if (string.IsNullOrEmpty(solutionDirectory) || !Directory.Exists(solutionDirectory))
            {
                return(null);
            }

            IEnumerable <string> trxFiles = Directory.EnumerateFiles(solutionDirectory, "*.trx", SearchOption.AllDirectories);

            var sonarDocuments = new List <SonarDocument>();

            foreach (string trxFile in trxFiles)
            {
                _logger.LogInformation($"Parsing: {trxFile}");
                TrxDocument   trxDocument   = _trxParser.Deserialize(trxFile);
                SonarDocument sonarDocument = Convert(trxDocument, solutionDirectory, useAbsolutePath);

                if (sonarDocument != null)
                {
                    sonarDocuments.Add(sonarDocument);
                }
            }

            return(Merge(sonarDocuments));
        }
示例#2
0
        private SonarDocument Convert(TrxDocument trxDocument, string solutionDirectory, bool useAbsolutePath)
        {
            var sonarDocument = new SonarDocument();

            foreach (var trxResult in trxDocument.Results)
            {
                try
                {
                    var unitTest = trxResult.GetUnitTest(trxDocument);

                    var testFile = unitTest.GetTestFile(solutionDirectory, useAbsolutePath);


                    var file = sonarDocument.GetFile(testFile);

                    if (file == null)
                    {
                        file = new File(testFile);
                        sonarDocument.Files.Add(file);
                    }

                    var testCase = new TestCase(trxResult.TestName, Utils.ToSonarDuration(trxResult.Duration));

                    if (trxResult.Outcome != Outcome.Passed)
                    {
                        if (trxResult.Outcome == Outcome.NotExecuted)
                        {
                            testCase.Skipped = new Skipped();
                            this.logger.LogInformation($"Skipped: {trxResult.TestName}");
                        }
                        else
                        {
                            testCase.Failure = new Failure(trxResult.Output?.ErrorInfo?.Message ?? string.Empty, trxResult.Output?.ErrorInfo?.StackTrace ?? string.Empty);
                            this.logger.LogInformation($"Failure: {trxResult.TestName}");
                        }
                    }
                    else
                    {
                        this.logger.LogInformation($"Passed: {trxResult.TestName}");
                    }

                    file.TestCases.Add(testCase);
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex.Message);
                }
            }
            return(sonarDocument);
        }
示例#3
0
        private SonarDocument Convert(TrxDocument trxDocument, string solutionDirectory, bool useAbsolutePath)
        {
            var sonarDocument = new SonarDocument();

            foreach (var trxResult in trxDocument.Results)
            {
                var unitTest = trxResult.GetUnitTest(trxDocument);

                var testFile = unitTest.GetTestFile(solutionDirectory, useAbsolutePath);

                /*
                 * if (!System.IO.File.Exists(Path.Combine(solutionDirectory,testFile)))
                 * {
                 *  this.logger.LogWarning($"Test file seems to be wrong - Unable to find it: {testFile}");
                 * }
                 */

                var file = sonarDocument.GetFile(testFile);

                if (file == null)
                {
                    file = new File(testFile);
                    sonarDocument.Files.Add(file);
                }

                var testCase = new TestCase(trxResult.TestName, Utils.ToSonarDuration(trxResult.Duration));

                if (trxResult.Outcome != Outcome.Passed)
                {
                    if (trxResult.Outcome == Outcome.NotExecuted)
                    {
                        testCase.Skipped = new Skipped();
                        this.logger.LogInformation($"Skipped: {trxResult.TestName}");
                    }
                    else
                    {
                        testCase.Failure = new Failure(trxResult.Output?.ErrorInfo?.Message, trxResult.Output?.ErrorInfo?.StackTrace);
                        this.logger.LogInformation($"Failure: {trxResult.TestName}");
                    }
                }
                else
                {
                    this.logger.LogInformation($"Passed: {trxResult.TestName}");
                }

                file.TestCases.Add(testCase);
            }
            return(sonarDocument);
        }
示例#4
0
        private SonarDocument Merge(List <SonarDocument> sonarDocuments)
        {
            this.logger.LogInformation("Merge {0} file(s).", sonarDocuments.Count);
            if (sonarDocuments.Count == 1)
            {
                return(sonarDocuments.FirstOrDefault());
            }

            var result = new SonarDocument();

            foreach (var sonarDocument in sonarDocuments)
            {
                foreach (var sonarFile in sonarDocument.Files)
                {
                    result.Files.Add(sonarFile);
                }
            }
            return(result);
        }
示例#5
0
        private SonarDocument Merge(IReadOnlyList <SonarDocument> sonarDocuments)
        {
            _logger.LogInformation("Merge {0} file(s).", sonarDocuments.Count);
            if (sonarDocuments.Count == 1)
            {
                return(sonarDocuments[0]);
            }

            var result = new SonarDocument();

            foreach (SonarDocument sonarDocument in sonarDocuments)
            {
                foreach (File sonarFile in sonarDocument.Files)
                {
                    result.Files.Add(sonarFile);
                }
            }

            return(result);
        }
示例#6
0
        private static void Main(string[] args)
        {
            var serviceCollection = new ServiceCollection();

            ConfigureServices(serviceCollection);

            using ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            var app = new CommandLineApplication
            {
                Name        = "Trx To Sonar",
                Description = ""
            };

            app.HelpOption("-?|-h|--help");

            CommandOption solutionDirectoryOption =
                app.Option("-d", "Solution Directory to parse.", CommandOptionType.SingleValue);
            CommandOption outputOption       = app.Option("-o", "Output filename.", CommandOptionType.SingleValue);
            CommandOption absolutePathOption = app.Option("-a|--absolute", "Use Absolute Path", CommandOptionType.NoValue);

            app.OnExecute(
                () =>
            {
                if (solutionDirectoryOption.HasValue() && outputOption.HasValue())
                {
                    var converter = serviceProvider.GetService <IConverter>();
                    SonarDocument sonarDocument = converter.Parse(solutionDirectoryOption.Value(), absolutePathOption.HasValue());
                    converter.Save(sonarDocument, outputOption.Value());
                }
                else
                {
                    app.ShowHint();
                }

                return(0);
            });

            app.Execute(args);
        }
示例#7
0
 public bool Save(SonarDocument sonarDocument, string outputFilename)
 {
     return(this.sonarParser.Save(sonarDocument, outputFilename));
 }
示例#8
0
 public static File GetFile(this SonarDocument sonarDocument, string testFile)
 {
     return(sonarDocument.Files.FirstOrDefault(x => x.Path == testFile));
 }