Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test switch scopes should read switch default values dynamically at initialization time #12490

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

Tanya-Solyanik
Copy link
Member

@Tanya-Solyanik Tanya-Solyanik commented Nov 15, 2024

This enables turning AppContext switches on and off within the unit test regardless of the default value

Microsoft Reviewers: Open in CodeFlow

// Provides default values for switches if they're not always false by default
private static bool GetSwitchDefaultValue(string switchName)
{
if (switchName == "Switch.System.Runtime.Serialization.SerializationGuard")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THese switches are not owned by us, and this method is not responsible for setting their defaults.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only 2 switches in this assembly are "false" by default. THis file is in a common folder, but is used only by System.Drawing.Common, we could either make is shared between winforms and S.D.C, but I don't see benefit in doing this. I'd rather move it under the S.D.C root

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

Attention: Patch coverage is 43.39623% with 30 lines in your changes missing coverage. Please review.

Project coverage is 75.72766%. Comparing base (d5bd070) to head (0b483e2).
Report is 3 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #12490         +/-   ##
===================================================
- Coverage   75.73525%   75.72766%   -0.00759%     
===================================================
  Files           3153        3151          -2     
  Lines         635807      635802          -5     
  Branches       46975       46978          +3     
===================================================
- Hits          481530      481478         -52     
- Misses        150842      150895         +53     
+ Partials        3435        3429          -6     
Flag Coverage Δ
Debug 75.72766% <43.39623%> (-0.00759%) ⬇️
integration 18.26609% <30.43478%> (-0.00434%) ⬇️
production 49.30093% <65.21739%> (-0.00953%) ⬇️
test 97.04481% <26.66667%> (-0.00626%) ⬇️
unit 46.25316% <65.21739%> (-0.03704%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.


private void ResetLocalSwitches()
{
_testAccessor.s_anchorLayoutV2 = 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class modifies statics and is not used in our tests. In fact we have alternatives to it - we can set specific switches via their specific scopes instead of changing the Target Framework name property.

@@ -91,48 +91,47 @@ private static bool GetSwitchValue(string switchName, ref int cachedSwitchValue)
{
cachedSwitchValue = isSwitchEnabled ? 1 /*true*/ : -1 /*false*/;
}
else if (!hasSwitch)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is executed in the test codepath, when "TestSwitch.LocalAppContext.DisableCaching" is on, when we access the switch value for the first time in the process. Setting switch in the AppContext dictionaries will ensure that the correct value is read in AppContext.TryGetSwitch call. This is needed for tests that run the same scenario with different switch values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not a comment in the code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a description of one of the issues fixed in this PR, the comment was intended to highlight this fix among other changes. All it does is describe exactly what the code is doing, I don't see any value in it as a code comment.

Copy link
Member

@lonitra lonitra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Tanya-Solyanik Tanya-Solyanik merged commit 4491491 into dotnet:main Nov 15, 2024
8 checks passed
@Tanya-Solyanik Tanya-Solyanik deleted the switch-scopes branch November 15, 2024 18:28
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0 Preview1 milestone Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants