1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
from mock import patch |
4
|
|
|
import pytest |
5
|
|
|
import tempfile |
6
|
|
|
import os |
7
|
|
|
import re |
8
|
|
|
|
9
|
|
|
from git_app_version.__main__ import main as git_app_version_main |
10
|
|
|
import git_app_version.version |
11
|
|
|
from git_app_version.helper.pyversion import PY3 |
12
|
|
|
|
13
|
|
|
@pytest.fixture() |
14
|
|
|
def tmpdir(tmpdir_factory): |
15
|
|
|
cwd = os.getcwd() |
16
|
|
|
new_cwd = tmpdir_factory.mktemp('git_repo') |
17
|
|
|
new_cwd_path = str(new_cwd) |
18
|
|
|
os.chdir(new_cwd_path) |
19
|
|
|
yield new_cwd_path |
20
|
|
|
os.chdir(cwd) |
21
|
|
|
|
22
|
|
|
def _git_commit(message): |
23
|
|
|
os.system('git commit --allow-empty -m "{}"'.format(message)) |
24
|
|
|
|
25
|
|
|
def _git_tag(version): |
26
|
|
|
_git_commit("release: {}".format(version)) |
27
|
|
|
os.system('git tag -am {0} {0}'.format(version)) |
28
|
|
|
|
29
|
|
|
def _git_init(version='0.1.2'): |
30
|
|
|
os.system('git init') |
31
|
|
|
os.system('git config user.email "[email protected]"') |
32
|
|
|
os.system('git config user.name "User Test"') |
33
|
|
|
|
34
|
|
|
_git_commit('initial commit') |
35
|
|
|
_git_tag(version) |
36
|
|
|
|
37
|
|
|
def test_version(capsys): |
38
|
|
|
arg = ['-V'] |
39
|
|
|
with pytest.raises(SystemExit): |
40
|
|
|
git_app_version_main((arg)) |
41
|
|
|
|
42
|
|
|
out, err = capsys.readouterr() |
43
|
|
|
std_to_test = out if PY3 else err |
44
|
|
|
assert std_to_test == 'git-app-version '+git_app_version.version.__version__+"\n" |
45
|
|
|
|
46
|
|
|
def test_not_git_repository(tmpdir, capsys): |
47
|
|
|
arg = [] |
48
|
|
|
assert git_app_version_main((arg)) == 1 |
49
|
|
|
out, err = capsys.readouterr() |
50
|
|
|
assert err == "" |
51
|
|
|
assert out == "Error Writing version config file : The directory '{}' is not a git repository.\n".format(tmpdir) |
52
|
|
|
|
53
|
|
|
def test_quiet(tmpdir, capsys): |
54
|
|
|
arg = ['-q'] |
55
|
|
|
|
56
|
|
|
_git_init() |
57
|
|
|
capsys.readouterr() |
58
|
|
|
|
59
|
|
|
exit_code = git_app_version_main((arg)) |
60
|
|
|
output_path = os.path.join(tmpdir, 'version.json') |
61
|
|
|
|
62
|
|
|
out, err = capsys.readouterr() |
63
|
|
|
assert err == '' |
64
|
|
|
assert out == '' |
65
|
|
|
assert exit_code == 0 |
66
|
|
|
|
67
|
|
|
assert os.path.exists(output_path) |
68
|
|
|
|
69
|
|
|
def test_verbose(tmpdir, capsys): |
70
|
|
|
arg = ['-v'] |
71
|
|
|
|
72
|
|
|
_git_init() |
73
|
|
|
capsys.readouterr() |
74
|
|
|
|
75
|
|
|
exit_code = git_app_version_main((arg)) |
76
|
|
|
output_path = os.path.join(tmpdir, 'version.json') |
77
|
|
|
|
78
|
|
|
out, err = capsys.readouterr() |
79
|
|
|
assert err == '' |
80
|
|
|
assert out.find('Git commit :') != -1 |
81
|
|
|
assert out.find('version = 0.1.2') != -1 |
82
|
|
|
assert re.search("Git commit informations stored in {}\n".format(output_path), out) |
83
|
|
|
|
84
|
|
|
assert os.path.exists(output_path) |
85
|
|
|
assert exit_code == 0 |
86
|
|
|
|
87
|
|
|
def test_json(tmpdir, capsys): |
88
|
|
|
arg = [] |
89
|
|
|
|
90
|
|
|
_git_init() |
91
|
|
|
capsys.readouterr() |
92
|
|
|
|
93
|
|
|
exit_code = git_app_version_main((arg)) |
94
|
|
|
output_path = os.path.join(tmpdir, 'version.json') |
95
|
|
|
|
96
|
|
|
out, err = capsys.readouterr() |
97
|
|
|
assert err == '' |
98
|
|
|
assert out == "Git commit informations stored in {}\n".format(output_path) |
99
|
|
|
|
100
|
|
|
assert os.path.exists(output_path) |
101
|
|
|
assert exit_code == 0 |
102
|
|
|
|