-
Notifications
You must be signed in to change notification settings - Fork 130
/
test_init.py
138 lines (120 loc) · 4.21 KB
/
test_init.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
import builtins
from contextlib import contextmanager
from pathlib import Path
from tempfile import TemporaryDirectory
from testpath import assert_isfile
from unittest.mock import patch
import pytoml
from flit import init
@contextmanager
def patch_data_dir():
with TemporaryDirectory() as td:
with patch.object(init, 'get_data_dir', lambda: Path(td)):
yield td
def test_store_defaults():
with patch_data_dir():
assert init.get_defaults() == {}
d = {'author': 'Test'}
init.store_defaults(d)
assert init.get_defaults() == d
def fake_input(entries):
it = iter(entries)
def inner(prompt):
try:
return next(it)
except StopIteration:
raise EOFError
return inner
def faking_input(entries):
return patch.object(builtins, 'input', fake_input(entries))
def test_prompt_options():
ti = init.TerminalIniter()
with faking_input(['4', '1']):
res = ti.prompt_options('Pick one', [('A', 'Apple'), ('B', 'Banana')])
assert res == 'A'
# Test with a default
with faking_input(['']):
res = ti.prompt_options('Pick one', [('A', 'Apple'), ('B', 'Banana')],
default='B')
assert res == 'B'
@contextmanager
def make_dir(files=(), dirs=()):
with TemporaryDirectory() as td:
tdp = Path(td)
for d in dirs:
(tdp / d).mkdir()
for f in files:
(tdp / f).touch()
yield td
def test_guess_module_name():
with make_dir(['foo.py', 'foo-bar.py', 'test_foo.py', 'setup.py']) as td:
ib = init.IniterBase(td)
assert ib.guess_module_name() == 'foo'
with make_dir(['baz/__init__.py', 'tests/__init__.py'], ['baz', 'tests']) as td:
ib = init.IniterBase(td)
assert ib.guess_module_name() == 'baz'
with make_dir(['foo.py', 'bar.py']) as td:
ib = init.IniterBase(td)
assert ib.guess_module_name() is None
def test_write_license():
with TemporaryDirectory() as td:
ib = init.IniterBase(td)
ib.write_license('mit', 'Thomas Kluyver')
assert_isfile(Path(td, 'LICENSE'))
def test_init():
responses = ['foo', # Module name
'Thomas Kluyver', # Author
'thomas@example.com', # Author email
'http://example.com/', # Home page
'1' # License (1 -> MIT)
]
with TemporaryDirectory() as td, \
patch_data_dir(), \
faking_input(responses):
ti = init.TerminalIniter(td)
ti.initialise()
assert_isfile('pyproject.toml')
assert_isfile('LICENSE')
def test_init_homepage_and_license_are_optional():
responses = ['test_module_name',
'Test Author',
'test_email@example.com',
'', # Home page omitted
'4', # Skip - choose a license later
]
with TemporaryDirectory() as td, \
patch_data_dir(), \
faking_input(responses):
ti = init.TerminalIniter(td)
ti.initialise()
with Path(td, 'pyproject.toml').open() as f:
data = pytoml.load(f)
assert not Path(td, 'LICENSE').exists()
metadata = data['tool']['flit']['metadata']
assert metadata == {
'author': 'Test Author',
'author-email': 'test_email@example.com',
'module': 'test_module_name',
}
def test_init_homepage_validator():
responses = ['test_module_name',
'Test Author',
'test_email@example.com',
'www.uh-oh-spagghetti-o.com', # fails validation
'https://www.example.org', # passes
'4', # Skip - choose a license later
]
with TemporaryDirectory() as td, \
patch_data_dir(), \
faking_input(responses):
ti = init.TerminalIniter(td)
ti.initialise()
with Path(td, 'pyproject.toml').open() as f:
data = pytoml.load(f)
metadata = data['tool']['flit']['metadata']
assert metadata == {
'author': 'Test Author',
'author-email': 'test_email@example.com',
'home-page': 'https://www.example.org',
'module': 'test_module_name',
}