runtests 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import sys
  6. import tempfile
  7. from subprocess import check_call, check_output
  8. PACKAGE_NAME = os.environ.get(
  9. 'CLI_PACKAGE_NAME', 'github.com/urfave/cli'
  10. )
  11. def main(sysargs=sys.argv[:]):
  12. targets = {
  13. 'vet': _vet,
  14. 'test': _test,
  15. 'gfmxr': _gfmxr,
  16. 'toc': _toc,
  17. }
  18. parser = argparse.ArgumentParser()
  19. parser.add_argument(
  20. 'target', nargs='?', choices=tuple(targets.keys()), default='test'
  21. )
  22. args = parser.parse_args(sysargs[1:])
  23. targets[args.target]()
  24. return 0
  25. def _test():
  26. if check_output('go version'.split()).split()[2] < 'go1.2':
  27. _run('go test -v .'.split())
  28. return
  29. coverprofiles = []
  30. for subpackage in ['', 'altsrc']:
  31. coverprofile = 'cli.coverprofile'
  32. if subpackage != '':
  33. coverprofile = '{}.coverprofile'.format(subpackage)
  34. coverprofiles.append(coverprofile)
  35. _run('go test -v'.split() + [
  36. '-coverprofile={}'.format(coverprofile),
  37. ('{}/{}'.format(PACKAGE_NAME, subpackage)).rstrip('/')
  38. ])
  39. combined_name = _combine_coverprofiles(coverprofiles)
  40. _run('go tool cover -func={}'.format(combined_name).split())
  41. os.remove(combined_name)
  42. def _gfmxr():
  43. _run(['gfmxr', '-c', str(_gfmxr_count()), '-s', 'README.md'])
  44. def _vet():
  45. _run('go vet ./...'.split())
  46. def _toc():
  47. _run(['node_modules/.bin/markdown-toc', '-i', 'README.md'])
  48. _run(['git', 'diff', '--quiet'])
  49. def _run(command):
  50. print('runtests: {}'.format(' '.join(command)), file=sys.stderr)
  51. check_call(command)
  52. def _gfmxr_count():
  53. with open('README.md') as infile:
  54. lines = infile.read().splitlines()
  55. return len(filter(_is_go_runnable, lines))
  56. def _is_go_runnable(line):
  57. return line.startswith('package main')
  58. def _combine_coverprofiles(coverprofiles):
  59. combined = tempfile.NamedTemporaryFile(
  60. suffix='.coverprofile', delete=False
  61. )
  62. combined.write('mode: set\n')
  63. for coverprofile in coverprofiles:
  64. with open(coverprofile, 'r') as infile:
  65. for line in infile.readlines():
  66. if not line.startswith('mode: '):
  67. combined.write(line)
  68. combined.flush()
  69. name = combined.name
  70. combined.close()
  71. return name
  72. if __name__ == '__main__':
  73. sys.exit(main())