Completed
Push — master ( ff5eb4...4f4209 )
by Charles
02:36
created

test_all()   F

Complexity

Conditions 17

Size

Total Lines 26

Duplication

Lines 16
Ratio 61.54 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
c 1
b 0
f 0
dl 16
loc 26
rs 2.7204

How to fix   Complexity   

Complexity

Complex classes like test_all() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
3
import os
4
import re
5
6
import pytest
7
from click.testing import CliRunner
8
9
import git_app_version.version
10
from git_app_version.__main__ import dump as git_app_version_main
11
from test_helpers import git_utils
12
13
14
@pytest.fixture()
15
def tmpdir(tmpdir_factory):
16
    cwd = os.getcwd()
17
    new_cwd = tmpdir_factory.mktemp('empty')
18
    new_cwd_path = str(new_cwd)
19
    os.chdir(new_cwd_path)
20
    yield new_cwd_path
21
    os.chdir(cwd)
22
23
24
@pytest.fixture()
25
def git_repo(tmpdir_factory):
26
    cwd = os.getcwd()
27
    new_cwd = tmpdir_factory.mktemp('git_repo')
28
    new_cwd_path = str(new_cwd)
29
    os.chdir(new_cwd_path)
30
    repo = git_utils.init(repo_dir=new_cwd_path)
31
    git_utils.commit(repo, message='commit 1',)
32
    git_utils.tag(repo, version='0.1.2',)
33
    yield repo
34
    os.chdir(cwd)
35
36
37
def test_version():
38
    runner = CliRunner()
39
40
    arg = ['--version']
41
    expected = 'git-app-version ' + git_app_version.version.__version__ + "\n"
42
43
    result = runner.invoke(git_app_version_main, arg)
44
    assert result.exit_code == 0
45
    assert result.output == expected
46
47
48
def test_not_git_repository(tmpdir):
49
    runner = CliRunner()
50
51
    arg = [tmpdir]
52
    expected = ("Error Writing version config file :"
53
                " The directory '{}' is not a git repository.\n")
54
55
    result = runner.invoke(git_app_version_main, arg)
56
    assert result.exit_code == 1
57
    assert result.output == expected.format(tmpdir)
58
59
60
def test_quiet(git_repo):
61
    runner = CliRunner()
62
63
    arg = ['-q', git_repo.working_tree_dir]
64
    output_path = os.path.join(git_repo.working_tree_dir, 'version.json')
65
66
    result = runner.invoke(git_app_version_main, arg)
67
68
    assert result.exit_code == 0
69
    assert os.path.exists(output_path)
70
71
72
def test_json(git_repo):
73 View Code Duplication
    runner = CliRunner()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
74
75
    arg = [git_repo.working_tree_dir]
76
    output_path = os.path.join(git_repo.working_tree_dir, 'version.json')
77
78
    result = runner.invoke(git_app_version_main, arg)
79
80
    assert result.output.find('Git commit :') != -1
81
    assert re.search(r"version\s+0.1.2", result.output)
82
    assert result.output.find('written to :') != -1
83
    assert result.output.find(output_path) != -1
84
85
    assert os.path.exists(output_path)
86
    assert result.exit_code == 0
87
88
def test_all(git_repo):
89 View Code Duplication
    runner = CliRunner()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
90
91
    arg = ['-f', 'all', git_repo.working_tree_dir]
92
    output_path = os.path.join(git_repo.working_tree_dir, 'version')
93
94
    result = runner.invoke(git_app_version_main, arg)
95
96
    assert result.output.find('Git commit :') != -1
97
    assert re.search(r"version\s+0.1.2", result.output)
98
    assert result.output.find('written to :') != -1
99
    assert result.output.find(output_path+'.json') != -1
100
    assert result.output.find(output_path+'.yml') != -1
101
    assert result.output.find(output_path+'.xml') != -1
102
    assert result.output.find(output_path+'.sh') != -1
103
    assert result.output.find(output_path+'.ini') != -1
104
    assert result.output.find(output_path+'.csv') != -1
105
106
    assert os.path.exists(output_path+'.json')
107
    assert os.path.exists(output_path+'.yml')
108
    assert os.path.exists(output_path+'.xml')
109
    assert os.path.exists(output_path+'.sh')
110
    assert os.path.exists(output_path+'.ini')
111
    assert os.path.exists(output_path+'.csv')
112
113
    assert result.exit_code == 0
114
115
def test_metadata(git_repo):
116
    runner = CliRunner()
117
118
    arg = ['-m', 'foo=bar', '-m', 'desc=custom', git_repo.working_tree_dir]
119
    output_path = os.path.join(git_repo.working_tree_dir, 'version.json')
120
121
    result = runner.invoke(git_app_version_main, arg)
122
123
    assert result.output.find('Git commit :') != -1
124
    assert re.search(r"version\s+0.1.2", result.output)
125
    assert re.search(r"foo\s+bar", result.output)
126
    assert re.search(r"desc\s+custom", result.output)
127
    assert result.output.find(output_path) != -1
128
129
    assert os.path.exists(output_path)
130
    assert result.exit_code == 0
131
132
133
def test_metadata_reserved_key(git_repo):
134
    runner = CliRunner()
135
136
    bad_key = 'deploy_date'
137
    arg = ['-m', bad_key + '=foo', git_repo.working_tree_dir]
138
139
    expected = (
140
        "Error: Invalid value for \"--meta\" / \"-m\": {} is a reserved key\n")
141
142
    result = runner.invoke(git_app_version_main, arg)
143
    assert result.exit_code == 2
144
    assert result.output.find(expected.format(bad_key)) != -1
145
146
147
def test_metadata_invalid_format(git_repo):
148
    runner = CliRunner()
149
150
    bad_key = 'foo'
151
    arg = ['-m', bad_key, git_repo.working_tree_dir]
152
153
    expected = "Error: Invalid value for \"--meta\" / \"-m\":"
154
    " {} is not a valid meta data string e.g. : <key>=<value>\n"
155
156
    result = runner.invoke(git_app_version_main, arg)
157
    assert result.exit_code == 2
158
    assert result.output.find(expected.format(bad_key)) != -1
159