Skip to content

Commit 67cda5f

Browse files
committed
Refactor default env to use clean_python
1 parent bff0631 commit 67cda5f

1 file changed

Lines changed: 38 additions & 13 deletions

File tree

‎tests/activation/test_prompts.py‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import sys
77
from collections import defaultdict
8+
from pathlib import Path
89
from textwrap import dedent
910

1011
import pytest
@@ -61,10 +62,9 @@ def check(platform, shell):
6162

6263

6364
@pytest.fixture(scope="module")
64-
def tmp_root(tmp_path_factory):
65+
def custom_prompt_root(tmp_path_factory):
6566
"""Provide Path to root with default and custom venvs created."""
66-
root = tmp_path_factory.mktemp("env_root")
67-
virtualenv.create_environment(str(root / ENV_DEFAULT), no_setuptools=True, no_pip=True, no_wheel=True)
67+
root = tmp_path_factory.mktemp("custom_prompt")
6868
virtualenv.create_environment(
6969
str(root / ENV_CUSTOM), prompt=PREFIX_CUSTOM, no_setuptools=True, no_pip=True, no_wheel=True
7070
)
@@ -76,6 +76,27 @@ def tmp_root(tmp_path_factory):
7676
return root, bin_dir_name
7777

7878

79+
@pytest.fixture(scope="module")
80+
def clean_python_root(clean_python):
81+
root = Path(clean_python[0]).resolve().parent
82+
bin_dir_name = os.path.split(clean_python[1])[-1]
83+
84+
return root, bin_dir_name
85+
86+
87+
@pytest.fixture(scope="module")
88+
def get_work_root(clean_python_root, custom_prompt_root):
89+
def pick_root(env):
90+
if env == ENV_DEFAULT:
91+
return clean_python_root
92+
elif env == ENV_CUSTOM:
93+
return custom_prompt_root
94+
else:
95+
raise ValueError("Invalid test environment")
96+
97+
return pick_root
98+
99+
79100
@pytest.fixture(scope="module")
80101
def shell_info():
81102
class ShellInfo:
@@ -135,7 +156,7 @@ def clean_env():
135156

136157
@pytest.mark.parametrize("shell", SHELL_LIST)
137158
@pytest.mark.parametrize("env", [ENV_DEFAULT, ENV_CUSTOM])
138-
def test_suppressed_prompt(shell, env, tmp_root, clean_env, shell_info, platform_check_skip):
159+
def test_suppressed_prompt(shell, env, get_work_root, clean_env, shell_info, platform_check_skip):
139160
"""Confirm VIRTUAL_ENV_DISABLE_PROMPT suppresses prompt changes on activate."""
140161
platform_check_skip(sys.platform, shell)
141162

@@ -144,10 +165,12 @@ def test_suppressed_prompt(shell, env, tmp_root, clean_env, shell_info, platform
144165

145166
clean_env.update({VIRTUAL_ENV_DISABLE_PROMPT: "1"})
146167

168+
work_root = get_work_root(env)
169+
147170
# The extra "{prompt}" here copes with some oddity of xonsh in certain emulated terminal
148171
# contexts: xonsh can dump stuff into the first line of the recorded script output,
149172
# so we have to include a dummy line of output that can get munged w/o consequence.
150-
(tmp_root[0] / script_name).write_text(
173+
(work_root[0] / script_name).write_text(
151174
dedent(
152175
"""\
153176
{preamble}
@@ -161,34 +184,36 @@ def test_suppressed_prompt(shell, env, tmp_root, clean_env, shell_info, platform
161184
preamble=shell_info.preamble_cmds[shell],
162185
prompt=shell_info.prompt_cmds[shell],
163186
act_script=shell_info.activate_scripts[shell],
164-
bindir=tmp_root[1],
187+
bindir=work_root[1],
165188
)
166189
)
167190
)
168191

169192
command = "{} {} > {}".format(shell_info.execute_cmds[shell], script_name, output_name)
170193

171-
assert 0 == subprocess.call(command, cwd=str(tmp_root[0]), shell=True, env=clean_env)
194+
assert 0 == subprocess.call(command, cwd=str(work_root[0]), shell=True, env=clean_env)
172195

173-
lines = (tmp_root[0] / output_name).read_bytes().split(b"\n")
196+
lines = (work_root[0] / output_name).read_bytes().split(b"\n")
174197

175198
# Is the prompt suppressed?
176199
assert lines[1] == lines[2], lines
177200

178201

179202
@pytest.mark.parametrize("shell", SHELL_LIST)
180203
@pytest.mark.parametrize(["env", "prefix"], [(ENV_DEFAULT, PREFIX_DEFAULT), (ENV_CUSTOM, PREFIX_CUSTOM)])
181-
def test_activated_prompt(shell, env, prefix, tmp_root, shell_info, platform_check_skip):
204+
def test_activated_prompt(shell, env, prefix, get_work_root, shell_info, platform_check_skip):
182205
"""Confirm prompt modification behavior with and without --prompt specified."""
183206
platform_check_skip(sys.platform, shell)
184207

185208
script_name = shell_info.script_template.format(shell, "normal", env, shell_info.testscript_extensions[shell])
186209
output_name = shell_info.output_template.format(shell, "normal", env)
187210

211+
work_root = get_work_root(env)
212+
188213
# The extra "{prompt}" here copes with some oddity of xonsh in certain emulated terminal
189214
# contexts: xonsh can dump stuff into the first line of the recorded script output,
190215
# so we have to include a dummy line of output that can get munged w/o consequence.
191-
(tmp_root[0] / script_name).write_text(
216+
(work_root[0] / script_name).write_text(
192217
dedent(
193218
"""\
194219
{preamble}
@@ -205,16 +230,16 @@ def test_activated_prompt(shell, env, prefix, tmp_root, shell_info, platform_che
205230
preamble=shell_info.preamble_cmds[shell],
206231
prompt=shell_info.prompt_cmds[shell],
207232
act_script=shell_info.activate_scripts[shell],
208-
bindir=tmp_root[1],
233+
bindir=work_root[1],
209234
)
210235
)
211236
)
212237

213238
command = "{} {} > {}".format(shell_info.execute_cmds[shell], script_name, output_name)
214239

215-
assert 0 == subprocess.call(command, cwd=str(tmp_root[0]), shell=True)
240+
assert 0 == subprocess.call(command, cwd=str(work_root[0]), shell=True)
216241

217-
lines = (tmp_root[0] / output_name).read_bytes().split(b"\n")
242+
lines = (work_root[0] / output_name).read_bytes().split(b"\n")
218243

219244
# Before activation and after deactivation
220245
assert lines[1] == lines[3], lines

0 commit comments

Comments
 (0)