interact_unix_test.go 1.6 KB

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