From ffba8064edb6c56f0106638026859b9f2e8ce579 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 10 Feb 2021 11:11:50 -0800 Subject: [PATCH] terminal: StreamsForTesting helper This is to allow convenient testing of functions that are designed to work directly with *terminal.Streams or the individual stream objects inside. Because the InputStream and OutputStream APIs expose directly an *os.File, this does some extra work to set up OS-level pipes so we can capture the output into local buffers to make test assertions against. The idea here is to keep the tricky stuff we need for testing confined to the test codepaths, so that the "real" codepaths don't end up needing to work around abstractions that are otherwise unnecessary. --- internal/terminal/testing.go | 191 +++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 internal/terminal/testing.go diff --git a/internal/terminal/testing.go b/internal/terminal/testing.go new file mode 100644 index 000000000..2830b5d0b --- /dev/null +++ b/internal/terminal/testing.go @@ -0,0 +1,191 @@ +package terminal + +import ( + "fmt" + "io" + "os" + "strings" + "sync" + "testing" +) + +// StreamsForTesting is a helper for test code that is aiming to test functions +// that interact with the input and output streams. +// +// This particular function is for the simple case of a function that only +// produces output: the returned input stream is connected to the system's +// "null device", as if a user had run Terraform with I/O redirection like +//