github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/cat_test.go (about)

     1  // Copyright (c) 2015-2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"bytes"
    22  	"io"
    23  	"testing"
    24  )
    25  
    26  func TestPrettyStdout(t *testing.T) {
    27  	testCases := []struct {
    28  		originText string
    29  		prettyText string
    30  	}{
    31  		{"", ""},
    32  		{"text", "text"},
    33  		// Check with new lines
    34  		{"text\r\n", "text\r\n"},
    35  		{"\ttext\n", "\ttext\n"},
    36  		// Print some unicode characters and check if it is not altered
    37  		{"добро пожаловать.", "добро пожаловать."},
    38  		// Print colored text
    39  		{"\x1b\x5b\x33\x31\x6d\x66\x61\x69\x6c", "^?[31mfail"},
    40  		// Print clear screen
    41  		{"\x1b\x63", "^?c"},
    42  		// Random data
    43  		{"\x3d\xef\xd2\xb5", "=�ҵ"},
    44  	}
    45  
    46  	for i, testCase := range testCases {
    47  		reader := bytes.NewReader([]byte(testCase.originText))
    48  		fakeStdout := bytes.NewBuffer([]byte(""))
    49  		n, err := io.Copy(newPrettyStdout(fakeStdout), reader)
    50  		if err != nil {
    51  			t.Fatalf("Test %d: %v\n", i+1, err)
    52  		}
    53  		if int(n) != len(testCase.originText) {
    54  			t.Fatalf("Test %d: copy error\n", i+1)
    55  		}
    56  		prettyText, err := io.ReadAll(fakeStdout)
    57  		if err != nil {
    58  			t.Fatalf("Test %d: %v", i+1, err)
    59  		}
    60  		if string(prettyText) != testCase.prettyText {
    61  			t.Fatalf("Test %d: expected output `%s`, found output `%s`", i+1, testCase.prettyText, string(prettyText))
    62  		}
    63  	}
    64  }