github.com/yimialmonte/fabric@v2.1.1+incompatible/core/config/config_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package config
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/spf13/viper"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestConfig_dirExists(t *testing.T) {
    19  	tmpF := os.TempDir()
    20  	exists := dirExists(tmpF)
    21  	assert.True(t, exists,
    22  		"%s directory exists but dirExists returned false", tmpF)
    23  
    24  	tmpF = "/blah-" + time.Now().Format(time.RFC3339Nano)
    25  	exists = dirExists(tmpF)
    26  	assert.False(t, exists,
    27  		"%s directory does not exist but dirExists returned true",
    28  		tmpF)
    29  }
    30  
    31  func TestConfig_InitViper(t *testing.T) {
    32  	// Case 1: use viper instance to call InitViper
    33  	v := viper.New()
    34  	err := InitViper(v, "")
    35  	assert.NoError(t, err, "Error returned by InitViper")
    36  
    37  	// Case 2: default viper instance to call InitViper
    38  	err = InitViper(nil, "")
    39  	assert.NoError(t, err, "Error returned by InitViper")
    40  }
    41  
    42  func TestConfig_GetPath(t *testing.T) {
    43  	// Case 1: non existent viper property
    44  	path := GetPath("foo")
    45  	assert.Equal(t, "", path, "GetPath should have returned empty string for path 'foo'")
    46  
    47  	// Case 2: viper property that has absolute path
    48  	viper.Set("testpath", "/test/config.yml")
    49  	path = GetPath("testpath")
    50  	assert.Equal(t, "/test/config.yml", path)
    51  }
    52  
    53  func TestConfig_TranslatePathInPlace(t *testing.T) {
    54  	// Case 1: relative path
    55  	p := "foo"
    56  	TranslatePathInPlace(OfficialPath, &p)
    57  	assert.NotEqual(t, "foo", p, "TranslatePathInPlace failed to translate path %s", p)
    58  
    59  	// Case 2: absolute path
    60  	p = "/foo"
    61  	TranslatePathInPlace(OfficialPath, &p)
    62  	assert.Equal(t, "/foo", p, "TranslatePathInPlace failed to translate path %s", p)
    63  }