Skip to content

Commit f81b7d0

Browse files
committed
Add basic unit tests and ci
1 parent b61fa9c commit f81b7d0

File tree

13 files changed

+406
-9
lines changed

13 files changed

+406
-9
lines changed

.github/workflows/ci.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: ci
2+
on: [push, pull_request]
3+
4+
jobs:
5+
fmt:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v1
9+
- uses: actions/cache@v1
10+
with:
11+
path: ~/go/pkg/mod
12+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
13+
restore-keys: |
14+
${{ runner.os }}-go-
15+
- name: fmt
16+
uses: ./ci/image
17+
with:
18+
args: ./ci/fmt.sh
19+
lint:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v1
23+
- uses: actions/cache@v1
24+
with:
25+
path: ~/go/pkg/mod
26+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
27+
restore-keys: |
28+
${{ runner.os }}-go-
29+
- name: lint
30+
uses: ./ci/image
31+
with:
32+
args: ./ci/lint.sh
33+
test:
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v1
37+
- uses: actions/cache@v1
38+
with:
39+
path: ~/go/pkg/mod
40+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
41+
restore-keys: |
42+
${{ runner.os }}-go-
43+
- name: test
44+
uses: ./ci/image
45+
with:
46+
args: go test ./...

ci/fmt.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
echo "Formatting..."
3+
4+
go mod tidy
5+
gofmt -w -s .
6+
goimports -w "-local=$$(go list -m)" .
7+
8+
if [ "$CI" != "" ]; then
9+
if [[ $(git ls-files --other --modified --exclude-standard) != "" ]]; then
10+
echo "Files need generation or are formatted incorrectly:"
11+
git -c color.ui=always status | grep --color=no '\e\[31m'
12+
echo "Please run the following locally:"
13+
echo " ./ci/fmt.sh"
14+
exit 1
15+
fi
16+
fi

ci/image/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM golang:1
2+
3+
ENV GOFLAGS="-mod=readonly"
4+
ENV CI=true
5+
6+
RUN go get golang.org/x/tools/cmd/goimports
7+
RUN go get golang.org/x/lint/golint
8+
RUN go get github.com/mattn/goveralls

ci/lint.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
echo "Linting..."
4+
5+
go vet ./...
6+
golint -set_exit_status ./...

client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func RemoteExecer(conn *websocket.Conn) Execer {
2121
return remoteExec{conn: conn}
2222
}
2323

24+
// Command represents an external command to be run
2425
type Command struct {
2526
Command string
2627
Args []string

client_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package wsep
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"io/ioutil"
7+
"net"
8+
"testing"
9+
10+
"cdr.dev/slog/sloggers/slogtest/assert"
11+
"cdr.dev/wsep/internal/proto"
12+
"github.com/google/go-cmp/cmp"
13+
)
14+
15+
func TestRemoteStdin(t *testing.T) {
16+
inputs := []string{
17+
"pwd",
18+
"echo 123\n456",
19+
"\necho 123456\n",
20+
}
21+
22+
for _, tcase := range inputs {
23+
server, client := net.Pipe()
24+
var stdin io.WriteCloser = remoteStdin{
25+
conn: client,
26+
}
27+
go func() {
28+
defer client.Close()
29+
_, err := stdin.Write([]byte(tcase))
30+
assert.Success(t, "write to stdin", err)
31+
}()
32+
33+
bytecmp := cmp.Comparer(bytes.Equal)
34+
35+
msg, err := ioutil.ReadAll(server)
36+
assert.Success(t, "read from server", err)
37+
38+
header, body := proto.SplitMessage(msg)
39+
40+
assert.Equal(t, "stdin body", body, []byte(tcase), bytecmp)
41+
assert.Equal(t, "stdin header", header, []byte(`{"type":"stdin"}`), bytecmp)
42+
}
43+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ module cdr.dev/wsep
33
go 1.14
44

55
require (
6+
cdr.dev/slog v1.3.0
67
github.com/creack/pty v1.1.11
8+
github.com/google/go-cmp v0.4.0
79
github.com/spf13/pflag v1.0.5
810
go.coder.com/cli v0.4.0
911
go.coder.com/flog v0.0.0-20190906214207-47dd47ea0512
10-
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
1112
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
1213
nhooyr.io/websocket v1.8.6
1314
)

go.sum

Lines changed: 232 additions & 2 deletions
Large diffs are not rendered by default.

internal/proto/clientmsg.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package proto
22

3+
// Client message header type
34
const (
45
TypeStart = "start"
56
TypeResize = "resize_header"
67
TypeStdin = "stdin"
78
TypeCloseStdin = "close_stdin"
89
)
910

11+
// ClientResizeHeader specifies a terminal window resize request
1012
type ClientResizeHeader struct {
1113
Type string `json:"type"`
1214
Rows uint16 `json:"rows"`
1315
Cols uint16 `json:"cols"`
1416
}
1517

18+
// ClientStartHeader specifies a request to start command
1619
type ClientStartHeader struct {
1720
Type string `json:"type"`
1821
Command Command `json:"command"`

internal/proto/protocol.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ func SplitMessage(b []byte) (header []byte, body []byte) {
2828
}
2929

3030
type headerWriter struct {
31-
w io.WriteCloser
31+
w io.Writer
3232
header []byte
3333
}
3434

3535
// WithHeader adds the given header to all writes
36-
func WithHeader(w io.WriteCloser, header []byte) io.WriteCloser {
36+
func WithHeader(w io.Writer, header []byte) io.Writer {
3737
return headerWriter{
3838
header: header,
3939
w: w,
@@ -48,7 +48,3 @@ func (h headerWriter) Write(b []byte) (int, error) {
4848
}
4949
return len(b), nil // TODO: potential buggy
5050
}
51-
52-
func (h headerWriter) Close() error {
53-
return h.w.Close()
54-
}

0 commit comments

Comments
 (0)