2015-12-10 17:19:38 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-08-22 07:59:59 -04:00
|
|
|
package commands
|
|
|
|
|
2018-04-11 02:55:50 -04:00
|
|
|
import (
|
2018-04-11 03:38:58 -04:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-03-11 02:07:37 -05:00
|
|
|
"path/filepath"
|
2018-07-18 03:43:31 -04:00
|
|
|
"runtime"
|
2018-10-03 08:58:09 -04:00
|
|
|
"strings"
|
2018-04-11 02:55:50 -04:00
|
|
|
"testing"
|
2018-04-11 03:38:58 -04:00
|
|
|
"time"
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2018-04-11 03:38:58 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2022-03-14 11:34:23 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2014-08-22 07:59:59 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-04-11 02:55:50 -04:00
|
|
|
)
|
2014-08-22 07:59:59 -04:00
|
|
|
|
2022-02-21 13:12:04 -05:00
|
|
|
// Issue 9518
|
|
|
|
func TestServerPanicOnConfigError(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
config := `
|
|
|
|
[markup]
|
|
|
|
[markup.highlight]
|
|
|
|
linenos='table'
|
|
|
|
`
|
|
|
|
|
2022-05-18 03:47:55 -04:00
|
|
|
r := runServerTest(c, 0, config)
|
2022-02-21 13:12:04 -05:00
|
|
|
|
2022-03-11 02:07:37 -05:00
|
|
|
c.Assert(r.err, qt.IsNotNil)
|
|
|
|
c.Assert(r.err.Error(), qt.Contains, "cannot parse 'Highlight.LineNos' as bool:")
|
2022-02-21 13:12:04 -05:00
|
|
|
}
|
|
|
|
|
2022-03-11 02:07:37 -05:00
|
|
|
func TestServerFlags(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
assertPublic := func(c *qt.C, r serverTestResult, renderStaticToDisk bool) {
|
|
|
|
c.Assert(r.err, qt.IsNil)
|
2022-05-18 03:47:55 -04:00
|
|
|
c.Assert(r.homesContent[0], qt.Contains, "Environment: development")
|
2022-03-11 02:07:37 -05:00
|
|
|
c.Assert(r.publicDirnames["myfile.txt"], qt.Equals, renderStaticToDisk)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
flag string
|
|
|
|
assert func(c *qt.C, r serverTestResult)
|
|
|
|
}{
|
|
|
|
{"", func(c *qt.C, r serverTestResult) {
|
|
|
|
assertPublic(c, r, false)
|
|
|
|
}},
|
|
|
|
{"--renderToDisk", func(c *qt.C, r serverTestResult) {
|
|
|
|
assertPublic(c, r, true)
|
|
|
|
}},
|
2022-03-21 04:35:15 -04:00
|
|
|
{"--renderStaticToDisk", func(c *qt.C, r serverTestResult) {
|
|
|
|
assertPublic(c, r, true)
|
|
|
|
}},
|
2022-03-11 02:07:37 -05:00
|
|
|
} {
|
|
|
|
c.Run(test.flag, func(c *qt.C) {
|
|
|
|
config := `
|
|
|
|
baseURL="https://example.org"
|
|
|
|
`
|
|
|
|
|
|
|
|
var args []string
|
|
|
|
if test.flag != "" {
|
|
|
|
args = strings.Split(test.flag, "=")
|
|
|
|
}
|
|
|
|
|
2022-05-18 03:47:55 -04:00
|
|
|
r := runServerTest(c, 1, config, args...)
|
2022-03-11 02:07:37 -05:00
|
|
|
|
|
|
|
test.assert(c, r)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-18 04:28:54 -04:00
|
|
|
func TestServerBugs(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
2022-05-18 03:47:55 -04:00
|
|
|
name string
|
|
|
|
config string
|
|
|
|
flag string
|
|
|
|
numservers int
|
|
|
|
assert func(c *qt.C, r serverTestResult)
|
2022-04-18 04:28:54 -04:00
|
|
|
}{
|
|
|
|
// Issue 9788
|
2022-05-18 03:47:55 -04:00
|
|
|
{"PostProcess, memory", "", "", 1, func(c *qt.C, r serverTestResult) {
|
2022-04-18 04:28:54 -04:00
|
|
|
c.Assert(r.err, qt.IsNil)
|
2022-05-18 03:47:55 -04:00
|
|
|
c.Assert(r.homesContent[0], qt.Contains, "PostProcess: /foo.min.css")
|
2022-04-18 04:28:54 -04:00
|
|
|
}},
|
2022-05-18 03:47:55 -04:00
|
|
|
{"PostProcess, disk", "", "--renderToDisk", 1, func(c *qt.C, r serverTestResult) {
|
2022-04-18 04:28:54 -04:00
|
|
|
c.Assert(r.err, qt.IsNil)
|
2022-05-18 03:47:55 -04:00
|
|
|
c.Assert(r.homesContent[0], qt.Contains, "PostProcess: /foo.min.css")
|
|
|
|
}},
|
|
|
|
// Isue 9901
|
|
|
|
{"Multihost", `
|
|
|
|
defaultContentLanguage = 'en'
|
|
|
|
[languages]
|
|
|
|
[languages.en]
|
|
|
|
baseURL = 'https://example.com'
|
|
|
|
title = 'My blog'
|
|
|
|
weight = 1
|
|
|
|
[languages.fr]
|
|
|
|
baseURL = 'https://example.fr'
|
|
|
|
title = 'Mon blogue'
|
|
|
|
weight = 2
|
|
|
|
`, "", 2, func(c *qt.C, r serverTestResult) {
|
|
|
|
c.Assert(r.err, qt.IsNil)
|
|
|
|
for i, s := range []string{"My blog", "Mon blogue"} {
|
|
|
|
c.Assert(r.homesContent[i], qt.Contains, s)
|
|
|
|
}
|
2022-04-18 04:28:54 -04:00
|
|
|
}},
|
|
|
|
} {
|
|
|
|
c.Run(test.name, func(c *qt.C) {
|
2022-05-18 03:47:55 -04:00
|
|
|
if test.config == "" {
|
|
|
|
test.config = `
|
2022-04-18 04:28:54 -04:00
|
|
|
baseURL="https://example.org"
|
|
|
|
`
|
2022-05-18 03:47:55 -04:00
|
|
|
}
|
2022-04-18 04:28:54 -04:00
|
|
|
|
|
|
|
var args []string
|
|
|
|
if test.flag != "" {
|
|
|
|
args = strings.Split(test.flag, "=")
|
|
|
|
}
|
2022-05-18 03:47:55 -04:00
|
|
|
r := runServerTest(c, test.numservers, test.config, args...)
|
2022-04-18 04:28:54 -04:00
|
|
|
test.assert(c, r)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-11 02:07:37 -05:00
|
|
|
type serverTestResult struct {
|
|
|
|
err error
|
2022-05-18 03:47:55 -04:00
|
|
|
homesContent []string
|
2022-03-11 02:07:37 -05:00
|
|
|
publicDirnames map[string]bool
|
|
|
|
}
|
|
|
|
|
2022-05-18 03:47:55 -04:00
|
|
|
func runServerTest(c *qt.C, getNumHomes int, config string, args ...string) (result serverTestResult) {
|
2022-03-21 04:35:15 -04:00
|
|
|
dir := createSimpleTestSite(c, testSiteConfig{configTOML: config})
|
2018-04-11 03:38:58 -04:00
|
|
|
|
2022-03-14 11:34:23 -04:00
|
|
|
sp, err := helpers.FindAvailablePort()
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
port := sp.Port
|
2018-04-11 03:38:58 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
|
|
|
|
2018-04-12 03:31:53 -04:00
|
|
|
stop := make(chan bool)
|
2018-04-11 03:38:58 -04:00
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
b := newCommandsBuilder()
|
|
|
|
scmd := b.newServerCmdSignaled(stop)
|
2018-04-11 03:38:58 -04:00
|
|
|
|
|
|
|
cmd := scmd.getCommand()
|
2022-03-11 02:07:37 -05:00
|
|
|
args = append([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)}, args...)
|
|
|
|
cmd.SetArgs(args)
|
2018-04-11 03:38:58 -04:00
|
|
|
|
2022-03-14 11:34:23 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
wg.Go(func() error {
|
2022-02-21 13:12:04 -05:00
|
|
|
_, err := cmd.ExecuteC()
|
2022-03-14 11:34:23 -04:00
|
|
|
return err
|
|
|
|
})
|
2018-04-11 03:38:58 -04:00
|
|
|
|
2022-05-18 03:47:55 -04:00
|
|
|
if getNumHomes > 0 {
|
2022-03-18 03:54:44 -04:00
|
|
|
// Esp. on slow CI machines, we need to wait a little before the web
|
|
|
|
// server is ready.
|
|
|
|
time.Sleep(567 * time.Millisecond)
|
2022-05-18 03:47:55 -04:00
|
|
|
result.homesContent = make([]string, getNumHomes)
|
|
|
|
for i := 0; i < getNumHomes; i++ {
|
|
|
|
func() {
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", port+i))
|
|
|
|
c.Check(err, qt.IsNil)
|
|
|
|
c.Check(resp.StatusCode, qt.Equals, http.StatusOK)
|
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
result.homesContent[i] = helpers.ReaderToString(resp.Body)
|
|
|
|
}
|
|
|
|
}()
|
2022-03-18 03:54:44 -04:00
|
|
|
}
|
2022-02-21 13:12:04 -05:00
|
|
|
}
|
2018-04-11 03:38:58 -04:00
|
|
|
|
2022-03-21 04:35:15 -04:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2022-03-18 03:54:44 -04:00
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
case stop <- true:
|
|
|
|
}
|
2022-03-11 02:07:37 -05:00
|
|
|
|
|
|
|
pubFiles, err := os.ReadDir(filepath.Join(dir, "public"))
|
2022-03-18 03:54:44 -04:00
|
|
|
c.Check(err, qt.IsNil)
|
2022-03-11 02:07:37 -05:00
|
|
|
result.publicDirnames = make(map[string]bool)
|
|
|
|
for _, f := range pubFiles {
|
|
|
|
result.publicDirnames[f.Name()] = true
|
|
|
|
}
|
|
|
|
|
2022-03-14 11:34:23 -04:00
|
|
|
result.err = wg.Wait()
|
2022-03-18 03:54:44 -04:00
|
|
|
|
2022-03-11 02:07:37 -05:00
|
|
|
return
|
|
|
|
|
2018-04-11 03:38:58 -04:00
|
|
|
}
|
|
|
|
|
2018-04-11 02:55:50 -04:00
|
|
|
func TestFixURL(t *testing.T) {
|
2014-08-22 07:59:59 -04:00
|
|
|
type data struct {
|
|
|
|
TestName string
|
2015-03-11 13:34:57 -04:00
|
|
|
CLIBaseURL string
|
|
|
|
CfgBaseURL string
|
2014-08-22 07:59:59 -04:00
|
|
|
AppendPort bool
|
|
|
|
Port int
|
|
|
|
Result string
|
|
|
|
}
|
|
|
|
tests := []data{
|
2015-01-16 05:13:03 -05:00
|
|
|
{"Basic http localhost", "", "http://foo.com", true, 1313, "http://localhost:1313/"},
|
|
|
|
{"Basic https production, http localhost", "", "https://foo.com", true, 1313, "http://localhost:1313/"},
|
|
|
|
{"Basic subdir", "", "http://foo.com/bar", true, 1313, "http://localhost:1313/bar/"},
|
|
|
|
{"Basic production", "http://foo.com", "http://foo.com", false, 80, "http://foo.com/"},
|
|
|
|
{"Production subdir", "http://foo.com/bar", "http://foo.com/bar", false, 80, "http://foo.com/bar/"},
|
2016-05-12 19:06:56 -04:00
|
|
|
{"No http", "", "foo.com", true, 1313, "//localhost:1313/"},
|
|
|
|
{"Override configured port", "", "foo.com:2020", true, 1313, "//localhost:1313/"},
|
|
|
|
{"No http production", "foo.com", "foo.com", false, 80, "//foo.com/"},
|
|
|
|
{"No http production with port", "foo.com", "foo.com", true, 2020, "//foo.com:2020/"},
|
|
|
|
{"No config", "", "", true, 1313, "//localhost:1313/"},
|
2014-08-22 07:59:59 -04:00
|
|
|
}
|
|
|
|
|
2019-01-07 11:25:27 -05:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.TestName, func(t *testing.T) {
|
|
|
|
b := newCommandsBuilder()
|
|
|
|
s := b.newServerCmd()
|
2022-03-21 04:35:15 -04:00
|
|
|
v := config.NewWithTestDefaults()
|
2019-01-07 11:25:27 -05:00
|
|
|
baseURL := test.CLIBaseURL
|
|
|
|
v.Set("baseURL", test.CfgBaseURL)
|
|
|
|
s.serverAppend = test.AppendPort
|
|
|
|
s.serverPort = test.Port
|
|
|
|
result, err := s.fixURL(v, baseURL, s.serverPort)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %s", err)
|
|
|
|
}
|
|
|
|
if result != test.Result {
|
|
|
|
t.Errorf("Expected %q, got %q", test.Result, result)
|
|
|
|
}
|
|
|
|
})
|
2014-08-22 07:59:59 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 03:43:31 -04:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
func TestRemoveErrorPrefixFromLog(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-10-03 08:58:09 -04:00
|
|
|
content := `ERROR 2018/10/07 13:11:12 Error while rendering "home": template: _default/baseof.html:4:3: executing "main" at <partial "logo" .>: error calling partial: template: partials/logo.html:5:84: executing "partials/logo.html" at <$resized.AHeight>: can't evaluate field AHeight in type *resource.Image
|
|
|
|
ERROR 2018/10/07 13:11:12 Rebuild failed: logged 1 error(s)
|
|
|
|
`
|
|
|
|
|
|
|
|
withoutError := removeErrorPrefixFromLog(content)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(strings.Contains(withoutError, "ERROR"), qt.Equals, false)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 03:43:31 -04:00
|
|
|
func isWindowsCI() bool {
|
|
|
|
return runtime.GOOS == "windows" && os.Getenv("CI") != ""
|
|
|
|
}
|