From c3ffc3aca3a641d3605b7779569df006c6001cc8 Mon Sep 17 00:00:00 2001 From: noamcattan Date: Sun, 13 Oct 2024 10:34:56 +0300 Subject: [PATCH] cleanup test server --- atlasaction/gitlab_ci_test.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/atlasaction/gitlab_ci_test.go b/atlasaction/gitlab_ci_test.go index a9835ea..1508a92 100644 --- a/atlasaction/gitlab_ci_test.go +++ b/atlasaction/gitlab_ci_test.go @@ -9,7 +9,6 @@ import ( "net/http" "net/http/httptest" "os" - "path" "path/filepath" "strconv" "testing" @@ -70,7 +69,7 @@ func newMockHandler(dir string) http.Handler { r.Methods(http.MethodPut).Path("/projects/{project}/merge_requests/{mr}/notes/{note}"). HandlerFunc(func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - if _, err := os.Stat(path.Join(dir, vars["note"])); err != nil { + if _, err := os.Stat(filepath.Join(dir, vars["note"])); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } var body struct { @@ -88,14 +87,22 @@ func newMockHandler(dir string) http.Handler { } func TestGitlabCI(t *testing.T) { + var srv *httptest.Server + cleanup := func() { + if srv != nil { + srv.Close() + } + } + defer cleanup() testscript.Run(t, testscript.Params{ Dir: "testdata/gitlab", Setup: func(env *testscript.Env) error { - commentsDir := path.Join(env.WorkDir, "comments") + commentsDir := filepath.Join(env.WorkDir, "comments") if err := os.Mkdir(commentsDir, os.ModePerm); err != nil { return err } - srv := httptest.NewServer(newMockHandler(commentsDir)) + cleanup() + srv = httptest.NewServer(newMockHandler(commentsDir)) env.Setenv("GITLAB_CI", "true") env.Setenv("CI_PROJECT_ID", "1") env.Setenv("CI_API_V4_URL", srv.URL)