github.com/appscode/helm@v3.0.0-alpha.1+incompatible/cmd/helm/root_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"k8s.io/client-go/util/homedir"
    25  )
    26  
    27  func TestRootCmd(t *testing.T) {
    28  	defer resetEnv()()
    29  
    30  	tests := []struct {
    31  		name, args, home string
    32  		envars           map[string]string
    33  	}{
    34  		{
    35  			name: "defaults",
    36  			args: "home",
    37  			home: filepath.Join(homedir.HomeDir(), ".helm"),
    38  		},
    39  		{
    40  			name: "with --home set",
    41  			args: "--home /foo",
    42  			home: "/foo",
    43  		},
    44  		{
    45  			name: "subcommands with --home set",
    46  			args: "home --home /foo",
    47  			home: "/foo",
    48  		},
    49  		{
    50  			name:   "with $HELM_HOME set",
    51  			args:   "home",
    52  			envars: map[string]string{"HELM_HOME": "/bar"},
    53  			home:   "/bar",
    54  		},
    55  		{
    56  			name:   "subcommands with $HELM_HOME set",
    57  			args:   "home",
    58  			envars: map[string]string{"HELM_HOME": "/bar"},
    59  			home:   "/bar",
    60  		},
    61  		{
    62  			name:   "with $HELM_HOME and --home set",
    63  			args:   "home --home /foo",
    64  			envars: map[string]string{"HELM_HOME": "/bar"},
    65  			home:   "/foo",
    66  		},
    67  	}
    68  
    69  	// ensure not set locally
    70  	os.Unsetenv("HELM_HOME")
    71  
    72  	for _, tt := range tests {
    73  		t.Run(tt.name, func(t *testing.T) {
    74  			defer os.Unsetenv("HELM_HOME")
    75  
    76  			for k, v := range tt.envars {
    77  				os.Setenv(k, v)
    78  			}
    79  
    80  			cmd, _, err := executeActionCommand(tt.args)
    81  			if err != nil {
    82  				t.Fatalf("unexpected error: %s", err)
    83  			}
    84  
    85  			if settings.Home.String() != tt.home {
    86  				t.Errorf("expected home %q, got %q", tt.home, settings.Home)
    87  			}
    88  			homeFlag := cmd.Flag("home").Value.String()
    89  			homeFlag = os.ExpandEnv(homeFlag)
    90  			if homeFlag != tt.home {
    91  				t.Errorf("expected home %q, got %q", tt.home, homeFlag)
    92  			}
    93  		})
    94  	}
    95  }