Issues (17)

tests/__init__.py (1 issue)

1
"""
2
Common modules, functions, pytest fixtures and contextmanager are placed
3
in this module that can be used by submodules.
4
"""
5
import json
6
import os
7
import shlex
8
import subprocess
9
from contextlib import contextmanager
10
11
12
@contextmanager
13
def bake_cookie(cookies, *args, **kwargs):
14
    """
15
    Bake the cookiecutter project with some test context.
16
17
    :param cookies: pytest_cookies.Cookies
18
    :param (): All other positional args will be passed to cookies.bake call.
19
    :param (kwargs): All keyword arguments will be passed to cookies.bake call,
20
        Caller can override or add additional context by passing in extra_context
21
            kwarg.
22
    """
23
    extra_context = {
24
        "project_slug": "baked_cookie",
25
        "project_author_name": "Cookie Baker",
26
        "project_author_email": "[email protected]",
27
    }
28
29
    if "extra_context" not in kwargs:
30
        kwargs["extra_context"] = extra_context
31
    elif not isinstance(kwargs["extra_context"], dict):
32
        raise TypeError('extra_context has to be a type of "dict"')
33
    else:
34
        kwargs["extra_context"].update(extra_context)
35
36
    result = cookies.bake(*args, **kwargs)
37
38
    try:
39
        yield result
40
    finally:
41
        print(result)
42
43
44
@contextmanager
45
def in_dir(dir_path):
46
    """
47
    Switch context of the directory to execute code within that directory and
48
    switch out to the caller's current directory upon completion.
49
50
    :param dir_path: The path to chdir into and yield
51
    """
52
    current_path = os.getcwd()
53
    try:
54
        os.chdir(dir_path)
55
        yield
56
    finally:
57
        os.chdir(current_path)
58
59
60
def subprocess_in_dir(command, dirpath=None):
61
    """
62
    Run a command via subprocess from inside a given directory and checking
63
    the return code. Will raise an exception if the exit code is not zero.
64
    :param command: Command that will be passed to subprocess.check_call. The
65
        command will be passed on to subprocess.check_call after being processed
66
        by shlex.split.
67
    :param dirpath: String, path of the directory the command is being run.
68
        Default: None, if None, the current directory will be used.
69
    """
70
    if dirpath is None:
71
        dirpath = os.getcwd()
72
73
    subprocess.check_call(shlex.split(command), cwd=dirpath)
74
75
76
def load_cookiecutter_json():
77
    """
78
    Load the cookiecutter.json file and return it as dict
79
    """
80
    cookiecutter_json_file_path = os.path.join(
81
        os.path.dirname(__file__), os.pardir, "cookiecutter.json"
82
    )
83
    with open(cookiecutter_json_file_path) as fp:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "fp" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
84
        data = json.load(fp)
85
86
    return data
87