Skip to content

Commit

Permalink
Fix CLI tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 13, 2023
1 parent 7dfdff9 commit 0918e80
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 10 deletions.
16 changes: 15 additions & 1 deletion commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func newHugoBuilder(r *rootCommand, s *serverCommand, onConfigLoaded ...func(rel

func newServerCommand() *serverCommand {
var c *serverCommand
c = &serverCommand{}
c = &serverCommand{
quit: make(chan bool),
}
return c
}

Expand Down Expand Up @@ -361,6 +363,14 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, net.Listener, string
} else {
mu.Handle(u.Path, http.StripPrefix(u.Path, fileserver))
}
if r.IsTestRun() {
var shutDownOnce sync.Once
mu.HandleFunc("/__stop", func(w http.ResponseWriter, r *http.Request) {
shutDownOnce.Do(func() {
close(f.c.quit)
})
})
}

endpoint := net.JoinHostPort(f.c.serverInterface, strconv.Itoa(port))

Expand Down Expand Up @@ -405,6 +415,7 @@ type serverCommand struct {

*hugoBuilder

quit chan bool // Closed when the server should shut down. Used in tests only.
serverPorts []serverPortListener
doLiveReload bool

Expand Down Expand Up @@ -829,6 +840,9 @@ func (c *serverCommand) serve() error {
err = func() error {
for {
select {
case <-c.quit:
fmt.Println("Shutting down server")
return nil
case <-sigs:
return nil
case <-ctx.Done():
Expand Down
18 changes: 15 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ import (
)

func TestCommands(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skip on Windows for now; TODO1")
}
p := commonTestScriptsParam
p.Dir = "testscripts/commands"
testscript.Run(t, p)
Expand Down Expand Up @@ -347,6 +344,21 @@ var commonTestScriptsParam = testscript.Params{
}

},
"stopServer": func(ts *testscript.TestScript, neg bool, args []string) {
baseURL := ts.Getenv("HUGOTEST_BASEURL_0")
if baseURL == "" {
ts.Fatalf("HUGOTEST_BASEURL_0 not set")
}
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
resp, err := http.Get(baseURL + "__stop")
if err != nil {
ts.Fatalf("failed to shutdown server: %v", err)
}
defer resp.Body.Close()

},
},
}

Expand Down
3 changes: 3 additions & 0 deletions testscripts/commands/hugo__watch.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Test the hugo command.

# See https://github.com/rogpeppe/go-internal/issues/228
windows skip

hugo -w &

sleep
Expand Down
3 changes: 2 additions & 1 deletion testscripts/commands/server.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ httpget ${HUGOTEST_BASEURL_0}doesnotexist 'custom 404'
# By defauilt, the server renders to memory.
! exists public/index.html

stop
stopServer
! stderr .

-- hugo.toml --
title = "Hugo Server Test"
Expand Down
3 changes: 2 additions & 1 deletion testscripts/commands/server__edit_config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ mv edits/addlanguage.toml hugo.toml
httpget $HUGOTEST_BASEURL_0 'Title: Hugo New Server Test' $HUGOTEST_BASEURL_0
httpget ${HUGOTEST_BASEURL_0}nn/ 'Hugo Nynorsk Server Test' ${HUGOTEST_BASEURL_0}nn/

stop
stopServer
! stderr .

-- hugo.toml --
title = "Hugo Server Test"
Expand Down
3 changes: 2 additions & 1 deletion testscripts/commands/server__edit_content.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ ls public/staticfiles
cp stdout lsstaticfiles_2.txt
cmp lsstaticfiles_1.txt lsstaticfiles_2.txt

stop
stopServer
! stderr .

-- hugo.toml --
title = "Hugo Server Test"
Expand Down
3 changes: 2 additions & 1 deletion testscripts/commands/server__multihost.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ waitServer
httpget $HUGOTEST_BASEURL_0 'Title: Hugo Server Test' $HUGOTEST_BASEURL_0
httpget $HUGOTEST_BASEURL_1 'Title: Hugo Serveur Test' $HUGOTEST_BASEURL_1

stop
stopServer
! stderr .

-- hugo.toml --
title = "Hugo Server Test"
Expand Down
3 changes: 2 additions & 1 deletion testscripts/commands/server_render_static_to_disk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ httpget $HUGOTEST_BASEURL_0 'Title: Hugo Server Test' $HUGOTEST_BASEURL_0
! exists public/index.html
exists public/mystatic.txt

stop
stopServer
! stderr .

-- hugo.toml --
title = "Hugo Server Test"
Expand Down
3 changes: 2 additions & 1 deletion testscripts/commands/server_render_to_memory.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ httpget $HUGOTEST_BASEURL_0 'Title: Hugo Server Test' $HUGOTEST_BASEURL_0
! exists public/index.html
! exists public/mystatic.txt

stop
stopServer
! stderr .

-- hugo.toml --
title = "Hugo Server Test"
Expand Down

0 comments on commit 0918e80

Please sign in to comment.