github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/complete/line_test.go (about)

     1  // Copyright 2012-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package complete
     6  
     7  import (
     8  	"log"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/u-root/u-root/pkg/termios"
    13  )
    14  
    15  // TestSh tests a bash-like line completer.
    16  // It is intended to be used interactively for now
    17  // and will exit if INTERACTIVE is not set.
    18  func TestSh(t *testing.T) {
    19  	if os.Getenv("INTERACTIVE") == "" {
    20  		t.Skip()
    21  	}
    22  	Debug = t.Logf
    23  	tty, err := termios.New()
    24  	if err != nil {
    25  		log.Fatal(err)
    26  	}
    27  	r, err := tty.Raw()
    28  	if err != nil {
    29  		log.Printf("non-fatal cannot get tty: %v", err)
    30  	}
    31  	defer func() {
    32  		if err := tty.Set(r); err != nil {
    33  			t.Error(err)
    34  		}
    35  	}()
    36  
    37  	f := NewFileCompleter("")
    38  	p, err := NewPathCompleter()
    39  	if err != nil {
    40  		log.Fatal(err)
    41  	}
    42  
    43  	bin := NewMultiCompleter(NewStringCompleter([]string{"exit"}), p, f)
    44  	l := NewNewerLineReader(bin, f)
    45  	l.Prompt = "Prompt% "
    46  	for !l.EOF {
    47  		if err := l.ReadLine(tty, tty); err != nil {
    48  			t.Logf("looperr: %v", err)
    49  		}
    50  		if _, err := tty.Write([]byte("\r\n")); err != nil {
    51  			t.Error(err)
    52  		}
    53  	}
    54  	t.Log("All done!")
    55  }