mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-15 15:27:04 +00:00
parent
1e233b1c45
commit
a7d00fc39e
3 changed files with 72 additions and 3 deletions
|
@ -66,7 +66,7 @@ for rendering in Hugo.`,
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
now := time.Now().Format(time.RFC3339)
|
now := time.Now().Format("2006-01-02")
|
||||||
prepender := func(filename string) string {
|
prepender := func(filename string) string {
|
||||||
name := filepath.Base(filename)
|
name := filepath.Base(filename)
|
||||||
base := strings.TrimSuffix(name, path.Ext(name))
|
base := strings.TrimSuffix(name, path.Ext(name))
|
||||||
|
|
|
@ -39,6 +39,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type serverCmd struct {
|
type serverCmd struct {
|
||||||
|
// Can be used to stop the server. Useful in tests
|
||||||
|
stop <-chan bool
|
||||||
|
// Can be used to receive notification about when the server is started. Useful in tests.
|
||||||
|
started chan<- bool
|
||||||
|
|
||||||
disableLiveReload bool
|
disableLiveReload bool
|
||||||
navigateToChanged bool
|
navigateToChanged bool
|
||||||
renderToDisk bool
|
renderToDisk bool
|
||||||
|
@ -55,7 +60,11 @@ type serverCmd struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServerCmd() *serverCmd {
|
func newServerCmd() *serverCmd {
|
||||||
cc := &serverCmd{}
|
return newServerCmdSignaled(nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newServerCmdSignaled(stop <-chan bool, started chan<- bool) *serverCmd {
|
||||||
|
cc := &serverCmd{stop: stop, started: started}
|
||||||
|
|
||||||
cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{
|
cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{
|
||||||
Use: "server",
|
Use: "server",
|
||||||
|
@ -394,9 +403,20 @@ func (c *commandeer) serve(s *serverCmd) error {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.started != nil {
|
||||||
|
s.started <- true
|
||||||
|
}
|
||||||
|
|
||||||
jww.FEEDBACK.Println("Press Ctrl+C to stop")
|
jww.FEEDBACK.Println("Press Ctrl+C to stop")
|
||||||
|
|
||||||
<-sigs
|
if s.stop != nil {
|
||||||
|
select {
|
||||||
|
case <-sigs:
|
||||||
|
case <-s.stop:
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
<-sigs
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,11 +14,60 @@
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/helpers"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestServer(t *testing.T) {
|
||||||
|
assert := require.New(t)
|
||||||
|
dir, err := createSimpleTestSite(t)
|
||||||
|
assert.NoError(err)
|
||||||
|
|
||||||
|
// Let us hope that this port is available on all systems ...
|
||||||
|
port := 1331
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
os.RemoveAll(dir)
|
||||||
|
}()
|
||||||
|
|
||||||
|
stop, started := make(chan bool), make(chan bool)
|
||||||
|
|
||||||
|
scmd := newServerCmdSignaled(stop, started)
|
||||||
|
|
||||||
|
cmd := scmd.getCommand()
|
||||||
|
cmd.SetArgs([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
_, err = cmd.ExecuteC()
|
||||||
|
assert.NoError(err)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-started:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("server start took too long")
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Get("http://localhost:1331/")
|
||||||
|
assert.NoError(err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
homeContent := helpers.ReaderToString(resp.Body)
|
||||||
|
|
||||||
|
assert.Contains(homeContent, "List: Hugo Commands")
|
||||||
|
|
||||||
|
// Stop the server.
|
||||||
|
stop <- true
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestFixURL(t *testing.T) {
|
func TestFixURL(t *testing.T) {
|
||||||
type data struct {
|
type data struct {
|
||||||
TestName string
|
TestName string
|
||||||
|
|
Loading…
Add table
Reference in a new issue