File: test_input.py

package info (click to toggle)
python-consolekit 1.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,152 kB
  • sloc: python: 2,835; makefile: 8
file content (195 lines) | stat: -rw-r--r-- 5,481 bytes parent folder | download
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
# stdlib
from typing import Type

# 3rd party
import click
import pytest
from click import echo
from coincidence.regressions import AdvancedDataRegressionFixture
from domdf_python_tools.stringlist import StringList

# this package
from consolekit.input import choice, confirm, prompt
from consolekit.testing import _click_major


def test_choice_letters(
		capsys,
		monkeypatch,
		advanced_data_regression: AdvancedDataRegressionFixture,
		):

	inputs = iter(['F', 'G', 'D'])

	def fake_input(prompt: str) -> str:
		value = next(inputs)
		print(f"{prompt}{value}".rstrip())
		return value

	monkeypatch.setattr(click.termui, "visible_prompt_func", fake_input)

	echo("Configuration file '/etc/sudoers'")
	echo("==> Modified (by you or by a script) since installation.")
	echo("==> Package distributor has shipped an updated version.")
	echo("What would you like to do about it ?  Your options are:")
	options = {
			'Y': "install the package maintainer's version",
			'N': "keep your currently-installed version",
			'D': "show the differences between the versions",
			'Z': "start a shell to examine the situation"
			}
	assert choice(text="*** sudoers", options=options, default='N') == 'D'

	advanced_data_regression.check(list(StringList(capsys.readouterr().out.splitlines())))


@pytest.mark.parametrize(
		"click_version",
		[
				pytest.param(
						'7',
						marks=pytest.mark.skipif(_click_major == 8, reason="Output differs on click 8"),
						),
				pytest.param(
						'8',
						marks=pytest.mark.skipif(_click_major != 8, reason="Output differs on click 8"),
						),
				]
		)
def test_choice_numbers(
		capsys,
		monkeypatch,
		advanced_data_regression: AdvancedDataRegressionFixture,
		click_version: str,
		):

	inputs = iter(["20", '0', '5'])

	def fake_input(prompt: str) -> str:
		value = next(inputs)
		print(f"{prompt}{value}".rstrip())
		return value

	monkeypatch.setattr(click.termui, "visible_prompt_func", fake_input)

	echo("What is the Development Status of this project?")
	options = [
			"Planning",
			"Pre-Alpha",
			"Alpha",
			"Beta",
			"Production/Stable",
			"Mature",
			"Inactive",
			]
	assert choice(text='', options=options, start_index=1) == 4

	advanced_data_regression.check(list(StringList(capsys.readouterr().out.splitlines())))


def test_confirm(
		capsys,
		monkeypatch,
		advanced_data_regression: AdvancedDataRegressionFixture,
		):

	inputs = iter(['Y', 'N', '', '', "yEs", "No", "gkjhkhjv", ''])

	def fake_input(prompt: str) -> str:
		value = next(inputs)
		print(f"{prompt}{value}".rstrip())
		return value

	monkeypatch.setattr(click.termui, "visible_prompt_func", fake_input)

	assert confirm(text="Do you wish to delete all files in '/' ?", default=False) is True
	assert confirm(text="Do you wish to delete all files in '/' ?", default=False) is False
	assert confirm(text="Do you wish to delete all files in '/' ?", default=False) is False
	assert confirm(text="Do you wish to delete all files in '/' ?", default=True) is True
	assert confirm(text="Do you wish to delete all files in '/' ?", default=False) is True
	assert confirm(text="Do you wish to delete all files in '/' ?", default=True) is False
	assert confirm(text="Do you wish to delete all files in '/' ?", default=True) is True

	advanced_data_regression.check(list(StringList(capsys.readouterr().out.splitlines())))


def test_prompt(
		capsys,
		monkeypatch,
		advanced_data_regression: AdvancedDataRegressionFixture,
		):

	inputs = iter([
			'',
			'',
			'',
			'',
			"24",
			"Bond007",
			"badpassword",
			"baspassword",
			"badpassword",
			"badpassword",
			"badpassword",
			"badpassword",
			])

	def fake_input(prompt: str) -> str:
		value = next(inputs)
		print(f"{prompt}{value}".rstrip())
		return value

	monkeypatch.setattr(click.termui, "visible_prompt_func", fake_input)

	assert prompt(text="What is your age", prompt_suffix="? ", type=click.INT) == 24

	assert prompt(text="Username", type=click.STRING) == "Bond007"
	assert prompt(text="Password", type=click.STRING, confirmation_prompt=True) == "badpassword"
	assert prompt(
			text="Password",
			type=click.STRING,
			confirmation_prompt="Are you sure about that? ",
			) == "badpassword"

	advanced_data_regression.check(list(StringList(capsys.readouterr().out.splitlines())))


@pytest.mark.parametrize("exception", [KeyboardInterrupt, EOFError])
def test_prompt_abort(
		capsys,
		monkeypatch,
		advanced_data_regression: AdvancedDataRegressionFixture,
		exception: Type[Exception],
		):

	def fake_input(prompt: str) -> str:
		print(f"{prompt}", end='')
		raise exception

	monkeypatch.setattr(click.termui, "visible_prompt_func", fake_input)

	with pytest.raises(click.Abort, match="^$"):
		prompt(text="Password", type=click.STRING, confirmation_prompt=True)

	assert list(StringList(capsys.readouterr().out.splitlines())) == ["Password:"]


@pytest.mark.parametrize("exception", [KeyboardInterrupt, EOFError])
def test_confirm_abort(
		capsys,
		monkeypatch,
		advanced_data_regression: AdvancedDataRegressionFixture,
		exception: Type[Exception],
		):

	def fake_input(prompt: str) -> str:
		print(f"{prompt}", end='')
		raise exception

	monkeypatch.setattr(click.termui, "visible_prompt_func", fake_input)

	with pytest.raises(click.Abort, match="^$"):
		confirm(text="Do you wish to delete all files in '/' ?", default=False)

	expected = ["Do you wish to delete all files in '/' ? [y/N]:"]
	assert list(StringList(capsys.readouterr().out.splitlines())) == expected