Skip to content

Commit c532a16

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

File tree

3 files changed

+31
-37
lines changed

3 files changed

+31
-37
lines changed

test/t/conftest.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ def load_completion_for(bash: pexpect.spawn, cmd: str) -> bool:
329329
return False
330330
return True
331331

332-
333332
def assert_bash_exec(
334333
bash: pexpect.spawn,
335334
cmd: str,
@@ -386,6 +385,24 @@ def assert_bash_exec(
386385

387386
return output
388387

388+
def _bash_copy_variable(bash: pexpect.spawn, src_var: str, dst_var: str):
389+
assert_bash_exec(
390+
bash,
391+
"if [[ ${%s+set} ]]; then %s=${%s}; else unset -v %s; fi"
392+
% (src_var, dst_var, src_var, dst_var)
393+
)
394+
395+
def bash_save_variable(
396+
bash: pexpect.spawn,
397+
varname: str,
398+
new_value: Optional[str] = None
399+
):
400+
_bash_copy_variable(bash, varname, "_bash_completion_test_" + varname)
401+
if new_value:
402+
assert_bash_exec(bash, "%s=%s" % (varname, shlex.quote(str(new_value))))
403+
404+
def bash_restore_variable(bash: pexpect.spawn, varname: str):
405+
_bash_copy_variable(bash, "_bash_completion_test_" + varname, varname)
389406

390407
def get_env(bash: pexpect.spawn) -> List[str]:
391408
return [
@@ -499,11 +516,7 @@ def assert_complete(
499516
pytest.xfail(xfail)
500517
cwd = kwargs.get("cwd")
501518
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-
)
519+
bash_save_variable(bash, "OLDPWD")
507520
assert_bash_exec(bash, "cd '%s'" % cwd)
508521
env_prefix = "_BASHCOMP_TEST_"
509522
env = kwargs.get("env", {})
@@ -568,11 +581,7 @@ def assert_complete(
568581
)
569582
if cwd:
570583
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-
)
584+
bash_restore_variable(bash, "OLDPWD")
576585
return result
577586

578587

test/t/test_man.py

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

3-
from conftest import assert_bash_exec, assert_complete, prepare_fixture_dir
3+
from conftest import assert_bash_exec, assert_complete, prepare_fixture_dir, bash_save_variable, bash_restore_variable
44

55

66
@pytest.mark.bashcomp(
@@ -103,15 +103,10 @@ def test_9(self, bash, completion):
103103

104104
@pytest.mark.complete(require_cmd=True)
105105
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")
113-
)
106+
bash_save_variable(bash, "MANPATH", "%s:%s/man" % (TestMan.manpath, colonpath))
107+
assert_bash_exec(bash, 'export MANPATH')
114108
completion = assert_complete(bash, "man Bash::C")
109+
bash_restore_variable(bash, "MANPATH")
115110
assert completion == "ompletion"
116111

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

test/t/unit/test_unit_known_hosts_real.py

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

33
import pytest
44

5-
from conftest import assert_bash_exec
5+
from conftest import assert_bash_exec, bash_save_variable, bash_restore_variable
66

77

88
@pytest.mark.bashcomp(
@@ -126,27 +126,20 @@ def test_included_configs(self, bash, hosts):
126126
# fixtures/_known_hosts_real/.ssh/config_question_mark
127127
expected.append("question_mark")
128128

129-
assert_bash_exec(
130-
bash, 'OLDHOME="$HOME"; HOME="%s/_known_hosts_real"' % bash.cwd
131-
)
129+
bash_save_variable(bash, "HOME", "%s/_known_hosts_real" % bash.cwd)
132130
output = assert_bash_exec(
133131
bash,
134132
"unset -v COMPREPLY COMP_KNOWN_HOSTS_WITH_HOSTFILE; "
135133
"_known_hosts_real -aF _known_hosts_real/config_include ''; "
136134
r'printf "%s\n" "${COMPREPLY[@]}"',
137135
want_output=True,
138136
)
139-
assert_bash_exec(bash, 'HOME="$OLDHOME"')
137+
bash_restore_variable(bash, "HOME")
140138
assert sorted(set(output.strip().split())) == sorted(expected)
141139

142140
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-
)
141+
bash_save_variable(bash, "HOME", "%s/_known_hosts_real" % bash.cwd)
142+
bash_save_variable(bash, "OLDPWD")
150143
output = assert_bash_exec(
151144
bash,
152145
"cd _known_hosts_real; "
@@ -156,11 +149,8 @@ def test_no_globbing(self, bash):
156149
"cd - &>/dev/null",
157150
want_output=True,
158151
)
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"')
152+
bash_restore_variable(bash, "OLDPWD")
153+
bash_restore_variable(bash, "HOME")
164154
completion = sorted(set(output.strip().split()))
165155
assert "gee" in completion
166156
assert "gee-filename-canary" not in completion

0 commit comments

Comments
 (0)