Skip to content

Commit 4293b61

Browse files
authored
add tests
1 parent 725553e commit 4293b61

File tree

2 files changed

+121
-23
lines changed

2 files changed

+121
-23
lines changed

cmd/main.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,44 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
107107
return loadConfigWithLogger(caddy.Log(), configFile, adapterName)
108108
}
109109

110+
func isCaddyfile(configFile, adapterName string) (bool, error) {
111+
if adapterName == "caddyfile" {
112+
return true, nil
113+
}
114+
115+
// as a special case, if a config file starts with "caddyfile" or
116+
// has a ".caddyfile" extension, and no adapter is specified, and
117+
// no adapter module name matches the extension, assume
118+
// caddyfile adapter for convenience
119+
baseConfig := strings.ToLower(filepath.Base(configFile))
120+
baseConfigExt := filepath.Ext(baseConfig)
121+
startsOrEndsInCaddyfile := strings.HasPrefix(baseConfig, "caddyfile") || strings.HasSuffix(baseConfig, ".caddyfile")
122+
notCaddyfileNorJSON := (baseConfigExt != "" && baseConfigExt != ".caddyfile" && baseConfigExt != ".json")
123+
124+
if baseConfigExt == ".json" {
125+
return false, nil
126+
}
127+
128+
// If the adapter is not specified,
129+
// the config file does not starts with "caddyfile",
130+
// the config file has an extension,
131+
// and isn't a JSON file (e.g. Caddyfile.yaml),
132+
// then we don't know what the config format is.
133+
if adapterName == "" && startsOrEndsInCaddyfile && notCaddyfileNorJSON {
134+
return false, fmt.Errorf("ambiguous config file format; please specify adapter (use --adapter)")
135+
}
136+
137+
// If the config file starts or ends with "caddyfile",
138+
// the extension of the config file is not ".json", AND
139+
// the user did not specify an adapter, then we assume it's Caddyfile.
140+
if startsOrEndsInCaddyfile &&
141+
baseConfigExt != ".json" &&
142+
adapterName == "" {
143+
return true, nil
144+
}
145+
return false, nil
146+
}
147+
110148
func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, error) {
111149
// if no logger is provided, use a nop logger
112150
// just so we don't have to check for nil
@@ -157,30 +195,10 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
157195
}
158196
}
159197

160-
// as a special case, if a config file starts with "caddyfile" or
161-
// has a ".caddyfile" extension, and no adapter is specified, and
162-
// no adapter module name matches the extension, assume
163-
// caddyfile adapter for convenience
164-
baseConfig := strings.ToLower(filepath.Base(configFile))
165-
baseConfigExt := filepath.Ext(baseConfig)
166-
startsOrEndsInCaddyfile := strings.HasPrefix(baseConfig, "caddyfile") || strings.HasSuffix(baseConfig, ".caddyfile")
167-
168-
// If the adapter is not specified,
169-
// the config file does not starts with "caddyfile",
170-
// the config file has an extension,
171-
// and isn't a JSON file (e.g. Caddyfile.yaml),
172-
// then we don't know what the config format is.
173-
if adapterName == "" && startsOrEndsInCaddyfile && (baseConfigExt != "" && baseConfigExt != ".caddyfile" && baseConfigExt != ".json") {
174-
return nil, "", fmt.Errorf("ambiguous config file format; please specify adapter (use --adapter)")
175-
}
176-
177-
// If the config file starts or ends with "caddyfile",
178-
// the extension of the config file is not ".json", AND
179-
// the user did not specify an adapter, then we assume it's Caddyfile.
180-
if startsOrEndsInCaddyfile &&
181-
baseConfigExt != ".json" &&
182-
adapterName == "" {
198+
if yes, err := isCaddyfile(configFile, adapterName); yes {
183199
adapterName = "caddyfile"
200+
} else if err != nil {
201+
return nil, "", err
184202
}
185203

186204
// load config adapter

cmd/main_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,83 @@ here"
168168
}
169169
}
170170
}
171+
172+
func Test_isCaddyfile(t *testing.T) {
173+
type args struct {
174+
configFile string
175+
adapterName string
176+
}
177+
tests := []struct {
178+
name string
179+
args args
180+
want bool
181+
wantErr bool
182+
}{
183+
{
184+
name: "local Caddyfile without adapter",
185+
args: args{
186+
configFile: "./Caddyfile",
187+
adapterName: "",
188+
},
189+
want: true,
190+
wantErr: false,
191+
},
192+
{
193+
name: "local caddyfile with adapter",
194+
args: args{
195+
configFile: "./Caddyfile",
196+
adapterName: "caddyfile",
197+
},
198+
want: true,
199+
wantErr: false,
200+
},
201+
{
202+
name: "ends with .caddyfile with adapter",
203+
args: args{
204+
configFile: "./conf.caddyfile",
205+
adapterName: "caddyfile",
206+
},
207+
want: true,
208+
wantErr: false,
209+
},
210+
{
211+
name: "ends with .caddyfile without adapter",
212+
args: args{
213+
configFile: "./conf.caddyfile",
214+
adapterName: "",
215+
},
216+
want: true,
217+
wantErr: false,
218+
},
219+
{
220+
name: "config is Caddyfile.yaml without adapter",
221+
args: args{
222+
configFile: "./Caddyfile.yaml",
223+
adapterName: "",
224+
},
225+
want: false,
226+
wantErr: true,
227+
},
228+
{
229+
name: "json is not caddyfile but not error",
230+
args: args{
231+
configFile: "./Caddyfile.json",
232+
adapterName: "",
233+
},
234+
want: false,
235+
wantErr: false,
236+
},
237+
}
238+
for _, tt := range tests {
239+
t.Run(tt.name, func(t *testing.T) {
240+
got, err := isCaddyfile(tt.args.configFile, tt.args.adapterName)
241+
if (err != nil) != tt.wantErr {
242+
t.Errorf("isCaddyfile() error = %v, wantErr %v", err, tt.wantErr)
243+
return
244+
}
245+
if got != tt.want {
246+
t.Errorf("isCaddyfile() = %v, want %v", got, tt.want)
247+
}
248+
})
249+
}
250+
}

0 commit comments

Comments
 (0)