github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/commands/server_test.go (about)

     1  // Copyright 2015 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package commands
    15  
    16  import (
    17  	"fmt"
    18  	"net/http"
    19  	"os"
    20  	"runtime"
    21  	"strings"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/gohugoio/hugo/config"
    26  	"github.com/gohugoio/hugo/helpers"
    27  
    28  	qt "github.com/frankban/quicktest"
    29  )
    30  
    31  func TestServer(t *testing.T) {
    32  	if isWindowsCI() {
    33  		// TODO(bep) not sure why server tests have started to fail on the Windows CI server.
    34  		t.Skip("Skip server test on appveyor")
    35  	}
    36  	c := qt.New(t)
    37  	dir, clean, err := createSimpleTestSite(t, testSiteConfig{})
    38  	defer clean()
    39  	c.Assert(err, qt.IsNil)
    40  
    41  	// Let us hope that this port is available on all systems ...
    42  	port := 1331
    43  
    44  	defer func() {
    45  		os.RemoveAll(dir)
    46  	}()
    47  
    48  	stop := make(chan bool)
    49  
    50  	b := newCommandsBuilder()
    51  	scmd := b.newServerCmdSignaled(stop)
    52  
    53  	cmd := scmd.getCommand()
    54  	cmd.SetArgs([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)})
    55  
    56  	go func() {
    57  		_, err = cmd.ExecuteC()
    58  		c.Assert(err, qt.IsNil)
    59  	}()
    60  
    61  	// There is no way to know exactly when the server is ready for connections.
    62  	// We could improve by something like https://golang.org/pkg/net/http/httptest/#Server
    63  	// But for now, let us sleep and pray!
    64  	time.Sleep(2 * time.Second)
    65  
    66  	resp, err := http.Get("http://localhost:1331/")
    67  	c.Assert(err, qt.IsNil)
    68  	defer resp.Body.Close()
    69  	homeContent := helpers.ReaderToString(resp.Body)
    70  
    71  	c.Assert(homeContent, qt.Contains, "List: Hugo Commands")
    72  	c.Assert(homeContent, qt.Contains, "Environment: development")
    73  
    74  	// Stop the server.
    75  	stop <- true
    76  }
    77  
    78  func TestFixURL(t *testing.T) {
    79  	type data struct {
    80  		TestName   string
    81  		CLIBaseURL string
    82  		CfgBaseURL string
    83  		AppendPort bool
    84  		Port       int
    85  		Result     string
    86  	}
    87  	tests := []data{
    88  		{"Basic http localhost", "", "http://foo.com", true, 1313, "http://localhost:1313/"},
    89  		{"Basic https production, http localhost", "", "https://foo.com", true, 1313, "http://localhost:1313/"},
    90  		{"Basic subdir", "", "http://foo.com/bar", true, 1313, "http://localhost:1313/bar/"},
    91  		{"Basic production", "http://foo.com", "http://foo.com", false, 80, "http://foo.com/"},
    92  		{"Production subdir", "http://foo.com/bar", "http://foo.com/bar", false, 80, "http://foo.com/bar/"},
    93  		{"No http", "", "foo.com", true, 1313, "//localhost:1313/"},
    94  		{"Override configured port", "", "foo.com:2020", true, 1313, "//localhost:1313/"},
    95  		{"No http production", "foo.com", "foo.com", false, 80, "//foo.com/"},
    96  		{"No http production with port", "foo.com", "foo.com", true, 2020, "//foo.com:2020/"},
    97  		{"No config", "", "", true, 1313, "//localhost:1313/"},
    98  	}
    99  
   100  	for _, test := range tests {
   101  		t.Run(test.TestName, func(t *testing.T) {
   102  			b := newCommandsBuilder()
   103  			s := b.newServerCmd()
   104  			v := config.New()
   105  			baseURL := test.CLIBaseURL
   106  			v.Set("baseURL", test.CfgBaseURL)
   107  			s.serverAppend = test.AppendPort
   108  			s.serverPort = test.Port
   109  			result, err := s.fixURL(v, baseURL, s.serverPort)
   110  			if err != nil {
   111  				t.Errorf("Unexpected error %s", err)
   112  			}
   113  			if result != test.Result {
   114  				t.Errorf("Expected %q, got %q", test.Result, result)
   115  			}
   116  		})
   117  	}
   118  }
   119  
   120  func TestRemoveErrorPrefixFromLog(t *testing.T) {
   121  	c := qt.New(t)
   122  	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
   123  ERROR 2018/10/07 13:11:12 Rebuild failed: logged 1 error(s)
   124  `
   125  
   126  	withoutError := removeErrorPrefixFromLog(content)
   127  
   128  	c.Assert(strings.Contains(withoutError, "ERROR"), qt.Equals, false)
   129  }
   130  
   131  func isWindowsCI() bool {
   132  	return runtime.GOOS == "windows" && os.Getenv("CI") != ""
   133  }