github.com/ava-labs/viper@v1.7.2-0.20210125155433-65cc0421b384/util_test.go (about)

     1  // Copyright © 2016 Steve Francia <spf@spf13.com>.
     2  //
     3  // Use of this source code is governed by an MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // Viper is a application configuration system.
     7  // It believes that applications can be configured a variety of ways
     8  // via flags, ENVIRONMENT variables, configuration files retrieved
     9  // from the file system, or a remote key/value store.
    10  
    11  package viper
    12  
    13  import (
    14  	"os"
    15  	"path/filepath"
    16  	"reflect"
    17  	"testing"
    18  
    19  	"github.com/ava-labs/viper/internal/testutil"
    20  )
    21  
    22  func TestCopyAndInsensitiviseMap(t *testing.T) {
    23  	var (
    24  		given = map[string]interface{}{
    25  			"Foo": 32,
    26  			"Bar": map[interface{}]interface {
    27  			}{
    28  				"ABc": "A",
    29  				"cDE": "B",
    30  			},
    31  		}
    32  		expected = map[string]interface{}{
    33  			"foo": 32,
    34  			"bar": map[string]interface {
    35  			}{
    36  				"abc": "A",
    37  				"cde": "B",
    38  			},
    39  		}
    40  	)
    41  
    42  	got := copyAndInsensitiviseMap(given)
    43  
    44  	if !reflect.DeepEqual(got, expected) {
    45  		t.Fatalf("Got %q\nexpected\n%q", got, expected)
    46  	}
    47  
    48  	if _, ok := given["foo"]; ok {
    49  		t.Fatal("Input map changed")
    50  	}
    51  
    52  	if _, ok := given["bar"]; ok {
    53  		t.Fatal("Input map changed")
    54  	}
    55  
    56  	m := given["Bar"].(map[interface{}]interface{})
    57  	if _, ok := m["ABc"]; !ok {
    58  		t.Fatal("Input map changed")
    59  	}
    60  }
    61  
    62  func TestAbsPathify(t *testing.T) {
    63  	home := userHomeDir()
    64  	homer := filepath.Join(home, "homer")
    65  	wd, _ := os.Getwd()
    66  
    67  	testutil.Setenv(t, "HOMER_ABSOLUTE_PATH", homer)
    68  	testutil.Setenv(t, "VAR_WITH_RELATIVE_PATH", "relative")
    69  
    70  	tests := []struct {
    71  		input  string
    72  		output string
    73  	}{
    74  		{"", wd},
    75  		{"sub", filepath.Join(wd, "sub")},
    76  		{"./", wd},
    77  		{"./sub", filepath.Join(wd, "sub")},
    78  		{"$HOME", home},
    79  		{"$HOME/", home},
    80  		{"$HOME/sub", filepath.Join(home, "sub")},
    81  		{"$HOMER_ABSOLUTE_PATH", homer},
    82  		{"$HOMER_ABSOLUTE_PATH/", homer},
    83  		{"$HOMER_ABSOLUTE_PATH/sub", filepath.Join(homer, "sub")},
    84  		{"$VAR_WITH_RELATIVE_PATH", filepath.Join(wd, "relative")},
    85  		{"$VAR_WITH_RELATIVE_PATH/", filepath.Join(wd, "relative")},
    86  		{"$VAR_WITH_RELATIVE_PATH/sub", filepath.Join(wd, "relative", "sub")},
    87  	}
    88  
    89  	for _, test := range tests {
    90  		got := absPathify(test.input)
    91  		if got != test.output {
    92  			t.Errorf("Got %v\nexpected\n%q", got, test.output)
    93  		}
    94  	}
    95  }