Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/validate_test.go
2498 views
1
// Copyright (c) 2024 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"io"
9
"testing"
10
11
"github.com/google/go-cmp/cmp"
12
"github.com/stretchr/testify/assert"
13
)
14
15
type MockTerminalReader struct {
16
Data [][]byte
17
Index int
18
Errors []error
19
}
20
21
func (m *MockTerminalReader) Recv() ([]byte, error) {
22
if m.Index >= len(m.Data) {
23
return nil, io.EOF
24
}
25
data := m.Data[m.Index]
26
err := m.Errors[m.Index]
27
m.Index++
28
return data, err
29
}
30
31
func TestProcessTerminalOutput(t *testing.T) {
32
tests := []struct {
33
name string
34
input [][]byte
35
expected []string
36
}{
37
{
38
name: "Simple line",
39
input: [][]byte{[]byte("Hello, World!\n")},
40
expected: []string{"Hello, World!"},
41
},
42
{
43
name: "Windows line ending",
44
input: [][]byte{[]byte("Hello\r\nWorld\r\n")},
45
expected: []string{"Hello", "World"},
46
},
47
{
48
name: "Updating line",
49
input: [][]byte{
50
[]byte("Hello, World!\r"),
51
[]byte("Hello, World 2!\r"),
52
[]byte("Hello, World 3!\n"),
53
},
54
expected: []string{"Hello, World!", "Hello, World 2!", "Hello, World 3!"},
55
},
56
{
57
name: "Backspace",
58
input: [][]byte{[]byte("Helloo\bWorld\n")},
59
expected: []string{"HelloWorld"},
60
},
61
{
62
name: "Partial UTF-8",
63
input: [][]byte{[]byte("Hello, 世"), []byte("界\n")},
64
expected: []string{"Hello, 世界"},
65
},
66
{
67
name: "Partial emoji",
68
input: [][]byte{
69
[]byte("Hello "),
70
{240, 159},
71
{145, 141},
72
[]byte("!\n"),
73
},
74
expected: []string{"Hello 👍!"},
75
},
76
{
77
name: "Multiple lines in one receive",
78
input: [][]byte{[]byte("Line1\nLine2\nLine3\n")},
79
expected: []string{"Line1", "Line2", "Line3"},
80
},
81
}
82
83
for _, test := range tests {
84
t.Run(test.name, func(t *testing.T) {
85
reader := &MockTerminalReader{
86
Data: test.input,
87
Errors: make([]error, len(test.input)),
88
}
89
90
var actual []string
91
printLine := func(line string) {
92
actual = append(actual, line)
93
}
94
95
err := processTerminalOutput(reader, printLine)
96
assert.NoError(t, err)
97
98
if diff := cmp.Diff(test.expected, actual); diff != "" {
99
t.Errorf("processTerminalOutput() mismatch (-want +got):\n%s", diff)
100
}
101
})
102
}
103
}
104
105