github.com/cmalfait/terraform@v0.11.12-beta1/helper/experiment/experiment_test.go (about) 1 package experiment 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 "testing" 8 ) 9 10 // Test experiments 11 var ( 12 X_test1 = newBasicID("test1", "TEST1", false) 13 X_test2 = newBasicID("test2", "TEST2", true) 14 ) 15 16 // Reinitializes the package to a clean slate 17 func testReinit() { 18 All = []ID{X_test1, X_test2, x_force} 19 reload() 20 } 21 22 func init() { 23 testReinit() 24 25 // Clear all env vars so they don't affect tests 26 for _, id := range All { 27 os.Unsetenv(fmt.Sprintf("TF_X_%s", id.Env())) 28 } 29 } 30 31 func TestDefault(t *testing.T) { 32 testReinit() 33 34 if Enabled(X_test1) { 35 t.Fatal("test1 should not be enabled") 36 } 37 38 if !Enabled(X_test2) { 39 t.Fatal("test2 should be enabled") 40 } 41 } 42 43 func TestEnv(t *testing.T) { 44 os.Setenv("TF_X_TEST2", "0") 45 defer os.Unsetenv("TF_X_TEST2") 46 47 testReinit() 48 49 if Enabled(X_test2) { 50 t.Fatal("test2 should be enabled") 51 } 52 } 53 54 func TestFlag(t *testing.T) { 55 testReinit() 56 57 // Verify default 58 if !Enabled(X_test2) { 59 t.Fatal("test2 should be enabled") 60 } 61 62 // Setup a flag set 63 fs := flag.NewFlagSet("test", flag.ContinueOnError) 64 Flag(fs) 65 fs.Parse([]string{"-Xtest2=false"}) 66 67 if Enabled(X_test2) { 68 t.Fatal("test2 should not be enabled") 69 } 70 } 71 72 func TestFlag_overEnv(t *testing.T) { 73 os.Setenv("TF_X_TEST2", "1") 74 defer os.Unsetenv("TF_X_TEST2") 75 76 testReinit() 77 78 // Verify default 79 if !Enabled(X_test2) { 80 t.Fatal("test2 should be enabled") 81 } 82 83 // Setup a flag set 84 fs := flag.NewFlagSet("test", flag.ContinueOnError) 85 Flag(fs) 86 fs.Parse([]string{"-Xtest2=false"}) 87 88 if Enabled(X_test2) { 89 t.Fatal("test2 should not be enabled") 90 } 91 } 92 93 func TestForce(t *testing.T) { 94 os.Setenv("TF_X_FORCE", "1") 95 defer os.Unsetenv("TF_X_FORCE") 96 97 testReinit() 98 99 if !Force() { 100 t.Fatal("should force") 101 } 102 } 103 104 func TestForce_flag(t *testing.T) { 105 os.Unsetenv("TF_X_FORCE") 106 107 testReinit() 108 109 // Setup a flag set 110 fs := flag.NewFlagSet("test", flag.ContinueOnError) 111 Flag(fs) 112 fs.Parse([]string{"-Xforce"}) 113 114 if !Force() { 115 t.Fatal("should force") 116 } 117 }