Skip to content

Commit d6ee6ae

Browse files
committed
fix(test): define the xUnit constant that generated code actually reads
The SDK defined XUNIT_ENTRYPOINT_DISABLE_WARNINGS, but no xUnit release ever read it: 4.0.0 had no such switch, and 4.0.1 shipped it as XUNIT_GENERATED_DISABLE_WARNINGS, extended to its AOT source generation. Analyzer warnings in xUnit's generated entry point therefore stayed live and could fail a warnings-as-errors build. The SDK now defines the shipped name; EnableXunitEntryPointDisableWarnings remains the opt-out. The consumer smoke test now asserts that xUnit's generated entry point tests the constant the SDK defines, so a future upstream rename fails the suite instead of silently disabling the suppression. See xunit/xunit#3606
1 parent 6b9c47c commit d6ee6ae

5 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ the listed default; explicit values win unless the behavior is identified as man
257257
| `HeadlessCoverageSettingsPath` | packaged Test SDK path | Absolute path to the SDK-owned `default.runsettings`; query this evaluated property when an external runner needs the canonical coverage denominator policy. |
258258
| `EnableDefaultTestSettings` | enabled unless `false` | Set `false` to own all MTP command-line defaults. |
259259
| `MinimumExpectedTests` | `1` | Sets the MTP minimum-test guard. Set `0` to omit only the SDK-supplied `--minimum-expected-tests` argument; this does not guarantee that a zero-test run succeeds. |
260-
| `EnableXunitEntryPointDisableWarnings` | `true` when a supported xUnit v3 package is directly referenced | Set `false` to prevent the SDK from adding `XUNIT_ENTRYPOINT_DISABLE_WARNINGS`. |
260+
| `EnableXunitEntryPointDisableWarnings` | `true` when a supported xUnit v3 package is directly referenced | xUnit v3 4.0.1 and later wrap their generated entry point and AOT source in `#pragma warning disable` when `XUNIT_GENERATED_DISABLE_WARNINGS` is defined, so analyzer warnings cannot fail a build on code the consumer does not own. Set `false` to prevent the SDK from adding the constant. |
261261
| `OptimizeTestRun` | enabled unless `false` | Set `false` to keep analyzers enabled during MTP's test-build phase. |
262262
| `DisableSupportPackageInformation` | `false` | Set `true` to opt out of Headless package metadata and symbol policy. |
263263
| `SearchReadmeFileAbove` | `false` | Searches parent directories for a package README. |

‎src/Headless.NET.Sdk.Test/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ maintain a consumer-owned runsettings file.
7575
- Microsoft Testing Platform only; VSTest and `Microsoft.NET.Test.Sdk` are not injected.
7676
- Restore-visible crash dump, hang dump, hot reload, retry, TRX, and coverage extensions.
7777
- Default TRX output, crash and hang dumps, and `MinimumExpectedTests=1`. Set it to a positive value to change the MTP guard, or `0` to omit only the SDK-supplied argument; omission does not guarantee that a zero-test run succeeds. `EnableDefaultTestSettings=false` removes all SDK-owned MTP arguments.
78-
- A direct supported xUnit v3 package reference adds `XUNIT_ENTRYPOINT_DISABLE_WARNINGS` once while preserving other constants. Set `EnableXunitEntryPointDisableWarnings=false` to opt out; test-project classification alone does not enable it.
78+
- A direct supported xUnit v3 package reference adds `XUNIT_GENERATED_DISABLE_WARNINGS` once while preserving other constants; xUnit v3 4.0.1 and later then suppress warnings in their generated code. Set `EnableXunitEntryPointDisableWarnings=false` to opt out; test-project classification alone does not enable it.
7979
- Coverage enabled on CI, with `HeadlessCoverageSettingsPath` exposing the packaged denominator policy, and analyzer work skipped during the test-build phase unless explicitly retained.
8080
- Mandatory Headless analyzer infrastructure, configurable banned-API policy, and mandatory audit and CI policies with narrow test-code severity relaxations.
8181

‎src/Headless.NET.Sdk/build/SupportTestProjects.targets‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@
111111
>$(TestingPlatformCommandLineArguments) --minimum-expected-tests $(MinimumExpectedTests)TestingPlatformCommandLineArguments>
112112
PropertyGroup>
113113

114-
114+
115120
<PropertyGroup>
116121
<_HeadlessXunitEntryPointDisableWarningsTarget>_HeadlessConfigureXunitEntryPointDisableWarnings_HeadlessXunitEntryPointDisableWarningsTarget>
117122
PropertyGroup>
@@ -132,15 +137,15 @@
132137
<EnableXunitEntryPointDisableWarnings Condition="'$(EnableXunitEntryPointDisableWarnings)' == ''"
133138
>trueEnableXunitEntryPointDisableWarnings>
134139
<_HeadlessXunitEntryPointDisableWarningsAlreadyDefined Condition="'$(MSBuildProjectExtension)' == '.vbproj'"
135-
>$([System.String]::Copy(',$(DefineConstants),').Contains(',XUNIT_ENTRYPOINT_DISABLE_WARNINGS=-1,'))_HeadlessXunitEntryPointDisableWarningsAlreadyDefined>
140+
>$([System.String]::Copy(',$(DefineConstants),').Contains(',XUNIT_GENERATED_DISABLE_WARNINGS=-1,'))_HeadlessXunitEntryPointDisableWarningsAlreadyDefined>
136141
<_HeadlessXunitEntryPointDisableWarningsAlreadyDefined Condition="'$(MSBuildProjectExtension)' != '.vbproj'"
137-
>$([System.String]::Copy(';$(DefineConstants);').Contains(';XUNIT_ENTRYPOINT_DISABLE_WARNINGS;'))_HeadlessXunitEntryPointDisableWarningsAlreadyDefined>
142+
>$([System.String]::Copy(';$(DefineConstants);').Contains(';XUNIT_GENERATED_DISABLE_WARNINGS;'))_HeadlessXunitEntryPointDisableWarningsAlreadyDefined>
138143
<DefineConstants
139144
Condition="'$(_HeadlessXunitEntryPointDisableWarningsAlreadyDefined)' != 'true' AND '$(MSBuildProjectExtension)' == '.vbproj'"
140-
>$(DefineConstants),XUNIT_ENTRYPOINT_DISABLE_WARNINGS=-1DefineConstants>
145+
>$(DefineConstants),XUNIT_GENERATED_DISABLE_WARNINGS=-1DefineConstants>
141146
<DefineConstants
142147
Condition="'$(_HeadlessXunitEntryPointDisableWarningsAlreadyDefined)' != 'true' AND '$(MSBuildProjectExtension)' != '.vbproj'"
143-
>$(DefineConstants);XUNIT_ENTRYPOINT_DISABLE_WARNINGSDefineConstants>
148+
>$(DefineConstants);XUNIT_GENERATED_DISABLE_WARNINGSDefineConstants>
144149
PropertyGroup>
145150
Target>
146151

‎tests/Headless.NET.Sdk.Tests.Integrations/ContractConsumerBehaviorTests.Testing.cs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task should_restore_required_mtp_extensions_and_run_a_clean_consume
4545
additionalFiles: new Dictionary<string, string>(StringComparer.Ordinal)
4646
{
4747
["ContractSmokeTests.cs"] =
48-
"#if !XUNIT_ENTRYPOINT_DISABLE_WARNINGS\n#error XUNIT_ENTRYPOINT_DISABLE_WARNINGS is not defined\n#endif\nusing Xunit; namespace ConsumerProject; public sealed class ContractSmokeTests { [Fact] public void passes() => Assert.True(true); }",
48+
"#if !XUNIT_GENERATED_DISABLE_WARNINGS\n#error XUNIT_GENERATED_DISABLE_WARNINGS is not defined\n#endif\nusing Xunit; namespace ConsumerProject; public sealed class ContractSmokeTests { [Fact] public void passes() => Assert.True(true); }",
4949
}
5050
);
5151
await UpdateProjectAsync(
@@ -117,5 +117,20 @@ await UpdateProjectAsync(
117117
$"{test.Output}{Environment.NewLine}Evaluated properties:{Environment.NewLine}{evaluation.Output}"
118118
);
119119
Assert.Contains("Passed", test.Output, StringComparison.OrdinalIgnoreCase);
120+
121+
// The SDK-defined constant only helps when it is the name xUnit's generated code tests for;
122+
// a renamed upstream symbol would otherwise leave the suppression silently inert.
123+
var entryPoint = Assert.Single(
124+
Directory.EnumerateFiles(
125+
Path.Combine(project.RootDirectory, "obj"),
126+
"XunitAutoGeneratedEntryPoint.cs",
127+
SearchOption.AllDirectories
128+
)
129+
);
130+
Assert.Contains(
131+
"#if XUNIT_GENERATED_DISABLE_WARNINGS",
132+
await File.ReadAllTextAsync(entryPoint, TestContext.Current.CancellationToken),
133+
StringComparison.Ordinal
134+
);
120135
}
121136
}

‎tests/Headless.NET.Sdk.Tests.Integrations/SdkIntegrationTests.ProjectTypes.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,13 @@ bool useSdkConsumption
212212

213213
Assert.Equal("true", properties["EnableXunitEntryPointDisableWarnings"]);
214214
Assert.Contains("UNRELATED_CONSTANT", constants);
215-
Assert.Single(constants, constant => constant == "XUNIT_ENTRYPOINT_DISABLE_WARNINGS");
215+
Assert.Single(constants, constant => constant == "XUNIT_GENERATED_DISABLE_WARNINGS");
216216
}
217217

218218
[Theory]
219219
[InlineData("false", "UNRELATED_CONSTANT", 0)]
220-
[InlineData(null, "UNRELATED_CONSTANT;XUNIT_ENTRYPOINT_DISABLE_WARNINGS", 1)]
221-
[InlineData(null, "UNRELATED_XUNIT_ENTRYPOINT_DISABLE_WARNINGS_CONSTANT", 1)]
220+
[InlineData(null, "UNRELATED_CONSTANT;XUNIT_GENERATED_DISABLE_WARNINGS", 1)]
221+
[InlineData(null, "UNRELATED_XUNIT_GENERATED_DISABLE_WARNINGS_CONSTANT", 1)]
222222
public async Task should_respect_xunit_entrypoint_warning_constant_overrides(
223223
string? optOut,
224224
string initialConstants,
@@ -250,7 +250,7 @@ int expectedConstantCount
250250

251251
Assert.Equal(
252252
expectedConstantCount,
253-
constants.Count(constant => constant == "XUNIT_ENTRYPOINT_DISABLE_WARNINGS")
253+
constants.Count(constant => constant == "XUNIT_GENERATED_DISABLE_WARNINGS")
254254
);
255255
Assert.Contains(initialConstants.Split(';')[0], constants);
256256
}
@@ -268,7 +268,7 @@ public async Task should_not_infer_xunit_entrypoint_warning_constant_from_test_p
268268
var properties = await project.EvaluateHeadlessPropertiesAsync();
269269

270270
Assert.Empty(properties["EnableXunitEntryPointDisableWarnings"]);
271-
Assert.DoesNotContain("XUNIT_ENTRYPOINT_DISABLE_WARNINGS", properties["DefineConstants"]);
271+
Assert.DoesNotContain("XUNIT_GENERATED_DISABLE_WARNINGS", properties["DefineConstants"]);
272272
}
273273

274274
[Fact]

0 commit comments

Comments
 (0)