forked from microsoft/TemplateStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileEncodingTests.cs
More file actions
91 lines (79 loc) · 3.13 KB
/
FileEncodingTests.cs
File metadata and controls
91 lines (79 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
namespace Microsoft.Templates.Test
{
[Collection("Unit Test Templates")]
[Trait("Type", "CodeStyle")]
[Trait("ExecutionSet", "Minimum")]
[Trait("ExecutionSet", "TemplateValidation")]
public class FileEncodingTests
{
[Fact]
public void EnsureAllTemplateFilesAreEncodedCorrectly()
{
// This is the relative path from where the test assembly will run from
const string templatesRoot = "../../../../../Templates";
var interestedExtensions = new[]
{
".appxmanifest",
".cs",
".csproj",
".json",
".md",
".resw",
".vb",
".vbproj",
".xaml",
".xml",
};
foreach (var file in GetFiles(templatesRoot))
{
if (interestedExtensions.Contains(Path.GetExtension(file)))
{
if (!IsEncodedCodedCorrectly(file))
{
// Non breaking space leads to wrong characters saving with bom
if (File.ReadAllText(file).Contains('\u00A0'))
{
Assert.True(false, $"File ({file}) contains non breaking whitespaces, please remove them before running the encoding script. '");
}
// Throw an assertion failure here and stop checking other files.
// We don't need to check every file if at least one fails as all can be fixed at once.
Assert.True(false, $"At least one file ({file}) is not encoded correctly. Ensure all template files are encoded correctly with the script at '_utils/Set-All-Text-File-Template-Encodings.ps1'");
}
}
}
// If we get here all files were encoded correctly
Assert.True(true);
}
private bool IsEncodedCodedCorrectly(string filePath)
{
var buffer = new byte[3];
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
fs.Read(buffer, 0, 3);
}
// UTF-8 with Byte Order Mark (BOM aka Signature) starts EF BB BF
return buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf;
}
private IEnumerable<string> GetFiles(string directory)
{
foreach (var dir in Directory.GetDirectories(directory))
{
foreach (var file in Directory.GetFiles(dir))
{
yield return file;
}
foreach (var file in GetFiles(dir))
{
yield return file;
}
}
}
}
}