]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Make sure that HugoSites is always closed when done
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 20 Oct 2024 09:25:16 +0000 (11:25 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 20 Oct 2024 11:04:58 +0000 (13:04 +0200)
Including all the integration tests.

commands/commandeer.go
commands/server.go
deps/deps.go
hugolib/integrationtest_builder.go

index 06565d45d7acaa6f6be99a5110438a9df73d8056..ad2adf3a289b3a41b271f03b1c78ae044569f7bd 100644 (file)
@@ -42,6 +42,7 @@ import (
        "github.com/gohugoio/hugo/common/hugo"
        "github.com/gohugoio/hugo/common/loggers"
        "github.com/gohugoio/hugo/common/paths"
+       "github.com/gohugoio/hugo/common/types"
        "github.com/gohugoio/hugo/config"
        "github.com/gohugoio/hugo/config/allconfig"
        "github.com/gohugoio/hugo/deps"
@@ -66,6 +67,12 @@ func Execute(args []string) error {
        }
        args = mapLegacyArgs(args)
        cd, err := x.Execute(context.Background(), args)
+       if cd != nil {
+               if closer, ok := cd.Root.Command.(types.Closer); ok {
+                       closer.Close()
+               }
+       }
+
        if err != nil {
                if err == errHelp {
                        cd.CobraCommand.Help()
@@ -149,6 +156,18 @@ func (r *rootCommand) isVerbose() bool {
        return r.logger.Level() <= logg.LevelInfo
 }
 
+func (r *rootCommand) Close() error {
+       if r.hugoSites != nil {
+               r.hugoSites.DeleteFunc(func(key configKey, value *hugolib.HugoSites) bool {
+                       if value != nil {
+                               value.Close()
+                       }
+                       return false
+               })
+       }
+       return nil
+}
+
 func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) {
        h, err := r.Hugo(cfg)
        if err != nil {
index b16bf314883df9afd4eaf53b380e4693feb8e06e..84d4165f0cc4f5b31963e05c21a30f44e909f641 100644 (file)
@@ -1012,10 +1012,6 @@ func (c *serverCommand) serve() error {
                c.r.Println("Error:", err)
        }
 
-       if h := c.hugoTry(); h != nil {
-               h.Close()
-       }
-
        ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
        defer cancel()
        wg2, ctx := errgroup.WithContext(ctx)
index e137aed7b10366f4a35e38bc95c4c5bbe808d9f2..4389036cbdfc31bc2cee4dfa4b3ab49aa1b067ba 100644 (file)
@@ -98,6 +98,8 @@ type Deps struct {
        // TODO(bep) rethink this re. a plugin setup, but this will have to do for now.
        WasmDispatchers *warpc.Dispatchers
 
+       isClosed bool
+
        *globalErrHandler
 }
 
@@ -345,6 +347,11 @@ func (d *Deps) TextTmpl() tpl.TemplateParseFinder {
 }
 
 func (d *Deps) Close() error {
+       if d.isClosed {
+               return nil
+       }
+       d.isClosed = true
+
        if d.MemCache != nil {
                d.MemCache.Stop()
        }
index 551b807dba0d4018b0edda05644d5b5ae2494180..b45defb42d29fcfd3b8c8c2fa6d371f3737efc90 100644 (file)
@@ -421,6 +421,12 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
                s.Assert(err, qt.IsNil)
        }
 
+       s.Cleanup(func() {
+               if h := s.H; h != nil {
+                       s.Assert(h.Close(), qt.IsNil)
+               }
+       })
+
        return s
 }