Completed
Push — break-up-hooks-tests ( 90c82a )
by Michael
01:19
created

hook_shell_script_with_context()   B

Complexity

Conditions 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 28
rs 8.8571
1
# -*- coding: utf-8 -*-
2
3
import os
4
import pytest
5
import sys
6
import textwrap
7
8
from cookiecutter import hooks
9
10
11
@pytest.fixture
12
def hook_shell_script_with_context(tmpdir):
13
    script_dir = tmpdir
14
15
    if sys.platform.startswith('win'):
16
        post_gen_hook_file = script_dir / 'post_gen_project.bat'
17
        post_hook_content = textwrap.dedent(
18
            u"""\
19
            @echo off
20
21
            echo post generation hook
22
            echo. >{{cookiecutter.file}}
23
            """
24
        )
25
        post_gen_hook_file.write_text(post_hook_content, encoding='utf8')
26
    else:
27
        post_gen_hook_file = script_dir / 'post_gen_project.sh'
28
        post_hook_content = textwrap.dedent(
29
            u"""\
30
            #!/bin/bash
31
32
            echo 'post generation hook';
33
            touch '{{cookiecutter.file}}'
34
            """
35
        )
36
        post_gen_hook_file.write_text(post_hook_content, encoding='utf8')
37
38
    return str(post_gen_hook_file)
39
40
41
def test_run_script_with_context(hook_shell_script_with_context):
42
    """Execute a hook script, passing a context"""
43
44
    hooks.run_script_with_context(
45
        hook_shell_script_with_context,
46
        'tests',
47
        {
48
            'cookiecutter': {
49
                'file': 'context_post.txt'
50
            }
51
        })
52
    assert os.path.isfile('tests/context_post.txt')
53
    assert 'tests' not in os.getcwd()
54
55
    os.remove('tests/context_post.txt')
56