...

Source file src/github.com/spf13/viper/util_test.go

Documentation: github.com/spf13/viper

     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  	"testing"
    17  
    18  	slog "github.com/sagikazarmark/slog-shim"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestCopyAndInsensitiviseMap(t *testing.T) {
    23  	var (
    24  		given = map[string]any{
    25  			"Foo": 32,
    26  			"Bar": map[any]any{
    27  				"ABc": "A",
    28  				"cDE": "B",
    29  			},
    30  		}
    31  		expected = map[string]any{
    32  			"foo": 32,
    33  			"bar": map[string]any{
    34  				"abc": "A",
    35  				"cde": "B",
    36  			},
    37  		}
    38  	)
    39  
    40  	got := copyAndInsensitiviseMap(given)
    41  
    42  	assert.Equal(t, expected, got)
    43  	_, ok := given["foo"]
    44  	assert.False(t, ok)
    45  	_, ok = given["bar"]
    46  	assert.False(t, ok)
    47  
    48  	m := given["Bar"].(map[any]any)
    49  	_, ok = m["ABc"]
    50  	assert.True(t, ok)
    51  }
    52  
    53  func TestAbsPathify(t *testing.T) {
    54  	skipWindows(t)
    55  
    56  	home := userHomeDir()
    57  	homer := filepath.Join(home, "homer")
    58  	wd, _ := os.Getwd()
    59  
    60  	t.Setenv("HOMER_ABSOLUTE_PATH", homer)
    61  	t.Setenv("VAR_WITH_RELATIVE_PATH", "relative")
    62  
    63  	tests := []struct {
    64  		input  string
    65  		output string
    66  	}{
    67  		{"", wd},
    68  		{"sub", filepath.Join(wd, "sub")},
    69  		{"./", wd},
    70  		{"./sub", filepath.Join(wd, "sub")},
    71  		{"$HOME", home},
    72  		{"$HOME/", home},
    73  		{"$HOME/sub", filepath.Join(home, "sub")},
    74  		{"$HOMER_ABSOLUTE_PATH", homer},
    75  		{"$HOMER_ABSOLUTE_PATH/", homer},
    76  		{"$HOMER_ABSOLUTE_PATH/sub", filepath.Join(homer, "sub")},
    77  		{"$VAR_WITH_RELATIVE_PATH", filepath.Join(wd, "relative")},
    78  		{"$VAR_WITH_RELATIVE_PATH/", filepath.Join(wd, "relative")},
    79  		{"$VAR_WITH_RELATIVE_PATH/sub", filepath.Join(wd, "relative", "sub")},
    80  	}
    81  
    82  	for _, test := range tests {
    83  		got := absPathify(slog.Default(), test.input)
    84  		assert.Equal(t, test.output, got)
    85  	}
    86  }
    87  

View as plain text