You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.9 KiB

  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. 'gfmrun': _gfmrun,
  16. 'toc': _toc,
  17. 'gen': _gen,
  18. }
  19. parser = argparse.ArgumentParser()
  20. parser.add_argument(
  21. 'target', nargs='?', choices=tuple(targets.keys()), default='test'
  22. )
  23. args = parser.parse_args(sysargs[1:])
  24. targets[args.target]()
  25. return 0
  26. def _test():
  27. if check_output('go version'.split()).split()[2] < 'go1.2':
  28. _run('go test -v .')
  29. return
  30. coverprofiles = []
  31. for subpackage in ['', 'altsrc']:
  32. coverprofile = 'cli.coverprofile'
  33. if subpackage != '':
  34. coverprofile = '{}.coverprofile'.format(subpackage)
  35. coverprofiles.append(coverprofile)
  36. _run('go test -v'.split() + [
  37. '-coverprofile={}'.format(coverprofile),
  38. ('{}/{}'.format(PACKAGE_NAME, subpackage)).rstrip('/')
  39. ])
  40. combined_name = _combine_coverprofiles(coverprofiles)
  41. _run('go tool cover -func={}'.format(combined_name))
  42. os.remove(combined_name)
  43. def _gfmrun():
  44. go_version = check_output('go version'.split()).split()[2]
  45. if go_version < 'go1.3':
  46. print('runtests: skip on {}'.format(go_version), file=sys.stderr)
  47. return
  48. _run(['gfmrun', '-c', str(_gfmrun_count()), '-s', 'README.md'])
  49. def _vet():
  50. _run('go vet ./...')
  51. def _toc():
  52. _run('node_modules/.bin/markdown-toc -i README.md')
  53. _run('git diff --exit-code')
  54. def _gen():
  55. go_version = check_output('go version'.split()).split()[2]
  56. if go_version < 'go1.5':
  57. print('runtests: skip on {}'.format(go_version), file=sys.stderr)
  58. return
  59. _run('go generate ./...')
  60. _run('git diff --exit-code')
  61. def _run(command):
  62. if hasattr(command, 'split'):
  63. command = command.split()
  64. print('runtests: {}'.format(' '.join(command)), file=sys.stderr)
  65. check_call(command)
  66. def _gfmrun_count():
  67. with open('README.md') as infile:
  68. lines = infile.read().splitlines()
  69. return len(filter(_is_go_runnable, lines))
  70. def _is_go_runnable(line):
  71. return line.startswith('package main')
  72. def _combine_coverprofiles(coverprofiles):
  73. combined = tempfile.NamedTemporaryFile(
  74. suffix='.coverprofile', delete=False
  75. )
  76. combined.write('mode: set\n')
  77. for coverprofile in coverprofiles:
  78. with open(coverprofile, 'r') as infile:
  79. for line in infile.readlines():
  80. if not line.startswith('mode: '):
  81. combined.write(line)
  82. combined.flush()
  83. name = combined.name
  84. combined.close()
  85. return name
  86. if __name__ == '__main__':
  87. sys.exit(main())