interact_unix_test.go 1.6 KB

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