aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/urfave/cli.v1/runtests
blob: ee22bdeed5581b8a001f50a4cab9f0663a53518e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
from __future__ import print_function

import argparse
import os
import sys
import tempfile

from subprocess import check_call, check_output


PACKAGE_NAME = os.environ.get(
    'CLI_PACKAGE_NAME', 'github.com/urfave/cli'
)


def main(sysargs=sys.argv[:]):
    targets = {
        'vet': _vet,
        'test': _test,
        'gfmrun': _gfmrun,
        'toc': _toc,
        'gen': _gen,
    }

    parser = argparse.ArgumentParser()
    parser.add_argument(
        'target', nargs='?', choices=tuple(targets.keys()), default='test'
    )
    args = parser.parse_args(sysargs[1:])

    targets[args.target]()
    return 0


def _test():
    if check_output('go version'.split()).split()[2] < 'go1.2':
        _run('go test -v .')
        return

    coverprofiles = []
    for subpackage in ['', 'altsrc']:
        coverprofile = 'cli.coverprofile'
        if subpackage != '':
            coverprofile = '{}.coverprofile'.format(subpackage)

        coverprofiles.append(coverprofile)

        _run('go test -v'.split() + [
            '-coverprofile={}'.format(coverprofile),
            ('{}/{}'.format(PACKAGE_NAME, subpackage)).rstrip('/')
        ])

    combined_name = _combine_coverprofiles(coverprofiles)
    _run('go tool cover -func={}'.format(combined_name))
    os.remove(combined_name)


def _gfmrun():
    go_version = check_output('go version'.split()).split()[2]
    if go_version < 'go1.3':
        print('runtests: skip on {}'.format(go_version), file=sys.stderr)
        return
    _run(['gfmrun', '-c', str(_gfmrun_count()), '-s', 'README.md'])


def _vet():
    _run('go vet ./...')


def _toc():
    _run('node_modules/.bin/markdown-toc -i README.md')
    _run('git diff --exit-code')


def _gen():
    go_version = check_output('go version'.split()).split()[2]
    if go_version < 'go1.5':
        print('runtests: skip on {}'.format(go_version), file=sys.stderr)
        return

    _run('go generate ./...')
    _run('git diff --exit-code')


def _run(command):
    if hasattr(command, 'split'):
        command = command.split()
    print('runtests: {}'.format(' '.join(command)), file=sys.stderr)
    check_call(command)


def _gfmrun_count():
    with open('README.md') as infile:
        lines = infile.read().splitlines()
        return len(filter(_is_go_runnable, lines))


def _is_go_runnable(line):
    return line.startswith('package main')


def _combine_coverprofiles(coverprofiles):
    combined = tempfile.NamedTemporaryFile(
        suffix='.coverprofile', delete=False
    )
    combined.write('mode: set\n')

    for coverprofile in coverprofiles:
        with open(coverprofile, 'r') as infile:
            for line in infile.readlines():
                if not line.startswith('mode: '):
                    combined.write(line)

    combined.flush()
    name = combined.name
    combined.close()
    return name


if __name__ == '__main__':
    sys.exit(main())