2018-07-11 08:50:42 -04:00
|
|
|
"""Test enter"""
|
|
|
|
|
|
|
|
import os
|
2023-07-10 10:14:33 -04:00
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"shell, success",
|
|
|
|
[
|
|
|
|
("delete", True), # if there is no shell variable, bash creates it
|
|
|
|
("", False),
|
|
|
|
("/usr/bin/env", True),
|
|
|
|
("noexec", False),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"shell-missing",
|
|
|
|
"shell-empty",
|
|
|
|
"shell-env",
|
|
|
|
"shell-noexec",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
2020-11-17 15:38:31 -05:00
|
|
|
def test_enter(runner, yadm_cmd, paths, shell, success):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Enter tests"""
|
|
|
|
env = os.environ.copy()
|
2023-07-10 15:43:17 -04:00
|
|
|
if shell == "delete":
|
2018-07-11 08:50:42 -04:00
|
|
|
# remove shell
|
2023-07-10 15:43:17 -04:00
|
|
|
if "SHELL" in env:
|
|
|
|
del env["SHELL"]
|
|
|
|
elif shell == "noexec":
|
2018-07-11 08:50:42 -04:00
|
|
|
# specify a non-executable path
|
2023-07-10 15:43:17 -04:00
|
|
|
noexec = paths.root.join("noexec")
|
|
|
|
noexec.write("")
|
2018-07-11 08:50:42 -04:00
|
|
|
noexec.chmod(0o664)
|
2023-07-10 15:43:17 -04:00
|
|
|
env["SHELL"] = str(noexec)
|
2018-07-11 08:50:42 -04:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
env["SHELL"] = shell
|
2019-09-29 19:34:36 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=yadm_cmd("enter"), env=env)
|
2018-07-11 08:50:42 -04:00
|
|
|
assert run.success == success
|
2023-07-10 15:43:17 -04:00
|
|
|
prompt = f"yadm shell ({paths.repo})"
|
2018-07-11 08:50:42 -04:00
|
|
|
if success:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.out.startswith("Entering yadm repo")
|
|
|
|
assert run.out.rstrip().endswith("Leaving yadm repo")
|
|
|
|
assert run.err == ""
|
2021-01-05 15:57:32 -05:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "does not refer to an executable" in run.err
|
|
|
|
if "env" in shell:
|
|
|
|
assert f"GIT_DIR={paths.repo}" in run.out
|
|
|
|
assert f"GIT_WORK_TREE={paths.work}" in run.out
|
|
|
|
assert f"PROMPT={prompt}" in run.out
|
|
|
|
assert f"PS1={prompt}" in run.out
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"shell, opts, path",
|
|
|
|
[
|
|
|
|
("bash", "--norc", "\\w"),
|
|
|
|
("csh", "-f", "%~"),
|
|
|
|
("zsh", "-f", "%~"),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"bash",
|
|
|
|
"csh",
|
|
|
|
"zsh",
|
|
|
|
],
|
|
|
|
)
|
2019-12-16 09:47:35 -05:00
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"cmd",
|
|
|
|
[False, "cmd", "cmd-bad-exit"],
|
|
|
|
ids=["no-cmd", "cmd", "cmd-bad-exit"],
|
|
|
|
)
|
2020-12-29 18:58:51 -05:00
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"term",
|
|
|
|
["", "dumb"],
|
|
|
|
ids=["term-empty", "term-dumb"],
|
|
|
|
)
|
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
|
|
|
def test_enter_shell_ops(runner, yadm_cmd, paths, shell, opts, path, cmd, term):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Enter tests for specific shell options"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
change_exit = "\nfalse" if cmd == "cmd-bad-exit" else ""
|
2019-12-16 09:47:35 -05:00
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
# Create custom shell to detect options passed
|
|
|
|
custom_shell = paths.root.join(shell)
|
2023-07-10 15:43:17 -04:00
|
|
|
custom_shell.write(f"#!/bin/sh\necho OPTS=$*\necho PROMPT=$PROMPT{change_exit}")
|
2018-07-11 08:50:42 -04:00
|
|
|
custom_shell.chmod(0o775)
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
test_cmd = ["test1", "test2", "test3"]
|
2019-12-08 15:39:03 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
enter_cmd = ["enter"]
|
2019-12-08 15:39:03 -05:00
|
|
|
if cmd:
|
|
|
|
enter_cmd += test_cmd
|
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
env = os.environ.copy()
|
2023-07-10 15:43:17 -04:00
|
|
|
env["TERM"] = term
|
|
|
|
env["SHELL"] = custom_shell
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
if shell == "zsh" and term == "dumb":
|
|
|
|
opts += " --no-zle"
|
2020-12-29 18:58:51 -05:00
|
|
|
|
2020-11-17 15:38:31 -05:00
|
|
|
run = runner(command=yadm_cmd(*enter_cmd), env=env)
|
2023-07-10 15:43:17 -04:00
|
|
|
if cmd == "cmd-bad-exit":
|
2019-12-16 09:47:35 -05:00
|
|
|
assert run.failure
|
|
|
|
else:
|
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert f"OPTS={opts}" in run.out
|
|
|
|
assert f"PROMPT=yadm shell ({paths.repo}) {path} >" in run.out
|
2019-12-08 15:39:03 -05:00
|
|
|
if cmd:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "-c " + " ".join(test_cmd) in run.out
|
|
|
|
assert "Entering yadm repo" not in run.out
|
|
|
|
assert "Leaving yadm repo" not in run.out
|
2019-12-08 15:39:03 -05:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "Entering yadm repo" in run.out
|
|
|
|
assert "Leaving yadm repo" in run.out
|