@ -17,6 +17,10 @@ limitations under the License.
package cmd
package cmd
import (
import (
"bytes"
"encoding/json"
"regexp"
"strings"
"testing"
"testing"
)
)
@ -25,10 +29,139 @@ func TestEnv(t *testing.T) {
name : "completion for env" ,
name : "completion for env" ,
cmd : "__complete env ''" ,
cmd : "__complete env ''" ,
golden : "output/env-comp.txt" ,
golden : "output/env-comp.txt" ,
} , {
name : "completion for env output flag" ,
cmd : "__complete env --output ''" ,
golden : "output/env-output-comp.txt" ,
} , {
name : "env with invalid output format" ,
cmd : "env --output table" ,
golden : "output/env-output-invalid.txt" ,
wantError : true ,
} }
} }
runTestCmd ( t , tests )
runTestCmd ( t , tests )
}
}
func TestEnvOutputDefault ( t * testing . T ) {
// Pin the default format: without --output, every line must keep the
// historic KEY="VALUE" shape. The values are machine-specific, so
// assert the shape rather than compare against a golden file.
_ , out , err := executeActionCommand ( "env" )
if err != nil {
t . Fatal ( err )
}
if ! strings . Contains ( out , ` HELM_BIN=" ` ) {
t . Errorf ( "expected HELM_BIN in default output, got %q" , out )
}
lineRe := regexp . MustCompile ( ` ^[A-Z0-9_]+=".*"$ ` )
for line := range strings . SplitSeq ( strings . TrimSuffix ( out , "\n" ) , "\n" ) {
if ! lineRe . MatchString ( line ) {
t . Errorf ( "line %q does not match KEY=\"VALUE\" format" , line )
}
}
// Single-variable mode without --output keeps printing the bare value.
_ , out , err = executeActionCommand ( "env HELM_BURST_LIMIT" )
if err != nil {
t . Fatal ( err )
}
if out != "100\n" {
t . Errorf ( "expected %q, got %q" , "100\n" , out )
}
}
func TestEnvOutputJSON ( t * testing . T ) {
// The full listing contains machine-specific paths, so validate the
// shape instead of comparing against a golden file.
_ , out , err := executeActionCommand ( "env --output json" )
if err != nil {
t . Fatal ( err )
}
var envVars map [ string ] string
if err := json . Unmarshal ( [ ] byte ( out ) , & envVars ) ; err != nil {
t . Fatalf ( "expected valid JSON output, got %q: %s" , out , err )
}
if _ , ok := envVars [ "HELM_BIN" ] ; ! ok {
t . Errorf ( "expected HELM_BIN in JSON output, got %v" , envVars )
}
}
func TestEnvFormatWrite ( t * testing . T ) {
envVars := map [ string ] string {
"HELM_BIN" : "/usr/local/bin/helm" ,
"HELM_DEBUG" : "false" ,
"HELM_NAMESPACE" : "default" ,
}
tests := [ ] struct {
name string
format envFormat
want string
} { {
name : "key=value format" ,
format : envFormatKeyValue ,
want : ` HELM_BIN = "/usr/local/bin/helm"
HELM_DEBUG = "false"
HELM_NAMESPACE = "default"
` ,
} , {
name : "json format" ,
format : envFormatJSON ,
want : ` { "HELM_BIN":"/usr/local/bin/helm","HELM_DEBUG":"false","HELM_NAMESPACE":"default"} ` + "\n" ,
} , {
name : "yaml format" ,
format : envFormatYAML ,
want : ` HELM_BIN : / usr / local / bin / helm
HELM_DEBUG : "false"
HELM_NAMESPACE : default
` ,
} }
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
var buf bytes . Buffer
if err := tt . format . write ( & buf , envVars ) ; err != nil {
t . Fatal ( err )
}
if got := buf . String ( ) ; got != tt . want {
t . Errorf ( "expected %q, got %q" , tt . want , got )
}
} )
}
}
func TestEnvFormatWriteSingle ( t * testing . T ) {
tests := [ ] struct {
name string
format envFormat
want string
} { {
name : "key=value format prints the bare value" ,
format : envFormatKeyValue ,
want : "default\n" ,
} , {
name : "json format" ,
format : envFormatJSON ,
want : ` { "HELM_NAMESPACE":"default"} ` + "\n" ,
} , {
name : "yaml format" ,
format : envFormatYAML ,
want : "HELM_NAMESPACE: default\n" ,
} }
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
var buf bytes . Buffer
if err := tt . format . writeSingle ( & buf , "HELM_NAMESPACE" , "default" ) ; err != nil {
t . Fatal ( err )
}
if got := buf . String ( ) ; got != tt . want {
t . Errorf ( "expected %q, got %q" , tt . want , got )
}
} )
}
}
func TestEnvFileCompletion ( t * testing . T ) {
func TestEnvFileCompletion ( t * testing . T ) {
checkFileCompletion ( t , "env" , false )
checkFileCompletion ( t , "env" , false )
checkFileCompletion ( t , "env HELM_BIN" , false )
checkFileCompletion ( t , "env HELM_BIN" , false )