55package config
66
77import (
8+ "math/rand"
9+ "os"
10+ "strconv"
811 "strings"
912 "testing"
13+ "time"
1014
15+ "github.com/scaleway/scaleway-cli/pkg/scwversion"
1116 . "github.com/smartystreets/goconvey/convey"
1217)
1318
@@ -23,6 +28,83 @@ func TestGetConfigFilePath(t *testing.T) {
2328 })
2429}
2530
31+ func TestGetConfigFilePathEnv (t * testing.T ) {
32+ Convey ("Testing GetConfigFilePath() with env variable" , t , func () {
33+ os .Setenv ("SCW_CONFIG_PATH" , "./config_testdata1" )
34+ configPath , err := GetConfigFilePath ()
35+ So (err , ShouldBeNil )
36+ So (configPath , ShouldEqual , "./config_testdata1" )
37+ os .Unsetenv ("SCW_CONFIG_PATH" )
38+ })
39+ }
40+
41+ func TestGetConfig (t * testing.T ) {
42+ Convey ("Testing GetConfig() with and without env variable" , t , func () {
43+ rand .Seed (time .Now ().UTC ().UnixNano ())
44+ randOrg := strconv .FormatInt (rand .Int63 (), 16 )
45+ randToken := strconv .FormatInt (rand .Int63 (), 16 )
46+ cfg := & Config {
47+ Organization : strings .Trim (randOrg , "\n " ),
48+ Token : strings .Trim (randToken , "\n " ),
49+ }
50+ os .Setenv ("SCW_CONFIG_PATH" , "./config_testdata1" )
51+ err := cfg .Save ("" )
52+ So (err , ShouldBeNil )
53+ cfg , err = GetConfig ("./config_testdata1" )
54+ So (cfg .Organization , ShouldEqual , randOrg )
55+ So (cfg .Token , ShouldEqual , randToken )
56+ os .Unsetenv ("SCW_CONFIG_PATH" )
57+ cfg , err = GetConfig ("./config_testdata1" )
58+ So (err , ShouldBeNil )
59+ So (cfg .Organization , ShouldEqual , randOrg )
60+ So (cfg .Token , ShouldEqual , randToken )
61+ os .Setenv ("SCW_ORGANIZATION" , randOrg )
62+ os .Setenv ("SCW_TOKEN" , randToken )
63+ cfg , err = GetConfig ("" )
64+ So (err , ShouldBeNil )
65+ So (cfg .Organization , ShouldEqual , randOrg )
66+ So (cfg .Token , ShouldEqual , randToken )
67+ os .Unsetenv ("SCW_ORGANIZATION" )
68+ os .Unsetenv ("SCW_TOKEN" )
69+ })
70+ }
71+
72+ func TestSave (t * testing.T ) {
73+ Convey ("Testing SaveConfig() with and without env variable" , t , func () {
74+ os .Setenv ("SCW_CONFIG_PATH" , "./config_testdata2" )
75+ rand .Seed (time .Now ().UTC ().UnixNano ())
76+ randOrg := strconv .FormatInt (rand .Int63 (), 16 )
77+ randToken := strconv .FormatInt (rand .Int63 (), 16 )
78+ cfg := & Config {
79+ Organization : strings .Trim (randOrg , "\n " ),
80+ Token : strings .Trim (randToken , "\n " ),
81+ }
82+ err := cfg .Save ("" )
83+ So (err , ShouldBeNil )
84+ cfg , err = GetConfig ("" )
85+ So (err , ShouldBeNil )
86+ So (cfg .Version , ShouldEqual , scwversion .VERSION )
87+ So (cfg .Organization , ShouldEqual , randOrg )
88+ So (cfg .Token , ShouldEqual , randToken )
89+ os .Unsetenv ("SCW_CONFIG_PATH" )
90+
91+ randOrg = strconv .FormatInt (rand .Int63 (), 16 )
92+ randToken = strconv .FormatInt (rand .Int63 (), 16 )
93+ cfg = & Config {
94+ Organization : strings .Trim (randOrg , "\n " ),
95+ Token : strings .Trim (randToken , "\n " ),
96+ }
97+ err = cfg .Save ("./config_testdata2" )
98+ So (err , ShouldBeNil )
99+ cfg , err = GetConfig ("./config_testdata2" )
100+ So (err , ShouldBeNil )
101+ So (cfg .Version , ShouldEqual , scwversion .VERSION )
102+ So (cfg .Organization , ShouldEqual , randOrg )
103+ So (cfg .Token , ShouldEqual , randToken )
104+
105+ })
106+ }
107+
26108func TestGetHomeDir (t * testing.T ) {
27109 Convey ("Testing GetHomeDir()" , t , func () {
28110 homedir , err := GetHomeDir ()
0 commit comments