Skip to content

Commit

Permalink
fix(tests): readd sync lock
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 2, 2023
1 parent d7c8f81 commit c455b87
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions testscript/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -59,6 +60,7 @@ func TestMain(m *testing.M) {
func TestScript(t *testing.T) {
flag.Parse()

var lock sync.Mutex
mkkey := func(name string) (string, *keygen.SSHKeyPair) {
path := filepath.Join(t.TempDir(), name)
pair, err := keygen.New(path, keygen.WithKeyType(keygen.Ed25519), keygen.WithWrite())
Expand Down Expand Up @@ -158,6 +160,7 @@ func TestScript(t *testing.T) {

ctx := config.WithContext(context.Background(), cfg)

lock.Lock()
// XXX: Right now, --sync-hooks is the only flag option we have for
// the serve command.
// TODO: Find a way to test different flag options.
Expand All @@ -172,8 +175,11 @@ func TestScript(t *testing.T) {
e.T().Fatal(err)
}
}()
lock.Unlock()

e.Defer(func() {
lock.Lock()
defer lock.Unlock()
if cmd.Process == nil {
e.T().Fatal("process not started")
}
Expand Down Expand Up @@ -225,6 +231,72 @@ func cmdSoft(key ssh.Signer) func(ts *testscript.TestScript, neg bool, args []st
}
}

<<<<<<< Updated upstream
=======
func cmdUI(key ssh.Signer) func(ts *testscript.TestScript, neg bool, args []string) {
return func(ts *testscript.TestScript, neg bool, args []string) {
if len(args) < 1 {
ts.Fatalf("usage: ui <stdin file>")
return
}

cli, err := ssh.Dial(
"tcp",
net.JoinHostPort("localhost", ts.Getenv("SSH_PORT")),
&ssh.ClientConfig{
User: "admin",
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
},
)
ts.Check(err)
defer cli.Close()

sess, err := cli.NewSession()
ts.Check(err)
defer sess.Close()

// XXX: this is a hack to make the UI tests work
// cmp command always complains about an extra newline
// in the output
defer ts.Stdout().Write([]byte("\n"))

sess.Stdout = ts.Stdout()
sess.Stderr = ts.Stderr()

stdin, err := sess.StdinPipe()
ts.Check(err)

go func() {
defer stdin.Close()
in := ts.ReadFile(args[0])
reader := bufio.NewReader(strings.NewReader(in))
for {
r, _, err := reader.ReadRune()
if err == io.EOF {
break
}
ts.Check(err)
stdin.Write([]byte{byte(r)}) // nolint: errcheck
// Wait for the UI to process the input
<-time.After(100 * time.Millisecond)
}
}()

// XXX: we use "dumb" to tell the server not to render colors
// to make our lives easier :)
err = sess.RequestPty("dumb", 40, 80, ssh.TerminalModes{})
ts.Check(err)

check(ts, sess.Run(""), neg)
}
}

var keyToRune = map[string]rune{
"esc": rune(tea.KeyEscape),
}

>>>>>>> Stashed changes
// P.S. Windows sucks!
func cmdDos2Unix(ts *testscript.TestScript, neg bool, args []string) {
if neg {
Expand Down

0 comments on commit c455b87

Please sign in to comment.