runtests

  1#!/usr/bin/env python
  2from __future__ import print_function
  3
  4import argparse
  5import os
  6import sys
  7import tempfile
  8
  9from subprocess import check_call, check_output
 10
 11
 12PACKAGE_NAME = os.environ.get(
 13    'CLI_PACKAGE_NAME', 'github.com/urfave/cli'
 14)
 15
 16
 17def main(sysargs=sys.argv[:]):
 18    targets = {
 19        'vet': _vet,
 20        'test': _test,
 21        'gfmrun': _gfmrun,
 22        'toc': _toc,
 23        'gen': _gen,
 24    }
 25
 26    parser = argparse.ArgumentParser()
 27    parser.add_argument(
 28        'target', nargs='?', choices=tuple(targets.keys()), default='test'
 29    )
 30    args = parser.parse_args(sysargs[1:])
 31
 32    targets[args.target]()
 33    return 0
 34
 35
 36def _test():
 37    if check_output('go version'.split()).split()[2] < 'go1.2':
 38        _run('go test -v .')
 39        return
 40
 41    coverprofiles = []
 42    for subpackage in ['', 'altsrc']:
 43        coverprofile = 'cli.coverprofile'
 44        if subpackage != '':
 45            coverprofile = '{}.coverprofile'.format(subpackage)
 46
 47        coverprofiles.append(coverprofile)
 48
 49        _run('go test -v'.split() + [
 50            '-coverprofile={}'.format(coverprofile),
 51            ('{}/{}'.format(PACKAGE_NAME, subpackage)).rstrip('/')
 52        ])
 53
 54    combined_name = _combine_coverprofiles(coverprofiles)
 55    _run('go tool cover -func={}'.format(combined_name))
 56    os.remove(combined_name)
 57
 58
 59def _gfmrun():
 60    go_version = check_output('go version'.split()).split()[2]
 61    if go_version < 'go1.3':
 62        print('runtests: skip on {}'.format(go_version), file=sys.stderr)
 63        return
 64    _run(['gfmrun', '-c', str(_gfmrun_count()), '-s', 'README.md'])
 65
 66
 67def _vet():
 68    _run('go vet ./...')
 69
 70
 71def _toc():
 72    _run('node_modules/.bin/markdown-toc -i README.md')
 73    _run('git diff --exit-code')
 74
 75
 76def _gen():
 77    go_version = check_output('go version'.split()).split()[2]
 78    if go_version < 'go1.5':
 79        print('runtests: skip on {}'.format(go_version), file=sys.stderr)
 80        return
 81
 82    _run('go generate ./...')
 83    _run('git diff --exit-code')
 84
 85
 86def _run(command):
 87    if hasattr(command, 'split'):
 88        command = command.split()
 89    print('runtests: {}'.format(' '.join(command)), file=sys.stderr)
 90    check_call(command)
 91
 92
 93def _gfmrun_count():
 94    with open('README.md') as infile:
 95        lines = infile.read().splitlines()
 96        return len(filter(_is_go_runnable, lines))
 97
 98
 99def _is_go_runnable(line):
100    return line.startswith('package main')
101
102
103def _combine_coverprofiles(coverprofiles):
104    combined = tempfile.NamedTemporaryFile(
105        suffix='.coverprofile', delete=False
106    )
107    combined.write('mode: set\n')
108
109    for coverprofile in coverprofiles:
110        with open(coverprofile, 'r') as infile:
111            for line in infile.readlines():
112                if not line.startswith('mode: '):
113                    combined.write(line)
114
115    combined.flush()
116    name = combined.name
117    combined.close()
118    return name
119
120
121if __name__ == '__main__':
122    sys.exit(main())