1
0
Fork 0
mirror of synced 2024-11-18 23:25:35 -05:00
yadm/test/test_enter.py

118 lines
3.1 KiB
Python
Raw Permalink Normal View History

"""Test enter"""
import os
2023-07-10 10:14:33 -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")
def test_enter(runner, yadm_cmd, paths, shell, success):
"""Enter tests"""
env = os.environ.copy()
2023-07-10 15:43:17 -04:00
if shell == "delete":
# remove shell
2023-07-10 15:43:17 -04:00
if "SHELL" in env:
del env["SHELL"]
elif shell == "noexec":
# specify a non-executable path
2023-07-10 15:43:17 -04:00
noexec = paths.root.join("noexec")
noexec.write("")
noexec.chmod(0o664)
2023-07-10 15:43:17 -04:00
env["SHELL"] = str(noexec)
else:
2023-07-10 15:43:17 -04:00
env["SHELL"] = shell
2023-07-10 15:43:17 -04:00
run = runner(command=yadm_cmd("enter"), env=env)
assert run.success == success
2023-07-10 15:43:17 -04:00
prompt = f"yadm shell ({paths.repo})"
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 == ""
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
@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):
"""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
# 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}")
custom_shell.chmod(0o775)
2023-07-10 15:43:17 -04:00
test_cmd = ["test1", "test2", "test3"]
2023-07-10 15:43:17 -04:00
enter_cmd = ["enter"]
if cmd:
enter_cmd += test_cmd
env = os.environ.copy()
2023-07-10 15:43:17 -04:00
env["TERM"] = term
env["SHELL"] = custom_shell
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
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
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
else:
2023-07-10 15:43:17 -04:00
assert "Entering yadm repo" in run.out
assert "Leaving yadm repo" in run.out