/
githubmirror
/
bash-completion
Обзор
Документация
Войти
/
githubmirror
/
bash-completion
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
test/t/unit/test_unit_load.py
144 строки
6 KB
Koichi Murase
test(TestCompLoad.fixture_dir): specify the scope
31 дек 2025, 10:40
31 дек 2025, 10:40
e5013f6
Код
Авторство
О чём код?
import os import pytest from conftest import assert_bash_exec, bash_env_saved @pytest.mark.bashcomp(cmd=None, cwd="_comp_load") class TestCompLoad: @pytest.fixture(scope="class") def fixture_dir(self, bash, tmp_path_factory): """Construct the fixture directory in a temporary directory. Some of the tests use specific setups of symbolic links. However, if we put the symbolic links in the static fixture directory, Automake resolves them for tarballs. As a result, the tests fail when the files are extracted from the tarballs. There does not seem to be any option to change the behavior of Automake. We instead manually set up all symbolic links needed for the tests here. The other normal files and directories are statically included in the repository as "/test/fixtures/_comp_load". We first copy the statically included files and directories to a temporary directory and set up symbolic links. """ tmpdir = tmp_path_factory.mktemp("bash-completion._comp_load.") # Note: I tried to use # # shutil.copytree(os.getcwd(), tmpdir, dirs_exist_ok=True) # # but it turned out that shutil.copytree of Python 3.7 (used in centos7 # and debian10 in the docker images) does not provide the # "dirs_exist_ok" option. We continue to use "cp -R" here, but we may # update this line after we drop centos7 and debian10. assert_bash_exec(bash, "cp -R %s/* %s/" % (os.getcwd(), tmpdir)) bin_dir = tmpdir / "bin" bin_dir.mkdir() cmd1 = bin_dir / "cmd1" cmd2 = bin_dir / "cmd2" cmd1.symlink_to("../prefix1/bin/cmd1") cmd2.symlink_to("../prefix1/sbin/cmd2") return str(tmpdir) def test_userdir_1(self, bash, fixture_dir): with bash_env_saved(bash) as bash_env: bash_env.chdir(fixture_dir) bash_env.write_variable( "BASH_COMPLETION_USER_DIR", "$PWD/userdir1:$PWD/userdir2:$BASH_COMPLETION_USER_DIR", quote=False, ) bash_env.write_variable( "PATH", "$PWD/prefix1/bin:$PWD/prefix1/sbin", quote=False ) output = assert_bash_exec( bash, "_comp_load cmd1", want_output=True ) assert output.strip() == "cmd1: sourced from userdir1" output = assert_bash_exec( bash, "_comp_load cmd2", want_output=True ) assert output.strip() == "cmd2: sourced from userdir2" def test_PATH_1(self, bash, fixture_dir): with bash_env_saved(bash) as bash_env: bash_env.chdir(fixture_dir) bash_env.write_variable( "PATH", "$PWD/prefix1/bin:$PWD/prefix1/sbin", quote=False ) output = assert_bash_exec( bash, "_comp_load cmd1", want_output=True ) assert output.strip() == "cmd1: sourced from prefix1" output = assert_bash_exec( bash, "_comp_load cmd2", want_output=True ) assert output.strip() == "cmd2: sourced from prefix1" output = assert_bash_exec( bash, "complete -p cmd2", want_output=True ) assert " cmd2" in output output = assert_bash_exec( bash, 'complete -p "$PWD/prefix1/sbin/cmd2"', want_output=True ) assert "/prefix1/sbin/cmd2" in output def test_cmd_path_1(self, bash, fixture_dir): with bash_env_saved(bash) as bash_env: bash_env.chdir(fixture_dir) assert_bash_exec(bash, "complete -r cmd1 || :", want_output=None) output = assert_bash_exec( bash, "_comp_load prefix1/bin/cmd1", want_output=True ) assert output.strip() == "cmd1: sourced from prefix1" output = assert_bash_exec( bash, 'complete -p "$PWD/prefix1/bin/cmd1"', want_output=True ) assert "/prefix1/bin/cmd1" in output assert_bash_exec(bash, "! complete -p cmd1", want_output=None) output = assert_bash_exec( bash, "_comp_load prefix1/sbin/cmd2", want_output=True ) assert output.strip() == "cmd2: sourced from prefix1" output = assert_bash_exec( bash, "_comp_load bin/cmd1", want_output=True ) assert output.strip() == "cmd1: sourced from prefix1" output = assert_bash_exec( bash, "_comp_load bin/cmd2", want_output=True ) assert output.strip() == "cmd2: sourced from prefix1" def test_cmd_path_2(self, bash, fixture_dir): with bash_env_saved(bash) as bash_env: bash_env.chdir(fixture_dir) bash_env.write_variable("PATH", "$PWD/bin:$PATH", quote=False) output = assert_bash_exec( bash, "_comp_load cmd1", want_output=True ) assert output.strip() == "cmd1: sourced from prefix1" output = assert_bash_exec( bash, "_comp_load cmd2", want_output=True ) assert output.strip() == "cmd2: sourced from prefix1" def test_cmd_intree_precedence(self, bash, fixture_dir): """ Test in-tree, i.e. completions/$cmd relative to the main script has precedence over location derived from PATH. """ with bash_env_saved(bash) as bash_env: bash_env.chdir(fixture_dir) bash_env.write_variable("PATH", "$PWD/prefix1/bin", quote=False) # The in-tree `sh` completion should be loaded here, # and cause no output, unlike our `$PWD/prefix1/bin/sh` canary. assert_bash_exec(bash, "_comp_load sh", want_output=False) def test_option_like_cmd_name(self, bash): assert_bash_exec(bash, "! _comp_load -- --non-existent")