File: test_file.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (61 lines) | stat: -rwxr-xr-x 1,350 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python

# Simple helper script to find and call the test case for a source file
# Intended to be called from a hotkey from an IDE while editing
# source files

# Assumes CWD is set by the IDE to the folder of the file to be tested
# Need to figure out the above project folder to run tests.


import os
import re
import subprocess


comment_re = re.compile(r'^\s*#\s*Tests\s*:', re.I)


def get_project_dir(file):
	pwd = os.getcwd()
	file = os.path.join(pwd, file)
	if pwd.endswith('tests'):
		pwd, x = file.rsplit('tests', 1)
	else:
		pwd, x = file.rsplit('zim', 1)
	return pwd, file[len(pwd):]


def get_test_names(file):
	basename, ext = os.path.basename(file).split('.', 1)
	if basename == '__init__':
		dir = os.path.dirname(file)
		basename = os.path.basename(dir)

	for line in open(file):
		m = comment_re.match(line)
		if m:
			i = len(m.group(0))
			tests = line[i:].strip().split()
			break
	else:
		tests = [basename]

	if "plugins" in os.path.dirname(file):
		tests.insert(0, 'plugins')

	return tests

def run_tests(pwd, names):
	argv = [os.path.join(pwd, 'test.py')] + names
	print(' '.join(argv))
	return subprocess.call(argv, cwd=pwd)


if __name__ == '__main__':
	import sys
	file = sys.argv[1]
	pwd, file = get_project_dir(file)
	names = get_test_names(os.path.join(pwd, file))
	re = run_tests(pwd, names)
	sys.exit(re)