Skip to content

Commit abd9771

Browse files
committed
add tests for if conditions
Signed-off-by: Tin Lai <[email protected]>
1 parent 5c38079 commit abd9771

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

examples/if-conditions-test.yaml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
session_name: if conditions test conditions
2+
environment:
3+
foo: 'false'
4+
bar: '1'
5+
F: '0'
6+
windows:
7+
- window_name: window all false
8+
panes:
9+
- if: ${foo}
10+
shell_command:
11+
- echo pane 1
12+
- if:
13+
shell: '[ 1 -gt 2 ]'
14+
shell_command:
15+
- echo pane 2
16+
- if:
17+
shell_var: ${F}
18+
- window_name: window 2 of 3 true
19+
panes:
20+
- if:
21+
shell: '[ "foo" = "bar" ]'
22+
shell_command:
23+
- echo pane 3
24+
- if:
25+
shell: '[ "hello" != "byte" ]'
26+
shell_command:
27+
- echo pane 4
28+
- if:
29+
python: '2**4 == 16'
30+
shell_command:
31+
- echo pane 5
32+
- window_name: window 2 of 4 true
33+
panes:
34+
- if:
35+
shell_var: 'FALSE'
36+
shell_command:
37+
- echo pane 6
38+
- if:
39+
shell_var: ${bar}
40+
shell_command:
41+
- echo pane 7
42+
- if:
43+
python: import os; not os.path.isdir('/a/very/random/path')
44+
shell_command:
45+
- echo pane 8
46+
- if: ${non_existing_var}
47+
shell_command:
48+
- echo pane 9

examples/if-conditions.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ windows:
1919
- echo the above is a false statement
2020
# python condition
2121
- if:
22-
python: import os; os.path.isdir('${PWD}')
22+
python: import os; os.path.isdir(os.path.expandvars('${PWD}'))
2323
shell_command:
2424
- echo "checking for PWD (${PWD}) is a directory in python"
2525
- python -m http.server

tests/workspace/test_builder.py

+28
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,34 @@ def test_blank_pane_spawn(
563563
assert len(window4.panes) == 2
564564

565565

566+
def test_if_conditions(
567+
session: Session,
568+
) -> None:
569+
"""
570+
Test various ways of spawning panes with conditions from a tmuxp configuration.
571+
"""
572+
yaml_workspace_file = EXAMPLE_PATH / "if-conditions-test.yaml"
573+
test_config = ConfigReader._from_file(yaml_workspace_file)
574+
575+
test_config = loader.expand(test_config)
576+
builder = WorkspaceBuilder(session_config=test_config, server=session.server)
577+
builder.build(session=session)
578+
579+
assert session == builder.session
580+
581+
with pytest.raises(ObjectDoesNotExist):
582+
window1 = session.windows.get(window_name="window all false")
583+
assert window1 is None
584+
585+
window2 = session.windows.get(window_name="window 2 of 3 true")
586+
assert window2 is not None
587+
assert len(window2.panes) == 2
588+
589+
window3 = session.windows.get(window_name="window 2 of 4 true")
590+
assert window3 is not None
591+
assert len(window3.panes) == 2
592+
593+
566594
def test_start_directory(session: Session, tmp_path: pathlib.Path) -> None:
567595
"""Test workspace builder setting start_directory relative to current directory."""
568596
test_dir = tmp_path / "foo bar"

0 commit comments

Comments
 (0)