Skip to content

Commit 18ab3b7

Browse files
committed
test(conftest): add utilities "bash_{save,restore}_variable(bash,var)"
1 parent 397e6b0 commit 18ab3b7

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

test/t/conftest.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,28 @@ def assert_bash_exec(
387387
return output
388388

389389

390+
def _bash_copy_variable(bash: pexpect.spawn, src_var: str, dst_var: str):
391+
assert_bash_exec(
392+
bash,
393+
"if [[ ${%s+set} ]]; then %s=${%s}; else unset -v %s; fi"
394+
% (src_var, dst_var, src_var, dst_var),
395+
)
396+
397+
398+
def bash_save_variable(
399+
bash: pexpect.spawn, varname: str, new_value: Optional[str] = None
400+
):
401+
_bash_copy_variable(bash, varname, "_bash_completion_test_" + varname)
402+
if new_value:
403+
assert_bash_exec(
404+
bash, "%s=%s" % (varname, shlex.quote(str(new_value)))
405+
)
406+
407+
408+
def bash_restore_variable(bash: pexpect.spawn, varname: str):
409+
_bash_copy_variable(bash, "_bash_completion_test_" + varname, varname)
410+
411+
390412
def get_env(bash: pexpect.spawn) -> List[str]:
391413
return [
392414
x
@@ -499,11 +521,7 @@ def assert_complete(
499521
pytest.xfail(xfail)
500522
cwd = kwargs.get("cwd")
501523
if cwd:
502-
assert_bash_exec(
503-
bash,
504-
"if [[ ${OLDPWD+set} ]]; then _bash_completion_test_OLDPWD=$OLDPWD; else unset -v _bash_completion_test_OLDPWD; fi",
505-
want_output=None,
506-
)
524+
bash_save_variable(bash, "OLDPWD")
507525
assert_bash_exec(bash, "cd '%s'" % cwd)
508526
env_prefix = "_BASHCOMP_TEST_"
509527
env = kwargs.get("env", {})
@@ -568,11 +586,7 @@ def assert_complete(
568586
)
569587
if cwd:
570588
assert_bash_exec(bash, "cd - >/dev/null")
571-
assert_bash_exec(
572-
bash,
573-
"if [[ ${_bash_completion_test_OLDPWD+set} ]]; then OLDPWD=$_bash_completion_test_OLDPWD; else unset -v OLDPWD; fi",
574-
want_output=None,
575-
)
589+
bash_restore_variable(bash, "OLDPWD")
576590
return result
577591

578592

test/t/test_man.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import pytest
22

3-
from conftest import assert_bash_exec, assert_complete, prepare_fixture_dir
3+
from conftest import (
4+
assert_bash_exec,
5+
assert_complete,
6+
bash_restore_variable,
7+
bash_save_variable,
8+
prepare_fixture_dir,
9+
)
410

511

612
@pytest.mark.bashcomp(
@@ -103,15 +109,12 @@ def test_9(self, bash, completion):
103109

104110
@pytest.mark.complete(require_cmd=True)
105111
def test_10(self, request, bash, colonpath):
106-
assert_bash_exec(
107-
bash,
108-
'manpath=${MANPATH-}; export MANPATH="%s:%s/man"'
109-
% (TestMan.manpath, colonpath),
110-
)
111-
request.addfinalizer(
112-
lambda: assert_bash_exec(bash, "MANPATH=$manpath")
112+
bash_save_variable(
113+
bash, "MANPATH", "%s:%s/man" % (TestMan.manpath, colonpath)
113114
)
115+
assert_bash_exec(bash, "export MANPATH")
114116
completion = assert_complete(bash, "man Bash::C")
117+
bash_restore_variable(bash, "MANPATH")
115118
assert completion == "ompletion"
116119

117120
@pytest.mark.complete("man -", require_cmd=True)

test/t/unit/test_unit_known_hosts_real.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import pytest
44

5-
from conftest import assert_bash_exec
5+
from conftest import (
6+
assert_bash_exec,
7+
bash_restore_variable,
8+
bash_save_variable,
9+
)
610

711

812
@pytest.mark.bashcomp(
@@ -126,27 +130,20 @@ def test_included_configs(self, bash, hosts):
126130
# fixtures/_known_hosts_real/.ssh/config_question_mark
127131
expected.append("question_mark")
128132

129-
assert_bash_exec(
130-
bash, 'OLDHOME="$HOME"; HOME="%s/_known_hosts_real"' % bash.cwd
131-
)
133+
bash_save_variable(bash, "HOME", "%s/_known_hosts_real" % bash.cwd)
132134
output = assert_bash_exec(
133135
bash,
134136
"unset -v COMPREPLY COMP_KNOWN_HOSTS_WITH_HOSTFILE; "
135137
"_known_hosts_real -aF _known_hosts_real/config_include ''; "
136138
r'printf "%s\n" "${COMPREPLY[@]}"',
137139
want_output=True,
138140
)
139-
assert_bash_exec(bash, 'HOME="$OLDHOME"')
141+
bash_restore_variable(bash, "HOME")
140142
assert sorted(set(output.strip().split())) == sorted(expected)
141143

142144
def test_no_globbing(self, bash):
143-
assert_bash_exec(
144-
bash, 'OLDHOME="$HOME"; HOME="%s/_known_hosts_real"' % bash.cwd
145-
)
146-
assert_bash_exec(
147-
bash,
148-
"if [[ ${OLDPWD+set} ]]; then _bash_completion_test_OLDPWD=$OLDPWD; else unset -v _bash_completion_test_OLDPWD; fi",
149-
)
145+
bash_save_variable(bash, "HOME", "%s/_known_hosts_real" % bash.cwd)
146+
bash_save_variable(bash, "OLDPWD")
150147
output = assert_bash_exec(
151148
bash,
152149
"cd _known_hosts_real; "
@@ -156,11 +153,8 @@ def test_no_globbing(self, bash):
156153
"cd - &>/dev/null",
157154
want_output=True,
158155
)
159-
assert_bash_exec(
160-
bash,
161-
"if [[ ${_bash_completion_test_OLDPWD+set} ]]; then OLDPWD=$_bash_completion_test_OLDPWD; else unset -v OLDPWD; fi",
162-
)
163-
assert_bash_exec(bash, 'HOME="$OLDHOME"')
156+
bash_restore_variable(bash, "OLDPWD")
157+
bash_restore_variable(bash, "HOME")
164158
completion = sorted(set(output.strip().split()))
165159
assert "gee" in completion
166160
assert "gee-filename-canary" not in completion

0 commit comments

Comments
 (0)