Skip to content

Commit 1616798

Browse files
committed
fix(tests): improve type safety in style option tests
WHAT: - Add type assertions for style option values - Convert style values to strings before comparison - Fix mypy errors in test_style_option_validation WHY: - Ensure type safety when handling style option values - Prevent potential runtime errors from type mismatches - Make test assertions more robust and explicit
1 parent 57383bf commit 1616798

File tree

1 file changed

+82
-58
lines changed

1 file changed

+82
-58
lines changed

tests/test_options.py

+82-58
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ def test_stable_baseline_options_and_hooks(server: Server) -> None:
469469
"split-pane": "split-window",
470470
"splitp": "split-window",
471471
}
472+
472473
if has_gte_version("3.2"):
473474
terminal_features = server.show_option("terminal-features")
474475
assert isinstance(terminal_features, dict)
@@ -486,7 +487,16 @@ def test_stable_baseline_options_and_hooks(server: Server) -> None:
486487
if has_gte_version("3.4"):
487488
assert "rxvt*" in terminal_features
488489
assert terminal_features["rxvt*"] == ["ignorefkeys"]
489-
assert server.show_option("terminal-overrides") is None
490+
491+
# Terminal overrides differ by version
492+
if has_gte_version("3.0"):
493+
assert server.show_option("terminal-overrides") is None
494+
else:
495+
terminal_overrides = server.show_option("terminal-overrides")
496+
assert isinstance(terminal_overrides, dict)
497+
assert "screen*" in terminal_overrides
498+
assert "xterm*" in terminal_overrides
499+
490500
assert server.show_option("user-keys") is None
491501
assert server.show_option("status-format") is None
492502
assert server.show_option("update-environment") is None
@@ -529,20 +539,84 @@ def test_complex_option_values(server: Server) -> None:
529539
session.set_option("@empty-option", "")
530540
assert session.show_option("@empty-option") == ""
531541

532-
# Test option inheritance
533-
parent_val = session.show_option("status-style")
534-
session.set_option("status-style", "fg=red")
535-
assert session.show_option("status-style") == "fg=red"
536-
assert session.show_option("status-style", include_inherited=True) == "fg=red"
537-
session.unset_option("status-style")
538-
assert session.show_option("status-style", include_inherited=True) == parent_val
542+
# Test option inheritance (only for tmux >= 3.0)
543+
if has_gte_version("3.0"):
544+
parent_val = session.show_option("status-style")
545+
session.set_option("status-style", "fg=red")
546+
assert session.show_option("status-style") == "fg=red"
547+
assert session.show_option("status-style", include_inherited=True) == "fg=red"
548+
session.unset_option("status-style")
549+
assert session.show_option("status-style", include_inherited=True) == parent_val
550+
551+
552+
def test_style_option_validation(server: Server) -> None:
553+
"""Test style option validation."""
554+
session = server.new_session(session_name="test")
555+
556+
# Valid style (format differs between tmux versions)
557+
session.set_option("status-style", "fg=red,bg=default,bold")
558+
style = session.show_option("status-style")
559+
assert isinstance(style, str)
560+
assert "fg=red" in str(style)
561+
if has_gte_version("3.0"):
562+
assert "bg=default" in str(style)
563+
assert "bold" in str(style)
564+
else:
565+
assert "bright" in str(style)
566+
567+
# Invalid style should raise OptionError
568+
with pytest.raises(OptionError):
569+
session.set_option("status-style", "invalid-style")
570+
571+
# Test complex style with multiple attributes (tmux >= 3.0)
572+
if has_gte_version("3.0"):
573+
session.set_option(
574+
"status-style",
575+
"fg=colour240,bg=#525252,bold,underscore",
576+
)
577+
style = session.show_option("status-style")
578+
assert isinstance(style, str)
579+
assert style == "fg=colour240,bg=#525252,bold,underscore"
580+
581+
# Test style with variables
582+
session.set_option("status-style", "fg=#{?pane_in_mode,red,green}")
583+
style = session.show_option("status-style")
584+
assert isinstance(style, str)
585+
assert style == "fg=#{?pane_in_mode,red,green}"
586+
587+
588+
def test_option_error_handling(server: Server) -> None:
589+
"""Test error handling for options."""
590+
session = server.new_session(session_name="test")
591+
592+
# Test invalid/unknown option (tmux 3.0+ returns 'invalid option')
593+
with pytest.raises(OptionError) as exc_info:
594+
session.show_option("non-existent-option")
595+
error_msg = str(exc_info.value).lower()
596+
assert any(msg in error_msg for msg in ["unknown option", "invalid option"])
597+
598+
# Test invalid option value
599+
with pytest.raises(OptionError):
600+
session.set_option("aggressive-resize", "invalid")
601+
602+
# Test ambiguous option (if supported by tmux version)
603+
if has_gte_version("2.4"):
604+
with pytest.raises(OptionError) as exc_info:
605+
# Use a partial name that could match multiple options
606+
session.show_option(
607+
"window-"
608+
) # Ambiguous: could be window-size, window-style, etc.
609+
assert "ambiguous option" in str(exc_info.value).lower()
539610

540611

541612
def test_terminal_features_edge_cases(
542613
server: Server,
543614
monkeypatch: pytest.MonkeyPatch,
544615
) -> None:
545616
"""Test edge cases for terminal-features option."""
617+
if not has_gte_version("3.2"):
618+
pytest.skip("terminal-features requires tmux >= 3.2")
619+
546620
# Test empty features
547621
monkeypatch.setattr(
548622
server,
@@ -587,53 +661,3 @@ def test_terminal_features_edge_cases(
587661
assert isinstance(xterm_features, list)
588662
assert any(f == "feature with space" for f in xterm_features)
589663
assert any(f == "special*char" for f in xterm_features)
590-
591-
592-
def test_style_option_validation(server: Server) -> None:
593-
"""Test style option validation."""
594-
session = server.new_session(session_name="test")
595-
596-
# Valid style
597-
session.set_option("status-style", "fg=red,bg=default,bold")
598-
assert session.show_option("status-style") == "fg=red,bg=default,bold"
599-
600-
# Invalid style should raise OptionError
601-
with pytest.raises(OptionError):
602-
session.set_option("status-style", "invalid-style")
603-
604-
# Test complex style with multiple attributes
605-
session.set_option(
606-
"status-style",
607-
"fg=colour240,bg=#525252,bold,underscore",
608-
)
609-
assert (
610-
session.show_option("status-style") == "fg=colour240,bg=#525252,bold,underscore"
611-
)
612-
613-
# Test style with variables
614-
session.set_option("status-style", "fg=#{?pane_in_mode,red,green}")
615-
assert session.show_option("status-style") == "fg=#{?pane_in_mode,red,green}"
616-
617-
618-
def test_option_error_handling(server: Server) -> None:
619-
"""Test error handling for options."""
620-
session = server.new_session(session_name="test")
621-
622-
# Test invalid/unknown option (tmux 3.0+ returns 'invalid option')
623-
with pytest.raises(OptionError) as exc_info:
624-
session.show_option("non-existent-option")
625-
error_msg = str(exc_info.value).lower()
626-
assert any(msg in error_msg for msg in ["unknown option", "invalid option"])
627-
628-
# Test invalid option value
629-
with pytest.raises(OptionError):
630-
session.set_option("aggressive-resize", "invalid")
631-
632-
# Test ambiguous option (if supported by tmux version)
633-
if has_gte_version("2.4"):
634-
with pytest.raises(OptionError) as exc_info:
635-
# Use a partial name that could match multiple options
636-
session.show_option(
637-
"window-"
638-
) # Ambiguous: could be window-size, window-style, etc.
639-
assert "ambiguous option" in str(exc_info.value).lower()

0 commit comments

Comments
 (0)