Skip to content

Commit 7837574

Browse files
Add unit tests for package metadata
Add unit tests to verify that the project files contain the expected package metadata.
1 parent 7614d07 commit 7837574

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

test/AspNet.Security.OAuth.Providers.Tests/AspNet.Security.OAuth.Providers.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@
3030
<PackageReference Include="Moq" />
3131
<PackageReference Include="Shouldly" />
3232
</ItemGroup>
33+
34+
<ItemGroup>
35+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
36+
<_Parameter1>SolutionRoot</_Parameter1>
37+
<_Parameter2>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)../../'))</_Parameter2>
38+
</AssemblyAttribute>
39+
</ItemGroup>
40+
3341
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using System.IO;
10+
using System.Linq;
11+
using System.Reflection;
12+
using System.Threading;
13+
using System.Threading.Tasks;
14+
using System.Xml.Linq;
15+
using Shouldly;
16+
using Xunit;
17+
18+
namespace AspNet.Security.OAuth
19+
{
20+
public static class PackageMetadataTests
21+
{
22+
private static readonly string? _solutionRoot = typeof(PackageMetadataTests).Assembly
23+
.GetCustomAttributes<AssemblyMetadataAttribute>()
24+
.Where((p) => string.Equals("SolutionRoot", p.Key, StringComparison.Ordinal))
25+
.Select((p) => p.Value)
26+
.First();
27+
28+
public static IEnumerable<object[]> Projects()
29+
{
30+
foreach (string directory in Directory.EnumerateDirectories(Path.Combine(_solutionRoot!, "src")))
31+
{
32+
foreach (string project in Directory.EnumerateFiles(directory, "*.csproj"))
33+
{
34+
string projectName = Path.GetFileNameWithoutExtension(project);
35+
36+
foreach (string propertyName in new[] { "Authors", "Description", "PackageTags" })
37+
{
38+
yield return new object[] { projectName, propertyName };
39+
}
40+
}
41+
}
42+
}
43+
44+
[Theory]
45+
[MemberData(nameof(Projects))]
46+
public static async Task Project_Has_Expected_Package_Metadata(string projectName, string propertyName)
47+
{
48+
// Arrange
49+
string path = Path.Combine(_solutionRoot!, "src", projectName, projectName) + ".csproj";
50+
51+
using var stream = File.OpenRead(path);
52+
XElement project = await XElement.LoadAsync(stream, LoadOptions.None, CancellationToken.None);
53+
54+
AssertPackageMetadata(project, propertyName);
55+
}
56+
57+
private static void AssertPackageMetadata(XElement project, string propertyName)
58+
{
59+
bool found = false;
60+
61+
foreach (XElement item in project.Descendants("PropertyGroup").Descendants())
62+
{
63+
if (string.Equals(item.Name.LocalName, propertyName, StringComparison.Ordinal))
64+
{
65+
item.Value.ShouldNotBeNullOrWhiteSpace($"The {propertyName} MSBuild property has no value.");
66+
found = true;
67+
break;
68+
}
69+
}
70+
71+
found.ShouldBeTrue($"The {propertyName} MSBuild property cannot be found.");
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)