|
5 | 5 | package config |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "math/rand" |
| 9 | + "time" |
8 | 10 | "os" |
| 11 | + "strconv" |
9 | 12 | "strings" |
10 | 13 | "testing" |
11 | | - "strconv" |
12 | | - "math/rand" |
13 | 14 |
|
14 | 15 | "github.com/scaleway/scaleway-cli/pkg/scwversion" |
15 | 16 | . "github.com/smartystreets/goconvey/convey" |
@@ -38,37 +39,60 @@ func TestGetConfigFilePathEnv(t *testing.T) { |
38 | 39 | } |
39 | 40 |
|
40 | 41 | func TestGetConfig(t *testing.T) { |
41 | | - Convey("Testing GetConfig() with env variable", t, func() { |
| 42 | + Convey("Testing GetConfig() with and without env variable", t, func() { |
42 | 43 | os.Setenv("SCW_CONFIG_PATH", "./config_testdata1") |
43 | | - config, err := GetConfig() |
| 44 | + cfg := &Config{ |
| 45 | + Organization: strings.Trim("test_orgID", "\n"), |
| 46 | + Token: strings.Trim("test_token", "\n"), |
| 47 | + } |
| 48 | + err := cfg.Save("") |
44 | 49 | So(err, ShouldBeNil) |
45 | | - So(config.Version, ShouldEqual, "test_version") |
46 | | - So(config.Organization, ShouldEqual, "test_orgID") |
47 | | - So(config.Token, ShouldEqual, "test_token") |
| 50 | + cfg, err = GetConfig("./config_testdata1") |
| 51 | + So(cfg.Organization, ShouldEqual, "test_orgID") |
| 52 | + So(cfg.Token, ShouldEqual, "test_token") |
48 | 53 | os.Unsetenv("SCW_CONFIG_PATH") |
| 54 | + cfg, err = GetConfig("./config_testdata1") |
| 55 | + So(err, ShouldBeNil) |
| 56 | + So(cfg.Organization, ShouldEqual, "test_orgID") |
| 57 | + So(cfg.Token, ShouldEqual, "test_token") |
49 | 58 | }) |
50 | 59 | } |
51 | 60 |
|
52 | 61 | func TestSave(t *testing.T) { |
53 | | - Convey("Testing SaveConfig() with env variable", t, func() { |
| 62 | + Convey("Testing SaveConfig() with and without env variable", t, func() { |
54 | 63 | os.Setenv("SCW_CONFIG_PATH", "./config_testdata2") |
| 64 | + rand.Seed(time.Now().UTC().UnixNano()) |
55 | 65 | randOrg := strconv.FormatInt(rand.Int63(), 16) |
56 | 66 | randToken := strconv.FormatInt(rand.Int63(), 16) |
57 | 67 | cfg := &Config{ |
58 | 68 | Organization: strings.Trim(randOrg, "\n"), |
59 | 69 | Token: strings.Trim(randToken, "\n"), |
60 | 70 | } |
61 | | - err := cfg.Save() |
| 71 | + err := cfg.Save("") |
| 72 | + So(err, ShouldBeNil) |
| 73 | + cfg, err = GetConfig("") |
62 | 74 | So(err, ShouldBeNil) |
63 | 75 | So(cfg.Version, ShouldEqual, scwversion.VERSION) |
64 | 76 | So(cfg.Organization, ShouldEqual, randOrg) |
65 | 77 | So(cfg.Token, ShouldEqual, randToken) |
66 | 78 | os.Unsetenv("SCW_CONFIG_PATH") |
67 | | - }) |
68 | | -} |
69 | | - |
70 | 79 |
|
| 80 | + randOrg = strconv.FormatInt(rand.Int63(), 16) |
| 81 | + randToken = strconv.FormatInt(rand.Int63(), 16) |
| 82 | + cfg = &Config{ |
| 83 | + Organization: strings.Trim(randOrg, "\n"), |
| 84 | + Token: strings.Trim(randToken, "\n"), |
| 85 | + } |
| 86 | + err = cfg.Save("./config_testdata2") |
| 87 | + So(err, ShouldBeNil) |
| 88 | + cfg, err = GetConfig("./config_testdata2") |
| 89 | + So(err, ShouldBeNil) |
| 90 | + So(cfg.Version, ShouldEqual, scwversion.VERSION) |
| 91 | + So(cfg.Organization, ShouldEqual, randOrg) |
| 92 | + So(cfg.Token, ShouldEqual, randToken) |
71 | 93 |
|
| 94 | + }) |
| 95 | +} |
72 | 96 |
|
73 | 97 | func TestGetHomeDir(t *testing.T) { |
74 | 98 | Convey("Testing GetHomeDir()", t, func() { |
|
0 commit comments