-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support required property on test class.
- Loading branch information
1 parent
93f998b
commit 51348a3
Showing
5 changed files
with
191 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
function FindMSBuild () { | ||
if ($null -eq $env:OS) { | ||
try { | ||
return (Get-Command msbuild).Source; | ||
} | ||
catch { | ||
return $null; | ||
} | ||
} | ||
|
||
foreach ($program in ("C:\Program Files", "D:\Program Files")) { | ||
foreach ($vs in (Get-ChildItem ($program + "\Microsoft Visual Studio"))) { | ||
foreach ($vsv in (Get-ChildItem $vs.FullName)) { | ||
if (Test-Path ($vsv.FullName + "\MSBuild\Current\Bin\amd64\MSBuild.exe")) { | ||
return $vsv.FullName + "\MSBuild\Current\Bin\amd64\MSBuild.exe"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $null; | ||
} | ||
|
||
if (-not(Test-Path .packages)) { | ||
mkdir .packages | ||
} | ||
|
||
foreach ($csproj in (Get-ChildItem -r -filter *.csproj)) { | ||
$dir = "$([System.IO.Path]::GetDirectoryName($csproj.FullName))\bin\Release"; | ||
if (Test-Path $dir) { | ||
Remove-Item -Recurse -Force $dir; | ||
} | ||
} | ||
|
||
$MSBuild = FindMSBuild | ||
if ($null -eq $MSBuild) { | ||
dotnet build -c Release /p:IsPacking=true .. | ||
} | ||
else { | ||
& "$MSBuild" /r /m /v:m /p:Configuration=Release /p:IsPacking=true .. | ||
} | ||
|
||
foreach ($csproj in (Get-ChildItem -r -filter *.csproj)) { | ||
$dir = "$([System.IO.Path]::GetDirectoryName($csproj.FullName))\bin\Release"; | ||
|
||
if (Test-Path $dir) { | ||
$nupkg = Get-ChildItem "$([System.IO.Path]::GetDirectoryName($csproj.FullName))\bin\Release" | | ||
Where-Object { $_.Name.Endswith(".nupkg") } | | ||
Sort-Object -Property LastWriteTime -Descending | | ||
Select-Object -First 1; | ||
|
||
if ($null -ne $nupkg) { | ||
Copy-Item $nupkg.VersionInfo.FIleName (".packages\" + $nupkg.Name) -Force | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Xunit.DependencyInjection; | ||
|
||
internal static class ReflectionExtensions | ||
{ | ||
public static bool HasRequiredMemberAttribute(this Type type) | ||
{ | ||
for (var currentType = type; currentType is not null && currentType != typeof(object); | ||
currentType = currentType.BaseType) | ||
if (currentType.CustomAttributes.Any(cad => | ||
cad.AttributeType.FullName == "System.Runtime.CompilerServices.RequiredMemberAttribute")) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
public static bool HasRequiredMemberAttribute(this PropertyInfo propertyInfo) => propertyInfo.CustomAttributes.Any( | ||
cad => cad.AttributeType.FullName == "System.Runtime.CompilerServices.RequiredMemberAttribute"); | ||
|
||
public static bool HasSetsRequiredMembersAttribute(this ConstructorInfo constructorInfo) => | ||
constructorInfo.CustomAttributes.Any(cad => | ||
cad.AttributeType.FullName == "System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Xunit.DependencyInjection.Test; | ||
|
||
public class RequiredMemberTest | ||
{ | ||
public required ITestOutputHelper Output { get; set; } | ||
|
||
public required IDependency Dependency { get; init; } | ||
|
||
[Fact] | ||
public void Test() | ||
{ | ||
Assert.NotNull(Output); | ||
Assert.NotNull(Dependency); | ||
} | ||
} | ||
|
||
public class SetsRequiredMembersTest | ||
{ | ||
private readonly bool _isCtorSet; | ||
private ITestOutputHelper _output; | ||
private readonly IDependency _dependency; | ||
|
||
[method: SetsRequiredMembers] | ||
public SetsRequiredMembersTest(ITestOutputHelper output, IDependency dependency) | ||
{ | ||
_output = Output = output; | ||
_dependency = Dependency = dependency; | ||
|
||
_isCtorSet = true; | ||
} | ||
|
||
public required ITestOutputHelper Output | ||
{ | ||
get => _output; | ||
set | ||
{ | ||
if (_output != null) throw new InvalidOperationException(); | ||
|
||
_output = value; | ||
} | ||
} | ||
|
||
public required IDependency Dependency | ||
{ | ||
get => _dependency; | ||
init | ||
{ | ||
if (_dependency != null) throw new InvalidOperationException(); | ||
|
||
_dependency = value; | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Test() | ||
{ | ||
Assert.True(_isCtorSet); | ||
Assert.NotNull(Output); | ||
Assert.NotNull(Dependency); | ||
} | ||
} |