github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/shell/interact_unix_test.go (about)

     1  //go:build !windows && !plan9 && !js
     2  // +build !windows,!plan9,!js
     3  
     4  package shell
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/markusbkk/elvish/pkg/daemon"
    14  	"github.com/markusbkk/elvish/pkg/env"
    15  
    16  	. "github.com/markusbkk/elvish/pkg/prog/progtest"
    17  	. "github.com/markusbkk/elvish/pkg/testutil"
    18  )
    19  
    20  func TestInteract_NewRcFile_Default(t *testing.T) {
    21  	home := setupHomePaths(t)
    22  	MustWriteFile(
    23  		filepath.Join(home, ".config", "elvish", "rc.elv"), "echo hello new rc.elv")
    24  
    25  	Test(t, &Program{},
    26  		thatElvishInteract().WritesStdout("hello new rc.elv\n"),
    27  	)
    28  }
    29  
    30  func TestInteract_NewRcFile_XDG_CONFIG_HOME(t *testing.T) {
    31  	setupHomePaths(t)
    32  	xdgConfigHome := Setenv(t, env.XDG_CONFIG_HOME, TempDir(t))
    33  	MustWriteFile(
    34  		filepath.Join(xdgConfigHome, "elvish", "rc.elv"),
    35  		"echo hello XDG_CONFIG_HOME rc.elv")
    36  
    37  	Test(t, &Program{},
    38  		thatElvishInteract().WritesStdout("hello XDG_CONFIG_HOME rc.elv\n"),
    39  	)
    40  }
    41  
    42  func TestInteract_ConnectsToDaemon(t *testing.T) {
    43  	InTempDir(t)
    44  
    45  	// Run the daemon in the same process for simplicity.
    46  	daemonDone := make(chan struct{})
    47  	defer func() {
    48  		select {
    49  		case <-daemonDone:
    50  		case <-time.After(Scaled(2 * time.Second)):
    51  			t.Errorf("timed out waiting for daemon to quit")
    52  		}
    53  	}()
    54  	readyCh := make(chan struct{})
    55  	go func() {
    56  		daemon.Serve("sock", "db", daemon.ServeOpts{Ready: readyCh})
    57  		close(daemonDone)
    58  	}()
    59  	select {
    60  	case <-readyCh:
    61  		// Do nothing
    62  	case <-time.After(Scaled(2 * time.Second)):
    63  		t.Fatalf("timed out waiting for daemon to start")
    64  	}
    65  
    66  	Test(t, &Program{ActivateDaemon: daemon.Activate},
    67  		thatElvishInteract("-sock", "sock", "-db", "db").
    68  			WithStdin("use daemon; echo $daemon:pid\n").
    69  			WritesStdout(fmt.Sprintln(os.Getpid())),
    70  	)
    71  }