Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(tests): improves handling of cleanup func #21

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ func TestParseContent_Stdin(t *testing.T) {
var directText *string

// Replace stdin with a pipe for testing
_, w, cleanup := replaceStdin(t)
defer cleanup() // Ensure cleanup is called to restore the original stdin
_, w := replaceStdin(t)

// Write content to the pipe and close it
go func() {
Expand Down Expand Up @@ -210,7 +209,7 @@ t.Helper ()
}

// replaceStdin replaces os.Stdin with a pipe for testing purposes.
func replaceStdin(t *testing.T) (*os.File, *os.File, func()) {
func replaceStdin(t *testing.T) (*os.File, *os.File) {
t.Helper ()
// Create a pipe
r, w, err := os.Pipe()
Expand All @@ -225,11 +224,11 @@ t.Helper ()
os.Stdin = r

// Cleanup function to restore the original stdin and close the pipe
cleanup := func() {
t.Cleanup(func() {
os.Stdin = originalStdin
r.Close()
w.Close()
}
})

return r, w, cleanup
return r, w
}
Loading