1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
import os |
4
|
|
|
import re |
5
|
|
|
|
6
|
|
|
import pytest |
7
|
|
|
|
8
|
|
|
import git_app_version.version |
9
|
|
|
from git_app_version.__main__ import main as git_app_version_main |
10
|
|
|
from test_helpers import git_utils |
11
|
|
|
|
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
|
|
|
|
23
|
|
|
def test_version(capsys): |
24
|
|
|
arg = ['-V'] |
25
|
|
|
with pytest.raises(SystemExit): |
26
|
|
|
git_app_version_main((arg)) |
27
|
|
|
|
28
|
|
|
out, err = capsys.readouterr() |
29
|
|
|
expected = 'git-app-version ' + git_app_version.version.__version__ + "\n" |
30
|
|
|
assert out == expected or err == expected |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def test_not_git_repository(tmpdir, capsys): |
34
|
|
|
arg = [] |
35
|
|
|
assert git_app_version_main((arg)) == 1 |
36
|
|
|
out, err = capsys.readouterr() |
37
|
|
|
assert err == "" |
38
|
|
|
expected = ("Error Writing version config file :" |
39
|
|
|
" The directory '{}' is not a git repository.\n") |
40
|
|
|
assert out == expected.format(tmpdir) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def test_quiet(tmpdir, capsys): |
44
|
|
|
arg = ['-q'] |
45
|
|
|
|
46
|
|
|
git_utils.default_init() |
47
|
|
|
capsys.readouterr() |
48
|
|
|
|
49
|
|
|
exit_code = git_app_version_main((arg)) |
50
|
|
|
output_path = os.path.join(tmpdir, 'version.json') |
51
|
|
|
|
52
|
|
|
out, err = capsys.readouterr() |
53
|
|
|
assert err == '' |
54
|
|
|
assert out == '' |
55
|
|
|
assert exit_code == 0 |
56
|
|
|
|
57
|
|
|
assert os.path.exists(output_path) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def test_verbose(tmpdir, capsys): |
61
|
|
|
arg = ['-v'] |
62
|
|
|
|
63
|
|
|
git_utils.default_init() |
64
|
|
|
capsys.readouterr() |
65
|
|
|
|
66
|
|
|
exit_code = git_app_version_main((arg)) |
67
|
|
|
output_path = os.path.join(tmpdir, 'version.json') |
68
|
|
|
|
69
|
|
|
out, err = capsys.readouterr() |
70
|
|
|
assert err == '' |
71
|
|
|
assert out.find('Git commit :') != -1 |
72
|
|
|
assert out.find('version = 0.1.2') != -1 |
73
|
|
|
assert re.search( |
74
|
|
|
"Git commit informations stored in {}\n".format(output_path), out) |
75
|
|
|
|
76
|
|
|
assert os.path.exists(output_path) |
77
|
|
|
assert exit_code == 0 |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def test_json(tmpdir, capsys): |
81
|
|
|
arg = [] |
82
|
|
|
|
83
|
|
|
git_utils.default_init() |
84
|
|
|
capsys.readouterr() |
85
|
|
|
|
86
|
|
|
exit_code = git_app_version_main((arg)) |
87
|
|
|
output_path = os.path.join(tmpdir, 'version.json') |
88
|
|
|
|
89
|
|
|
out, err = capsys.readouterr() |
90
|
|
|
assert err == '' |
91
|
|
|
assert out == "Git commit informations stored in {}\n".format(output_path) |
92
|
|
|
|
93
|
|
|
assert os.path.exists(output_path) |
94
|
|
|
assert exit_code == 0 |
95
|
|
|
|