diff options
| author | Aurélien Aptel | 2015-11-16 01:00:25 +0100 |
|---|---|---|
| committer | Ted Zlatanov | 2015-11-18 14:24:35 -0500 |
| commit | 955e25dbcd0519d115f58b275923a71c04579e83 (patch) | |
| tree | a75f83e478b62b1de689c1e8c91e4a6cb67f68f3 /modules/modhelp.py | |
| parent | 218caccd968d16a1a8d1f336e72f211c3e169142 (diff) | |
| download | emacs-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/modhelp.py')
| -rwxr-xr-x | modules/modhelp.py | 178 |
1 files changed, 178 insertions, 0 deletions
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 | ||
| 2 | import os | ||
| 3 | import string | ||
| 4 | import subprocess as sp | ||
| 5 | import argparse | ||
| 6 | import re | ||
| 7 | |||
| 8 | EMACS = os.path.join('..', 'src', 'emacs') | ||
| 9 | |||
| 10 | def 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 | |||
| 17 | def 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 | |||
| 53 | def to_lisp_sym(sym): | ||
| 54 | sym = re.sub('[_ ]', '-', sym) | ||
| 55 | return sym | ||
| 56 | |||
| 57 | def to_c_sym(sym): | ||
| 58 | sym = re.sub('[- ]', '_', sym) | ||
| 59 | return sym | ||
| 60 | |||
| 61 | def 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 | |||
| 86 | def 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 | ||
| 108 | TEMPLATES = { | ||
| 109 | 'Makefile': string.Template(''' | ||
| 110 | ROOT = ../.. | ||
| 111 | |||
| 112 | CC = gcc | ||
| 113 | LD = gcc | ||
| 114 | CFLAGS = -ggdb3 -Wall | ||
| 115 | LDFLAGS = | ||
| 116 | |||
| 117 | all: ${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 | |||
| 130 | int plugin_is_GPL_compatible; | ||
| 131 | |||
| 132 | static 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 */ | ||
| 138 | static 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 */ | ||
| 148 | static 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 | |||
| 157 | int 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 | |||
| 177 | if __name__ == '__main__': | ||
| 178 | main() | ||