-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
firebase_test.go
107 lines (101 loc) · 2.37 KB
/
firebase_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package segments
import (
"path/filepath"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/mock"
"github.com/stretchr/testify/assert"
mock2 "github.com/stretchr/testify/mock"
)
func TestFirebaseSegment(t *testing.T) {
cases := []struct {
Case string
CfgData string
ActiveConfig string
ExpectedEnabled bool
ExpectedString string
}{
{
Case: "happy path",
ExpectedEnabled: true,
ActiveConfig: `{
"activeProjects": {
"path": "project-name"
}
}`,
ExpectedString: "project-name",
},
{
Case: "no active config",
ExpectedEnabled: false,
},
{
Case: "empty config",
ActiveConfig: "{}",
ExpectedEnabled: false,
},
{
Case: "bad config",
ActiveConfig: "{bad}",
ExpectedEnabled: false,
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Home").Return("home")
env.On("Pwd").Return("path")
fcPath := filepath.Join("home", ".config", "configstore", "firebase-tools.json")
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
env.On("Error", mock2.Anything).Return()
f := Firebase{
env: env,
}
f.Enabled()
assert.Equal(t, tc.ExpectedEnabled, f.Enabled())
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, f.Template(), f), tc.Case)
}
}
}
func TestGetFirebaseActiveConfig(t *testing.T) {
data :=
`{
"activeProjects": {
"path": "project-name"
}
}`
cases := []struct {
Case string
ActiveConfig string
ExpectedString string
ExpectedError string
}{
{
Case: "happy path",
ActiveConfig: data,
ExpectedString: data,
},
{
Case: "no active config",
ActiveConfig: "",
ExpectedError: FIREBASENOACTIVECONFIG,
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Home").Return("home")
configPath := filepath.Join("home", ".config", "configstore")
contentPath := filepath.Join(configPath, "firebase-tools.json")
env.On("FileContent", contentPath).Return(tc.ActiveConfig)
env.On("Error", mock2.Anything).Return()
f := Firebase{
env: env,
}
got, err := f.getActiveConfig(configPath)
assert.Equal(t, tc.ExpectedString, got, tc.Case)
if len(tc.ExpectedError) > 0 {
assert.EqualError(t, err, tc.ExpectedError, tc.Case)
} else {
assert.NoError(t, err, tc.Case)
}
}
}