File tree Expand file tree Collapse file tree
src/NuGet.Core/NuGet.Common
test/NuGet.Core.Tests/NuGet.Common.Test Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Copyright (c) .NET Foundation. All rights reserved.
2+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+ #if ! NET9_0_OR_GREATER
5+ namespace System . Runtime . CompilerServices
6+ {
7+ [ AttributeUsage ( AttributeTargets . Property , Inherited = false ) ]
8+ internal sealed class FeatureSwitchDefinitionAttribute : Attribute
9+ {
10+ public FeatureSwitchDefinitionAttribute ( string switchName ) => SwitchName = switchName ;
11+ public string SwitchName { get ; }
12+ }
13+ }
14+ #endif
Original file line number Diff line number Diff line change 1+ // Copyright (c) .NET Foundation. All rights reserved.
2+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+ using System ;
5+ using System . Runtime . CompilerServices ;
6+
7+ namespace NuGet . Common
8+ {
9+ public static class NuGetFeatureFlags
10+ {
11+ internal const string UsesNSJDeserializationSwitchName = "NuGet.UsesNSJDeserialization" ;
12+ internal const string UsesNSJDeserializationEnvVar = "NUGET_USES_NSJ_DESERIALIZATION" ;
13+
14+ /// <summary>
15+ /// <see langword="true"/> to use Newtonsoft.Json (default); <see langword="false"/> to use System.Text.Json.
16+ /// Override via AppContext switch <c>NuGet.UsesNSJDeserialization</c> or env var <c>NUGET_USES_NSJ_DESERIALIZATION</c>.
17+ /// </summary>
18+ [ FeatureSwitchDefinition ( UsesNSJDeserializationSwitchName ) ]
19+ public static bool UsesNSJDeserialization { get ; } = GetSwitch ( EnvironmentVariableWrapper . Instance ) ;
20+
21+ internal static bool GetSwitch ( IEnvironmentVariableReader env )
22+ {
23+ if ( AppContext . TryGetSwitch ( UsesNSJDeserializationSwitchName , out bool switchValue ) )
24+ {
25+ return switchValue ;
26+ }
27+
28+ string ? envValue = env . GetEnvironmentVariable ( UsesNSJDeserializationEnvVar ) ;
29+ return ! string . Equals ( envValue , "false" , StringComparison . OrdinalIgnoreCase ) ;
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 11#nullable enable
2+ NuGet.Common.NuGetFeatureFlags
3+ static NuGet.Common.NuGetFeatureFlags.UsesNSJDeserialization.get -> bool
Original file line number Diff line number Diff line change 1+ // Copyright (c) .NET Foundation. All rights reserved.
2+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+ using System . Collections . Generic ;
5+ using Test . Utility ;
6+ using Xunit ;
7+
8+ namespace NuGet . Common . Test
9+ {
10+ public class NuGetFeatureFlagsTests
11+ {
12+ [ Fact ]
13+ public void GetSwitch_WhenEnvVarNotSet_ReturnsTrue ( )
14+ {
15+ bool result = NuGetFeatureFlags . GetSwitch ( TestEnvironmentVariableReader . EmptyInstance ) ;
16+
17+ Assert . True ( result ) ;
18+ }
19+
20+ [ Theory ]
21+ [ InlineData ( "false" ) ]
22+ [ InlineData ( "False" ) ]
23+ [ InlineData ( "FALSE" ) ]
24+ public void GetSwitch_WhenEnvVarSetToFalse_ReturnsFalse ( string value )
25+ {
26+ var env = new TestEnvironmentVariableReader (
27+ new Dictionary < string , string > { [ NuGetFeatureFlags . UsesNSJDeserializationEnvVar ] = value } ) ;
28+
29+ bool result = NuGetFeatureFlags . GetSwitch ( env ) ;
30+
31+ Assert . False ( result ) ;
32+ }
33+
34+ [ Theory ]
35+ [ InlineData ( "true" ) ]
36+ [ InlineData ( "True" ) ]
37+ [ InlineData ( "0" ) ]
38+ [ InlineData ( "1" ) ]
39+ [ InlineData ( "anything" ) ]
40+ public void GetSwitch_WhenEnvVarSetToTrueOrUnrecognized_ReturnsTrue ( string value )
41+ {
42+ var env = new TestEnvironmentVariableReader (
43+ new Dictionary < string , string > { [ NuGetFeatureFlags . UsesNSJDeserializationEnvVar ] = value } ) ;
44+
45+ bool result = NuGetFeatureFlags . GetSwitch ( env ) ;
46+
47+ Assert . True ( result ) ;
48+ }
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments