behat.py 3.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# coding: utf-8
#
# Copyright (C) 2016 Savoir-faire Linux Inc. (<www.savoirfairelinux.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
from fabric.decorators import task

20
from ..core import *
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


@task
def init(rw=True):
    """
    Check installation and configure Behat.
    :param rw If the behat.yml file should be rewrited or not.
    """
    service = env.services['php']
    workspace = env.code_workspace
    host = env.services['web_server']
    site_root = env.code_site_root

    if not os.path.exists('{}/tests/behat/behat.yml'.format(workspace)) or rw:
        dk_run(service, cmd='cd {}/tests/behat && cp example.behat.yml '
                            'behat.yml'.format(workspace))
        dk_run(service, cmd="cd {}/tests/behat && sed -i 's@%DRUPAL_ROOT@{"
                            "}@g' behat.yml".format(workspace, site_root))
        dk_run(service, cmd="cd {}/tests/behat && sed -i 's@%URL@http://{"
                            "}@g' behat.yml".format(workspace, host))
        print(green('Behat is now properly configured. The configuration '
                    'file is {}/tests/behat/behat.yml'.format(workspace)))
    else:
        print(green('{}/tests/behat/behat.yml is already created.'
                    ''.format(workspace)))


@task
def install():
    """
    Install behat
    """
    service = env.services['php']
    workspace = env.code_workspace

    if not os.path.exists('{}/tests/behat/bin/behat'.format(env.workspace)):
        dk_run(service, user='root',
               cmd='chown -R {}:{} ../../tests'.format(env.apache_userid,
                                                       env.local_userid))
        dk_run(
            service,
            cmd='cd {}/tests/behat && composer install'.format(workspace))
        print(green('Behat has been properly installed with Composer in '
                    '{}/tests/behat/bin/behat'.format(workspace)))
    else:
        print(green('Behat is already installed!'))


@task
def run(pattern='~@wip&&~@disabled&&~@test'):
    """
    Execute the complete Behat tests suite.
    :param pattern: The tags to filter the tests
    """
    service = env.services['php']
    workspace = env.code_workspace

78
79
80
81
82
83
    dk_run(service, user='root',
           cmd='mkdir -p {}/logs/behat'.format(workspace))
    dk_run(service, user='root',
           cmd='chown -R {}:{} {}/logs/behat'.format(env.apache_userid,
                                                     env.local_userid,
                                                     workspace))
84
85
86
87
88
89
90
91
92
    if not os.path.exists('../tests/behat/bin/behat'):
        install()
    if not os.path.exists('{}/tests/behat/behat.yml'.format(workspace)):
        init()
    dk_run(
        service,
        r'cd {}/tests/behat && bin/behat --format junit --format pretty '
        r'--tags \"{}\" --colors'.format(workspace, pattern)
    )