Skip to content

Commit 5c176af

Browse files
committed
fix: guard against nil watch configurations
1 parent 62c4bdb commit 5c176af

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

internal/hooks/sdk_config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ func (w *WatchOpts) IsAvailable() bool {
9595

9696
// GetManifestWatchConfig returns manifest watch config
9797
func (w *WatchOpts) GetManifestWatchConfig() (paths []string, filterRegex string, enabled bool) {
98+
if w == nil {
99+
return nil, "", false
100+
}
98101
if w.Manifest != nil {
99102
return w.Manifest.Paths, w.Manifest.FilterRegex, len(w.Manifest.Paths) > 0
100103
}
@@ -104,6 +107,9 @@ func (w *WatchOpts) GetManifestWatchConfig() (paths []string, filterRegex string
104107

105108
// GetAppWatchConfig returns app watch config
106109
func (w *WatchOpts) GetAppWatchConfig() (paths []string, filterRegex string, enabled bool) {
110+
if w == nil {
111+
return nil, "", false
112+
}
107113
if w.App != nil {
108114
return w.App.Paths, w.App.FilterRegex, len(w.App.Paths) > 0
109115
}

internal/hooks/sdk_config_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,19 @@ func Test_WatchOpts_IsAvailable(t *testing.T) {
124124

125125
func Test_WatchOpts_GetManifestWatchConfig(t *testing.T) {
126126
tests := map[string]struct {
127-
watchOpts WatchOpts
127+
watchOpts *WatchOpts
128128
expectedPaths []string
129129
expectedRegex string
130130
expectedEnabled bool
131131
}{
132+
"Nil WatchOpts pointer": {
133+
watchOpts: nil,
134+
expectedPaths: nil,
135+
expectedRegex: "",
136+
expectedEnabled: false,
137+
},
132138
"Nested manifest config": {
133-
watchOpts: WatchOpts{
139+
watchOpts: &WatchOpts{
134140
Manifest: &ManifestWatchOpts{
135141
Paths: []string{"manifest.json", "workflows/"},
136142
FilterRegex: "\\.json$",
@@ -141,7 +147,7 @@ func Test_WatchOpts_GetManifestWatchConfig(t *testing.T) {
141147
expectedEnabled: true,
142148
},
143149
"Legacy flat config": {
144-
watchOpts: WatchOpts{
150+
watchOpts: &WatchOpts{
145151
Paths: []string{"manifest.json", "src/"},
146152
FilterRegex: "\\.(json|ts)$",
147153
},
@@ -150,7 +156,7 @@ func Test_WatchOpts_GetManifestWatchConfig(t *testing.T) {
150156
expectedEnabled: true,
151157
},
152158
"Nested config takes precedence over legacy": {
153-
watchOpts: WatchOpts{
159+
watchOpts: &WatchOpts{
154160
Paths: []string{"old-path/"},
155161
FilterRegex: "old-regex",
156162
Manifest: &ManifestWatchOpts{
@@ -163,7 +169,7 @@ func Test_WatchOpts_GetManifestWatchConfig(t *testing.T) {
163169
expectedEnabled: true,
164170
},
165171
"Empty nested manifest config": {
166-
watchOpts: WatchOpts{
172+
watchOpts: &WatchOpts{
167173
Manifest: &ManifestWatchOpts{
168174
Paths: []string{},
169175
},
@@ -173,7 +179,7 @@ func Test_WatchOpts_GetManifestWatchConfig(t *testing.T) {
173179
expectedEnabled: false,
174180
},
175181
"Empty legacy config": {
176-
watchOpts: WatchOpts{
182+
watchOpts: &WatchOpts{
177183
Paths: []string{},
178184
},
179185
expectedPaths: []string{},
@@ -193,13 +199,19 @@ func Test_WatchOpts_GetManifestWatchConfig(t *testing.T) {
193199

194200
func Test_WatchOpts_GetAppWatchConfig(t *testing.T) {
195201
tests := map[string]struct {
196-
watchOpts WatchOpts
202+
watchOpts *WatchOpts
197203
expectedPaths []string
198204
expectedRegex string
199205
expectedEnabled bool
200206
}{
207+
"Nil WatchOpts pointer": {
208+
watchOpts: nil,
209+
expectedPaths: nil,
210+
expectedRegex: "",
211+
expectedEnabled: false,
212+
},
201213
"Nested app config": {
202-
watchOpts: WatchOpts{
214+
watchOpts: &WatchOpts{
203215
App: &AppWatchOpts{
204216
Paths: []string{"src/", "functions/"},
205217
FilterRegex: "\\.(ts|js)$",
@@ -210,7 +222,7 @@ func Test_WatchOpts_GetAppWatchConfig(t *testing.T) {
210222
expectedEnabled: true,
211223
},
212224
"Legacy config does not enable app watching": {
213-
watchOpts: WatchOpts{
225+
watchOpts: &WatchOpts{
214226
Paths: []string{"manifest.json", "src/"},
215227
FilterRegex: "\\.(json|ts)$",
216228
},
@@ -219,7 +231,7 @@ func Test_WatchOpts_GetAppWatchConfig(t *testing.T) {
219231
expectedEnabled: false,
220232
},
221233
"Empty nested app config": {
222-
watchOpts: WatchOpts{
234+
watchOpts: &WatchOpts{
223235
App: &AppWatchOpts{
224236
Paths: []string{},
225237
},
@@ -229,7 +241,7 @@ func Test_WatchOpts_GetAppWatchConfig(t *testing.T) {
229241
expectedEnabled: false,
230242
},
231243
"Nil app config": {
232-
watchOpts: WatchOpts{},
244+
watchOpts: &WatchOpts{},
233245
expectedPaths: nil,
234246
expectedRegex: "",
235247
expectedEnabled: false,

0 commit comments

Comments
 (0)