Skip to content

Commit d19cae5

Browse files
committed
chore: move over some of the clibase changes
This will help reduce merge conflicts + review overhead in #6491.
1 parent 50432b8 commit d19cae5

File tree

9 files changed

+868
-38
lines changed

9 files changed

+868
-38
lines changed

cli/clibase/clibase.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// We will extend its usage to the rest of our application, completely replacing
66
// cobra/viper. It's also a candidate to be broken out into its own open-source
77
// library, so we avoid deep coupling with Coder concepts.
8+
//
9+
// The Command interface is loosely based on the chi middleware pattern and
10+
// http.Handler/HandlerFunc.
811
package clibase
912

1013
import (

cli/clibase/clibasetest/invokation.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package clibasetest
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"testing"
7+
8+
"github.com/coder/coder/cli/clibase"
9+
)
10+
11+
// IO is the standard input, output, and error for a command.
12+
type IO struct {
13+
Stdin *bytes.Buffer
14+
Stdout *bytes.Buffer
15+
Stderr *bytes.Buffer
16+
}
17+
18+
// FakeIO sets Stdin, Stdout, and Stderr to buffers.
19+
func FakeIO(i *clibase.Invokation) *IO {
20+
b := &IO{
21+
Stdin: bytes.NewBuffer(nil),
22+
Stdout: bytes.NewBuffer(nil),
23+
Stderr: bytes.NewBuffer(nil),
24+
}
25+
i.Stdout = b.Stdout
26+
i.Stderr = b.Stderr
27+
i.Stdin = b.Stdin
28+
return b
29+
}
30+
31+
type testWriter struct {
32+
prefix string
33+
t *testing.T
34+
}
35+
36+
func (w *testWriter) Write(p []byte) (n int, err error) {
37+
w.t.Helper()
38+
w.t.Log(w.prefix, string(p))
39+
return len(p), nil
40+
}
41+
42+
func TestWriter(t *testing.T, prefix string) io.Writer {
43+
return &testWriter{prefix: prefix, t: t}
44+
}
45+
46+
// Invoke creates a fake invokation and IO.
47+
func Invoke(cmd *clibase.Cmd, args ...string) (*clibase.Invokation, *IO) {
48+
i := cmd.Invoke(args...)
49+
return i, FakeIO(i)
50+
}

0 commit comments

Comments
 (0)