Passed
Push — master ( a7bde6...b29ecb )
by
unknown
01:08
created

test_determine_repo_dir_finds_subdirectories   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 19.72 %

Importance

Changes 0
Metric Value
wmc 7
eloc 49
dl 14
loc 71
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A template() 0 3 1
A test_local_repo_typo() 0 25 2
A test_should_find_existing_cookiecutter() 14 14 1
A cloned_cookiecutter_path() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
import io
3
import os
4
import pytest
5
6
from cookiecutter import repository, exceptions
7
8
9
@pytest.fixture
10
def template():
11
    return 'cookiecutter-pytest-plugin'
12
13
14
@pytest.fixture
15
def cloned_cookiecutter_path(user_config_data, template):
16
    cookiecutters_dir = user_config_data['cookiecutters_dir']
17
18
    cloned_template_path = os.path.join(cookiecutters_dir, template)
19
    if not os.path.exists(cloned_template_path):
20
        os.mkdir(cloned_template_path)  # might exist from other tests.
21
22
    subdir_template_path = os.path.join(cloned_template_path, 'my-dir')
23
    if not os.path.exists(subdir_template_path):
24
        os.mkdir(subdir_template_path)
25
    io.open(os.path.join(subdir_template_path, 'cookiecutter.json'), 'w')
26
27
    return subdir_template_path
28
29
30 View Code Duplication
def test_should_find_existing_cookiecutter(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
31
        template, user_config_data, cloned_cookiecutter_path):
32
33
    project_dir, cleanup = repository.determine_repo_dir(
34
        template,
35
        abbreviations={},
36
        clone_to_dir=user_config_data['cookiecutters_dir'],
37
        checkout=None,
38
        no_input=True,
39
        directory='my-dir',
40
    )
41
42
    assert cloned_cookiecutter_path == project_dir
43
    assert not cleanup
44
45
46
def test_local_repo_typo(template, user_config_data, cloned_cookiecutter_path):
47
    """An unknown local repository should raise a `RepositoryNotFound`
48
    exception.
49
    """
50
    with pytest.raises(exceptions.RepositoryNotFound) as err:
51
        repository.determine_repo_dir(
52
            template,
53
            abbreviations={},
54
            clone_to_dir=user_config_data['cookiecutters_dir'],
55
            checkout=None,
56
            no_input=True,
57
            directory='wrong-dir',
58
        )
59
60
    wrong_full_cookiecutter_path = os.path.join(
61
        os.path.dirname(cloned_cookiecutter_path),
62
        'wrong-dir'
63
    )
64
    assert str(err.value) == (
65
        'A valid repository for "{}" could not be found in the following '
66
        'locations:\n{}'.format(
67
            template,
68
            '\n'.join([
69
                os.path.join(template, 'wrong-dir'),
70
                wrong_full_cookiecutter_path
71
            ]),
72
        )
73
    )
74