github.com/fengjian1993/viper@v1.11.0/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/spf13/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  				"ABc": "A",
    28  				"cDE": "B",
    29  			},
    30  		}
    31  		expected = map[string]interface{}{
    32  			"foo": 32,
    33  			"bar": map[string]interface{}{
    34  				"abc": "A",
    35  				"cde": "B",
    36  			},
    37  		}
    38  	)
    39  
    40  	got := copyAndInsensitiviseMap(given)
    41  
    42  	if !reflect.DeepEqual(got, expected) {
    43  		t.Fatalf("Got %q\nexpected\n%q", got, expected)
    44  	}
    45  
    46  	if _, ok := given["foo"]; ok {
    47  		t.Fatal("Input map changed")
    48  	}
    49  
    50  	if _, ok := given["bar"]; ok {
    51  		t.Fatal("Input map changed")
    52  	}
    53  
    54  	m := given["Bar"].(map[interface{}]interface{})
    55  	if _, ok := m["ABc"]; !ok {
    56  		t.Fatal("Input map changed")
    57  	}
    58  }
    59  
    60  func TestAbsPathify(t *testing.T) {
    61  	skipWindows(t)
    62  
    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(jwwLogger{}, test.input)
    91  		if got != test.output {
    92  			t.Errorf("Got %v\nexpected\n%q", got, test.output)
    93  		}
    94  	}
    95  }