Path: blob/main/components/gitpod-cli/cmd/validate_test.go
2498 views
// Copyright (c) 2024 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"io"8"testing"910"github.com/google/go-cmp/cmp"11"github.com/stretchr/testify/assert"12)1314type MockTerminalReader struct {15Data [][]byte16Index int17Errors []error18}1920func (m *MockTerminalReader) Recv() ([]byte, error) {21if m.Index >= len(m.Data) {22return nil, io.EOF23}24data := m.Data[m.Index]25err := m.Errors[m.Index]26m.Index++27return data, err28}2930func TestProcessTerminalOutput(t *testing.T) {31tests := []struct {32name string33input [][]byte34expected []string35}{36{37name: "Simple line",38input: [][]byte{[]byte("Hello, World!\n")},39expected: []string{"Hello, World!"},40},41{42name: "Windows line ending",43input: [][]byte{[]byte("Hello\r\nWorld\r\n")},44expected: []string{"Hello", "World"},45},46{47name: "Updating line",48input: [][]byte{49[]byte("Hello, World!\r"),50[]byte("Hello, World 2!\r"),51[]byte("Hello, World 3!\n"),52},53expected: []string{"Hello, World!", "Hello, World 2!", "Hello, World 3!"},54},55{56name: "Backspace",57input: [][]byte{[]byte("Helloo\bWorld\n")},58expected: []string{"HelloWorld"},59},60{61name: "Partial UTF-8",62input: [][]byte{[]byte("Hello, 世"), []byte("界\n")},63expected: []string{"Hello, 世界"},64},65{66name: "Partial emoji",67input: [][]byte{68[]byte("Hello "),69{240, 159},70{145, 141},71[]byte("!\n"),72},73expected: []string{"Hello 👍!"},74},75{76name: "Multiple lines in one receive",77input: [][]byte{[]byte("Line1\nLine2\nLine3\n")},78expected: []string{"Line1", "Line2", "Line3"},79},80}8182for _, test := range tests {83t.Run(test.name, func(t *testing.T) {84reader := &MockTerminalReader{85Data: test.input,86Errors: make([]error, len(test.input)),87}8889var actual []string90printLine := func(line string) {91actual = append(actual, line)92}9394err := processTerminalOutput(reader, printLine)95assert.NoError(t, err)9697if diff := cmp.Diff(test.expected, actual); diff != "" {98t.Errorf("processTerminalOutput() mismatch (-want +got):\n%s", diff)99}100})101}102}103104105