From ca2e30e59c478aefefe9a841aba5b0be52e72b8d Mon Sep 17 00:00:00 2001 From: Zach Wasserman Date: Thu, 2 Mar 2023 19:11:15 -0600 Subject: [PATCH] Fix error writing coverage when running tests (#10278) Intended to fix this error we are seeing in CI: ``` error generating coverage report: write |1: file already closed ``` It seems like perhaps a change in the way the test coverage is reported in a recent Go version has interacted with the closing of stdout in these tests. # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Added/updated tests --- cmd/fleetctl/convert_test.go | 2 ++ cmd/fleetctl/trigger_test.go | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/fleetctl/convert_test.go b/cmd/fleetctl/convert_test.go index 6903d24221..655911e7c7 100644 --- a/cmd/fleetctl/convert_test.go +++ b/cmd/fleetctl/convert_test.go @@ -41,6 +41,7 @@ func TestConvertFileOutput(t *testing.T) { func TestConvertFileStdout(t *testing.T) { r, w, _ := os.Pipe() + oldStdout := os.Stdout os.Stdout = w // setup the cli and the convert command @@ -60,6 +61,7 @@ func TestConvertFileStdout(t *testing.T) { err = app.Run(args) require.NoError(t, err) + os.Stdout = oldStdout w.Close() out, _ := ioutil.ReadAll(r) require.Equal(t, expected, out) diff --git a/cmd/fleetctl/trigger_test.go b/cmd/fleetctl/trigger_test.go index 08f36838a3..f6c68a603f 100644 --- a/cmd/fleetctl/trigger_test.go +++ b/cmd/fleetctl/trigger_test.go @@ -13,7 +13,6 @@ import ( "github.com/fleetdm/fleet/v4/server/service" "github.com/fleetdm/fleet/v4/server/service/schedule" kitlog "github.com/go-kit/kit/log" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -50,6 +49,7 @@ func TestTrigger(t *testing.T) { } r, w, _ := os.Pipe() + oldStdout := os.Stdout os.Stdout = w _, _ = runServerWithMockedDS(t, &service.TestServerOpts{ @@ -78,6 +78,7 @@ func TestTrigger(t *testing.T) { assert.Equal(t, "", runAppForTest(t, c.args)) } + os.Stdout = oldStdout w.Close() out, _ := ioutil.ReadAll(r) outlines := strings.Split(string(out), "\n")