Skip to content

Commit

Permalink
Reset os.Stdin after test
Browse files Browse the repository at this point in the history
This test munges `os.Stdin`, which contains global state, so reset it
after the test is over to what it was before the test ran.

Also a little minor tidying up of `t.Cleanup()` to ensure all the
cleanups get registered on the same `defer` stack.

And check errors whenever a method may emit them to ensure no other
unexpected surprises.

This issue btw was flushed out by trying to run the tests with
`-count=2`.
  • Loading branch information
jeffwidman committed Aug 23, 2023
1 parent 394b5f8 commit 24ad2e9
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cmd/dependabot/internal/cmd/update_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cmd

import (
"github.com/dependabot/cli/internal/model"
"net/http"
"os"
"reflect"
"strings"
"testing"
"time"

"github.com/dependabot/cli/internal/model"
)

func Test_processInput(t *testing.T) {
Expand Down Expand Up @@ -147,12 +148,22 @@ func Test_extractInput(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
t.Cleanup(func() { os.Remove(tmp.Name()) })

tmp.WriteString(`{"job":{"package-manager":"go_modules"}}`)
_, err = tmp.WriteString(`{"job":{"package-manager":"go_modules"}}`)
if err != nil {
t.Fatal(err)
}
tmp.Close()

os.Stdin, _ = os.Open(tmp.Name())
// This test changes os.Stdin, which contains global state, so ensure we reset it after the test
originalStdIn := os.Stdin
t.Cleanup(func() { os.Stdin = originalStdIn })
os.Stdin, err = os.Open(tmp.Name())
if err != nil {
t.Fatal(err)
}

cmd := NewUpdateCommand()
input, err := extractInput(cmd)
if err != nil {
Expand Down

0 comments on commit 24ad2e9

Please sign in to comment.