PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/TRXMerge/TRXMerge/Program.cs

http://trxmerger.codeplex.com
C# | 229 lines | 181 code | 35 blank | 13 comment | 25 complexity | e7e946d797004241c93e9d3b2de48aab MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.IO;
  7. namespace TRXMerge
  8. {
  9. class Program
  10. {
  11. static int Main(string[] args)
  12. {
  13. string message = "\nUsage: TRXMerge <first trx file> <second trx file> <output XML file>\n\nInfo: Changes from second trx file will be overwritten on first if there are matching test cases";
  14. if (args.Length < 3)
  15. {
  16. Console.WriteLine(message);
  17. return 1;
  18. }
  19. else
  20. {
  21. if (!File.Exists(args[0])) { Console.WriteLine(message); return 1; }
  22. System.Xml.XmlDocument oDocFirst = new XmlDocument();
  23. oDocFirst.Load(MakeCompatXML(args[0]));
  24. if (!File.Exists(args[1])) { Console.WriteLine(message); return 1; }
  25. System.Xml.XmlDocument oDocSecond = new XmlDocument();
  26. oDocSecond.Load(MakeCompatXML(args[1]));
  27. ////locate sections in first and append data from second...
  28. XmlNode oNodeWhereInsert = oDocFirst.SelectSingleNode("//TestDefinitions");
  29. int i = 0;
  30. while (oDocSecond.SelectSingleNode("//TestDefinitions").ChildNodes.Count != i)
  31. {
  32. ////insert test only if it is not already present
  33. if (!IfTestExists(oDocFirst, oDocSecond.SelectSingleNode("//TestDefinitions").ChildNodes[i].Attributes["name"].Value))
  34. {
  35. oNodeWhereInsert.AppendChild(oDocFirst.ImportNode(oDocSecond.SelectSingleNode("//TestDefinitions").ChildNodes[i], true));
  36. }
  37. i++;
  38. }
  39. ////insert new results and update existing if present
  40. XmlNode oNodeWhereInsertResult = oDocFirst.SelectSingleNode("//Results");
  41. i = 0;
  42. while (oDocSecond.SelectSingleNode("//Results").ChildNodes.Count != i)
  43. {
  44. XmlNode oldNode;
  45. if (IfResultExists(oDocFirst, oDocSecond.SelectSingleNode("//Results").ChildNodes[i].Attributes["testName"].Value, out oldNode))
  46. {
  47. oNodeWhereInsertResult.RemoveChild(oldNode);
  48. oNodeWhereInsertResult.AppendChild(oDocFirst.ImportNode(oDocSecond.SelectSingleNode("//Results").ChildNodes[i], true));
  49. }
  50. else
  51. {
  52. oNodeWhereInsertResult.AppendChild(oDocFirst.ImportNode(oDocSecond.SelectSingleNode("//Results").ChildNodes[i], true));
  53. }
  54. i++;
  55. }
  56. if(File.Exists(args[2]))
  57. {
  58. File.Delete(args[2]);
  59. }
  60. oDocFirst.Save(args[2]);
  61. SetSummary(args[2]);
  62. return 0;
  63. }
  64. }
  65. public static summary GetSummary(XmlDocument doc)
  66. {
  67. summary s = new summary();
  68. s.total = -1;
  69. s.executed = -1;
  70. s.passed = -1;
  71. //XmlElement ele = doc.DocumentElement;
  72. XmlNode nTotal = doc.SelectNodes("//Counters/@total").Item(0);
  73. s.total = Convert.ToInt32(nTotal.InnerText);
  74. XmlNode nPass = doc.SelectNodes("//Counters/@passed").Item(0);
  75. s.passed = Convert.ToInt32(nPass.InnerText);
  76. XmlNode nExecuted = doc.SelectNodes("//Counters/@executed").Item(0);
  77. s.executed = Convert.ToInt32(nExecuted.InnerText);
  78. DateTime start;
  79. DateTime end0;
  80. XmlNode nStart = doc.SelectNodes("//Times/@start ").Item(0);
  81. start = Convert.ToDateTime(nStart.InnerText);
  82. XmlNode nEnd = doc.SelectNodes("//Times/@finish").Item(0);
  83. end0 = Convert.ToDateTime(nEnd.InnerText);
  84. //s.time = Math.Round((double)(end0 - start)/10000000.0,2); //ticks are in 100-nanoseconds
  85. s.time = ((end0 - start).TotalSeconds);
  86. return s;
  87. }
  88. public static string MakeCompatXML(string input)
  89. {
  90. //change the first tag to have no attributes - VSTS2008
  91. StringBuilder newFile = new StringBuilder();
  92. string temp = "";
  93. string nStr = "";
  94. string[] file = File.ReadAllLines(input);
  95. foreach (string line in file)
  96. {
  97. if (line.Contains("<TestRun id"))
  98. {
  99. nStr = line.Substring(0, 8) + ">";
  100. temp = line.Replace(line.ToString(), nStr);
  101. newFile.Append(temp + "\r");
  102. continue;
  103. }
  104. newFile.Append(line + "\r");
  105. }
  106. File.WriteAllText(input, newFile.ToString());
  107. return input;
  108. }
  109. public static bool IfTestExists(XmlDocument doc, string testName)
  110. {
  111. int i = 0;
  112. while (doc.SelectSingleNode("//TestDefinitions").ChildNodes.Count != i)
  113. {
  114. if (doc.SelectSingleNode("//TestDefinitions").ChildNodes[i].Attributes["name"].Value == testName)
  115. {
  116. return true;
  117. }
  118. i++;
  119. }
  120. return false;
  121. }
  122. public static bool IfResultExists(XmlDocument doc, string testName, out XmlNode oldNode)
  123. {
  124. int i = 0;
  125. while (doc.SelectSingleNode("//Results").ChildNodes.Count != i)
  126. {
  127. if (doc.SelectSingleNode("//Results").ChildNodes[i].Attributes["testName"].Value == testName)
  128. {
  129. oldNode = doc.SelectSingleNode("//Results").ChildNodes[i];
  130. return true;
  131. }
  132. i++;
  133. }
  134. oldNode = null;
  135. return false;
  136. }
  137. public static void SetSummary(string fileName)
  138. {
  139. System.Xml.XmlDocument oDoc = new XmlDocument();
  140. oDoc.Load(fileName);
  141. XmlNode master = oDoc.SelectSingleNode("//ResultSummary/Counters");
  142. summary oSummary;
  143. oSummary.passed = 0;
  144. //count the number of test cases for total
  145. oSummary.total = oDoc.SelectSingleNode("//TestDefinitions").ChildNodes.Count;
  146. //count the number of test cases executed from count of test results
  147. oSummary.executed = oDoc.SelectSingleNode("//Results").ChildNodes.Count;
  148. //count the number of passed test cases from results
  149. int i = 0;
  150. while (oDoc.SelectSingleNode("//Results").ChildNodes.Count != i)
  151. {
  152. if (oDoc.SelectSingleNode("//Results").ChildNodes[i].Attributes["outcome"].Value == "Passed")
  153. {
  154. oSummary.passed++;
  155. }
  156. i++;
  157. }
  158. ////update summary with new numbers
  159. master.Attributes["total"].Value = oSummary.total.ToString();
  160. master.Attributes["executed"].Value = oSummary.executed.ToString();
  161. master.Attributes["passed"].Value = oSummary.passed.ToString();
  162. ////locate and update times
  163. XmlNode oTimes = oDoc.SelectSingleNode("//Times");
  164. //add a new attribute elapsed time, original trx would not have that
  165. XmlAttribute elapsed = oDoc.CreateAttribute("elapsedtime");
  166. elapsed.Value = CalculateSummaryTime(oDoc).ToString();
  167. oTimes.Attributes.Append(elapsed);
  168. oDoc.Save(fileName);
  169. }
  170. public static double CalculateSummaryTime(XmlDocument doc)
  171. {
  172. DateTime sTime;
  173. DateTime eTime;
  174. double sDiff = 0;
  175. int i = 0;
  176. while (doc.SelectSingleNode("//Results").ChildNodes.Count != i)
  177. {
  178. sTime = Convert.ToDateTime(doc.SelectSingleNode("//Results").ChildNodes[i].Attributes["startTime"].Value);
  179. eTime = Convert.ToDateTime(doc.SelectSingleNode("//Results").ChildNodes[i].Attributes["endTime"].Value);
  180. //// calculate timespan for each test case and add them to calculate total time
  181. sDiff += (eTime - sTime).TotalSeconds;
  182. i++;
  183. }
  184. return sDiff;
  185. }
  186. }
  187. struct summary
  188. {
  189. public int total;
  190. public int executed;
  191. public int passed;
  192. public double time;
  193. }
  194. }