Skip to content

Commit f05fb3f

Browse files
authored
Merge pull request #4 from joncloud/migrate-readme-tests-to-xunit
Migrates readme tests from adhoc ps1 to xunit
2 parents 4175a31 + 7bb20d2 commit f05fb3f

File tree

7 files changed

+136
-36
lines changed

7 files changed

+136
-36
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Options:
3636
--timeout=<VALUE> Sets the timeout of the request using System.TimeSpan.TryParse (https://docs.microsoft.com/en-us/dotnet/api/system.timespan.parse)
3737
--version Displays the application verison.
3838
--xml=<ROOT_NAME> Renders the content arguments as application/xml using the optional xml root name.
39+
--stop-auto-redirects Prevents redirects from automatically being processed.
3940

4041
Content:
4142
Repeat as many content arguments to create content sent with the HTTP request. Alternatively pipe raw content send as the HTTP request content.

TestReadmeVersion.ps1

Lines changed: 0 additions & 30 deletions
This file was deleted.

azure-pipelines.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ variables:
1010
buildConfiguration: 'Release'
1111

1212
steps:
13-
- task: PowerShell@2
14-
displayName: Test Readme
15-
inputs:
16-
filePath: 'TestReadmeVersion.ps1'
17-
failOnStderr: true
18-
1913
- task: DotNetCoreCLI@2
2014
displayName: Pack https
2115
inputs:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Xml.Linq;
2+
3+
namespace Https.Tests
4+
{
5+
public class HttpsCsprojFixture
6+
{
7+
public XDocument Document { get; }
8+
9+
public HttpsCsprojFixture()
10+
{
11+
var path = "../../../../../src/https/https.csproj";
12+
13+
Document = XDocument.Load(path);
14+
}
15+
}
16+
}

tests/https.Tests/Readme.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using System.Text.RegularExpressions;
5+
6+
namespace Https.Tests
7+
{
8+
public class Readme
9+
{
10+
public string InstallationVersion { get; }
11+
public string UsageDocumentation { get; }
12+
13+
public Readme(string path)
14+
{
15+
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentNullException(nameof(path));
16+
17+
var sb = new StringBuilder();
18+
InstallationVersion = "";
19+
UsageDocumentation = "";
20+
21+
var lines = File.ReadLines(path);
22+
var record = false;
23+
foreach (var line in lines)
24+
{
25+
26+
if (line.StartsWith("```bash"))
27+
{
28+
record = true;
29+
}
30+
else if (line.StartsWith("```"))
31+
{
32+
var text = sb.ToString();
33+
sb.Clear();
34+
35+
if (text.StartsWith("dotnet tool"))
36+
{
37+
var match = Regex.Match(text, "dotnet tool install --global https --version (.+)-\\*");
38+
if (match.Success)
39+
{
40+
InstallationVersion = match.Groups[1].Value;
41+
}
42+
}
43+
else if (text.StartsWith("Usage"))
44+
{
45+
UsageDocumentation = text;
46+
}
47+
48+
record = false;
49+
}
50+
else if (record)
51+
{
52+
sb.AppendLine(line);
53+
}
54+
}
55+
}
56+
}
57+
}

tests/https.Tests/ReadmeFixture.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Https.Tests
2+
{
3+
public class ReadmeFixture
4+
{
5+
public Readme Readme { get; }
6+
public ReadmeFixture()
7+
{
8+
Readme = new Readme(
9+
"../../../../../README.md"
10+
);
11+
}
12+
}
13+
}

tests/https.Tests/ReadmeTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using System.Xml.Linq;
5+
using Xunit;
6+
7+
namespace Https.Tests
8+
{
9+
public class ReadmeTests : IClassFixture<HttpsCsprojFixture>, IClassFixture<ReadmeFixture>
10+
{
11+
readonly HttpsCsprojFixture _httpsCsprojFixture;
12+
readonly ReadmeFixture _readmeFixture;
13+
public ReadmeTests(HttpsCsprojFixture httpsCsprojFixture, ReadmeFixture readmeFixture)
14+
{
15+
_httpsCsprojFixture = httpsCsprojFixture;
16+
_readmeFixture = readmeFixture;
17+
}
18+
19+
[Fact]
20+
public void Installation_ShouldListSameVersionAsCsproj()
21+
{
22+
var versionPrefixElement = _httpsCsprojFixture.Document
23+
.Root
24+
.Elements("PropertyGroup")
25+
.Elements("VersionPrefix")
26+
.FirstOrDefault();
27+
28+
Assert.NotNull(versionPrefixElement);
29+
30+
var expected = versionPrefixElement.Value;
31+
var actual = _readmeFixture.Readme.InstallationVersion;
32+
33+
Assert.Equal(expected, actual);
34+
}
35+
36+
[Fact]
37+
public async Task Usage_ShouldListHelpDocument()
38+
{
39+
var httpsResult = await Https.ExecuteAsync("--help");
40+
41+
using var reader = new StreamReader(httpsResult.StdOut);
42+
43+
var expected = reader.ReadToEnd().TrimEnd();
44+
var actual = _readmeFixture.Readme.UsageDocumentation.TrimEnd();
45+
46+
Assert.Equal(expected, actual);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)