[Analyzers] Add analyzers for [EnvironmentRestorer] usage in tests#8402
Draft
lucaspimentel wants to merge 2 commits intomasterfrom
Draft
[Analyzers] Add analyzers for [EnvironmentRestorer] usage in tests#8402lucaspimentel wants to merge 2 commits intomasterfrom
[EnvironmentRestorer] usage in tests#8402lucaspimentel wants to merge 2 commits intomasterfrom
Conversation
DD0006 flags Environment.SetEnvironmentVariable calls in xUnit test methods that lack a corresponding [EnvironmentRestorer] attribute, preventing env var state from leaking between tests. DD0013 detects redundant [EnvironmentRestorer] attributes — either duplicated across methods (suggest class-level) or already covered by a class-level attribute (suggest removal). Both rules are disabled by default and enabled via .editorconfig. 🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
[EnvironmentRestorer] in tests
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
[EnvironmentRestorer] in tests[EnvironmentRestorer] usage in tests
Execution-Time Benchmarks Report ⏱️Execution-time results for samples comparing This PR (8402) and master. ✅ No regressions detected - check the details below Full Metrics ComparisonFakeDbCommand
HttpMessageHandler
Comparison explanationExecution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:
Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard. Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph). Duration chartsFakeDbCommand (.NET Framework 4.8)gantt
title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8402) - mean (72ms) : 70, 75
master - mean (76ms) : 73, 79
section Bailout
This PR (8402) - mean (76ms) : 74, 78
master - mean (81ms) : 78, 83
section CallTarget+Inlining+NGEN
This PR (8402) - mean (1,061ms) : 1020, 1102
master - mean (1,102ms) : 1061, 1143
FakeDbCommand (.NET Core 3.1)gantt
title Execution time (ms) FakeDbCommand (.NET Core 3.1)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8402) - mean (112ms) : 109, 115
master - mean (120ms) : 116, 124
section Bailout
This PR (8402) - mean (114ms) : 111, 116
master - mean (121ms) : 118, 124
section CallTarget+Inlining+NGEN
This PR (8402) - mean (780ms) : 760, 799
master - mean (810ms) : 786, 835
FakeDbCommand (.NET 6)gantt
title Execution time (ms) FakeDbCommand (.NET 6)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8402) - mean (99ms) : 97, 102
master - mean (105ms) : 100, 109
section Bailout
This PR (8402) - mean (101ms) : 99, 103
master - mean (105ms) : 103, 108
section CallTarget+Inlining+NGEN
This PR (8402) - mean (934ms) : 904, 964
master - mean (950ms) : 919, 982
FakeDbCommand (.NET 8)gantt
title Execution time (ms) FakeDbCommand (.NET 8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8402) - mean (99ms) : 96, 102
master - mean (104ms) : 101, 107
section Bailout
This PR (8402) - mean (99ms) : 97, 102
master - mean (105ms) : 103, 108
section CallTarget+Inlining+NGEN
This PR (8402) - mean (815ms) : 772, 859
master - mean (835ms) : 795, 876
HttpMessageHandler (.NET Framework 4.8)gantt
title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8402) - mean (189ms) : 186, 192
master - mean (189ms) : 186, 192
section Bailout
This PR (8402) - mean (192ms) : 191, 194
master - mean (192ms) : 191, 194
section CallTarget+Inlining+NGEN
This PR (8402) - mean (1,128ms) : 1087, 1170
master - mean (1,131ms) : 1085, 1177
HttpMessageHandler (.NET Core 3.1)gantt
title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8402) - mean (271ms) : 268, 275
master - mean (272ms) : 268, 276
section Bailout
This PR (8402) - mean (272ms) : 269, 274
master - mean (272ms) : 269, 275
section CallTarget+Inlining+NGEN
This PR (8402) - mean (918ms) : 895, 941
master - mean (921ms) : 892, 951
HttpMessageHandler (.NET 6)gantt
title Execution time (ms) HttpMessageHandler (.NET 6)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8402) - mean (265ms) : 260, 271
master - mean (265ms) : 261, 269
section Bailout
This PR (8402) - mean (265ms) : 263, 267
master - mean (265ms) : 263, 267
section CallTarget+Inlining+NGEN
This PR (8402) - mean (1,124ms) : 1064, 1183
master - mean (1,131ms) : 1092, 1170
HttpMessageHandler (.NET 8)gantt
title Execution time (ms) HttpMessageHandler (.NET 8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8402) - mean (263ms) : 259, 268
master - mean (263ms) : 259, 266
section Bailout
This PR (8402) - mean (264ms) : 261, 266
master - mean (263ms) : 260, 266
section CallTarget+Inlining+NGEN
This PR (8402) - mean (1,016ms) : 977, 1055
master - mean (1,012ms) : 967, 1058
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of changes
Add two new Roslyn analyzers to prevent environment variable leaks in xUnit tests:
DD0014: FlagsEnvironment.SetEnvironmentVariablecalls in test methods ([Fact],[Theory],[SkippableFact], etc.) that are not covered by a corresponding[EnvironmentRestorer]attribute at the method or class levelDD0013: Detects redundant[EnvironmentRestorer]attributes — either duplicated across methods (should be class-level) or already covered by a class-level attribute (should be removed)Also extends
EnvironmentRestorerAttributeto supportAttributeTargets.Methodin addition toAttributeTargets.Class.Reason for change
PR #8388 fixed several test files where
Environment.SetEnvironmentVariablewas called without proper cleanup, causing env var state to leak between tests (flaky/order-dependent failures). These analyzers prevent the same issues from recurring by catching them at compile time.Implementation details
isEnabledByDefault: false) and opt-in via.editorconfigDD0014usesCompilationStartActionto resolveXunit.FactAttributeonce, then checks allSetEnvironmentVariableinvocations in test methods. It handles both constant variable names (checks exact[EnvironmentRestorer]coverage) and non-constant names (always flags, suggests using a constant or#pragmasuppression)DD0013usesRegisterSymbolActiononINamedTypeSymbolto scan class members for duplicate or redundant[EnvironmentRestorer]attributestracer/test/.editorconfigis added enabling both rules forDatadog.Trace.TestsTest coverage
18 analyzer tests covering:
[Fact],[Theory],[SkippableFact]detection (semantic — checks attribute inheritance fromFactAttribute)[EnvironmentRestorer]suppression[EnvironmentRestorer]or#pragma)Other details
Related: #8388
The analyzer is intentionally shipped disabled. To enable, add to
.editorconfig: