2
2
3
3
using System ;
4
4
using System . Collections . Generic ;
5
+ using System . Linq ;
5
6
using System . Text . RegularExpressions ;
6
7
using YamlDotNet . Serialization ;
7
8
using YamlDotNet . Serialization . NamingConventions ;
@@ -76,6 +77,9 @@ public void Parse(string tapContent)
76
77
[ GeneratedRegex ( @"^#\s*(.*)$" ) ]
77
78
private static partial Regex CommentRegEx14 ( ) ;
78
79
80
+ [ GeneratedRegex ( ": \" (.*)\" $" ) ]
81
+ private static partial Regex YamlSanitizeUnescapedDoubeQuotesRegEx ( ) ;
82
+
79
83
/// <summary>
80
84
/// Parses the content of a TAP file.
81
85
/// </summary>
@@ -157,6 +161,38 @@ private void ParseVersion14(string[] lines)
157
161
158
162
private void ParseYamlVersion14 ( List < string > yamlLines )
159
163
{
164
+ // Deserializes YAML content into diagnostics of a TapTestPoint.
165
+ static void DeserializeYaml ( TapTestPoint testPoint , string yamlContent )
166
+ {
167
+ var deserializer = new DeserializerBuilder ( )
168
+ . WithNamingConvention ( CamelCaseNamingConvention . Instance )
169
+ . Build ( ) ;
170
+ var yamlData = deserializer . Deserialize < Dictionary < string , object > > ( yamlContent ) ;
171
+
172
+ foreach ( var entry in yamlData )
173
+ {
174
+ testPoint . Diagnostics [ entry . Key ] = entry . Value ;
175
+ }
176
+ }
177
+
178
+ // Tries to sanitizes invalid YAML content reported by linters.
179
+ static string SanitizeYaml ( string yamlContent )
180
+ {
181
+ var lines = yamlContent . Split ( '\n ' ) ;
182
+ return string . Join ( "\n " , lines . Select ( line =>
183
+ {
184
+ // Replace unescaped double quotes with escaped double quotes.
185
+ var match = YamlSanitizeUnescapedDoubeQuotesRegEx ( ) . Match ( line ) ;
186
+ if ( match . Success )
187
+ {
188
+ var value = match . Groups [ 1 ] . Value . Replace ( "\" " , "\\ \" " ) ;
189
+ return line . Replace ( match . Groups [ 1 ] . Value , value ) ;
190
+ }
191
+
192
+ return line ;
193
+ } ) ) ;
194
+ }
195
+
160
196
if ( this . Results . Count == 0 )
161
197
{
162
198
return ;
@@ -167,14 +203,13 @@ private void ParseYamlVersion14(List<string> yamlLines)
167
203
168
204
try
169
205
{
170
- var deserializer = new DeserializerBuilder ( )
171
- . WithNamingConvention ( CamelCaseNamingConvention . Instance )
172
- . Build ( ) ;
173
- var yamlData = deserializer . Deserialize < Dictionary < string , object > > ( yamlContent ) ;
174
- foreach ( var entry in yamlData )
175
- {
176
- lastResult . Diagnostics [ entry . Key ] = entry . Value ;
177
- }
206
+ DeserializeYaml ( lastResult , yamlContent ) ;
207
+ }
208
+ catch ( YamlDotNet . Core . SemanticErrorException )
209
+ {
210
+ // Attempt to sanitize and retry parsing
211
+ yamlContent = SanitizeYaml ( yamlContent ) ;
212
+ DeserializeYaml ( lastResult , yamlContent ) ;
178
213
}
179
214
catch ( Exception ex )
180
215
{
0 commit comments