GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coder/testutil/writer.go at main · coder/coder

Expand file tree

/

writer.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
62 lines (53 loc) · 1.65 KB
/

writer.go

Copy path

File metadata and controls

62 lines (53 loc) · 1.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package testutil
import (
"io"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/ssh"
"gvisor.dev/gvisor/pkg/context"
"cdr.dev/slog/v3"
"github.com/coder/serpent"
)
// Writer wraps an underlying io.Writer and provides friendlier methods to write to it, including logging.
type Writer struct {
t *testing.T
w io.Writer
l slog.Logger
}
func NewWriterAttachedToInvocation(t *testing.T, logger slog.Logger, invocation *serpent.Invocation) *Writer {
r, w := io.Pipe()
invocation.Stdin = r
// Close the pipe at the end of the test to ensure any goroutine in the Invocation that reads from stdin won't leak.
t.Cleanup(func() {
_ = w.Close()
})
return &Writer{
t: t,
w: w,
l: logger,
}
}
func NewWriterAttachedToSSHSession(t *testing.T, l slog.Logger, session *ssh.Session) *Writer {
r, w := io.Pipe()
session.Stdin = r
// Close the pipe at the end of the test to ensure any goroutine in the Invocation that reads from stdin won't leak.
t.Cleanup(func() {
_ = w.Close()
})
return &Writer{
t: t,
w: w,
l: l,
}
}
func (w *Writer) WriteLine(str string) {
w.t.Helper()
// Always write Windows style endings since our CLI prompt readers trim both out. Note this is *different* than what
// PTY-based tests do. On Unix-like operating systems we write a single carriage-return (\r) to delimit a line
// and the PTY translates it to a line feed (\n) for the CLI command to read. Here there is no translation.
newline := []byte{'\r', '\n'}
_, err := w.w.Write(append([]byte(str), newline...))
if assert.NoError(w.t, err, "write line failed") {
w.l.Debug(context.Background(), "wrote line", slog.F("line", str+string(newline)))
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.