behat.py 2.81 KB
Newer Older
1
from __future__ import unicode_literals
2
3
from fabric.api import task, roles, env
from fabric.colors import green
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

import helpers as h

@task
@roles('docker')
def init(rewrite=True):
    """
    Check installation and configure Behat.
    :param role Default 'role' where to run the task
    :param rewrite If the behat.yml file should be rewrited or not.
    """
    
    role = 'docker'
    workspace = env.docker_workspace
    host = env.site_hostname
    site_root = env.docker_site_root

    if not h.fab_exists(role, '{}/tests/behat/behat.yml'.format(workspace)) or rewrite:
        with h.fab_cd(role, '{}/tests/behat'.format(workspace)):
            h.fab_run(role, 'cp example.behat.yml behat.yml')
            h.fab_run(role, 'sed -i "s@%DRUPAL_ROOT@{}@g" behat.yml'.format(site_root))
            h.fab_run(role, 'sed -i "s@%URL@http://{}@g" behat.yml'.format(host))
            h.fab_run(role, 'echo "127.0.0.1  {}" >> /etc/hosts'.format(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()
@roles('docker')
def install():
    """
    Install behat
    """

    role = 'docker'
    workspace = env.docker_workspace

    if not h.fab_exists(role, '/usr/local/bin/behat'):
        with h.fab_cd(role, '{}/tests/behat'.format(workspace)):
            h.fab_run(role, 'curl -s https://getcomposer.org/installer | php')
            h.fab_run(role, 'php composer.phar install')
            h.fab_run(role, 'ln -s bin/behat /usr/local/bin/behat')
        print green('Behat has been properly installed with Composer in /usr/local/bin')
    else:
        print(green('Behat is already installed, no need for a new installation'))


@task
@roles('docker')
def run():
    """
    Execute the complete Behat tests suite.
    :param role Default 'role' where to run the task
    """

    role = 'docker'
    workspace = env.docker_workspace

    h.fab_run(role, 'mkdir -p {}/logs/behat'.format(workspace))
    # In the container behat is installed globaly, so check before install it inside the tests directory
    if not h.fab_exists(role, '/usr/local/bin/behat') or not h.fab_exists(role, '../tests/behat/bin/behat'):
        install()
    # If the configuration file behat.yml doesn't exist, call behat_init before run the test.
    if not h.fab_exists(role, '{}/tests/behat/behat.yml'.format(workspace)):
        init()
    with h.fab_cd(role, '{}/tests/behat'.format(workspace)):
        h.fab_run(role, 'behat --format junit --format pretty --tags "~@wip&&~@disabled&&~@test" --colors')
        # To run behat with only one test for example, comment previous line
        # and uncomment next one
        # fab_run(role, 'behat --format pretty --tags "~@wip&&~@disabled&&@yourTest" --colors')