|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/spf13/viper" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestConfig_dirExists(t *testing.T) { |
| 14 | + tmpF := os.TempDir() |
| 15 | + exists := dirExists(tmpF) |
| 16 | + assert.True(t, exists, |
| 17 | + "%s directory exists but dirExists returned false", tmpF) |
| 18 | + |
| 19 | + tmpF = "/blah-" + time.Now().Format(time.RFC3339Nano) |
| 20 | + exists = dirExists(tmpF) |
| 21 | + assert.False(t, exists, |
| 22 | + "%s directory does not exist but dirExists returned true", |
| 23 | + tmpF) |
| 24 | +} |
| 25 | + |
| 26 | +func TestConfig_AddDevConfigPath(t *testing.T) { |
| 27 | + // Case 1: use viper instance to call AddDevConfigPath |
| 28 | + v := viper.New() |
| 29 | + err := AddDevConfigPath(v) |
| 30 | + assert.NoError(t, err, "Error while adding dev config path to viper") |
| 31 | + |
| 32 | + // Case 2: default viper instance to call AddDevConfigPath |
| 33 | + err = AddDevConfigPath(nil) |
| 34 | + assert.NoError(t, err, "Error while adding dev config path to default viper") |
| 35 | + |
| 36 | + // Error case: GOPATH is empty |
| 37 | + gopath := os.Getenv("GOPATH") |
| 38 | + os.Setenv("GOPATH", "") |
| 39 | + defer os.Setenv("GOPATH", gopath) |
| 40 | + err = AddDevConfigPath(v) |
| 41 | + assert.Error(t, err, "GOPATH is empty, expected error from AddDevConfigPath") |
| 42 | +} |
| 43 | + |
| 44 | +func TestConfig_InitViper(t *testing.T) { |
| 45 | + // Case 1: use viper instance to call InitViper |
| 46 | + v := viper.New() |
| 47 | + err := InitViper(v, "") |
| 48 | + assert.NoError(t, err, "Error returned by InitViper") |
| 49 | + |
| 50 | + // Case 2: default viper instance to call InitViper |
| 51 | + err = InitViper(nil, "") |
| 52 | + assert.NoError(t, err, "Error returned by InitViper") |
| 53 | +} |
| 54 | + |
| 55 | +func TestConfig_GetPath(t *testing.T) { |
| 56 | + // Case 1: non existent viper property |
| 57 | + path := GetPath("foo") |
| 58 | + assert.Equal(t, "", path, "GetPath should have returned empty string for path 'foo'") |
| 59 | + |
| 60 | + // Case 2: viper property that has absolute path |
| 61 | + viper.Set("testpath", "/test/config.yml") |
| 62 | + path = GetPath("testpath") |
| 63 | + assert.Equal(t, "/test/config.yml", path) |
| 64 | +} |
| 65 | + |
| 66 | +func TestConfig_TranslatePathInPlace(t *testing.T) { |
| 67 | + // Case 1: relative path |
| 68 | + p := "foo" |
| 69 | + TranslatePathInPlace(OfficialPath, &p) |
| 70 | + assert.NotEqual(t, "foo", p, "TranslatePathInPlace failed to translate path %s", p) |
| 71 | + |
| 72 | + // Case 2: absolute path |
| 73 | + p = "/foo" |
| 74 | + TranslatePathInPlace(OfficialPath, &p) |
| 75 | + assert.Equal(t, "/foo", p, "TranslatePathInPlace failed to translate path %s", p) |
| 76 | +} |
| 77 | + |
| 78 | +func TestConfig_GetDevMspDir(t *testing.T) { |
| 79 | + // Success case |
| 80 | + _, err := GetDevMspDir() |
| 81 | + assert.NoError(t, err) |
| 82 | + |
| 83 | + // Error case: GOPATH is empty |
| 84 | + gopath := os.Getenv("GOPATH") |
| 85 | + os.Setenv("GOPATH", "") |
| 86 | + defer os.Setenv("GOPATH", gopath) |
| 87 | + _, err = GetDevMspDir() |
| 88 | + assert.Error(t, err, "GOPATH is empty, expected error from GetDevMspDir") |
| 89 | + |
| 90 | + // Error case: GOPATH is set to temp dir |
| 91 | + dir, err1 := ioutil.TempDir("/tmp", "devmspdir") |
| 92 | + assert.NoError(t, err1) |
| 93 | + defer os.RemoveAll(dir) |
| 94 | + os.Setenv("GOPATH", dir) |
| 95 | + _, err = GetDevMspDir() |
| 96 | + assert.Error(t, err, "GOPATH is set to temp dir, expected error from GetDevMspDir") |
| 97 | +} |
| 98 | + |
| 99 | +func TestConfig_GetDevConfigDir(t *testing.T) { |
| 100 | + gopath := os.Getenv("GOPATH") |
| 101 | + os.Setenv("GOPATH", "") |
| 102 | + defer os.Setenv("GOPATH", gopath) |
| 103 | + _, err := GetDevConfigDir() |
| 104 | + assert.Error(t, err, "GOPATH is empty, expected error from GetDevConfigDir") |
| 105 | +} |
0 commit comments