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() |
|
|
|
|
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
|
|
|
|
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
|
|
|
|
107
|
|
|
def test_metadata_reserved_key(git_repo): |
108
|
|
|
runner = CliRunner() |
109
|
|
|
|
110
|
|
|
bad_key = 'deploy_date' |
111
|
|
|
arg = ['-m', bad_key + '=foo', git_repo.working_tree_dir] |
112
|
|
|
|
113
|
|
|
expected = ( |
114
|
|
|
"Error: Invalid value for \"--meta\" / \"-m\": {} is a reserved key\n") |
115
|
|
|
|
116
|
|
|
result = runner.invoke(git_app_version_main, arg) |
117
|
|
|
assert result.exit_code == 2 |
118
|
|
|
assert result.output.find(expected.format(bad_key)) != -1 |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
def test_metadata_invalid_format(git_repo): |
122
|
|
|
runner = CliRunner() |
123
|
|
|
|
124
|
|
|
bad_key = 'foo' |
125
|
|
|
arg = ['-m', bad_key, git_repo.working_tree_dir] |
126
|
|
|
|
127
|
|
|
expected = "Error: Invalid value for \"--meta\" / \"-m\":" |
128
|
|
|
" {} is not a valid meta data string e.g. : <key>=<value>\n" |
129
|
|
|
|
130
|
|
|
result = runner.invoke(git_app_version_main, arg) |
131
|
|
|
assert result.exit_code == 2 |
132
|
|
|
assert result.output.find(expected.format(bad_key)) != -1 |
133
|
|
|
|