Skip to content

Commit

Permalink
test: fix failing wiki tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasticFiasco committed Apr 27, 2019
1 parent 6144c36 commit 29b9baf
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 29 deletions.
26 changes: 13 additions & 13 deletions test/Serilog.Sinks.HttpTests/Support/Fixtures/GitHubWikiFixture.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Serilog.Support.Fixtures
{
public class GitHubWikiFixture
{
private string[] rows;
private const string WikiUrl = "https://raw.githubusercontent.com/wiki/FantasticFiasco/serilog-sinks-http/{0}";
private const string DescriptionRegexFormat = "- `{0}` - (?<description>.*)$";

public void Load(string wikiPage)
private string pageContent;

public async Task LoadAsync(string wikiPage)
{
using (var client = new HttpClient())
{
rows = client
.GetStringAsync($"https://raw.githubusercontent.com/wiki/FantasticFiasco/serilog-sinks-http/{wikiPage}")
.Result
.Split('\n');
pageContent = await client.GetStringAsync(string.Format(WikiUrl, wikiPage));
}
}

public string GetDescription(string parameterName)
{
var pattern = $"- `{parameterName}` - ";
var descriptionRegex = new Regex(string.Format(DescriptionRegexFormat, parameterName), RegexOptions.Multiline);

var matchingRow = rows.SingleOrDefault(row => row.StartsWith(pattern));
if (matchingRow == null) throw new Exception($"GitHub wiki does not contain a description of parameter \"{parameterName}\"");
var match = descriptionRegex.Match(pageContent);
if (!match.Success) throw new Exception($"GitHub wiki does not contain a description of parameter \"{parameterName}\"");

return matchingRow
.Substring(pattern.Length)
.Replace("`", string.Empty); // Remove code annotation
return match.Groups["description"].Value
.Replace("`", string.Empty); // Remove code indicator
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
using System.Linq;
using System;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Serilog.Sinks.Http;
using Serilog.Sinks.Http.BatchFormatters;
using Serilog.Sinks.Http.TextFormatters;

namespace Serilog.Support.Fixtures
{
public class XmlDocumentationFixture
{
private readonly XDocument document;
private readonly Regex seeClassRegex;
private readonly Regex seeEnumRegex;
private readonly Regex paramRefRegex;

public XmlDocumentationFixture()
{
document = XDocument.Load("Serilog.Sinks.Http.xml");
seeClassRegex = new Regex(@"<see cref=""T:(?<fullName>[\w.]+)""\s*/>");
seeEnumRegex = new Regex(@"<see cref=""F:(?<fullName>[\w.]+)""\s*/>");
paramRefRegex = new Regex(@"<paramref name=""(?<parameterName>\w+)""\s*/>");
}

public string GetDescription(string extensionName, string parameterName)
Expand All @@ -29,20 +35,72 @@ public string GetDescription(string extensionName, string parameterName)
.Split("\n")
.Select(row => row.Trim())
.Where(row => row.Length > 0)
.Select(RemoveLinks);
.Select(RemoveSeeClassLinks)
.Select(RemoveSeeEnumLinks)
.Select(RemoveParamRefLinks);

return string.Join(" ", description);
}

private static string RemoveLinks(string description)
private string RemoveSeeClassLinks(string description)
{
return description
.Replace($"<see cref=\"T:{typeof(NormalRenderedTextFormatter).FullName}\" />", nameof(NormalRenderedTextFormatter))
.Replace($"<see cref=\"T:{typeof(DefaultBatchFormatter).FullName}\" />", nameof(DefaultBatchFormatter))
.Replace($"<see cref=\"T:{typeof(IHttpClient).FullName}\" />", nameof(IHttpClient))
.Replace("<see cref=\"F:Serilog.Events.LevelAlias.Minimum\" />", "LevelAlias.Minimum")
.Replace("<see cref=\"T:System.Net.Http.HttpClient\" />", "HttpClient")
.Replace("<paramref name=\"retainedBufferFileCountLimit\" />", "retainedBufferFileCountLimit");
var matches = seeClassRegex.Matches(description);

foreach (Match match in matches)
{
var type = ProbeType(match.Groups["fullName"].Value);

description = description.Replace(
match.Groups[0].Value,
type.Name);
}

return description;
}

private string RemoveSeeEnumLinks(string description)
{
var matches = seeEnumRegex.Matches(description);

foreach (Match match in matches)
{
var type = ProbeType(match.Groups["fullName"].Value);

if (type != null)
{
description = description.Replace(
match.Groups[0].Value,
type.Name);
}
else
{
// If documentation is specifying an enum value instead of the enum itself, we need
// to strip away the value
var parts = match.Groups["fullName"].Value.Split('.');
var fullName = string.Join('.', parts, 0, parts.Length - 1);
type = ProbeType(fullName);

description = description.Replace(
match.Groups[0].Value,
$"{type.Name}.{parts.Last()}");
}
}

return description;
}

private string RemoveParamRefLinks(string description)
{
var matches = paramRefRegex.Matches(description);

foreach (Match match in matches)
{
description = description.Replace(
match.Groups[0].Value,
match.Groups["parameterName"].Value);
}

return description;
}

private static string GetValue(XNode node)
Expand All @@ -54,5 +112,17 @@ private static string GetValue(XNode node)
return reader.ReadInnerXml();
}
}

private static Type ProbeType(string fullName)
{
Type ProbeTypeInAssembly(string assemblyName)
{
return Assembly.Load(assemblyName).GetType(fullName);
}

return ProbeTypeInAssembly("Serilog.Sinks.Http")
?? ProbeTypeInAssembly("Serilog")
?? ProbeTypeInAssembly("netstandard");
}
}
}
8 changes: 5 additions & 3 deletions test/Serilog.Sinks.HttpTests/WikiPageDocumentationShould.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Threading.Tasks;
using Serilog.Configuration;
using Serilog.Support.Fixtures;
using Shouldly;
Expand All @@ -20,11 +21,12 @@ public WikiPageDocumentationShould(GitHubWikiFixture gitHubWikiFixture, XmlDocum

[TheoryOnMasterBranch]
[InlineData("HTTP-sink.md", "Http")]
[InlineData("Durable-HTTP-sink.md", "DurableHttp")]
public void MatchCode(string wikiPage, string extensionName)
[InlineData("Durable-file-size-rolled-HTTP-sink.md", "DurableHttpUsingFileSizeRolledBuffers")]
[InlineData("Durable-time-rolled-HTTP-sink.md", "DurableHttpUsingTimeRolledBuffers")]
public async Task MatchCode(string wikiPage, string extensionName)
{
// Arrange
gitHubWikiFixture.Load(wikiPage);
await gitHubWikiFixture.LoadAsync(wikiPage);

var parameterNames = typeof(LoggerSinkConfigurationExtensions)
.GetMethod(extensionName)
Expand Down

0 comments on commit 29b9baf

Please sign in to comment.