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

hook_shell_script()   B

Complexity

Conditions 2

Size

Total Lines 29

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 29
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(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. >shell_post.txt
23
            """
24
        )
25
        post_gen_hook_file.write_text(post_hook_content, encoding='utf8')
26
27
    else:
28
        post_gen_hook_file = script_dir / 'post_gen_project.sh'
29
        post_hook_content = textwrap.dedent(
30
            u"""\
31
            #!/bin/bash
32
33
            echo 'post generation hook';
34
            touch 'shell_post.txt'
35
            """
36
        )
37
        post_gen_hook_file.write_text(post_hook_content, encoding='utf8')
38
39
    return str(post_gen_hook_file)
40
41
42
def test_run_script(hook_shell_script):
43
    """Execute a hook script, independently of project generation"""
44
    hooks.run_script(
45
        os.path.join(hook_shell_script)
46
    )
47
    assert os.path.isfile('shell_post.txt')
48
49
    os.remove('shell_post.txt')
50
51
52
def test_run_script_cwd(hook_shell_script):
53
    """Change directory before running hook"""
54
    hooks.run_script(
55
        hook_shell_script,
56
        'tests'
57
    )
58
    assert os.path.isfile('tests/shell_post.txt')
59
    assert 'tests' not in os.getcwd()
60
61
    os.remove('tests/shell_post.txt')
62