github.com/apptainer/singularity@v3.1.1+incompatible/pkg/util/copy/buffer_test.go (about) 1 // Copyright (c) 2019, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package copy 7 8 import ( 9 "testing" 10 11 "github.com/sylabs/singularity/internal/pkg/test" 12 ) 13 14 func TestNewTerminalBuffer(t *testing.T) { 15 test.DropPrivilege(t) 16 defer test.ResetPrivilege(t) 17 18 // Create and write some byte to terminal buffer 19 ntb := NewTerminalBuffer() 20 21 n, err := ntb.Write([]byte("te")) 22 if err != nil { 23 t.Error(err) 24 } 25 if n != 2 { 26 t.Errorf("wrong number of bytes written") 27 } 28 29 // Get the buffer content 30 line := ntb.Line() 31 if string(line) != "te" { 32 t.Errorf("wrong line returned: %s", line) 33 } 34 35 // Write content and end with a newline to clear buffer 36 n, err = ntb.Write([]byte("st\n")) 37 if err != nil { 38 t.Error(err) 39 } 40 41 // Test if buffer string is empty 42 if string(ntb.Line()) != "" { 43 t.Errorf("unexpected line returned") 44 } 45 }