runtests 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. }
  17. parser = argparse.ArgumentParser()
  18. parser.add_argument(
  19. 'target', nargs='?', choices=tuple(targets.keys()), default='test'
  20. )
  21. args = parser.parse_args(sysargs[1:])
  22. targets[args.target]()
  23. return 0
  24. def _test():
  25. if check_output('go version'.split()).split()[2] < 'go1.2':
  26. _run('go test -v .'.split())
  27. return
  28. coverprofiles = []
  29. for subpackage in ['', 'altsrc']:
  30. coverprofile = 'cli.coverprofile'
  31. if subpackage != '':
  32. coverprofile = '{}.coverprofile'.format(subpackage)
  33. coverprofiles.append(coverprofile)
  34. _run('go test -v'.split() + [
  35. '-coverprofile={}'.format(coverprofile),
  36. ('{}/{}'.format(PACKAGE_NAME, subpackage)).rstrip('/')
  37. ])
  38. combined_name = _combine_coverprofiles(coverprofiles)
  39. _run('go tool cover -func={}'.format(combined_name).split())
  40. os.remove(combined_name)
  41. def _gfmxr():
  42. _run(['gfmxr', '-c', str(_gfmxr_count()), '-s', 'README.md'])
  43. def _vet():
  44. _run('go vet ./...'.split())
  45. def _run(command):
  46. print('runtests: {}'.format(' '.join(command)), file=sys.stderr)
  47. check_call(command)
  48. def _gfmxr_count():
  49. with open('README.md') as infile:
  50. lines = infile.read().splitlines()
  51. return len(filter(_is_go_runnable, lines))
  52. def _is_go_runnable(line):
  53. return line.startswith('package main')
  54. def _combine_coverprofiles(coverprofiles):
  55. combined = tempfile.NamedTemporaryFile(
  56. suffix='.coverprofile', delete=False
  57. )
  58. combined.write('mode: set\n')
  59. for coverprofile in coverprofiles:
  60. with open(coverprofile, 'r') as infile:
  61. for line in infile.readlines():
  62. if not line.startswith('mode: '):
  63. combined.write(line)
  64. combined.flush()
  65. name = combined.name
  66. combined.close()
  67. return name
  68. if __name__ == '__main__':
  69. sys.exit(main())