PageRenderTime 52ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Mercurial.Net.Tests/AllFilesInProjectsAreUtf8Formatted.cs

#
C# | 104 lines | 101 code | 3 blank | 0 comment | 5 complexity | 7f31badcacc588c2167d2e021fad5756 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text.RegularExpressions;
  7. using NUnit.Framework;
  8. namespace Mercurial.Tests
  9. {
  10. [TestFixture]
  11. [Category("API")]
  12. public class AllFilesInProjectsAreUtf8Encoded
  13. {
  14. public static IEnumerable<string> AllFilesInProjectsInFolder(string sourceLocation)
  15. {
  16. return
  17. from filename in Directory.GetFiles(sourceLocation, "*.*", SearchOption.AllDirectories)
  18. let lowerFilename = filename.ToLower()
  19. where !lowerFilename.Contains(@"\.hg\")
  20. && !lowerFilename.Contains(@"\help\")
  21. && !lowerFilename.Contains(@"\bin\")
  22. && !lowerFilename.Contains(@"\obj\")
  23. && !lowerFilename.Contains(@"\packages\")
  24. && !lowerFilename.Contains(@"\_resharper.")
  25. select filename;
  26. }
  27. public static IEnumerable<string> AllFilesInProjects()
  28. {
  29. return AllFilesInProjectsInFolder(GetSourceLocation());
  30. }
  31. private static string GetSourceLocation()
  32. {
  33. string assemblyLocation = Assembly.GetExecutingAssembly().Location;
  34. string sourceLocation = Path.GetFullPath(Path.Combine(assemblyLocation, "..\\..\\..\\..\\"));
  35. return sourceLocation;
  36. }
  37. public IEnumerable<string> AllSourceFilesInProjectsInFolder(string sourceLocation)
  38. {
  39. var re = new Regex(@"\.(cs|sln|linq|nuspec|xml|hgignore|markdown|csproj|tt)$", RegexOptions.IgnoreCase);
  40. return
  41. from filename in AllFilesInProjectsInFolder(sourceLocation)
  42. where re.Match(filename).Success
  43. select filename;
  44. }
  45. public IEnumerable<string> AllSourceFilesInProjects()
  46. {
  47. return AllSourceFilesInProjectsInFolder(GetSourceLocation());
  48. }
  49. public IEnumerable<string> AllUnknownExtensions()
  50. {
  51. var re = new Regex(@"\.(cs|csproj|sln|linq|nuspec|nunit|hgignore|hgtags|fxcop|stylecop|suo|user|snk|markdown|xml|resharper|shfbproj|cache|dll|ini|config|metaproj|tmp|proj|tt)$", RegexOptions.IgnoreCase);
  52. return
  53. (from filename in AllFilesInProjects()
  54. where filename != null && !re.Match(filename).Success
  55. let extension = Path.GetExtension(filename)
  56. where !StringEx.IsNullOrWhiteSpace(extension)
  57. let lowerExtension = extension.ToLower()
  58. where lowerExtension.Length > 0
  59. select lowerExtension).Distinct();
  60. }
  61. [Test]
  62. [TestCaseSource("AllUnknownExtensions")]
  63. public void EnsureNoUnknownFileExtensionsSlipsBy(string extension)
  64. {
  65. if (!StringEx.IsNullOrWhiteSpace(extension))
  66. Assert.Fail("unknown file extension {0}", extension);
  67. }
  68. [Test]
  69. [TestCaseSource("AllSourceFilesInProjects")]
  70. public void EnsureAllFilesInProjectsAreUtf8Encoded(string filename)
  71. {
  72. byte[] bytes = File.ReadAllBytes(filename);
  73. if (bytes.Length >= 2)
  74. {
  75. if (bytes[0] == 0xfe && bytes[1] == 0xff)
  76. Assert.Fail("File {0} is UTF-16, big-endian encoded", filename);
  77. if (bytes[0] == 0xff && bytes[1] == 0xfe)
  78. Assert.Fail("File {0} is UTF-16, little-endian encoded", filename);
  79. }
  80. if (bytes.Length >= 3)
  81. {
  82. if (bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf)
  83. Assert.Fail("File {0} is UTF-8-encoded, but with byte-order-mark", filename);
  84. }
  85. if (bytes.Length < 4)
  86. return;
  87. if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0xfe && bytes[3] == 0xff)
  88. Assert.Fail("File {0} is UTF-32, big-endian encoded", filename);
  89. if (bytes[0] == 0xff && bytes[1] == 0xfe && bytes[2] == 0 && bytes[3] == 0)
  90. Assert.Fail("File {0} is UTF-32, big-endian encoded", filename);
  91. }
  92. }
  93. }