Skip to content

Commit 47500e5

Browse files
committed
refactor! Refactor CLI tests
1 parent b31a692 commit 47500e5

File tree

8 files changed

+1305
-1266
lines changed

8 files changed

+1305
-1266
lines changed

tests/cli/test_cli.py

Lines changed: 3 additions & 1266 deletions
Large diffs are not rendered by default.

tests/cli/test_convert.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import io
2+
import json
3+
import pathlib
4+
import typing as t
5+
6+
import pytest
7+
8+
from tmuxp import cli
9+
10+
11+
@pytest.mark.parametrize(
12+
"cli_args",
13+
[
14+
(["convert", "."]),
15+
(["convert", ".tmuxp.yaml"]),
16+
(["convert", ".tmuxp.yaml", "-y"]),
17+
(["convert", ".tmuxp.yml"]),
18+
(["convert", ".tmuxp.yml", "-y"]),
19+
],
20+
)
21+
def test_convert(
22+
cli_args: t.List[str],
23+
tmp_path: pathlib.Path,
24+
monkeypatch: pytest.MonkeyPatch,
25+
) -> None:
26+
# create dummy tmuxp yaml so we don't get yelled at
27+
filename = cli_args[1]
28+
if filename == ".":
29+
filename = ".tmuxp.yaml"
30+
file_ext = filename.rsplit(".", 1)[-1]
31+
assert file_ext in ["yaml", "yml"], file_ext
32+
workspace_file_path = tmp_path / filename
33+
workspace_file_path.write_text("\nsession_name: hello\n", encoding="utf-8")
34+
oh_my_zsh_path = tmp_path / ".oh-my-zsh"
35+
oh_my_zsh_path.mkdir()
36+
monkeypatch.setenv("HOME", str(tmp_path))
37+
38+
monkeypatch.chdir(tmp_path)
39+
40+
# If autoconfirm (-y) no need to prompt y
41+
input_args = "y\ny\n" if "-y" not in cli_args else ""
42+
43+
monkeypatch.setattr("sys.stdin", io.StringIO(input_args))
44+
try:
45+
cli.cli(cli_args)
46+
except SystemExit:
47+
pass
48+
tmuxp_json = tmp_path / ".tmuxp.json"
49+
assert tmuxp_json.exists()
50+
assert tmuxp_json.open().read() == json.dumps({"session_name": "hello"}, indent=2)
51+
52+
53+
@pytest.mark.parametrize(
54+
"cli_args",
55+
[
56+
(["convert", "."]),
57+
(["convert", ".tmuxp.json"]),
58+
(["convert", ".tmuxp.json", "-y"]),
59+
],
60+
)
61+
def test_convert_json(
62+
cli_args: t.List[str],
63+
tmp_path: pathlib.Path,
64+
monkeypatch: pytest.MonkeyPatch,
65+
) -> None:
66+
# create dummy tmuxp yaml so we don't get yelled at
67+
json_config = tmp_path / ".tmuxp.json"
68+
json_config.write_text('{"session_name": "hello"}', encoding="utf-8")
69+
oh_my_zsh_path = tmp_path / ".oh-my-zsh"
70+
oh_my_zsh_path.mkdir()
71+
monkeypatch.setenv("HOME", str(tmp_path))
72+
73+
monkeypatch.chdir(tmp_path)
74+
75+
# If autoconfirm (-y) no need to prompt y
76+
input_args = "y\ny\n" if "-y" not in cli_args else ""
77+
78+
monkeypatch.setattr("sys.stdin", io.StringIO(input_args))
79+
try:
80+
cli.cli(cli_args)
81+
except SystemExit:
82+
pass
83+
84+
tmuxp_yaml = tmp_path / ".tmuxp.yaml"
85+
assert tmuxp_yaml.exists()
86+
assert tmuxp_yaml.open().read() == "session_name: hello\n"

tests/cli/test_debug_info.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pathlib
2+
3+
import pytest
4+
5+
from tmuxp import cli
6+
7+
8+
def test_debug_info_cli(
9+
monkeypatch: pytest.MonkeyPatch,
10+
tmp_path: pathlib.Path,
11+
capsys: pytest.CaptureFixture,
12+
) -> None:
13+
monkeypatch.setenv("SHELL", "/bin/bash")
14+
15+
cli.cli(["debug-info"])
16+
cli_output = capsys.readouterr().out
17+
assert "environment" in cli_output
18+
assert "python version" in cli_output
19+
assert "system PATH" in cli_output
20+
assert "tmux version" in cli_output
21+
assert "libtmux version" in cli_output
22+
assert "tmuxp version" in cli_output
23+
assert "tmux path" in cli_output
24+
assert "tmuxp path" in cli_output
25+
assert "shell" in cli_output
26+
assert "tmux session" in cli_output
27+
assert "tmux windows" in cli_output
28+
assert "tmux panes" in cli_output
29+
assert "tmux global options" in cli_output
30+
assert "tmux window options" in cli_output

tests/cli/test_freeze.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import io
2+
import pathlib
3+
import typing as t
4+
5+
import pytest
6+
7+
from libtmux.server import Server
8+
from tmuxp import cli
9+
from tmuxp.config_reader import ConfigReader
10+
11+
12+
@pytest.mark.parametrize(
13+
"cli_args,inputs",
14+
[
15+
(["freeze", "myfrozensession"], ["y\n", "./la.yaml\n", "y\n"]),
16+
( # Exists
17+
["freeze", "myfrozensession"],
18+
["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
19+
),
20+
( # Imply current session if not entered
21+
["freeze"],
22+
["y\n", "./la.yaml\n", "y\n"],
23+
),
24+
(["freeze"], ["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"]), # Exists
25+
],
26+
)
27+
def test_freeze(
28+
server: "Server",
29+
cli_args: t.List[str],
30+
inputs: t.List[str],
31+
tmp_path: pathlib.Path,
32+
monkeypatch: pytest.MonkeyPatch,
33+
) -> None:
34+
monkeypatch.setenv("HOME", str(tmp_path))
35+
exists_yaml = tmp_path / "exists.yaml"
36+
exists_yaml.touch()
37+
38+
server.new_session(session_name="myfirstsession")
39+
server.new_session(session_name="myfrozensession")
40+
41+
# Assign an active pane to the session
42+
second_session = server.sessions[1]
43+
first_pane_on_second_session_id = second_session.windows[0].panes[0].pane_id
44+
assert first_pane_on_second_session_id
45+
monkeypatch.setenv("TMUX_PANE", first_pane_on_second_session_id)
46+
47+
monkeypatch.chdir(tmp_path)
48+
# Use tmux server (socket name) used in the test
49+
assert server.socket_name is not None
50+
cli_args = cli_args + ["-L", server.socket_name]
51+
52+
monkeypatch.setattr("sys.stdin", io.StringIO("".join(inputs)))
53+
try:
54+
cli.cli(cli_args)
55+
except SystemExit:
56+
pass
57+
58+
yaml_config_path = tmp_path / "la.yaml"
59+
assert yaml_config_path.exists()
60+
61+
yaml_config = yaml_config_path.open().read()
62+
frozen_config = ConfigReader._load(format="yaml", content=yaml_config)
63+
64+
assert frozen_config["session_name"] == "myfrozensession"
65+
66+
67+
@pytest.mark.parametrize(
68+
"cli_args,inputs",
69+
[
70+
( # Overwrite
71+
["freeze", "mysession", "--force"],
72+
["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
73+
),
74+
( # Imply current session if not entered
75+
["freeze", "--force"],
76+
["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
77+
),
78+
],
79+
)
80+
def test_freeze_overwrite(
81+
server: "Server",
82+
cli_args: t.List[str],
83+
inputs: t.List[str],
84+
tmp_path: pathlib.Path,
85+
monkeypatch: pytest.MonkeyPatch,
86+
) -> None:
87+
monkeypatch.setenv("HOME", str(tmp_path))
88+
exists_yaml = tmp_path / "exists.yaml"
89+
exists_yaml.touch()
90+
91+
server.new_session(session_name="mysession")
92+
93+
monkeypatch.chdir(tmp_path)
94+
# Use tmux server (socket name) used in the test
95+
assert server.socket_name is not None
96+
cli_args = cli_args + ["-L", server.socket_name]
97+
98+
monkeypatch.setattr("sys.stdin", io.StringIO("".join(inputs)))
99+
try:
100+
cli.cli(cli_args)
101+
except SystemExit:
102+
pass
103+
104+
yaml_config_path = tmp_path / "exists.yaml"
105+
assert yaml_config_path.exists()

tests/cli/test_import.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import io
2+
import pathlib
3+
import typing as t
4+
5+
import pytest
6+
7+
from tmuxp import cli
8+
9+
from ..fixtures import utils as test_utils
10+
11+
12+
@pytest.mark.parametrize("cli_args", [(["import"])])
13+
def test_import(
14+
cli_args: t.List[str],
15+
tmp_path: pathlib.Path,
16+
monkeypatch: pytest.MonkeyPatch,
17+
capsys: pytest.CaptureFixture,
18+
) -> None:
19+
cli.cli(cli_args)
20+
result = capsys.readouterr()
21+
assert "tmuxinator" in result.out
22+
assert "teamocil" in result.out
23+
24+
25+
@pytest.mark.parametrize(
26+
"cli_args,inputs",
27+
[
28+
(
29+
["import", "teamocil", "./.teamocil/config.yaml"],
30+
["\n", "y\n", "./la.yaml\n", "y\n"],
31+
),
32+
(
33+
["import", "teamocil", "./.teamocil/config.yaml"],
34+
["\n", "y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
35+
),
36+
(
37+
["import", "teamocil", "config"],
38+
["\n", "y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
39+
),
40+
],
41+
)
42+
def test_import_teamocil(
43+
cli_args: t.List[str],
44+
inputs: t.List[str],
45+
tmp_path: pathlib.Path,
46+
monkeypatch: pytest.MonkeyPatch,
47+
) -> None:
48+
teamocil_config = test_utils.read_workspace_file("import_teamocil/test4.yaml")
49+
50+
teamocil_path = tmp_path / ".teamocil"
51+
teamocil_path.mkdir()
52+
53+
teamocil_config_path = teamocil_path / "config.yaml"
54+
teamocil_config_path.write_text(teamocil_config, encoding="utf-8")
55+
56+
exists_yaml = tmp_path / "exists.yaml"
57+
exists_yaml.touch()
58+
59+
monkeypatch.setenv("HOME", str(tmp_path))
60+
61+
monkeypatch.chdir(tmp_path)
62+
monkeypatch.setattr("sys.stdin", io.StringIO("".join(inputs)))
63+
64+
try:
65+
cli.cli(cli_args)
66+
except SystemExit:
67+
pass
68+
69+
new_config_yaml = tmp_path / "la.yaml"
70+
assert new_config_yaml.exists()
71+
72+
73+
@pytest.mark.parametrize(
74+
"cli_args,inputs",
75+
[
76+
(
77+
["import", "tmuxinator", "./.tmuxinator/config.yaml"],
78+
["\n", "y\n", "./la.yaml\n", "y\n"],
79+
),
80+
(
81+
["import", "tmuxinator", "./.tmuxinator/config.yaml"],
82+
["\n", "y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
83+
),
84+
(
85+
["import", "tmuxinator", "config"],
86+
["\n", "y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
87+
),
88+
],
89+
)
90+
def test_import_tmuxinator(
91+
cli_args: t.List[str],
92+
inputs: t.List[str],
93+
tmp_path: pathlib.Path,
94+
monkeypatch: pytest.MonkeyPatch,
95+
) -> None:
96+
tmuxinator_config = test_utils.read_workspace_file("import_tmuxinator/test3.yaml")
97+
98+
tmuxinator_path = tmp_path / ".tmuxinator"
99+
tmuxinator_path.mkdir()
100+
101+
tmuxinator_config_path = tmuxinator_path / "config.yaml"
102+
tmuxinator_config_path.write_text(tmuxinator_config, encoding="utf-8")
103+
104+
exists_yaml = tmp_path / "exists.yaml"
105+
exists_yaml.touch()
106+
107+
monkeypatch.setenv("HOME", str(tmp_path))
108+
109+
monkeypatch.chdir(tmp_path)
110+
111+
monkeypatch.setattr("sys.stdin", io.StringIO("".join(inputs)))
112+
try:
113+
cli.cli(cli_args)
114+
except SystemExit:
115+
pass
116+
117+
new_config_yaml = tmp_path / "la.yaml"
118+
assert new_config_yaml.exists()

0 commit comments

Comments
 (0)