From b1706d7fd8b737da3bcba945c25848d62bd0f5a8 Mon Sep 17 00:00:00 2001 From: Oleg Yankovich Date: Mon, 2 Dec 2019 14:02:19 +0300 Subject: [PATCH] Add supporting source tag --- .../Converters/CoberturaConverter.cs | 11 ++++---- CoverageUploader/CoverageUploader/Help.cs | 27 +++++++++++++------ CoverageUploader/CoverageUploader/Program.cs | 24 +++++++++-------- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/CoverageUploader/CoverageUploader/Converters/CoberturaConverter.cs b/CoverageUploader/CoverageUploader/Converters/CoberturaConverter.cs index 5f7194a..bdb3c1f 100644 --- a/CoverageUploader/CoverageUploader/Converters/CoberturaConverter.cs +++ b/CoverageUploader/CoverageUploader/Converters/CoberturaConverter.cs @@ -12,17 +12,16 @@ class CoberturaConverter private const string _pathToLine = "lines/line"; private const string _pathToSource = "coverage/sources/source"; - public static JSONCoverage ConvertFromCobertura(string pathToCoverageFile) + public static JSONCoverage ConvertFromCobertura(string pathToCoverageFile, string repoRoot) { JSONCoverage jsonCoverage = new JSONCoverage(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(pathToCoverageFile); - XmlNode sourceNode = xmlDocument.SelectSingleNode(_pathToSource); - string source = string.Equals(sourceNode.Value, null) ? string.Empty : sourceNode.Value; + string source = xmlDocument.SelectSingleNode(_pathToSource).InnerText; + repoRoot = string.Equals(repoRoot, null) ? string.Empty : repoRoot; foreach (XmlNode xmlNode in xmlDocument.SelectNodes(_pathToClass)) - { - Console.WriteLine(Path.Combine(source, xmlNode.Attributes["filename"].Value)); - string fileName = Help.GetRelativeFilePath(Path.Combine(source, xmlNode.Attributes["filename"].Value)); + { + string fileName = Help.GetRelativeFilePath(Path.Combine(source, xmlNode.Attributes["filename"].Value), repoRoot); FileCoverage fileCoverage = new FileCoverage(fileName); var a = xmlNode.SelectNodes(_pathToLine).Count; foreach (XmlNode node in xmlNode.SelectNodes(_pathToLine)) diff --git a/CoverageUploader/CoverageUploader/Help.cs b/CoverageUploader/CoverageUploader/Help.cs index 4dfa046..6c24d2a 100644 --- a/CoverageUploader/CoverageUploader/Help.cs +++ b/CoverageUploader/CoverageUploader/Help.cs @@ -8,14 +8,25 @@ namespace CoverageUploader { class Help { - public static string GetRelativeFilePath(string path) - { - string rootFolder = path; - do - { - rootFolder = Directory.GetParent(rootFolder).FullName; - } while (!Repository.IsValid(rootFolder)); - return Path.GetRelativePath(rootFolder, path).Replace("\\","/"); + public static string GetRelativeFilePath(string path, string repoRoot) + { + string rootFolder = string.IsNullOrEmpty(repoRoot) ? path : repoRoot; + while (!Repository.IsValid(rootFolder)) + { + rootFolder = Directory.GetParent(rootFolder).FullName; + } + if (!string.IsNullOrEmpty(repoRoot)) { + DirectoryInfo relativeFullPath = Directory.GetParent(path); + string fileName = Path.GetFileName(path); + string relativePath = string.Empty; + while (!File.Exists(path)) + { + relativePath = Path.Combine(Path.Combine(relativeFullPath.Name), relativePath); + path = Path.Combine(rootFolder, Path.Combine(relativePath, fileName)); + relativeFullPath = Directory.GetParent(relativeFullPath.FullName); + } + } + return Path.GetRelativePath(rootFolder, path).Replace("\\", "/"); } } } diff --git a/CoverageUploader/CoverageUploader/Program.cs b/CoverageUploader/CoverageUploader/Program.cs index 2cd922e..383cd56 100644 --- a/CoverageUploader/CoverageUploader/Program.cs +++ b/CoverageUploader/CoverageUploader/Program.cs @@ -22,25 +22,19 @@ public class Arguments public string BitbucketURL { get; set; } [Option('f', "coverage.file", Required = true, HelpText = "Path to coverage file")] public string CoverageFile { get; set; } + [Option('r', "repository.root", Required = false, HelpText = "Path to repository root folder")] + public string RepositoryRoot { get; set; } } class Program { static int Main(string[] args) - { - Parser.Default.ParseArguments(args) - .WithParsed(arguments => RunArguments(arguments)); - return -1; - } - - private static int RunArguments(Arguments arguments) { try { - JSONCoverage jsonCoverage = CoberturaConverter.ConvertFromCobertura(arguments.CoverageFile); - var client = new Client(arguments.BitbucketURL, arguments.Username, arguments.Password); - var responseMessage = client.PostCoverageAsync(arguments.CommidID, jsonCoverage.ConvertToJson()); - Console.WriteLine(responseMessage.Result.ToString()); + args = new string[] { "-u", "5", "-p", "2", "-c", "3", "-b", "4", "-f", "D:\\ServerUnitTests.xml" }; + Parser.Default.ParseArguments(args) + .WithParsed(arguments => RunArguments(arguments)); return 0; } catch(Exception e) @@ -49,5 +43,13 @@ private static int RunArguments(Arguments arguments) return -1; } } + + private static void RunArguments(Arguments arguments) + { + JSONCoverage jsonCoverage = CoberturaConverter.ConvertFromCobertura(arguments.CoverageFile, arguments.RepositoryRoot); + var client = new Client(arguments.BitbucketURL, arguments.Username, arguments.Password); + var responseMessage = client.PostCoverageAsync(arguments.CommidID, jsonCoverage.ConvertToJson()); + Console.WriteLine(responseMessage.Result.ToString()); + } } }