11package codegate
22
33import (
4- "os"
54 "strings"
65 "testing"
76
87 "github.com/stretchr/testify/require"
98)
109
11- // Code gates are somewhat troublesome to test due to the reliance on a static environment variable
12- // at the time of the gate creation. All tests run in the same environment, so setting an environment
13- // variable in one test may affect other tests (depending on order.) DisableGates() testing only
14- // works because it is implemented to dynamically inspect the environment at call time and does not
15- // cache results.
10+ // Code gates are troublesome to test due to the reliance on static environment
11+ // variables at the time of the gate creation. All tests run in the same environment,
12+ // so setting an environment in one test may affect other tests. All tests in this
13+ // file should use unique gate names to avoid conflicts.
1614
1715func TestNoDisabledGates (t * testing.T ) {
1816 gateTestFoo := New ("FOO" )
1917 require .True (t , gateTestFoo .Enabled (), "arbitrary code behavior should be enabled by default" )
20- require .NotContains (t , DisabledGates (true ), "FOO" )
18+ require .NotContains (t , DisabledGates (), "FOO" )
2119}
2220
2321func TestGateNames (t * testing.T ) {
@@ -45,31 +43,45 @@ func TestGateNames(t *testing.T) {
4543}
4644
4745func TestDisableOneGate (t * testing.T ) {
48- _ = os .Setenv ("DISABLE_Bar " , "disabled" )
46+ t .Setenv ("DISABLE_CODE_Bar " , "disabled" )
4947
5048 // refresh disabled gates to pick up the changes to the environment
5149 // variables
52- DisabledGates ( true )
50+ resetDisabledGates ( )
5351
5452 gateTestBar := New ("Bar" )
5553 require .False (t , gateTestBar .Enabled (), "Bar should be disabled" )
5654 require .True (t , New ("Bar2" ).Enabled (), "Other gates should be enabled" )
57- require .Contains (t , DisabledGates (false ), "Bar" )
55+ require .Contains (t , DisabledGates (), "Bar" )
56+ }
57+
58+ func TestDisableOldGate (t * testing.T ) {
59+ t .Setenv ("DISABLE_S2CODE_Deprecated" , "disabled" )
60+
61+ // refresh disabled gates to pick up the changes to the environment
62+ // variables
63+ resetDisabledGates ()
64+
65+ gateTestDeprecated := New ("Deprecated" )
66+ require .False (t , gateTestDeprecated .Enabled (), "Deprecated should be disabled" )
67+ require .True (t , New ("Deprecated2" ).Enabled (), "Other gates should be enabled" )
68+ require .Contains (t , DisabledGates (), "Deprecated" )
5869}
5970
6071func TestDisableMultipleGates (t * testing.T ) {
6172 // create some random environment variables
62- _ = os .Setenv ("NOISE" , "LOUD" )
63- _ = os .Setenv ("MORE_NOISE" , "LOUDER" )
64- _ = os .Setenv ("DISABLED_CODE" , "" )
73+ t .Setenv ("NOISE" , "LOUD" )
74+ t .Setenv ("MORE_NOISE" , "LOUDER" )
75+ t .Setenv ("DISABLED_CODE" , "" )
76+ t .Setenv ("DISABLED_CODE_TOO" , "" )
6577
6678 // disable two gates
67- _ = os .Setenv ("DISABLE_Baz1 " , "disabled" )
68- _ = os .Setenv ("DISABLE_Baz3 " , "disabled" )
79+ t .Setenv ("DISABLE_CODE_Baz1 " , "disabled" )
80+ t .Setenv ("DISABLE_CODE_Baz3 " , "disabled" )
6981
7082 // refresh disabled gates to pick up the changes to the environment
7183 // variables
72- DisabledGates ( true )
84+ resetDisabledGates ( )
7385
7486 // define four gates
7587 gateTestBaz1 := New ("Baz1" )
@@ -82,20 +94,20 @@ func TestDisableMultipleGates(t *testing.T) {
8294 require .False (t , gateTestBaz3 .Enabled (), "Baz3 should be disabled" )
8395 require .True (t , gateTestBaz4 .Enabled (), "Baz4 should be enabled" )
8496
85- require .Contains (t , DisabledGates (false ), "Baz1" )
86- require .NotContains (t , DisabledGates (false ), "Baz2" )
87- require .Contains (t , DisabledGates (false ), "Baz3" )
88- require .NotContains (t , DisabledGates (false ), "Baz4" )
97+ require .Contains (t , DisabledGates (), "Baz1" )
98+ require .NotContains (t , DisabledGates (), "Baz2" )
99+ require .Contains (t , DisabledGates (), "Baz3" )
100+ require .NotContains (t , DisabledGates (), "Baz4" )
89101}
90102
91- func TestRefreshDisabledGates (t * testing.T ) {
103+ func TestResetDisabledGates (t * testing.T ) {
92104 // ensure no disabled gates at start
93- _ = os .Unsetenv ("DISABLE_Foo" )
94- require .NotContains (t , DisabledGates (false ), "Foo" )
105+ require .NotContains (t , DisabledGates (), "Foo" )
95106
96107 // disable Foo
97- _ = os .Setenv ("DISABLE_Foo " , "disabled" )
98- require .NotContains (t , DisabledGates (false ), "Foo" )
108+ t .Setenv ("DISABLE_CODE_Foo " , "disabled" )
109+ require .NotContains (t , DisabledGates (), "Foo" )
99110 // refresh disabled gates
100- require .Contains (t , DisabledGates (true ), "Foo" , "DisabledGates(true) should refresh the disabled gates" )
111+ resetDisabledGates ()
112+ require .Contains (t , DisabledGates (), "Foo" , "DisabledGates(true) should refresh the disabled gates" )
101113}
0 commit comments