aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authora <[email protected]>2024-06-18 21:17:51 -0500
committera <[email protected]>2024-06-18 21:17:51 -0500
commit841fe2544d7e0fe7cf938c818cd6c88085ce5879 (patch)
tree4115f3f29bdd02edbe3531d79c08dced2368d5d9
parentb19feec6dcb465ceeb15241e5dc251f6231f6d29 (diff)
downloadcaddy-841fe2544d7e0fe7cf938c818cd6c88085ce5879.tar.gz
caddy-841fe2544d7e0fe7cf938c818cd6c88085ce5879.zip
noot
-rw-r--r--caddytest/caddytest.go3
-rw-r--r--cmd/main.go19
2 files changed, 20 insertions, 2 deletions
diff --git a/caddytest/caddytest.go b/caddytest/caddytest.go
index a1ec97048..3de5b96b0 100644
--- a/caddytest/caddytest.go
+++ b/caddytest/caddytest.go
@@ -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-- {
diff --git a/cmd/main.go b/cmd/main.go
index 3c3ae6270..3e0359334 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -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 {