aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorAurélien Aptel2015-11-16 01:00:25 +0100
committerTed Zlatanov2015-11-18 14:24:35 -0500
commit955e25dbcd0519d115f58b275923a71c04579e83 (patch)
treea75f83e478b62b1de689c1e8c91e4a6cb67f68f3 /modules
parent218caccd968d16a1a8d1f336e72f211c3e169142 (diff)
downloademacs-955e25dbcd0519d115f58b275923a71c04579e83.tar.gz
emacs-955e25dbcd0519d115f58b275923a71c04579e83.zip
Add dynamic module test and helper script
Add 'modhelp.py' script (python2) to automate module testing and module generation. To build and test all modules in the modules/ dir $ ./modhelp.py test To generate a module from template code (good starting point) $ ./modhelp init mynewtestmodule See the script -h option for more documentation. * modules/modhelp.py: New module helper script. * modules/mod-test/Makefile: New file. Makefile for the test module. * modules/mod-test/mod-test.c: New file. Test module source file. * modules/mod-test/test.el: New file. ert test suite for the test module. * modules/.gitignore: New file. Local .gitignore file. Co-authored-by: Philipp Stephani <phst@google.com>
Diffstat (limited to 'modules')
-rw-r--r--modules/.gitignore8
-rw-r--r--modules/mod-test/Makefile15
-rw-r--r--modules/mod-test/mod-test.c254
-rw-r--r--modules/mod-test/test.el91
-rwxr-xr-xmodules/modhelp.py178
5 files changed, 546 insertions, 0 deletions
diff --git a/modules/.gitignore b/modules/.gitignore
new file mode 100644
index 00000000000..33a872ef84a
--- /dev/null
+++ b/modules/.gitignore
@@ -0,0 +1,8 @@
1# built modules
2*.so
3
4# built DOCFILEs
5*.doc
6
7# include makefile for now
8!Makefile
diff --git a/modules/mod-test/Makefile b/modules/mod-test/Makefile
new file mode 100644
index 00000000000..18778f00599
--- /dev/null
+++ b/modules/mod-test/Makefile
@@ -0,0 +1,15 @@
1
2ROOT = ../..
3
4CC = gcc
5LD = gcc
6CFLAGS = -ggdb3 -Wall
7LDFLAGS =
8
9all: mod-test.so
10
11%.so: %.o
12 $(LD) -shared $(LDFLAGS) -o $@ $<
13
14%.o: %.c
15 $(CC) $(CFLAGS) -I$(ROOT)/src -fPIC -c $<
diff --git a/modules/mod-test/mod-test.c b/modules/mod-test/mod-test.c
new file mode 100644
index 00000000000..2de53152b1b
--- /dev/null
+++ b/modules/mod-test/mod-test.c
@@ -0,0 +1,254 @@
1#include <assert.h>
2#include <stdio.h>
3#include <emacs_module.h>
4
5int plugin_is_GPL_compatible;
6
7/*
8 * Always return symbol 't'
9 */
10static emacs_value Fmod_test_return_t (emacs_env *env, int nargs, emacs_value args[], void *data)
11{
12 return env->intern (env, "t");
13}
14
15
16/*
17 * Expose simple sum function
18 */
19static int64_t sum (int64_t a, int64_t b)
20{
21 return a + b;
22}
23
24static emacs_value Fmod_test_sum (emacs_env *env, int nargs, emacs_value args[], void* data)
25{
26 int64_t a = env->extract_integer (env, args[0]);
27 int64_t b = env->extract_integer (env, args[1]);
28
29 int64_t r = sum(a, b);
30
31 return env->make_integer (env, r);
32}
33
34
35/*
36 * Signal '(error 56)
37 */
38static emacs_value Fmod_test_signal (emacs_env *env, int nargs, emacs_value args[], void* data)
39{
40 assert (env->non_local_exit_check (env) == emacs_funcall_exit_return);
41 env->non_local_exit_signal (env, env->intern (env, "error"), env->make_integer (env, 56));
42 return NULL;
43}
44
45
46/*
47 * Throw '(tag 65)
48 */
49static emacs_value Fmod_test_throw (emacs_env *env, int nargs, emacs_value args[], void* data)
50{
51 assert (env->non_local_exit_check (env) == emacs_funcall_exit_return);
52 env->non_local_exit_throw (env, env->intern (env, "tag"), env->make_integer (env, 65));
53 return NULL;
54}
55
56
57/*
58 * Call argument function, catch all non-local exists and return
59 * either normal result or a list describing the non-local exit.
60 */
61static emacs_value Fmod_test_non_local_exit_funcall (emacs_env *env, int nargs, emacs_value args[], void* data)
62{
63 assert (nargs == 1);
64 const emacs_value result = env->funcall (env, args[0], 0, NULL);
65 emacs_value non_local_exit_symbol, non_local_exit_data;
66 enum emacs_funcall_exit code = env->non_local_exit_get (env, &non_local_exit_symbol, &non_local_exit_data);
67 switch (code)
68 {
69 case emacs_funcall_exit_return:
70 return result;
71 case emacs_funcall_exit_signal:
72 {
73 env->non_local_exit_clear (env);
74 const emacs_value Flist = env->intern (env, "list");
75 emacs_value list_args[] = {env->intern (env, "signal"), non_local_exit_symbol, non_local_exit_data};
76 return env->funcall (env, Flist, 3, list_args);
77 }
78 case emacs_funcall_exit_throw:
79 {
80 env->non_local_exit_clear (env);
81 const emacs_value Flist = env->intern (env, "list");
82 emacs_value list_args[] = {env->intern (env, "throw"), non_local_exit_symbol, non_local_exit_data};
83 return env->funcall (env, Flist, 3, list_args);
84 }
85 }
86 /* never reached */
87 return env->intern (env, "nil");;
88}
89
90
91/*
92 * Return a global referrence
93 */
94static emacs_value Fmod_test_globref_make (emacs_env *env, int nargs, emacs_value args[], void* data)
95{
96 /* make a big string and make it global */
97 size_t i;
98 char str[26*100];
99
100 for (i = 0; i < sizeof (str); i++)
101 {
102 str[i] = 'a' + (i % 26);
103 }
104
105 /* we don't need to null-terminate str */
106 emacs_value lisp_str = env->make_string (env, str, sizeof (str));
107 return env->make_global_ref (env, lisp_str);
108}
109
110
111/*
112 * Return a copy of the argument string where every 'a' is replaced with 'b'.
113 */
114static emacs_value Fmod_test_string_a_to_b (emacs_env *env, int nargs, emacs_value args[], void* data)
115{
116 emacs_value lisp_str = args[0];
117 size_t size = 0;
118 char * buf = NULL;
119 size_t i;
120
121 env->copy_string_contents (env, lisp_str, buf, &size);
122 buf = malloc (size);
123 env->copy_string_contents (env, lisp_str, buf, &size);
124
125 for (i = 0; i+1 < size; i++) {
126 if (buf[i] == 'a')
127 buf[i] = 'b';
128 }
129
130 return env->make_string (env, buf, size-1);
131}
132
133
134/*
135 * Embedded pointers in lisp objects.
136 */
137
138/* C struct (pointer to) that will be embedded */
139struct super_struct
140{
141 int amazing_int;
142 char large_unused_buffer[512];
143};
144
145/* Associated finalizer */
146static void finalizer (void *p)
147{
148 if (p)
149 free (p);
150}
151
152/*
153 * Return a new user-pointer to a super_struct, with amazing_int set
154 * to the passed parameter.
155 */
156static emacs_value Fmod_test_userptr_make (emacs_env *env, int nargs, emacs_value args[], void *data)
157{
158 struct super_struct *p = calloc (1, sizeof(*p));
159 p->amazing_int = env->extract_integer (env, args[0]);
160 return env->make_user_ptr (env, finalizer, p);
161}
162
163/*
164 * Return the amazing_int of a passed 'user-pointer to a super_struct'.
165 */
166static emacs_value Fmod_test_userptr_get (emacs_env *env, int nargs, emacs_value args[], void *data)
167{
168 struct super_struct *p = env->get_user_ptr (env, args[0]);
169 return env->make_integer (env, p->amazing_int);
170}
171
172
173/*
174 * Fill vector in args[0] with value in args[1]
175 */
176static emacs_value Fmod_test_vector_fill (emacs_env *env, int nargs, emacs_value args[], void *data)
177{
178 size_t i;
179 emacs_value vec = args[0];
180 emacs_value val = args[1];
181 const size_t size = env->vec_size (env, vec);
182 for (i = 0; i < size; i++)
183 env->vec_set (env, vec, i, val);
184 return env->intern (env, "t");
185}
186
187
188/*
189 * Return whether all elements of vector in args[0] are 'eq' to value in args[1]
190 */
191static emacs_value Fmod_test_vector_eq (emacs_env *env, int nargs, emacs_value args[], void *data)
192{
193 size_t i;
194 emacs_value vec = args[0];
195 emacs_value val = args[1];
196 const size_t size = env->vec_size (env, vec);
197 for (i = 0; i < size; i++)
198 if (!env->eq (env, env->vec_get (env, vec, i), val))
199 return env->intern (env, "nil");
200 return env->intern (env, "t");
201}
202
203
204/*
205 * Lisp utilities for easier readability (simple wrappers)
206 */
207
208/* Provide FEATURE to Emacs */
209static void provide (emacs_env *env, const char *feature)
210{
211 emacs_value Qfeat = env->intern (env, feature);
212 emacs_value Qprovide = env->intern (env, "provide");
213 emacs_value args[] = { Qfeat };
214
215 env->funcall (env, Qprovide, 1, args);
216}
217
218/* Binds NAME to FUN */
219static void bind_function (emacs_env *env, const char *name, emacs_value Sfun)
220{
221 emacs_value Qfset = env->intern (env, "fset");
222 emacs_value Qsym = env->intern (env, name);
223 emacs_value args[] = { Qsym, Sfun };
224
225 env->funcall (env, Qfset, 2, args);
226}
227
228/*
229 * Module init function.
230 */
231int emacs_module_init (struct emacs_runtime *ert)
232{
233 emacs_env *env = ert->get_environment (ert);
234
235#define DEFUN(lsym, csym, amin, amax, doc, data) \
236 bind_function (env, lsym, env->make_function (env, amin, amax, csym, doc, data))
237
238 DEFUN ("mod-test-return-t", Fmod_test_return_t, 1, 1, NULL, NULL);
239 DEFUN ("mod-test-sum", Fmod_test_sum, 2, 2, "Return A + B", NULL);
240 DEFUN ("mod-test-signal", Fmod_test_signal, 0, 0, NULL, NULL);
241 DEFUN ("mod-test-throw", Fmod_test_throw, 0, 0, NULL, NULL);
242 DEFUN ("mod-test-non-local-exit-funcall", Fmod_test_non_local_exit_funcall, 1, 1, NULL, NULL);
243 DEFUN ("mod-test-globref-make", Fmod_test_globref_make, 0, 0, NULL, NULL);
244 DEFUN ("mod-test-string-a-to-b", Fmod_test_string_a_to_b, 1, 1, NULL, NULL);
245 DEFUN ("mod-test-userptr-make", Fmod_test_userptr_make, 1, 1, NULL, NULL);
246 DEFUN ("mod-test-userptr-get", Fmod_test_userptr_get, 1, 1, NULL, NULL);
247 DEFUN ("mod-test-vector-fill", Fmod_test_vector_fill, 2, 2, NULL, NULL);
248 DEFUN ("mod-test-vector-eq", Fmod_test_vector_eq, 2, 2, NULL, NULL);
249
250#undef DEFUN
251
252 provide (env, "mod-test");
253 return 0;
254}
diff --git a/modules/mod-test/test.el b/modules/mod-test/test.el
new file mode 100644
index 00000000000..1242b6a2ec2
--- /dev/null
+++ b/modules/mod-test/test.el
@@ -0,0 +1,91 @@
1;;
2;; Dynamic modules tests
3;;
4
5(require 'ert)
6
7(add-to-list 'load-path (file-name-directory (or #$ (expand-file-name (buffer-file-name)))))
8(require 'mod-test)
9
10;;
11;; basic tests
12;;
13
14(ert-deftest mod-test-sum-test ()
15 (should (= (mod-test-sum 1 2) 3)))
16
17(ert-deftest mod-test-sum-docstring ()
18 (should (string= (documentation 'mod-test-sum) "Return A + B")))
19
20;;
21;; non-local exists (throw, signal)
22;;
23
24(ert-deftest mod-test-non-local-exit-signal-test ()
25 (should-error (mod-test-signal)))
26
27(ert-deftest mod-test-non-local-exit-throw-test ()
28 (should (equal
29 (catch 'tag
30 (mod-test-throw)
31 (ert-fail "expected throw"))
32 65)))
33
34(ert-deftest mod-test-non-local-exit-funcall-normal ()
35 (should (equal (mod-test-non-local-exit-funcall (lambda () 23))
36 23)))
37
38(ert-deftest mod-test-non-local-exit-funcall-signal ()
39 (should (equal (mod-test-non-local-exit-funcall (lambda () (signal 'error '(32))))
40 '(signal error (32)))))
41
42(ert-deftest mod-test-non-local-exit-funcall-throw ()
43 (should (equal (mod-test-non-local-exit-funcall (lambda () (throw 'tag 32)))
44 '(throw tag 32))))
45
46;;
47;; string
48;;
49
50(defun multiply-string (s n)
51 (let ((res ""))
52 (dotimes (i n res)
53 (setq res (concat res s)))))
54
55(ert-deftest mod-test-globref-make-test ()
56 (let ((mod-str (mod-test-globref-make))
57 (ref-str (multiply-string "abcdefghijklmnopqrstuvwxyz" 100)))
58 (garbage-collect) ;; XXX: not enough to really test but it's something..
59 (should (string= ref-str mod-str))))
60
61(ert-deftest mod-test-string-a-to-b-test ()
62 (should (string= (mod-test-string-a-to-b "aaa") "bbb")))
63
64;;
65;; user-pointer
66;;
67
68(ert-deftest mod-test-userptr-fun-test ()
69 (let* ((n 42)
70 (v (mod-test-userptr-make n))
71 (r (mod-test-userptr-get v)))
72
73 (should (eq (type-of v) 'user-ptr))
74 (should (integerp r))
75 (should (= r n))))
76
77;; TODO: try to test finalizer
78
79;;
80;; vectors
81;;
82
83(ert-deftest mod-test-vector-test ()
84 (dolist (s '(2 10 100 1000))
85 (dolist (e '(42 foo "foo"))
86 (let* ((v-ref (make-vector 2 e))
87 (eq-ref (eq (aref v-ref 0) (aref v-ref 1)))
88 (v-test (make-vector s nil)))
89
90 (should (eq (mod-test-vector-fill v-test e) t))
91 (should (eq (mod-test-vector-eq v-test e) eq-ref))))))
diff --git a/modules/modhelp.py b/modules/modhelp.py
new file mode 100755
index 00000000000..ef41ba5a917
--- /dev/null
+++ b/modules/modhelp.py
@@ -0,0 +1,178 @@
1#!/usr/bin/env python
2import os
3import string
4import subprocess as sp
5import argparse
6import re
7
8EMACS = os.path.join('..', 'src', 'emacs')
9
10def find_modules():
11 modpaths = []
12 for (dirname, dirs, files) in os.walk('.'):
13 if 'Makefile' in files:
14 modpaths.append(dirname)
15 return modpaths
16
17def cmd_test(args):
18 mods = args.module
19 if not mods:
20 mods = find_modules()
21
22 make_cmd = ['make']
23 if args.force:
24 make_cmd.append('-B')
25
26 failed = []
27 for m in mods:
28 print '[*] %s: ------- start -------' % m
29 print '[*] %s: running make' % m
30 r = sp.call(make_cmd, cwd=m)
31 if r != 0:
32 print '[E] %s: make failed' % m
33 failed += [m]
34 continue
35
36 print '[*] %s: running test' % m
37 testpath = os.path.join(m, 'test.el')
38 if os.path.isfile(testpath):
39 emacs_cmd = [EMACS, '-batch', '-L', '.', '-l', 'ert', '-l', testpath, '-f', 'ert-run-tests-batch-and-exit']
40 print ' '.join(emacs_cmd)
41 r = sp.call(emacs_cmd)
42 if r != 0:
43 print '[E] %s: test failed' % m
44 failed += [m]
45 continue
46 else:
47 print '[W] %s: no test to run' % m
48
49 print '\n[*] %d/%d MODULES OK' % (len(mods)-len(failed), len(mods))
50 for m in failed:
51 print '\tfailed: %s' % m
52
53def to_lisp_sym(sym):
54 sym = re.sub('[_ ]', '-', sym)
55 return sym
56
57def to_c_sym(sym):
58 sym = re.sub('[- ]', '_', sym)
59 return sym
60
61def cmd_init(args):
62 if os.path.exists(args.module):
63 print "%s: file/dir '%s' already exists" % (__file__, args.module)
64 return
65
66 os.mkdir(args.module)
67
68 template_vars = {
69 'module': args.module,
70 'func': args.fun,
71 'c_file': '%s.c' % args.module,
72 'c_func': 'F%s_%s' % (to_c_sym(args.module), to_c_sym(args.fun)),
73 'lisp_func': '%s-%s' % (args.module, to_lisp_sym(args.fun)),
74 }
75
76 for path, t in TEMPLATES.items():
77 if isinstance(path, string.Template):
78 path = path.substitute(template_vars)
79 path = os.path.join(args.module, path)
80 print "writing %s..." % path
81 with open(path, "w+") as f:
82 f.write(t.substitute(template_vars))
83 print "done! you can run %s test %s" % (__file__, args.module)
84
85
86def main():
87 # path always written relative to this file
88 os.chdir(os.path.dirname(os.path.realpath(__file__)))
89
90 mainp = argparse.ArgumentParser()
91 subp = mainp.add_subparsers()
92
93 testp = subp.add_parser('test', help='run tests')
94 testp.add_argument('-f', '--force', action='store_true', help='force regeneration (make -B)')
95 testp.add_argument('module', nargs='*', help='path to module to test (default all)')
96 testp.set_defaults(func=cmd_test)
97
98 initp = subp.add_parser('init', help='create a test module from a template')
99 initp.add_argument('module', help='name of the new module')
100 initp.add_argument('-f', '--fun', default='fun', help='overide name of the default function')
101 initp.set_defaults(func=cmd_init)
102
103 args = mainp.parse_args()
104 args.func(args)
105
106
107# double the $ to escape python template syntax
108TEMPLATES = {
109 'Makefile': string.Template('''
110ROOT = ../..
111
112CC = gcc
113LD = gcc
114CFLAGS = -ggdb3 -Wall
115LDFLAGS =
116
117all: ${module}.so ${module}.doc
118
119%.so: %.o
120 $$(LD) -shared $$(LDFLAGS) -o $$@ $$<
121
122%.o: %.c
123 $$(CC) $$(CFLAGS) -I$$(ROOT)/src -fPIC -c $$<
124
125'''),
126
127 string.Template('${c_file}'): string.Template('''
128#include <emacs_module.h>
129
130int plugin_is_GPL_compatible;
131
132static emacs_value ${c_func} (emacs_env *env, int nargs, emacs_value args[], void *data)
133{
134 return env->intern (env, "t");
135}
136
137/* Binds NAME to FUN */
138static void bind_function (emacs_env *env, const char *name, emacs_value Sfun)
139{
140 emacs_value Qfset = env->intern (env, "fset");
141 emacs_value Qsym = env->intern (env, name);
142 emacs_value args[] = { Qsym, Sfun };
143
144 env->funcall (env, Qfset, 2, args);
145}
146
147/* Provide FEATURE to Emacs */
148static void provide (emacs_env *env, const char *feature)
149{
150 emacs_value Qfeat = env->intern (env, feature);
151 emacs_value Qprovide = env->intern (env, "provide");
152 emacs_value args[] = { Qfeat };
153
154 env->funcall (env, Qprovide, 1, args);
155}
156
157int emacs_module_init (struct emacs_runtime *ert)
158{
159 emacs_env *env = ert->get_environment (ert);
160 bind_function (env, "${lisp_func}", env->make_function (env, 1, 1, ${c_func}, "doc", NULL));
161 provide (env, "${module}");
162 return 0;
163}
164'''),
165 'test.el': string.Template('''
166(require 'ert)
167(require 'module-test-common)
168
169;; #$$ works when loading, buffer-file-name when evaluating from emacs
170(module-load (module-path (or #$$ (expand-file-name (buffer-file-name)))))
171
172(ert-deftest ${lisp_func}-test ()
173 (should (eq (${lisp_func} 42) t)))
174''')
175}
176
177if __name__ == '__main__':
178 main()