-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtest-helper.el
113 lines (89 loc) · 3.39 KB
/
test-helper.el
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
113
;;; test-helper.el --- Prodigy: Test helpers -*- lexical-binding: t; -*-
;; Copyright (C) 2014 Johan Andersson
;; Author: Johan Andersson <johan.rejeep@gmail.com>
;; Maintainer: Johan Andersson <johan.rejeep@gmail.com>
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;; Code:
(require 'f)
(require 'json)
(defvar prodigy-test/test-path
(f-parent (f-this-file)))
(defvar prodigy-test/root-path
(f-parent prodigy-test/test-path))
(defun plist-keys (plist)
"Return a list containing all the keys in PLIST."
(when plist
(cons
(car plist)
(plist-keys
(cddr plist)))))
(defvar prodigy-test/port 1333
"The test server will run on this port.")
(defun prodigy-test/make-service (&rest args)
"Create a test service.
If ARGS are specified, they will override the default."
(let ((server-path (f-expand "server.coffee" prodigy-test/test-path)))
(let* ((port prodigy-test/port)
(service
(list :name "Test Service"
:command "coffee"
:args (list server-path)
:port port
:env `(("PORT" ,(number-to-string port))))))
(mapc
(lambda (property)
(plist-put service property (plist-get args property)))
(plist-keys args))
(car (apply 'prodigy-define-service service)))))
(defun prodigy-test/post-message (service action &rest args)
"Post a message to the SERVICE process.
ACTION and ARGS are json encoded and sent to the process."
(let ((process
(make-network-process
:name "Test Server Connector"
:host "127.0.0.1"
:service (plist-get service :port))))
(process-send-string process (json-encode (cons action args)))))
(defun prodigy-test/log-lines (service n)
"Log N lines into the process ouptut."
(--dotimes n
(prodigy-test/post-message service 'log
(concat "Line " (number-to-string it)))))
(defmacro with-sandbox (&rest body)
"Yield BODY in a sandboxed environment."
`(with-temp-buffer
(setq prodigy-tags nil)
(setq prodigy-services nil)
(setq prodigy-status-list nil)
(setq prodigy-start-tryouts 1)
(setq prodigy-stop-tryouts 1)
(setq prodigy-filters nil)
(setq prodigy-view-truncate-by-default nil)
(prodigy-define-default-status-list)
(with-mock ,@body)))
(defun prodigy-test/delay (seconds callback)
"Wait SECONDS, then run function CALLBACK."
(declare (indent 1))
(run-at-time seconds nil callback))
(require 'prodigy (f-expand "prodigy" prodigy-test/root-path))
(require 'ert)
(require 'ert-async)
(require 'el-mock)
(eval-when-compile
(require 'cl))
(provide 'test-helper)
;;; test-helper.el ends here