PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Mercurial.Net.Tests/AllFilesInProjectsAreUtf8Formatted.cs

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