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
|
|
|
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
|
|
|
|