StringUsageReporter.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using MediaBrowser.Tests.ConsistencyTests.TextIndexing;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. namespace MediaBrowser.Tests.ConsistencyTests
  11. {
  12. /// <summary>
  13. /// This class contains tests for reporting the usage of localization string tokens
  14. /// in the dashboard-ui or similar.
  15. /// </summary>
  16. /// <remarks>
  17. /// <para>Run one of the two tests using Visual Studio's "Test Explorer":</para>
  18. /// <para>
  19. /// <list type="bullet">
  20. /// <item><see cref="ReportStringUsage"/></item>
  21. /// <item><see cref="ReportUnusedStrings"/></item>
  22. /// </list>
  23. /// </para>
  24. /// <para>
  25. /// On successful run, the bottom section of the test explorer will contain a link "Output".
  26. /// This link will open the test results, displaying the trace and two attachment links.
  27. /// One link will open the output folder, the other link will open the output xml file.
  28. /// </para>
  29. /// <para>
  30. /// The output xml file contains a stylesheet link to render the results as html.
  31. /// How that works depends on the default application configured for XML files:
  32. /// </para>
  33. /// <para><list type="bullet">
  34. /// <item><term>Visual Studio</term>
  35. /// <description>Will open in XML source view. To view the html result, click menu
  36. /// 'XML' => 'Start XSLT without debugging'</description></item>
  37. /// <item><term>Internet Explorer</term>
  38. /// <description>XSL transform will be applied automatically.</description></item>
  39. /// <item><term>Firefox</term>
  40. /// <description>XSL transform will be applied automatically.</description></item>
  41. /// <item><term>Chrome</term>
  42. /// <description>Does not work. Chrome is unable/unwilling to apply xslt transforms from local files.</description></item>
  43. /// </list></para>
  44. /// </remarks>
  45. [TestClass]
  46. public class StringUsageReporter
  47. {
  48. /// <summary>
  49. /// Root path of the web application
  50. /// </summary>
  51. /// <remarks>
  52. /// Can be an absolute path or a path relative to the binaries folder (bin\Debug).
  53. /// </remarks>
  54. public const string WebFolder = @"..\..\..\MediaBrowser.WebDashboard\dashboard-ui";
  55. /// <summary>
  56. /// Path to the strings file, relative to <see cref="WebFolder"/>.
  57. /// </summary>
  58. public const string StringsFile = @"strings\en-US.json";
  59. /// <summary>
  60. /// Path to the output folder
  61. /// </summary>
  62. /// <remarks>
  63. /// Can be an absolute path or a path relative to the binaries folder (bin\Debug).
  64. /// Important: When changing the output path, make sure that "StringCheck.xslt" is present
  65. /// to make the XML transform work.
  66. /// </remarks>
  67. public const string OutputPath = @".";
  68. /// <summary>
  69. /// List of file extension to search.
  70. /// </summary>
  71. public static string[] TargetExtensions = new[] { "js", "html" };
  72. /// <summary>
  73. /// List of paths to exclude from search.
  74. /// </summary>
  75. public static string[] ExcludePaths = new[] { @"\bower_components\", @"\thirdparty\" };
  76. private TestContext testContextInstance;
  77. /// <summary>
  78. ///Gets or sets the test context which provides
  79. ///information about and functionality for the current test run.
  80. ///</summary>
  81. public TestContext TestContext
  82. {
  83. get
  84. {
  85. return testContextInstance;
  86. }
  87. set
  88. {
  89. testContextInstance = value;
  90. }
  91. }
  92. [TestMethod]
  93. public void ReportStringUsage()
  94. {
  95. this.CheckDashboardStrings(false);
  96. }
  97. [TestMethod]
  98. public void ReportUnusedStrings()
  99. {
  100. this.CheckDashboardStrings(true);
  101. }
  102. private void CheckDashboardStrings(Boolean unusedOnly)
  103. {
  104. // Init Folders
  105. var currentDir = System.IO.Directory.GetCurrentDirectory();
  106. Trace("CurrentDir: {0}", currentDir);
  107. var rootFolderInfo = ResolveFolder(currentDir, WebFolder);
  108. Trace("Web Root: {0}", rootFolderInfo.FullName);
  109. var outputFolderInfo = ResolveFolder(currentDir, OutputPath);
  110. Trace("Output Path: {0}", outputFolderInfo.FullName);
  111. // Load Strings
  112. var stringsFileName = Path.Combine(rootFolderInfo.FullName, StringsFile);
  113. if (!File.Exists(stringsFileName))
  114. {
  115. throw new Exception(string.Format("Strings file not found: {0}", stringsFileName));
  116. }
  117. int lineNumbers;
  118. var stringsDic = this.CreateStringsDictionary(new FileInfo(stringsFileName), out lineNumbers);
  119. Trace("Loaded {0} strings from strings file containing {1} lines", stringsDic.Count, lineNumbers);
  120. var allFiles = rootFolderInfo.GetFiles("*", SearchOption.AllDirectories);
  121. var filteredFiles1 = allFiles.Where(f => TargetExtensions.Any(e => f.Name.EndsWith(e)));
  122. var filteredFiles2 = filteredFiles1.Where(f => !ExcludePaths.Any(p => f.FullName.Contains(p)));
  123. var selectedFiles = filteredFiles2.OrderBy(f => f.FullName).ToList();
  124. var wordIndex = IndexBuilder.BuildIndexFromFiles(selectedFiles, rootFolderInfo.FullName);
  125. Trace("Created word index from {0} files containing {1} individual words", selectedFiles.Count, wordIndex.Keys.Count);
  126. var outputFileName = Path.Combine(outputFolderInfo.FullName, string.Format("StringCheck_{0:yyyyMMddHHmmss}.xml", DateTime.Now));
  127. var settings = new XmlWriterSettings
  128. {
  129. Indent = true,
  130. Encoding = Encoding.UTF8,
  131. WriteEndDocumentOnClose = true
  132. };
  133. Trace("Output file: {0}", outputFileName);
  134. using (XmlWriter writer = XmlWriter.Create(outputFileName, settings))
  135. {
  136. writer.WriteStartDocument(true);
  137. // Write the Processing Instruction node.
  138. string xslText = "type=\"text/xsl\" href=\"StringCheck.xslt\"";
  139. writer.WriteProcessingInstruction("xml-stylesheet", xslText);
  140. writer.WriteStartElement("StringUsages");
  141. writer.WriteAttributeString("ReportTitle", unusedOnly ? "Unused Strings Report" : "String Usage Report");
  142. writer.WriteAttributeString("Mode", unusedOnly ? "UnusedOnly" : "All");
  143. foreach (var kvp in stringsDic)
  144. {
  145. var occurences = wordIndex.Find(kvp.Key);
  146. if (occurences == null || !unusedOnly)
  147. {
  148. ////Trace("{0}: {1}", kvp.Key, kvp.Value);
  149. writer.WriteStartElement("Dictionary");
  150. writer.WriteAttributeString("Token", kvp.Key);
  151. writer.WriteAttributeString("Text", kvp.Value);
  152. if (occurences != null && !unusedOnly)
  153. {
  154. foreach (var occurence in occurences)
  155. {
  156. writer.WriteStartElement("Occurence");
  157. writer.WriteAttributeString("FileName", occurence.FileName);
  158. writer.WriteAttributeString("FullPath", occurence.FullPath);
  159. writer.WriteAttributeString("LineNumber", occurence.LineNumber.ToString());
  160. writer.WriteEndElement();
  161. ////Trace(" {0}:{1}", occurence.FileName, occurence.LineNumber);
  162. }
  163. }
  164. writer.WriteEndElement();
  165. }
  166. }
  167. }
  168. TestContext.AddResultFile(outputFileName);
  169. TestContext.AddResultFile(outputFolderInfo.FullName);
  170. }
  171. private SortedDictionary<string, string> CreateStringsDictionary(FileInfo file, out int lineNumbers)
  172. {
  173. var dic = new SortedDictionary<string, string>();
  174. lineNumbers = 0;
  175. using (var reader = file.OpenText())
  176. {
  177. while (!reader.EndOfStream)
  178. {
  179. lineNumbers++;
  180. var words = reader
  181. .ReadLine()
  182. .Split(new[] { "\":" }, StringSplitOptions.RemoveEmptyEntries);
  183. if (words.Length == 2)
  184. {
  185. var token = words[0].Replace("\"", string.Empty).Trim();
  186. var text = words[1].Replace("\",", string.Empty).Replace("\"", string.Empty).Trim();
  187. if (dic.Keys.Contains(token))
  188. {
  189. throw new Exception(string.Format("Double string entry found: {0}", token));
  190. }
  191. dic.Add(token, text);
  192. }
  193. }
  194. }
  195. return dic;
  196. }
  197. private DirectoryInfo ResolveFolder(string currentDir, string folderPath)
  198. {
  199. if (folderPath.IndexOf(@"\:") != 1)
  200. {
  201. folderPath = Path.Combine(currentDir, folderPath);
  202. }
  203. var folderInfo = new DirectoryInfo(folderPath);
  204. if (!folderInfo.Exists)
  205. {
  206. throw new Exception(string.Format("Folder not found: {0}", folderInfo.FullName));
  207. }
  208. return folderInfo;
  209. }
  210. private void Trace(string message, params object[] parameters)
  211. {
  212. var formatted = string.Format(message, parameters);
  213. System.Diagnostics.Trace.WriteLine(formatted);
  214. }
  215. }
  216. }