[hgext] add a new "start-test" command (closes #290804)

authorDavid Douard <david.douard@logilab.fr>
changeset0ee0cb4434fe
branchdefault
phasedraft
hiddenyes
parent revision#037d295ccc1a [hgext] use **only** cwclientlib
child revision#fd29085a3944 [hgext] make show-review able to display apycot test results (closes #291045)
files modified by this revision
hgext/jpl/__init__.py
hgext/jpl/apycot.py
# HG changeset patch
# User David Douard <david.douard@logilab.fr>
# Date 1428621492 -7200
# Fri Apr 10 01:18:12 2015 +0200
# Node ID 0ee0cb4434fead5b741a147c97071c05ba2ad5d6
# Parent 037d295ccc1af7feb92ace89dcbb6c7e2693acd7
[hgext] add a new "start-test" command (closes #290804)

diff --git a/hgext/jpl/__init__.py b/hgext/jpl/__init__.py
@@ -18,11 +18,11 @@
1    endpoint = https://www.cubicweb.org/
2 
3  or, according cwo is the id of the endpoint in your cwclientlib_ config file::
4 
5    [jpl]
6 -  endpoint = cwo 
7 +  endpoint = cwo
8 
9  You may need `python-ndg-httpsclient`_ and `python-openssl`_ if
10  the forge application is using a SNI_ ssl configuration (ie. if you
11  get errors like::
12 
@@ -47,10 +47,11 @@
13      enabled = demandimport._import is __import__
14  demandimport.disable()
15  from .jplproxy import build_proxy
16  from .tasks import print_tasks
17  from .review import ask_review, show_review, sudo_make_me_a_ticket, assign
18 +from .apycot import create_test_execution
19  if enabled:
20      demandimport.enable()
21 
22  cmdtable = {}
23  command = cmdutil.command(cmdtable)
@@ -353,5 +354,43 @@
24 
25      with build_proxy(ui, opts) as client:
26          for rev in revs:
27              ticket = sudo_make_me_a_ticket(client, repo, rev, opts.get('done_in', ''))
28              ui.write("{0} {1}\n".format(rev, ticket[0][0]))
29 +
30 +
31 +@command('^start-test', [
32 +    ('r', 'rev', [], _('start apycot tests on the given revision(s)'), _('REV')),
33 +    ('l', 'label', 'quick python tests', _("the TestConfig's label to execute"), _('LABEL')),
34 +    ('o', 'option', [], _("options to add to the TestExecution"), _('OPTIONS')),
35 +    ]  + cnxopts,
36 +    _('[OPTION]... [-r] REV...'))
37 +def runapycot(ui, repo, *changesets, **opts):
38 +    """start Apycot tests for the given revisions.
39 +
40 +    By default, the revision used is the parent of the working
41 +    directory: use -r/--rev to specify a different revision.
42 +
43 +    """
44 +    changesets += tuple(opts.get('rev', []))
45 +    if not changesets:
46 +        changesets = ('.')
47 +    revs = scmutil.revrange(repo, changesets)
48 +    if not revs:
49 +        raise util.Abort(_('no working directory: please specify a revision'))
50 +    ctxhexs = [node.short(repo.lookup(rev)) for rev in revs]
51 +    label = opts.get('label', 'quick python tests')
52 +    options = dict(o.split('=') for o in opts.get('option', []))
53 +
54 +    with build_proxy(ui, opts) as client:
55 +        results = create_test_execution(client, ctxhexs, label, **options)
56 +        rset = [x[0][0] if x else None for x in results]
57 +        if all(rset):
58 +            ui.write('OK ({})\n'.format(', '.join(str(eid) for eid in rset)))
59 +        else:
60 +            if any(rset):
61 +                ui.write('PARTIAL ({})\n'.format(', '.join(str(eid)
62 +                                                  for eid in rset if eid)))
63 +            else:
64 +                ui.write('FAILED\n')
65 +            failed = (cset for cset, result in zip(ctxhexs, rset) if not result)
66 +            ui.write('  could not create tests for {}\n'.format(', '.join(failed)))
diff --git a/hgext/jpl/apycot.py b/hgext/jpl/apycot.py
@@ -0,0 +1,29 @@
67 +#!/usr/bin/python
68 +# -*- coding: utf-8
69 +
70 +START_TE_RQL = """\
71 +INSERT TestExecution TE:
72 +  TE branch %(changeset)s,
73 +  TE using_environment PE,
74 +  TE using_config TC,
75 +  TE execution_of R
76 +WHERE
77 +  TC use_recipe R,
78 +  PE local_repository REPO,
79 +  CS from_repository REPO,
80 +  CS changeset %(changeset)s ,
81 +  TC label %(label)s
82 +"""
83 +
84 +def create_test_execution(client, changesets, label, **kwargs):
85 +    rql = START_TE_RQL
86 +    args = {'label': label}
87 +    if kwargs:
88 +        options = u'\n'.join(u"%s=%s" % kv for kv in kwargs.items())
89 +        rql1, rql2 = rql.split('WHERE')
90 +        rql = '{0}, TE options %(options)s WHERE {1}'.format(rql1, rql2)
91 +        args['options'] = options
92 +
93 +    queries = [(rql, dict(changeset=cs, **args))
94 +               for cs in changesets]
95 +    return client.rqlio(queries)