From 0918e80f8954bc20e41e0254e13edd3f79d4a72a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 13 May 2023 19:02:34 +0200 Subject: [PATCH] Fix CLI tests on Windows --- commands/server.go | 16 +++++++++++++++- main_test.go | 18 +++++++++++++++--- testscripts/commands/hugo__watch.txt | 3 +++ testscripts/commands/server.txt | 3 ++- testscripts/commands/server__edit_config.txt | 3 ++- testscripts/commands/server__edit_content.txt | 3 ++- testscripts/commands/server__multihost.txt | 3 ++- .../commands/server_render_static_to_disk.txt | 3 ++- .../commands/server_render_to_memory.txt | 3 ++- 9 files changed, 45 insertions(+), 10 deletions(-) diff --git a/commands/server.go b/commands/server.go index 67b29fed758..4211798f090 100644 --- a/commands/server.go +++ b/commands/server.go @@ -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 } @@ -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)) @@ -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 @@ -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(): diff --git a/main_test.go b/main_test.go index 822472fefe0..c2b0c759389 100644 --- a/main_test.go +++ b/main_test.go @@ -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) @@ -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() + + }, }, } diff --git a/testscripts/commands/hugo__watch.txt b/testscripts/commands/hugo__watch.txt index cb29b8c2e1e..cf392f3d062 100644 --- a/testscripts/commands/hugo__watch.txt +++ b/testscripts/commands/hugo__watch.txt @@ -1,5 +1,8 @@ # Test the hugo command. +# See https://github.com/rogpeppe/go-internal/issues/228 +windows skip + hugo -w & sleep diff --git a/testscripts/commands/server.txt b/testscripts/commands/server.txt index 07743fbb606..abec31b1a14 100644 --- a/testscripts/commands/server.txt +++ b/testscripts/commands/server.txt @@ -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" diff --git a/testscripts/commands/server__edit_config.txt b/testscripts/commands/server__edit_config.txt index e27e0c3bae2..e3972bf071e 100644 --- a/testscripts/commands/server__edit_config.txt +++ b/testscripts/commands/server__edit_config.txt @@ -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" diff --git a/testscripts/commands/server__edit_content.txt b/testscripts/commands/server__edit_content.txt index a169f2290da..5a2d9d502fc 100644 --- a/testscripts/commands/server__edit_content.txt +++ b/testscripts/commands/server__edit_content.txt @@ -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" diff --git a/testscripts/commands/server__multihost.txt b/testscripts/commands/server__multihost.txt index 85fcd25c9b5..492cac855a5 100644 --- a/testscripts/commands/server__multihost.txt +++ b/testscripts/commands/server__multihost.txt @@ -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" diff --git a/testscripts/commands/server_render_static_to_disk.txt b/testscripts/commands/server_render_static_to_disk.txt index 6b86df1c702..f2f10e78fc1 100644 --- a/testscripts/commands/server_render_static_to_disk.txt +++ b/testscripts/commands/server_render_static_to_disk.txt @@ -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" diff --git a/testscripts/commands/server_render_to_memory.txt b/testscripts/commands/server_render_to_memory.txt index ce1c957dd17..53ab74aa514 100644 --- a/testscripts/commands/server_render_to_memory.txt +++ b/testscripts/commands/server_render_to_memory.txt @@ -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"