Completed
Push — break-up-hooks-tests ( 75c50f...87fe28 )
by Michael
34s
created

shell_hook_content()   B

Complexity

Conditions 3

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate_hook() 0 31 2
1
# -*- coding: utf-8 -*-
2
3
import pytest
4
import sys
5
import textwrap
6
7
8
@pytest.fixture
9
def shell_hook_content():
10
    def generate_hook(project_phase):
11
        if sys.platform.startswith('win'):
12
            hook_extension = 'bat'
13
            hook_content = textwrap.dedent(
14
                u"""\
15
                @echo off
16
17
                echo {} hook
18
                echo. >shell_post.txt
19
                """.format(
20
                    project_phase.replace('_', ' '),
21
                )
22
            )
23
        else:
24
            hook_extension = 'sh'
25
            hook_content = textwrap.dedent(
26
                u"""\
27
                #!/bin/bash
28
29
                echo '{} hook';
30
                touch 'shell_post.txt'
31
                """.format(
32
                    project_phase.replace('_', ' '),
33
                )
34
            )
35
36
        hook_filename = '{}.{}'.format(
37
            project_phase,
38
            hook_extension
39
        )
40
        return hook_filename, hook_content
41
42
    return generate_hook
43
44
45
@pytest.fixture
46
def repo_dir_with_hooks(tmpdir, shell_hook_content):
47
    """Yield a cookiecutter directory with hook files."""
48
49
    repo_dir = tmpdir
50
    hooks_dir = repo_dir.mkdir('hooks')
51
52
    pre_hook_content = textwrap.dedent(
53
        u"""\
54
        #!/usr/bin/env python
55
        # -*- coding: utf-8 -*-
56
        from __future__ import print_function
57
58
        print('pre generation hook')
59
        f = open('python_pre.txt', 'w')
60
        f.close()
61
        """
62
    )
63
    pre_gen_hook_file = hooks_dir / 'pre_gen_project.py'
64
    pre_gen_hook_file.write_text(pre_hook_content, encoding='utf8')
65
66
    hook_filename, hook_content = shell_hook_content('post_gen_project')
67
    hook_filename = hooks_dir / hook_filename
68
    hook_filename.write_text(hook_content, encoding='utf8')
69
70
    yield str(repo_dir)
71
72
    repo_dir.remove()
73