Skip to content

Commit

Permalink
Add cocoa fact attribute and related unit tests (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Sandy Armstrong <[email protected]>
Co-authored-by: Andrew Arnott <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2023
1 parent 8d8e639 commit f3d5569
Show file tree
Hide file tree
Showing 24 changed files with 594 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,6 @@ MigrationBackup/

# dotnet tool local install directory
.store/

# File built by MacOS
.DS_Store
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ The easiest way to restore packages may be to run `init.ps1` which automatically
to the feeds that packages for this repo come from, if any.
`dotnet restore` or `nuget restore` also work but may require extra steps to authenticate to any applicable feeds.

MacOS specific code that was added in #68 requires that `init.ps1` be run in the repo within a terminal *and devenv.exe must be spawned from that same window* in order for Visual Studio for Windows to be able to find the required macos workload.
Note that `init.ps1 -InstallLocality machine` in broken due to dotnet workloads problems such as https://github.com/dotnet/sdk/issues/30230.

## Building

Building, testing, and packing this repository can be done by using the standard dotnet CLI commands (e.g. `dotnet build`, `dotnet test`, `dotnet pack`, etc.).
Expand Down
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<TargetingWindows Condition=" '$(TargetFramework)' == 'net472' or $(TargetFramework.EndsWith('-windows')) ">true</TargetingWindows>
<TargetingMacos Condition="$(TargetFramework.Contains('macos'))">true</TargetingMacos>

<!-- https://github.com/dotnet/msbuild/blob/main/documentation/ProjectReference-Protocol.md#setplatform-negotiation -->
<EnableDynamicPlatformResolution>true</EnableDynamicPlatformResolution>
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageVersion Include="Microsoft.CodeCoverage" Version="17.5.0-release-20230105-01" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageVersion Include="xunit.extensibility.execution" Version="2.4.2" />
<PackageVersion Include="xunit.runner.console" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageVersion Include="xunit" Version="2.4.2" />
</ItemGroup>
Expand Down
19 changes: 19 additions & 0 deletions Xunit.StaFact.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xunit.StaFact.Tests", "test\Xunit.StaFact.Tests\Xunit.StaFact.Tests.csproj", "{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xunit.StaFact.Tests.Mac", "test\Xunit.StaFact.Tests.Mac\Xunit.StaFact.Tests.Mac.csproj", "{077DFB77-0E63-4399-BD0B-4F9D9AC80495}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B9635B8C-19E8-4D04-ACA4-A391D558D80A}"
ProjectSection(SolutionItems) = preProject
src\.editorconfig = src\.editorconfig
Expand All @@ -38,24 +40,41 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
DebugMac|Any CPU = DebugMac|Any CPU
Release|Any CPU = Release|Any CPU
ReleaseMac|Any CPU = ReleaseMac|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6}.DebugMac|Any CPU.ActiveCfg = Debug|Any CPU
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6}.DebugMac|Any CPU.Build.0 = Debug|Any CPU
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6}.Release|Any CPU.Build.0 = Release|Any CPU
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6}.ReleaseMac|Any CPU.ActiveCfg = Release|Any CPU
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6}.ReleaseMac|Any CPU.Build.0 = Release|Any CPU
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}.DebugMac|Any CPU.ActiveCfg = Debug|Any CPU
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}.DebugMac|Any CPU.Build.0 = Debug|Any CPU
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}.Release|Any CPU.Build.0 = Release|Any CPU
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}.ReleaseMac|Any CPU.ActiveCfg = Release|Any CPU
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E}.ReleaseMac|Any CPU.Build.0 = Release|Any CPU
{077DFB77-0E63-4399-BD0B-4F9D9AC80495}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{077DFB77-0E63-4399-BD0B-4F9D9AC80495}.DebugMac|Any CPU.ActiveCfg = Debug|Any CPU
{077DFB77-0E63-4399-BD0B-4F9D9AC80495}.DebugMac|Any CPU.Build.0 = Debug|Any CPU
{077DFB77-0E63-4399-BD0B-4F9D9AC80495}.Release|Any CPU.ActiveCfg = Release|Any CPU
{077DFB77-0E63-4399-BD0B-4F9D9AC80495}.ReleaseMac|Any CPU.ActiveCfg = Release|Any CPU
{077DFB77-0E63-4399-BD0B-4F9D9AC80495}.ReleaseMac|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8C500C20-0CD6-4257-A9ED-C571DA6E68D6} = {B9635B8C-19E8-4D04-ACA4-A391D558D80A}
{9F99EC1E-7FF7-419D-84F7-3CE96D653C2E} = {1E94DC16-537C-4D14-A201-ED3140A994EB}
{077DFB77-0E63-4399-BD0B-4F9D9AC80495} = {1E94DC16-537C-4D14-A201-ED3140A994EB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DA3510A0-C12F-4470-B8A6-197F0D32CB7C}
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
- template: dotnet.yml
parameters:
RunTests: ${{ parameters.RunTests }}
RunMacTests: true

- job: WrapUp
dependsOn:
Expand Down
18 changes: 17 additions & 1 deletion azure-pipelines/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
parameters:
RunTests:
- name: RunTests
- name: RunMacTests
type: boolean
default: false

steps:

Expand All @@ -10,6 +13,19 @@ steps:
displayName: 🧪 dotnet test
condition: and(succeeded(), ${{ parameters.RunTests }})

- ${{ if parameters.RunMacTests }}:
- script: dotnet build --no-restore -c $(BuildConfiguration) ./test/Xunit.StaFact.Tests.Mac/Xunit.StaFact.Tests.Mac.csproj -t:Test -p:BuildProjectReferences=false
displayName: 🧪 Mac-specific tests

- task: PublishTestResults@2
inputs:
testResultsFormat: XUnit
testResultsFiles: ./test_logs/Xunit.StaFact.Tests.Mac.xml
testRunTitle: Xunit.StaFact.Tests.Mac (net6.0-macos, MacOS)
displayName: 📢 Publish Mac-specific tests results
condition: succeededOrFailed()
continueOnError: true

- powershell: azure-pipelines/variables/_pipelines.ps1
failOnStderr: true
displayName: ⚙ Update pipeline variables based on build outputs
Expand Down
8 changes: 7 additions & 1 deletion init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ if (!$NoPrerequisites) {
if ($env:OS -eq 'Windows_NT') {
$EnvVars['PROCDUMP_PATH'] = & "$PSScriptRoot\azure-pipelines\Get-ProcDump.ps1"
}

# Install specific macOS workload version in order to support usage on older .NET and macOS
dotnet workload install macos --from-rollback-file $PSScriptRoot/workloads.json
}

# Workaround nuget credential provider bug that causes very unreliable package restores on Azure Pipelines
Expand All @@ -77,7 +80,10 @@ try {

if (!$NoRestore -and $PSCmdlet.ShouldProcess("NuGet packages", "Restore")) {
Write-Host "Restoring NuGet packages" -ForegroundColor $HeaderColor
dotnet restore
if ($IsMacOS) {
$restoreArgs = "/p:Configuration=DebugMac"
}
dotnet restore $restoreArgs
if ($lastexitcode -ne 0) {
throw "Failure while restoring packages."
}
Expand Down
16 changes: 16 additions & 0 deletions src/Xunit.StaFact/Mac/CocoaFactAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the Ms-PL license. See LICENSE file in the project root for full license information.

using Xunit.Sdk;

namespace Xunit;

/// <summary>
/// Identifies an xunit test that starts on with a <see cref="System.Threading.SynchronizationContext"/>
/// running on <see cref="Foundation.NSRunLoop.Main"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[XunitTestCaseDiscoverer("Xunit.Sdk.CocoaFactDiscoverer", ThisAssembly.AssemblyName)]
public class CocoaFactAttribute : FactAttribute
{
}
16 changes: 16 additions & 0 deletions src/Xunit.StaFact/Mac/CocoaTheoryAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the Ms-PL license. See LICENSE file in the project root for full license information.

using Xunit.Sdk;

namespace Xunit;

/// <summary>
/// Identifies an xunit theory that starts on with a <see cref="System.Threading.SynchronizationContext"/>
/// running on <see cref="Foundation.NSRunLoop.Main"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[XunitTestCaseDiscoverer("Xunit.Sdk.CocoaTheoryDiscoverer", ThisAssembly.AssemblyName)]
public class CocoaTheoryAttribute : TheoryAttribute
{
}
35 changes: 35 additions & 0 deletions src/Xunit.StaFact/Sdk.Mac/CocoaFactDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the Ms-PL license. See LICENSE file in the project root for full license information.

using System.Runtime.CompilerServices;

namespace Xunit.Sdk;

/// <summary>
/// The discovery class for <see cref="CocoaFactDiscoverer"/>.
/// </summary>
public class CocoaFactDiscoverer : FactDiscoverer
{
private readonly IMessageSink diagnosticMessageSink;

/// <summary>
/// Initializes a new instance of the <see cref="CocoaFactDiscoverer"/> class.
/// </summary>
/// <param name="diagnosticMessageSink">The diagnostic message sink.</param>
public CocoaFactDiscoverer(IMessageSink diagnosticMessageSink)
: base(diagnosticMessageSink)
{
this.diagnosticMessageSink = diagnosticMessageSink;
}

protected override IXunitTestCase CreateTestCase(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
if (testMethod.Method.ReturnType.Name == "System.Void" &&
testMethod.Method.GetCustomAttributes(typeof(AsyncStateMachineAttribute)).Any())
{
return new ExecutionErrorTestCase(this.diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), TestMethodDisplayOptions.None, testMethod, "Async void methods are not supported.");
}

return new UITestCase(UITestCase.SyncContextType.Cocoa, this.diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod);
}
}
Loading

0 comments on commit f3d5569

Please sign in to comment.