This commit is contained in:
a 2024-06-18 21:17:51 -05:00
parent b19feec6dc
commit 841fe2544d
No known key found for this signature in database
GPG key ID: 374BC539FE795AF0
2 changed files with 20 additions and 2 deletions

View file

@ -200,9 +200,8 @@ func (tc *Tester) startServer() error {
}
// start inprocess caddy server
os.Args = []string{"caddy", "run", "--config", f.Name(), "--adapter", "caddyfile"}
go func() {
caddycmd.Main()
caddycmd.MainForTesting("run", "--config", tc.configFileName, "--adapter", "caddyfile")
}()
// wait for caddy admin api to start. it should happen quickly.
for retries := 10; retries > 0 && isCaddyAdminRunning() != nil; retries-- {

View file

@ -34,6 +34,7 @@ import (
"time"
"github.com/caddyserver/certmagic"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
@ -81,6 +82,24 @@ func Main() {
}
}
// MainForTesting implements the main function of the caddy command, used internally for testing
func MainForTesting(args ...string) error {
// create a root command for testing which will not pollute the global namespace, and does not
// call os.Exit().
tmpRootCmp := cobra.Command{
Use: rootCmd.Use,
Long: rootCmd.Long,
Example: rootCmd.Example,
SilenceUsage: rootCmd.SilenceUsage,
Version: rootCmd.Version,
}
tmpRootCmp.SetArgs(args)
if err := rootCmd.Execute(); err != nil {
return err
}
return nil
}
// handlePingbackConn reads from conn and ensures it matches
// the bytes in expect, or returns an error if it doesn't.
func handlePingbackConn(conn net.Conn, expect []byte) error {