2015-12-10 22:19:38 +00: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 11:59:59 +00:00
|
|
|
package commands
|
|
|
|
|
2018-04-11 06:55:50 +00:00
|
|
|
import (
|
2018-04-11 07:38:58 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-07-18 07:43:31 +00:00
|
|
|
"runtime"
|
2018-10-03 12:58:09 +00:00
|
|
|
"strings"
|
2018-04-11 06:55:50 +00:00
|
|
|
"testing"
|
2018-04-11 07:38:58 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2014-08-22 11:59:59 +00:00
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-04-11 06:55:50 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
2014-08-22 11:59:59 +00:00
|
|
|
|
2018-04-11 07:38:58 +00:00
|
|
|
func TestServer(t *testing.T) {
|
2018-07-18 07:43:31 +00:00
|
|
|
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")
|
|
|
|
}
|
2019-08-10 19:05:17 +00:00
|
|
|
c := qt.New(t)
|
2019-02-01 07:40:53 +00:00
|
|
|
dir, err := createSimpleTestSite(t, testSiteConfig{})
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-04-11 07:38:58 +00:00
|
|
|
|
|
|
|
// Let us hope that this port is available on all systems ...
|
|
|
|
port := 1331
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
|
|
|
|
2018-04-12 07:31:53 +00:00
|
|
|
stop := make(chan bool)
|
2018-04-11 07:38:58 +00:00
|
|
|
|
2018-04-13 06:42:29 +00:00
|
|
|
b := newCommandsBuilder()
|
|
|
|
scmd := b.newServerCmdSignaled(stop)
|
2018-04-11 07:38:58 +00:00
|
|
|
|
|
|
|
cmd := scmd.getCommand()
|
|
|
|
cmd.SetArgs([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err = cmd.ExecuteC()
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-04-11 07:38:58 +00:00
|
|
|
}()
|
|
|
|
|
2018-04-12 07:31:53 +00:00
|
|
|
// 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!
|
2018-07-18 07:43:31 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
2018-04-11 07:38:58 +00:00
|
|
|
|
|
|
|
resp, err := http.Get("http://localhost:1331/")
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-04-11 07:38:58 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
homeContent := helpers.ReaderToString(resp.Body)
|
|
|
|
|
2019-08-10 19:05:17 +00:00
|
|
|
c.Assert(homeContent, qt.Contains, "List: Hugo Commands")
|
|
|
|
c.Assert(homeContent, qt.Contains, "Environment: development")
|
2018-04-11 07:38:58 +00:00
|
|
|
|
|
|
|
// Stop the server.
|
|
|
|
stop <- true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-11 06:55:50 +00:00
|
|
|
func TestFixURL(t *testing.T) {
|
2014-08-22 11:59:59 +00:00
|
|
|
type data struct {
|
|
|
|
TestName string
|
2015-03-11 17:34:57 +00:00
|
|
|
CLIBaseURL string
|
|
|
|
CfgBaseURL string
|
2014-08-22 11:59:59 +00:00
|
|
|
AppendPort bool
|
|
|
|
Port int
|
|
|
|
Result string
|
|
|
|
}
|
|
|
|
tests := []data{
|
2015-01-16 10:13:03 +00: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 23:06:56 +00: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 11:59:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 16:25:27 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.TestName, func(t *testing.T) {
|
|
|
|
b := newCommandsBuilder()
|
|
|
|
s := b.newServerCmd()
|
|
|
|
v := viper.New()
|
|
|
|
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 11:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 07:43:31 +00:00
|
|
|
|
2018-10-03 12:58:09 +00:00
|
|
|
func TestRemoveErrorPrefixFromLog(t *testing.T) {
|
2019-08-10 19:05:17 +00:00
|
|
|
c := qt.New(t)
|
2018-10-03 12:58:09 +00: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 19:05:17 +00:00
|
|
|
c.Assert(strings.Contains(withoutError, "ERROR"), qt.Equals, false)
|
2018-10-03 12:58:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-18 07:43:31 +00:00
|
|
|
func isWindowsCI() bool {
|
|
|
|
return runtime.GOOS == "windows" && os.Getenv("CI") != ""
|
|
|
|
}
|