forked from microsoft/TemplateStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeStyleEnforcementTests.cs
More file actions
201 lines (167 loc) · 7.82 KB
/
CodeStyleEnforcementTests.cs
File metadata and controls
201 lines (167 loc) · 7.82 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
namespace Microsoft.Templates.Test
{
[Collection("StyleCopCollection")]
[Trait("Type", "CodeStyle")]
[Trait("ExecutionSet", "Minimum")]
[Trait("ExecutionSet", "TemplateValidation")]
public class CodeStyleEnforcementTests
{
// This is the relative path from where the test assembly will run from
private const string TemplatesRoot = "..\\..\\..\\..\\..\\Templates";
[Fact]
public void EnsureCSharpCodeDoesNotUseThis()
{
var result = CodeIsNotUsed("this.", ".cs");
Assert.True(result.Item1, result.Item2);
}
[Fact]
public void EnsureTemplatesDoNotUseTabsInWhitespace()
{
// Some of the merge functionality includes whitespace in string comparisons.
// Ensuring all whitespace is spaces avoids issues where strings differ due to different whitespace (which can be hard to spot)
void EnsureTabsNotUsed(string fileExtension)
{
var result = CodeIsNotUsed('\t'.ToString(), fileExtension);
Assert.True(result.Item1, result.Item2);
}
EnsureTabsNotUsed("*.cs");
EnsureTabsNotUsed("*.vb");
}
[Fact]
public void EnsureCodeDoesNotUseOldTodoCommentIdentifier()
{
void EnsureUwpTemplatesNotUsed(string fileExtension)
{
var result = CodeIsNotUsed("UWPTemplates", fileExtension);
Assert.True(result.Item1, result.Item2);
}
EnsureUwpTemplatesNotUsed("*.cs");
EnsureUwpTemplatesNotUsed("*.vb");
}
[Fact]
public void EnsureVisualBasicCodeDoesNotIncludeCommonPortingIssues()
{
var foundErrors = new List<string>();
// Build tests will fail if these are included but this test is quicker than building everything
void CheckStringNotIncluded(string toSearchFor)
{
var result = CodeIsNotUsed(toSearchFor, ".vb");
if (!result.Item1)
{
foundErrors.Add(result.Item2);
}
}
void IfLineIncludes(string ifIncludes, string itMustAlsoInclude, params string[] unlessItContains)
{
foreach (var file in GetFiles(TemplatesRoot, ".vb"))
{
foreach (var line in File.ReadAllLines(file))
{
if (line.Contains(ifIncludes) && !line.Contains(itMustAlsoInclude))
{
var foundException = false;
if (unlessItContains != null)
{
foreach (var unless in unlessItContains)
{
if (line.Contains(unless))
{
foundException = true;
break;
}
}
}
if (!foundException)
{
foundErrors.Add($"The file '{file}' contains '{ifIncludes}' but doesn't also include '{itMustAlsoInclude}'.");
}
}
}
}
}
CheckStringNotIncluded("Namespace Param_RootNamespace."); // Root namespace is included by default in VB
CheckStringNotIncluded("Namespace Param_ItemNamespace."); // Root namespace is included by default in VB
CheckStringNotIncluded(";");
CheckStringNotIncluded("var "); // May be in commented our code included in template as an example
CheckStringNotIncluded("Var "); // May be in commented our code included in template as an example
CheckStringNotIncluded("Key ."); // Output by converter as part of object initializers
CheckStringNotIncluded("yield Return"); // Return not needed but converter includes it
CheckStringNotIncluded("yield return"); // Return not needed but converter includes it
CheckStringNotIncluded("wts__"); // temporary placeholder used during conversion
CheckStringNotIncluded("'''/");
IfLineIncludes(" As Task", itMustAlsoInclude: " Async ", unlessItContains: new[] { " MustOverride ", "Function RunAsync(", "Function RunAsyncInternal(", " FireAndForget(" });
IfLineIncludes("\"{", itMustAlsoInclude: "$");
Assert.True(foundErrors.Count == 0, string.Join(Environment.NewLine, foundErrors));
}
[Fact]
public void EnsureVisualBasicCodeDoesNotUseOnErrorGoto()
{
var result = CodeIsNotUsed("On Error Goto", "*.vb");
Assert.True(result.Item1, result.Item2);
}
[Fact]
public void EnsureVisualBasicCodeDoesNotContainMultipleConsecutiveBlankLines()
{
var result = CodeIsNotUsed($"{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}", "*.vb");
Assert.True(result.Item1, result.Item2);
}
[Fact]
public void EnsureVisualBasicCodeDoesNotIndicateParamsPassedByVal()
{
var result = CodeIsNotUsed("ByVal", "*.vb");
Assert.True(result.Item1, result.Item2);
}
// Disabled as failing on AppVeyor for some files with no obvious reason
////[Fact]
public void EnsureVisualBasicFilesEndWithSingleBlankLine()
{
var errorFiles = new List<string>();
foreach (var file in GetFiles(TemplatesRoot, "*.vb"))
{
var text = File.ReadAllText(file, Encoding.UTF8);
if (!text.EndsWith(Environment.NewLine, StringComparison.InvariantCulture)
|| text.EndsWith(Environment.NewLine + Environment.NewLine, StringComparison.InvariantCulture))
{
errorFiles.Add(file);
}
}
Assert.True(errorFiles.Count == 0, $"The following files don't end with a single NewLine{Environment.NewLine}{string.Join(Environment.NewLine, errorFiles)}");
}
private Tuple<bool, string> CodeIsNotUsed(string textThatShouldNotBeinTheFile, string fileExtension)
{
foreach (var file in GetFiles(TemplatesRoot, fileExtension))
{
if (File.ReadAllText(file).Contains(textThatShouldNotBeinTheFile))
{
// Throw an assertion failure here and stop checking other files.
// We don't need to check every file if at least one fails as this should just be a final verification.
return new Tuple<bool, string>(false, $"The file '{file}' contains '{textThatShouldNotBeinTheFile}' but based on our style guidelines it shouldn't.");
}
}
return new Tuple<bool, string>(true, string.Empty);
}
private IEnumerable<string> GetFiles(string directory, string extension = ".*")
{
foreach (var dir in Directory.GetDirectories(directory))
{
foreach (var file in Directory.GetFiles(dir, $"*{extension}"))
{
yield return file;
}
foreach (var file in GetFiles(dir, extension))
{
yield return file;
}
}
}
}
}