behat.py 2.64 KB
Newer Older
1
from __future__ import unicode_literals
2

3 4
from fabric.api import task, roles, env
from fabric.colors import green
5 6 7

import helpers as h

8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
@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))
27
            h.fab_run(role, 'echo "127.0.0.1 {}" | sudo tee -a /etc/hosts'.format(host))
28

29 30
        print green('Behat is now properly configured. The configuration file is {}/tests/behat/behat.yml'.format(workspace))
    else:
31
        print green('{}/tests/behat/behat.yml is already created.'.format(workspace))
32 33


34
@task
35 36 37 38 39 40 41 42 43 44 45 46 47
@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')
48

49 50 51 52 53 54 55
        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')
56
def run(tags='~@wip&&~@disabled&&~@test'):
57 58 59 60 61 62 63 64 65 66 67
    """
    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()
68

69 70 71
    # 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()
72

73
    with h.fab_cd(role, '{}/tests/behat'.format(workspace)):
74
        h.fab_run(role, 'behat --format junit --format pretty --tags "{}" --colors'.format(tags))