hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit aebde49b884c3b5ef73d8e1a01fca8a1354ac5b9
parent 4ada09415dfa4c25c4fe9473fecf9e51ec740900
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Mon, 21 Feb 2022 19:12:04 +0100

commands: Fix server panic regression

And now with a proper server test.

Fixes #9518
Fixes #9530
Fixes #9539

Diffstat:
Mcommands/commandeer.go | 2+-
Mcommands/commands_test.go | 6+++---
Mcommands/server_test.go | 49++++++++++++++++++++++++++++++++++++++-----------
Mhugolib/hugo_sites.go | 3+++
4 files changed, 45 insertions(+), 15 deletions(-)
diff --git a/commands/commandeer.go b/commands/commandeer.go
@@ -422,7 +422,7 @@ func (c *commandeer) loadConfig() error {
 		}
 		c.hugoSites = h
 		// TODO(bep) improve.
-		if c.buildLock == nil {
+		if c.buildLock == nil && h != nil {
 			c.buildLock = h.LockBuild
 		}
 		close(c.created)
diff --git a/commands/commands_test.go b/commands/commands_test.go
@@ -329,7 +329,7 @@ type testSiteConfig struct {
 	contentDir string
 }
 
-func createSimpleTestSite(t *testing.T, cfg testSiteConfig) (string, func(), error) {
+func createSimpleTestSite(t testing.TB, cfg testSiteConfig) (string, func(), error) {
 	d, clean, e := htesting.CreateTempDir(hugofs.Os, "hugo-cli")
 	if e != nil {
 		return "", nil, e
@@ -392,12 +392,12 @@ Environment: {{ hugo.Environment }}
 	return d, clean, nil
 }
 
-func writeFile(t *testing.T, filename, content string) {
+func writeFile(t testing.TB, filename, content string) {
 	must(t, os.MkdirAll(filepath.Dir(filename), os.FileMode(0755)))
 	must(t, ioutil.WriteFile(filename, []byte(content), os.FileMode(0755)))
 }
 
-func must(t *testing.T, err error) {
+func must(t testing.TB, err error) {
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/commands/server_test.go b/commands/server_test.go
@@ -29,12 +29,33 @@ import (
 )
 
 func TestServer(t *testing.T) {
-	if isWindowsCI() {
-		// TODO(bep) not sure why server tests have started to fail on the Windows CI server.
-		t.Skip("Skip server test on appveyor")
-	}
 	c := qt.New(t)
-	dir, clean, err := createSimpleTestSite(t, testSiteConfig{})
+
+	homeContent, err := runServerTestAndGetHome(c, "")
+
+	c.Assert(err, qt.IsNil)
+	c.Assert(homeContent, qt.Contains, "List: Hugo Commands")
+	c.Assert(homeContent, qt.Contains, "Environment: development")
+}
+
+// Issue 9518
+func TestServerPanicOnConfigError(t *testing.T) {
+	c := qt.New(t)
+
+	config := `
+[markup]
+[markup.highlight]
+linenos='table'
+`
+
+	_, err := runServerTestAndGetHome(c, config)
+
+	c.Assert(err, qt.IsNotNil)
+	c.Assert(err.Error(), qt.Contains, "cannot parse 'Highlight.LineNos' as bool:")
+}
+
+func runServerTestAndGetHome(c *qt.C, config string) (string, error) {
+	dir, clean, err := createSimpleTestSite(c, testSiteConfig{configTOML: config})
 	defer clean()
 	c.Assert(err, qt.IsNil)
 
@@ -45,6 +66,7 @@ func TestServer(t *testing.T) {
 		os.RemoveAll(dir)
 	}()
 
+	errors := make(chan error)
 	stop := make(chan bool)
 
 	b := newCommandsBuilder()
@@ -54,25 +76,30 @@ func TestServer(t *testing.T) {
 	cmd.SetArgs([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)})
 
 	go func() {
-		_, err = cmd.ExecuteC()
-		c.Assert(err, qt.IsNil)
+		_, err := cmd.ExecuteC()
+		if err != nil {
+			errors <- err
+		}
 	}()
 
+	select {
 	// There is no way to know exactly when the server is ready for connections.
 	// We could improve by something like https://golang.org/pkg/net/http/httptest/#Server
 	// But for now, let us sleep and pray!
-	time.Sleep(2 * time.Second)
+	case <-time.After(2 * time.Second):
+	case err := <-errors:
+		return "", err
+	}
 
 	resp, err := http.Get("http://localhost:1331/")
 	c.Assert(err, qt.IsNil)
 	defer resp.Body.Close()
 	homeContent := helpers.ReaderToString(resp.Body)
 
-	c.Assert(homeContent, qt.Contains, "List: Hugo Commands")
-	c.Assert(homeContent, qt.Contains, "Environment: development")
-
 	// Stop the server.
 	stop <- true
+
+	return homeContent, nil
 }
 
 func TestFixURL(t *testing.T) {
diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go
@@ -390,6 +390,9 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
 	}
 
 	h.Deps = sites[0].Deps
+	if h.Deps == nil {
+		return nil, initErr
+	}
 
 	// Only needed in server mode.
 	// TODO(bep) clean up the running vs watching terms