File tree Expand file tree Collapse file tree
src/NuGet.Core/NuGet.Protocol 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 . Diagnostics . CodeAnalysis
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 . Diagnostics . CodeAnalysis ;
6+ using NuGet . Common ;
7+
8+ namespace NuGet . Shared
9+ {
10+ internal static class NuGetFeatureFlags
11+ {
12+ internal const string UsesNSJDeserializationSwitchName = "NuGet.UsesNSJDeserialization" ;
13+ internal const string UsesNSJDeserializationEnvVar = "NUGET_USES_NSJ_DESERIALIZATION" ;
14+
15+ private static readonly Lazy < bool > _isNSJDeserializationEnabledByEnvironment =
16+ new Lazy < bool > ( ( ) => IsNSJDeserializationEnabledByEnvironment ( EnvironmentVariableWrapper . Instance ) ) ;
17+
18+ /// <summary>Feature switch for NSJ deserialization. Defaults to <see langword="true"/>.</summary>
19+ [ FeatureSwitchDefinition ( UsesNSJDeserializationSwitchName ) ]
20+ internal static bool NSJDeserializationFeatureSwitch { get ; } =
21+ ! AppContext . TryGetSwitch ( UsesNSJDeserializationSwitchName , out bool value ) || value ;
22+
23+ /// <summary>Returns <see langword="false"/> when env var <c>NUGET_USES_NSJ_DESERIALIZATION</c> is <c>false</c>.</summary>
24+ internal static bool IsNSJDeserializationEnabledByEnvironment ( IEnvironmentVariableReader ? env = null )
25+ {
26+ if ( env is null )
27+ {
28+ return _isNSJDeserializationEnabledByEnvironment . Value ;
29+ }
30+
31+ string ? envValue = env . GetEnvironmentVariable ( UsesNSJDeserializationEnvVar ) ;
32+ return ! string . Equals ( envValue , "false" , StringComparison . OrdinalIgnoreCase ) ;
33+ }
34+ }
35+ }
Original file line number Diff line number Diff line change 3939 <Compile Include =" $(SharedDirectory)\HashCodeCombiner.cs" />
4040 <Compile Include =" $(SharedDirectory)\NoAllocEnumerateExtensions.cs" />
4141 <Compile Include =" $(SharedDirectory)\NullableAttributes.cs" />
42+ <Compile Include =" $(SharedDirectory)\NuGetFeatureFlags.cs" />
43+ <Compile Include =" $(SharedDirectory)\AotCompatibilityAttributes.cs" />
4244 <Compile Include =" $(SharedDirectory)\SimplePool.cs" />
4345 <Compile Include =" $(SharedDirectory)\StringBuilderPool.cs" />
4446 <Compile Include =" $(SharedDirectory)\TaskResult.cs" />
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 NuGet . Shared ;
6+ using Test . Utility ;
7+ using Xunit ;
8+
9+ namespace NuGet . Protocol . Tests
10+ {
11+ public class NuGetFeatureFlagsTests
12+ {
13+ [ Fact ]
14+ public void NSJDeserializationFeatureSwitch_Default_ReturnsTrue ( )
15+ {
16+ Assert . True ( NuGetFeatureFlags . NSJDeserializationFeatureSwitch ) ;
17+ }
18+
19+ [ Fact ]
20+ public void IsNSJDeserializationEnabledByEnvironment_WhenEnvVarNotSet_ReturnsTrue ( )
21+ {
22+ Assert . True ( NuGetFeatureFlags . IsNSJDeserializationEnabledByEnvironment ( TestEnvironmentVariableReader . EmptyInstance ) ) ;
23+ }
24+
25+ [ Theory ]
26+ [ InlineData ( "false" ) ]
27+ [ InlineData ( "False" ) ]
28+ [ InlineData ( "FALSE" ) ]
29+ public void IsNSJDeserializationEnabledByEnvironment_WhenEnvVarSetToFalse_ReturnsFalse ( string value )
30+ {
31+ var env = new TestEnvironmentVariableReader (
32+ new Dictionary < string , string > { [ NuGetFeatureFlags . UsesNSJDeserializationEnvVar ] = value } ) ;
33+
34+ Assert . False ( NuGetFeatureFlags . IsNSJDeserializationEnabledByEnvironment ( env ) ) ;
35+ }
36+
37+ [ Theory ]
38+ [ InlineData ( "true" ) ]
39+ [ InlineData ( "True" ) ]
40+ [ InlineData ( "0" ) ]
41+ [ InlineData ( "1" ) ]
42+ [ InlineData ( "anything" ) ]
43+ public void IsNSJDeserializationEnabledByEnvironment_WhenEnvVarSetToTrueOrUnrecognized_ReturnsTrue ( string value )
44+ {
45+ var env = new TestEnvironmentVariableReader (
46+ new Dictionary < string , string > { [ NuGetFeatureFlags . UsesNSJDeserializationEnvVar ] = value } ) ;
47+
48+ Assert . True ( NuGetFeatureFlags . IsNSJDeserializationEnabledByEnvironment ( env ) ) ;
49+ }
50+ }
51+ }
Original file line number Diff line number Diff line change 99 <Compile Include =" $(SharedDirectory)\*.cs" Exclude =" bin\**;obj\**;**\*.xproj;packages\**" />
1010 <Compile Remove =" $(SharedDirectory)\IsExternalInit.cs" />
1111 <Compile Remove =" $(SharedDirectory)\RequiredModifierAttributes.cs" />
12+ <Compile Remove =" $(SharedDirectory)\AotCompatibilityAttributes.cs" />
13+ <Compile Remove =" $(SharedDirectory)\NuGetFeatureFlags.cs" />
1214 </ItemGroup >
1315 <ItemGroup >
1416 <ProjectReference Include =" ..\..\TestUtilities\Test.Utility\Test.Utility.csproj" />
You can’t perform that action at this time.
0 commit comments