-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathconfig_test.go
61 lines (51 loc) · 1.96 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package config
import (
"fmt"
"os"
"testing"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestGetConfigPath(t *testing.T) {
t.Run("read config.ini from ARDUINO_CREATE_AGENT_CONFIG", func(t *testing.T) {
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/fromenv/config.ini")
defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
configPath := GetConfigPath()
assert.Equal(t, "./testdata/fromenv/config.ini", configPath.String())
})
t.Run("panic if config.ini does not exist", func(t *testing.T) {
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/nonexistent_config.ini")
defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
defer func() {
if r := recover(); r != nil {
entry, ok := r.(*logrus.Entry)
if !ok {
t.Errorf("Expected panic of type *logrus.Entry but got %T", r)
} else {
assert.Equal(t, "config from env var ./testdata/nonexistent_config.ini does not exists", entry.Message)
}
} else {
t.Errorf("Expected panic but did not get one")
}
}()
GetConfigPath()
})
t.Run("read config.ini from $HOME", func(t *testing.T) {
os.Setenv("HOME", "./testdata/home")
defer os.Unsetenv("HOME")
configPath := GetConfigPath()
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
})
t.Run("fallback old : read config.ini where the binary is launched", func(t *testing.T) {
src, _ := os.Executable()
paths.New(src).Parent().Join("config.ini").Create() // create a config.ini in the same directory as the binary
// The fallback path is the directory where the binary is launched
fmt.Println(src)
os.Setenv("HOME", "./testdata/noconfig") // force to not have a config in the home directory
defer os.Unsetenv("HOME")
// expect it creates a config.ini in the same directory as the binary
configPath := GetConfigPath()
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
})
}