summaryrefslogtreecommitdiff
path: root/test/terminal-here-test.el
blob: 02c28647b6ba3543f27e55b2a17a477cf9a2b177 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
;;; -*- lexical-binding: t; -*-

(defmacro with-terminal-here-mocks (&rest body)
  "Stub out some process interaction functions"
  `(with-mock
     (stub set-process-sentinel)
     (stub set-process-query-on-exit-flag)
     ,@body))



(ert-deftest terminal-here-launch-alias ()
  (with-terminal-here-mocks
   (mock (terminal-here-launch-in-directory *))
   (terminal-here)))

(ert-deftest linux-default-command ()
  (with-terminal-here-mocks
   (mock (start-process (terminal-here-find-terminal) *
                        (terminal-here-find-terminal)))
   (let ((system-type 'gnu/linux))
     (custom-reevaluate-setting 'terminal-here-terminal-command)
     (terminal-here-launch-in-directory "adir"))))

(ert-deftest osx-default-command ()
  (with-terminal-here-mocks
   (mock (start-process "open" * "open" "-a" "Terminal.app" "."))
   (let ((system-type 'darwin))
     (custom-reevaluate-setting 'terminal-here-terminal-command)
     (terminal-here-launch-in-directory "adir"))))

(ert-deftest windows-default-command ()
  (with-terminal-here-mocks
   (mock (start-process "cmd.exe" *  "cmd.exe" "/C" "start" "cmd.exe"))
   (let ((system-type 'windows-nt))
     (custom-reevaluate-setting 'terminal-here-terminal-command)
     (terminal-here-launch-in-directory "adir"))))



(ert-deftest custom-terminal-command-as-list ()
  (with-terminal-here-mocks
   (mock (start-process "1" * "1" "2" "3"))
   (validate-setq terminal-here-terminal-command '("1" "2" "3"))
   (terminal-here-launch-in-directory "adir")))

(ert-deftest custom-terminal-command-as-function ()
  (with-terminal-here-mocks
   (mock (start-process "1" * "1" "2" "3" "adir"))
   (validate-setq terminal-here-terminal-command (lambda (dir) (list "1" "2" "3" dir)))
   (terminal-here-launch-in-directory "adir")))

(ert-deftest custom-terminal-command-as-junk-rejected ()
  (with-terminal-here-mocks
   (should-error
    (validate-setq terminal-here-terminal-command "astring")
    :type 'user-error)))



(ert-deftest no-project-root-function ()
  ;; Can't use validate setq here because you aren't allowed to set this to nil
  ;; by hand.
  (setq terminal-here-project-root-function nil)
  (should-error (terminal-here-project-launch) :type 'user-error))

(ert-deftest with-project-root-function ()
  (let ((project-root-finder (lambda () "" "vc-root")))
    (validate-setq terminal-here-project-root-function project-root-finder)
    (with-terminal-here-mocks
     (mock (terminal-here--run-command * "vc-root"))
     (terminal-here-project-launch))))

(ert-deftest project-root-finds-nothing ()
  (validate-setq terminal-here-project-root-function (lambda () nil))
  (should-error (terminal-here-project-launch :type 'user-error)))



(ert-deftest sudo-tramp ()
  (with-terminal-here-mocks
   (mock (terminal-here--run-command * "/etc/emacs/"))
   (terminal-here-launch-in-directory "/sudo:root@localhost:/etc/emacs/"))

  (with-terminal-here-mocks
   (mock (terminal-here--run-command * "/etc/emacs/"))
   (terminal-here-launch-in-directory "/sudo:postgres@localhost:/etc/emacs/"))

  (with-terminal-here-mocks
   (mock (terminal-here--run-command *  "/etc/emacs/"))
   (terminal-here-launch-in-directory "/sudo:root@127.0.0.1:/etc/emacs/")))



(ert-deftest parse-ssh-dir ()
  (should (equal (terminal-here--parse-ssh-dir "/ssh:buildbot:/home/buildbot/") (list "buildbot" "/home/buildbot/")))
  (should (equal (terminal-here--parse-ssh-dir "/ssh:david@pi:/home/pi/") (list "david@pi" "/home/pi/")))
  (should (equal (terminal-here--parse-ssh-dir "/ssh:root@192.168.0.1:/etc/hosts") (list "root@192.168.0.1" "/etc/hosts")))

  (should-not (terminal-here--parse-ssh-dir "/home/buildbot/"))
  (should-not (terminal-here--parse-ssh-dir "/ssh/foo/bar")))

(ert-deftest ssh-tramp ()
  (cl-letf* ((launch-command nil)
             ((symbol-function 'terminal-here--run-command)
              (lambda (command _dir)
                (setq launch-command command))))
    (validate-setq terminal-here-command-flag "-k")
    (terminal-here-launch-in-directory "/ssh:david@pi:/home/pi/")
    (should (equal (car launch-command) (terminal-here-find-terminal)))
    (should (equal (cadr launch-command) "-k"))
    (should (equal (caddr launch-command) "ssh"))))