@@ -3,8 +3,12 @@ package param_parsing
33
44import (
55 "os"
6+ "os/exec"
7+ "regexp"
8+ "strings"
69 "testing"
710
11+ "github.com/gookit/color"
812 "github.com/warrensbox/terraform-switcher/lib"
913)
1014
@@ -97,3 +101,81 @@ func TestGetParamsFromEnvironment_log_level_from_env(t *testing.T) {
97101 t .Errorf ("Determined log level is not matching. Got %q, expected %q" , params .LogLevel , expected )
98102 }
99103}
104+
105+ func TestNoColorEnvVar (t * testing.T ) {
106+ envVarName := "NO_COLOR"
107+ _ = os .Setenv (envVarName , "true" )
108+ goCommandArgs := []string {"run" , "../../main.go" , "--dry-run" , "1.10.5" }
109+
110+ t .Logf ("Testing %q env var" , envVarName )
111+
112+ out , err := exec .Command ("go" , goCommandArgs ... ).CombinedOutput ()
113+ _ = os .Unsetenv (envVarName )
114+ if err != nil {
115+ t .Fatalf ("Unexpected failure: \" %v\" , output: %q" , err , string (out ))
116+ }
117+
118+ matched , err := regexp .MatchString (ansiCodesRegex , string (out ))
119+ if err != nil {
120+ t .Fatalf ("Unexpected failure: \" %v\" , output: %q" , err , string (out ))
121+ }
122+
123+ if matched {
124+ t .Errorf ("Expected no ANSI color codes in output, but found some: %q" , string (out ))
125+ } else {
126+ t .Log ("Success: no ANSI color codes in output" )
127+ }
128+ }
129+
130+ func TestForceColorEnvVar (t * testing.T ) {
131+ envVarName := "FORCE_COLOR"
132+ if ! color .SupportColor () {
133+ t .Skipf ("Skipping test for %q env var as terminal doesn't support colors" , envVarName )
134+ }
135+
136+ _ = os .Setenv (envVarName , "true" )
137+ goCommandArgs := []string {"run" , "../../main.go" , "--dry-run" , "1.10.5" }
138+
139+ t .Logf ("Testing %q env var" , envVarName )
140+
141+ out , err := exec .Command ("go" , goCommandArgs ... ).CombinedOutput ()
142+ _ = os .Unsetenv (envVarName )
143+ if err != nil {
144+ t .Fatalf ("Unexpected failure: \" %v\" , output: %q" , err , string (out ))
145+ }
146+
147+ matched , err := regexp .MatchString (ansiCodesRegex , string (out ))
148+ if err != nil {
149+ t .Fatalf ("Unexpected failure: \" %v\" , output: %q" , err , string (out ))
150+ }
151+
152+ if ! matched {
153+ t .Errorf ("Expected ANSI color codes in output, but found none: %q" , string (out ))
154+ } else {
155+ t .Log ("Success: found ANSI color codes in output" )
156+ }
157+ }
158+
159+ func TestNoAndForceColorEnvVars (t * testing.T ) {
160+ envVarNameForceColor := "FORCE_COLOR"
161+ _ = os .Setenv (envVarNameForceColor , "true" )
162+ envVarNameNoColor := "NO_COLOR"
163+ _ = os .Setenv (envVarNameNoColor , "true" )
164+
165+ expectedOutput := "FATAL (env) Cannot force color and disable color at the same time. Please choose either of them."
166+
167+ goCommandArgs := []string {"run" , "../../main.go" , "--dry-run" , "1.10.5" }
168+
169+ t .Logf ("Testing %q and %q env vars both present" , envVarNameForceColor , envVarNameNoColor )
170+
171+ out , _ := exec .Command ("go" , goCommandArgs ... ).CombinedOutput () // nolint:errcheck // We want to test the output even if it fails
172+
173+ _ = os .Unsetenv (envVarNameForceColor )
174+ _ = os .Unsetenv (envVarNameNoColor )
175+
176+ if ! strings .Contains (string (out ), expectedOutput ) {
177+ t .Errorf ("Expected %q, got: %q" , expectedOutput , out )
178+ } else {
179+ t .Logf ("Success: %q" , expectedOutput )
180+ }
181+ }
0 commit comments