utils_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package ssh
  3. import (
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "testing"
  9. "kitty/tools/utils/shlex"
  10. "github.com/google/go-cmp/cmp"
  11. )
  12. var _ = fmt.Print
  13. func TestGetSSHOptions(t *testing.T) {
  14. m := SSHOptions()
  15. if m["w"] != "local_tun[:remote_tun]" {
  16. cmd := exec.Command(SSHExe())
  17. cmd.Stdout = os.Stdout
  18. cmd.Stderr = os.Stderr
  19. cmd.Run()
  20. t.Fatalf("Unexpected set of SSH options: %#v", m)
  21. }
  22. }
  23. func TestParseSSHArgs(t *testing.T) {
  24. split := func(x string) []string {
  25. ans, err := shlex.Split(x)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if len(ans) == 0 {
  30. ans = []string{}
  31. }
  32. return ans
  33. }
  34. p := func(args, expected_ssh_args, expected_server_args, expected_extra_args string, expected_passthrough bool) {
  35. ssh_args, server_args, passthrough, extra_args, err := ParseSSHArgs(split(args), "--kitten")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. check := func(a, b any) {
  40. diff := cmp.Diff(a, b)
  41. if diff != "" {
  42. t.Fatalf("Unexpected value for args: %#v\n%s", args, diff)
  43. }
  44. }
  45. check(split(expected_ssh_args), ssh_args)
  46. check(split(expected_server_args), server_args)
  47. check(split(expected_extra_args), extra_args)
  48. check(expected_passthrough, passthrough)
  49. }
  50. p(`localhost`, ``, `localhost`, ``, false)
  51. p(`-- localhost`, ``, `localhost`, ``, false)
  52. p(`-46p23 localhost sh -c "a b"`, `-4 -6 -p 23`, `localhost sh -c "a b"`, ``, false)
  53. p(`-46p23 -S/moose -W x:6 -- localhost sh -c "a b"`, `-4 -6 -p 23 -S /moose -W x:6`, `localhost sh -c "a b"`, ``, false)
  54. p(`--kitten=abc -np23 --kitten xyz host`, `-n -p 23`, `host`, `--kitten abc --kitten xyz`, true)
  55. }
  56. func TestRelevantKittyOpts(t *testing.T) {
  57. tdir := t.TempDir()
  58. path := filepath.Join(tdir, "kitty.conf")
  59. os.WriteFile(path, []byte("term XXX\nshell_integration changed\nterm abcd"), 0o600)
  60. rko := read_relevant_kitty_opts(path)
  61. if rko.Term != "abcd" {
  62. t.Fatalf("Unexpected TERM: %s", RelevantKittyOpts().Term)
  63. }
  64. if rko.Shell_integration != "changed" {
  65. t.Fatalf("Unexpected shell_integration: %s", RelevantKittyOpts().Shell_integration)
  66. }
  67. }