forked from pyscaffold/pyscaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_info.py
223 lines (170 loc) · 7.1 KB
/
test_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import getpass
import os
import socket
from pathlib import Path
from unittest.mock import MagicMock as Mock
import pytest
from configupdater import ConfigUpdater
from pyscaffold import actions, cli, exceptions, info, repo, structure, templates
def test_username_with_git(git_mock):
username = info.username()
assert isinstance(username, str)
assert len(username) > 0
def test_username_with_no_git(nogit_mock):
username = info.username()
assert isinstance(username, str)
assert getpass.getuser() == username
def test_username_error(git_mock, monkeypatch):
fake_git = Mock(side_effect=exceptions.ShellCommandException)
monkeypatch.setattr(info.shell, "git", fake_git)
# on windows getpass might fail
monkeypatch.setattr(info.getpass, "getuser", Mock(side_effect=SystemError))
with pytest.raises(exceptions.GitNotConfigured):
info.username()
def test_email_with_git(git_mock):
email = info.email()
assert "@" in email
def test_email_with_nogit(nogit_mock):
email = info.email()
assert socket.gethostname() == email.split("@")[1]
def test_email_error(git_mock, monkeypatch):
fake_git = Mock(side_effect=exceptions.ShellCommandException)
monkeypatch.setattr(info.shell, "git", fake_git)
# on windows getpass might fail
monkeypatch.setattr(info.getpass, "getuser", Mock(side_effect=SystemError))
with pytest.raises(exceptions.GitNotConfigured):
info.email()
def test_git_is_installed(git_mock):
assert info.is_git_installed()
def test_git_is_wrongely_installed(nogit_mock):
assert not info.is_git_installed()
def test_git_is_not_installed(nogit_cmd_mock):
assert not info.is_git_installed()
def test_is_git_configured(git_mock):
assert info.is_git_configured()
def test_git_is_configured_via_env_vars(monkeypatch):
monkeypatch.setenv("GIT_AUTHOR_NAME", "John Doe")
monkeypatch.setenv("GIT_AUTHOR_EMAIL", "[email protected]")
assert info.is_git_configured()
def test_is_git_not_configured(noconfgit_mock):
assert not info.is_git_configured()
def test_check_git_not_installed(nogit_cmd_mock):
with pytest.raises(exceptions.GitNotInstalled):
info.check_git()
def test_check_git_not_configured(noconfgit_mock):
with pytest.raises(exceptions.GitNotConfigured):
info.check_git()
def test_check_git_installed_and_configured(git_mock):
info.check_git()
def test_project_without_args(tmpfolder):
old_args = [
"my_project",
"-u",
"http://www.blue-yonder.com/",
"-d",
"my description",
]
cli.main(old_args)
args = ["my_project"]
opts = cli.parse_args(args)
new_opts = info.project(opts)
assert new_opts["url"] == "http://www.blue-yonder.com/"
assert new_opts["package"] == "my_project"
assert new_opts["license"] == "MIT"
assert new_opts["description"] == "my description"
def test_project_with_args(tmpfolder):
old_args = [
"my_project",
"-u",
"http://www.blue-yonder.com/",
"-d",
"my description",
]
cli.main(old_args)
args = ["my_project", "-u", "http://www.google.com/", "-d", "other description"]
opts = cli.parse_args(args)
new_opts = info.project(opts)
assert new_opts["url"] == "http://www.google.com/"
assert new_opts["package"] == "my_project"
assert new_opts["description"] == "other description"
def test_project_with_no_setup(tmpfolder):
os.mkdir("my_project")
args = ["my_project"]
opts = cli.parse_args(args)
with pytest.raises(FileNotFoundError):
info.project(opts)
def test_project_with_wrong_setup(tmpfolder):
os.mkdir("my_project")
open("my_project/setup.py", "a").close()
args = ["my_project"]
opts = cli.parse_args(args)
with pytest.raises(FileNotFoundError):
info.project(opts)
def test_project_old_setupcfg(tmpfolder):
demoapp = Path(__file__).parent / "demoapp"
with pytest.raises(exceptions.PyScaffoldTooOld):
info.project({}, config_path=demoapp)
def test_project_extensions_not_found(tmpfolder):
_, opts = actions.get_default_options({}, {})
cfg = ConfigUpdater().read_string(templates.setup_cfg(opts))
cfg["pyscaffold"]["extensions"] = "x_foo_bar_x"
(tmpfolder / "setup.cfg").write(str(cfg))
with pytest.raises(exceptions.ExtensionNotFound) as exc:
info.project(opts)
assert "x_foo_bar_x" in str(exc.value)
@pytest.mark.no_fake_config_dir
def test_config_dir_error(monkeypatch):
# no_fake_config_dir => avoid previous mock of config_dir
# If for some reason something goes wrong when trying to find the config dir
user_config_dir_mock = Mock(side_effect=SystemError)
monkeypatch.setattr(info.platformdirs, "user_config_dir", user_config_dir_mock)
# And no default value is given
# Then an error should be raised
with pytest.raises(exceptions.ImpossibleToFindConfigDir):
print("config_dir", info.config_dir())
user_config_dir_mock.assert_called_once()
def test_config_file_default(monkeypatch):
# When config_dir does not find the correct config directory
monkeypatch.setattr(info, "config_dir", Mock(return_value=None))
# And there are a default file
demoapp_setup = Path(__file__).parent / "demoapp" / "setup.cfg"
# Then the default file should be returned
assert info.config_file(default=demoapp_setup) == demoapp_setup
def test_best_fit_license():
# If the name matches => return the name
for license in templates.licenses.keys():
assert info.best_fit_license(license) == license
# Lower case
assert info.best_fit_license("0bsd") == "0BSD"
# No dashes
assert info.best_fit_license("mpl2") == "MPL-2.0"
assert info.best_fit_license("gpl2") == "GPL-2.0-only"
assert info.best_fit_license("gpl3") == "GPL-3.0-only"
assert info.best_fit_license("bsd2") == "BSD-2-Clause"
assert info.best_fit_license("bsd3") == "BSD-3-Clause"
# Popular nicknames
assert info.best_fit_license("apache") == "Apache-2.0"
assert info.best_fit_license("artistic") == "Artistic-2.0"
assert info.best_fit_license("affero") == "AGPL-3.0-only"
assert info.best_fit_license("eclipse") == "EPL-1.0"
assert info.best_fit_license("bsd0") == "0BSD"
assert info.best_fit_license("new-bsd") == "BSD-3-Clause"
assert info.best_fit_license("simple-bsd") == "BSD-2-Clause"
assert info.best_fit_license("cc0") == "CC0-1.0"
assert info.best_fit_license("none") == "Proprietary"
assert info.best_fit_license("public-domain") == "Unlicense"
# Or later vs only
assert info.best_fit_license("gpl3-only") == "GPL-3.0-only"
assert info.best_fit_license("gpl2-later") == "GPL-2.0-or-later"
# Default
assert info.best_fit_license("") == "MIT"
def test_dirty_workspace(tmpfolder):
project = "my_project"
struct = {"dummyfile": "NO CONTENT"}
structure.create_structure(struct, dict(project_path=project))
repo.init_commit_repo(project, struct)
path = tmpfolder / project
assert info.is_git_workspace_clean(path)
with open(str(path / "dummyfile"), "w") as fh:
fh.write("CHANGED\n")
assert not info.is_git_workspace_clean(path)