add white list
This commit is contained in:
Родитель
a4ff700836
Коммит
4e51709ab0
|
@ -7,6 +7,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="YamlDotNet" Version="6.0.0" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Xunit.Abstractions;
|
using Xunit.Abstractions;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
namespace NonAsciiValidations.Tests
|
namespace NonAsciiValidations.Tests
|
||||||
{
|
{
|
||||||
|
@ -41,13 +42,46 @@ namespace NonAsciiValidations.Tests
|
||||||
ValidateOnlyAscii(yamlFullPath, detectionsYamlFileName);
|
ValidateOnlyAscii(yamlFullPath, detectionsYamlFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ValidateOnlyAscii(string yamlFilePath, string yamlFileName)
|
private bool ShouldSkipTemplateValidation(string templateId)
|
||||||
{
|
{
|
||||||
var yaml = File.ReadAllText(yamlFilePath);
|
return TemplatesToSkipValidationReader.WhiteListTemplates
|
||||||
var nonAsciiCharMatch = Regex.Match(yaml, @"[^\u0000-\u007F]+");
|
.Where(template => template.id == templateId)
|
||||||
Assert.False(nonAsciiCharMatch.Success, $"${yamlFileName} includes the non ascii char:{nonAsciiCharMatch.Value} string index:{nonAsciiCharMatch.Index}");
|
.Where(template => !string.IsNullOrWhiteSpace(template.validationFailReason))
|
||||||
|
.Where(template => !string.IsNullOrWhiteSpace(template.templateName))
|
||||||
|
.Any();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private void ValidateOnlyAscii(string yamlFilePath, string yamlFileName)
|
||||||
|
{
|
||||||
|
var yaml = File.ReadAllText(yamlFilePath);
|
||||||
|
var extracted = TryExtractTemplateId(yaml,out string id);
|
||||||
|
|
||||||
|
//we ignore specific templates
|
||||||
|
if (extracted && ShouldSkipTemplateValidation(id))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var nonAsciiCharMatch = Regex.Match(yaml, @"[^\u0000-\u007F]+");
|
||||||
|
Assert.False(nonAsciiCharMatch.Success, $"${yamlFileName} includes the non ascii char:{nonAsciiCharMatch.Value} string index:{nonAsciiCharMatch.Index}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryExtractTemplateId(string yaml,out string tempateId)
|
||||||
|
{
|
||||||
|
tempateId = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var deserializer = new DeserializerBuilder().Build();
|
||||||
|
var res = deserializer.Deserialize<dynamic>(yaml);
|
||||||
|
tempateId = res["id"];
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "a953f304-12e4-48ae-bedc-d58fb1b0c6a6",
|
||||||
|
"templateName": "Unicode Obfuscation in Command Line",
|
||||||
|
"validationFailReason": "include unicode by design"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace NonAsciiValidations.Tests
|
||||||
|
{
|
||||||
|
public class SkipTemplate
|
||||||
|
{
|
||||||
|
public string id;
|
||||||
|
public string templateName;
|
||||||
|
public string validationFailReason;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class TemplatesToSkipValidationReader
|
||||||
|
{
|
||||||
|
private const string SKipJsonFileName = "SkipValidationsTemplates.json";
|
||||||
|
|
||||||
|
static TemplatesToSkipValidationReader()
|
||||||
|
{
|
||||||
|
var jsonFilePath = Path.Combine(FilesTestData.GetSkipTemplatesPath(), SKipJsonFileName);
|
||||||
|
using (StreamReader r = new StreamReader(jsonFilePath))
|
||||||
|
{
|
||||||
|
string json = r.ReadToEnd();
|
||||||
|
WhiteListTemplates = JsonConvert.DeserializeObject<IEnumerable<SkipTemplate>>(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<SkipTemplate> WhiteListTemplates { get; private set; }
|
||||||
|
}
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче