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
|
|
|
@pytest.fixture() |
14
|
|
|
def tmpdir(tmpdir_factory): |
15
|
|
|
cwd = os.getcwd() |
16
|
|
|
new_cwd = tmpdir_factory.mktemp('empty') |
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
|
|
|
@pytest.fixture() |
24
|
|
|
def git_repo(tmpdir_factory): |
25
|
|
|
cwd = os.getcwd() |
26
|
|
|
new_cwd = tmpdir_factory.mktemp('git_repo') |
27
|
|
|
new_cwd_path = str(new_cwd) |
28
|
|
|
os.chdir(new_cwd_path) |
29
|
|
|
repo = git_utils.init(repo_dir=new_cwd_path) |
30
|
|
|
git_utils.commit(repo, message='commit 1',) |
31
|
|
|
git_utils.tag(repo, version='0.1.2',) |
32
|
|
|
yield repo |
33
|
|
|
os.chdir(cwd) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_version(): |
37
|
|
|
runner = CliRunner() |
38
|
|
|
|
39
|
|
|
arg = ['--version'] |
40
|
|
|
expected = 'git-app-version ' + git_app_version.version.__version__ + "\n" |
41
|
|
|
|
42
|
|
|
result = runner.invoke(git_app_version_main, arg) |
43
|
|
|
assert result.exit_code == 0 |
44
|
|
|
assert result.output == expected |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def test_not_git_repository(tmpdir): |
48
|
|
|
runner = CliRunner() |
49
|
|
|
|
50
|
|
|
arg = [tmpdir] |
51
|
|
|
expected = ("Error Writing version config file :" |
52
|
|
|
" The directory '{}' is not a git repository.\n") |
53
|
|
|
|
54
|
|
|
result = runner.invoke(git_app_version_main, arg) |
55
|
|
|
assert result.exit_code == 1 |
56
|
|
|
assert result.output == expected.format(tmpdir) |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
def test_quiet(git_repo): |
60
|
|
|
runner = CliRunner() |
61
|
|
|
|
62
|
|
|
arg = ['-q', git_repo.working_tree_dir] |
63
|
|
|
output_path = os.path.join(git_repo.working_tree_dir, 'version.json') |
64
|
|
|
|
65
|
|
|
result = runner.invoke(git_app_version_main, arg) |
66
|
|
|
|
67
|
|
|
assert result.exit_code == 0 |
68
|
|
|
assert os.path.exists(output_path) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_json(git_repo): |
72
|
|
|
runner = CliRunner() |
73
|
|
View Code Duplication |
|
|
|
|
|
74
|
|
|
arg = [git_repo.working_tree_dir] |
75
|
|
|
output_path = os.path.join(git_repo.working_tree_dir, 'version.json') |
76
|
|
|
|
77
|
|
|
result = runner.invoke(git_app_version_main, arg) |
78
|
|
|
|
79
|
|
|
assert result.output.find('Git commit :') != -1 |
80
|
|
|
assert re.search(r"version\s+0.1.2", result.output) |
81
|
|
|
assert result.output.find('written to :') != -1 |
82
|
|
|
assert result.output.find(output_path) != -1 |
83
|
|
|
|
84
|
|
|
assert os.path.exists(output_path) |
85
|
|
|
assert result.exit_code == 0 |
86
|
|
|
|
87
|
|
|
def test_metadata(git_repo): |
88
|
|
|
runner = CliRunner() |
89
|
|
View Code Duplication |
|
|
|
|
|
90
|
|
|
arg = ['-m', 'foo=bar', '-m', 'desc=custom', git_repo.working_tree_dir] |
91
|
|
|
output_path = os.path.join(git_repo.working_tree_dir, 'version.json') |
92
|
|
|
|
93
|
|
|
result = runner.invoke(git_app_version_main, arg) |
94
|
|
|
|
95
|
|
|
assert result.output.find('Git commit :') != -1 |
96
|
|
|
assert re.search(r"version\s+0.1.2", result.output) |
97
|
|
|
assert re.search(r"foo\s+bar", result.output) |
98
|
|
|
assert re.search(r"desc\s+custom", result.output) |
99
|
|
|
assert result.output.find(output_path) != -1 |
100
|
|
|
|
101
|
|
|
assert os.path.exists(output_path) |
102
|
|
|
assert result.exit_code == 0 |
103
|
|
|
|
104
|
|
|
def test_metadata_reserved_key(git_repo): |
105
|
|
|
runner = CliRunner() |
106
|
|
|
|
107
|
|
|
bad_key = 'deploy_date' |
108
|
|
|
arg = ['-m', bad_key+'=foo', git_repo.working_tree_dir] |
109
|
|
|
output_path = os.path.join(git_repo.working_tree_dir, 'version.json') |
110
|
|
|
|
111
|
|
|
expected = ("Error: Invalid value for \"--meta\" / \"-m\": {} is a reserved key\n") |
112
|
|
|
|
113
|
|
|
result = runner.invoke(git_app_version_main, arg) |
114
|
|
|
assert result.exit_code == 2 |
115
|
|
|
assert result.output.find(expected.format(bad_key)) != -1 |
116
|
|
|
|
117
|
|
|
def test_metadata_invalid_format(git_repo): |
118
|
|
|
runner = CliRunner() |
119
|
|
|
|
120
|
|
|
bad_key = 'foo' |
121
|
|
|
arg = ['-m', bad_key, git_repo.working_tree_dir] |
122
|
|
|
output_path = os.path.join(git_repo.working_tree_dir, 'version.json') |
123
|
|
|
|
124
|
|
|
expected = "Error: Invalid value for \"--meta\" / \"-m\": {} is not a valid meta data string e.g. : <key>=<value>\n" |
125
|
|
|
|
126
|
|
|
result = runner.invoke(git_app_version_main, arg) |
127
|
|
|
assert result.exit_code == 2 |
128
|
|
|
assert result.output.find(expected.format(bad_key)) != -1 |
129
|
|
|
|