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