Skip to content

Commit

Permalink
add in testIOHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
timl3136 committed Oct 25, 2024
1 parent 2c7710f commit 8ea87a9
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions tools/cli/admin_async_queue_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
package cli

import (
"bytes"
"flag"
"fmt"
"os"
"testing"

"github.com/golang/mock/gomock"
Expand All @@ -45,6 +43,7 @@ func TestAdminGetAsyncWFConfig(t *testing.T) {
name string
setupMocks func(*admin.MockClient)
expectedError string
expectedStr string
flagDomain string
mockDepsError error
mockContextError error
Expand All @@ -64,6 +63,7 @@ func TestAdminGetAsyncWFConfig(t *testing.T) {
Times(1)
},
expectedError: "",
expectedStr: "PredefinedQueueName",
flagDomain: "test-domain",
},
{
Expand Down Expand Up @@ -107,48 +107,28 @@ func TestAdminGetAsyncWFConfig(t *testing.T) {

// Set up mocks for the current test case
tt.setupMocks(adminClient)
ioHandler := &testIOHandler{}

// Create mock app with clientFactoryMock
app := NewCliApp(&clientFactoryMock{
serverAdminClient: adminClient,
})
}, WithIOHandler(ioHandler))

// Set up CLI context with flags
set := flag.NewFlagSet("test", 0)
set.String(FlagDomain, tt.flagDomain, "Domain flag")
c := cli.NewContext(app, set, nil)

r, w, _ := os.Pipe()

// Save the original os.Stdout
origStdout := os.Stdout
// Defer restoring os.Stdout back to its original state
defer func() {
os.Stdout = origStdout
w.Close()
}()

// Redirect os.Stdout to the pipe
os.Stdout = w

// Call the function under test
err := AdminGetAsyncWFConfig(c)

w.Close()
var buf bytes.Buffer
_, err2 := buf.ReadFrom(r)
if err2 != nil {
t.Fatalf("Failed to read from pipe: %v", err)
}
output := buf.String()

// Check the expected outcome
if tt.expectedError != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedError)
} else {
assert.NoError(t, err)
assert.Contains(t, output, "Async workflow queue config")
assert.Contains(t, ioHandler.outputBytes.String(), tt.expectedStr)
}
})
}
Expand Down

0 comments on commit 8ea87a9

Please sign in to comment.