| 1 | ''' |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | Simple testing tool. |
|---|
| 5 | TODO: find out about nosetest |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | ''' |
|---|
| 9 | |
|---|
| 10 | import os, re, sys |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | def getbuilddirroot(): |
|---|
| 14 | bdroot = ''.join([os.getcwd(), os.sep, 'build', os.sep]) |
|---|
| 15 | build_dir_p = re.compile(r'^lib') |
|---|
| 16 | for dir in os.listdir(bdroot): |
|---|
| 17 | if build_dir_p.match(dir): |
|---|
| 18 | bdroot = ''.join([bdroot, dir]) |
|---|
| 19 | break |
|---|
| 20 | return bdroot |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | if not sys.platform == 'linux2': |
|---|
| 24 | print 'BTSniffing development only on Linux for now' |
|---|
| 25 | exit() |
|---|
| 26 | |
|---|
| 27 | # get into project root |
|---|
| 28 | print "Checking we're in project root .... ", |
|---|
| 29 | bdroot = testsroot = None |
|---|
| 30 | if 'setup.py' in os.listdir(os.getcwd()): |
|---|
| 31 | print "yes" |
|---|
| 32 | print 'Doing disutils build' |
|---|
| 33 | os.system('python setup.py build') |
|---|
| 34 | bdroot = getbuilddirroot() |
|---|
| 35 | print 'build dir project root: ', bdroot |
|---|
| 36 | else: |
|---|
| 37 | raise IOError("run_tests: not in project root. Run script in BTSniffer root") |
|---|
| 38 | exit() |
|---|
| 39 | |
|---|
| 40 | print 'Checking that tests exist ... ', |
|---|
| 41 | if 'tests' in os.listdir(os.getcwd()): |
|---|
| 42 | print 'yes' |
|---|
| 43 | testsroot = ''.join([os.getcwd(), os.sep, 'tests']) |
|---|
| 44 | else: |
|---|
| 45 | raise IOError("run_tests: no tests folder.") |
|---|
| 46 | exit() |
|---|
| 47 | |
|---|
| 48 | # Get all tests files |
|---|
| 49 | testfiles = [] |
|---|
| 50 | testfile_p = re.compile(r'^[Tt]est') |
|---|
| 51 | for f in os.listdir(testsroot): |
|---|
| 52 | if testfile_p.search(f): |
|---|
| 53 | testfiles.append(f) |
|---|
| 54 | if len(testfiles) == 0: |
|---|
| 55 | raise IOError('Serious error') |
|---|
| 56 | # Copy the test files over |
|---|
| 57 | print 'Copying test files....' |
|---|
| 58 | for file in testfiles: |
|---|
| 59 | orgtest = os.sep.join([testsroot, file]) |
|---|
| 60 | print 'Copying %s to %s' % (orgtest, bdroot) |
|---|
| 61 | os.system('cp %s %s' % (os.sep.join([testsroot, file]), bdroot)) |
|---|
| 62 | |
|---|
| 63 | print 'Running tests...' |
|---|
| 64 | os.chdir(bdroot) |
|---|
| 65 | os.system('nosetests') |
|---|
| 66 | |
|---|
| 67 | print 'Cleaning up...' |
|---|
| 68 | for f in testfiles: |
|---|
| 69 | os.remove(f) |
|---|
| 70 | pycfile = ''.join([f, 'c']) |
|---|
| 71 | if os.path.exists(pycfile): |
|---|
| 72 | os.remove(pycfile) |
|---|
| 73 | |
|---|