From 62414399b1691d6572c4db10745d7ff93ffb4b7e Mon Sep 17 00:00:00 2001 From: tech Date: Thu, 21 May 2026 12:08:56 +0800 Subject: [PATCH] fix: clean up config.py unused imports, null-YAML safety, and add non-default roundtrip test --- src/autossh_mgr/config.py | 17 +++++++++-------- tests/unit/test_config.py | 21 ++++++++++++++++++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/autossh_mgr/config.py b/src/autossh_mgr/config.py index a9ace4c..6fd8676 100644 --- a/src/autossh_mgr/config.py +++ b/src/autossh_mgr/config.py @@ -1,7 +1,6 @@ import os -from dataclasses import dataclass, asdict, field +from dataclasses import dataclass, asdict, fields as dc_fields from pathlib import Path -from typing import Optional import click import yaml @@ -28,9 +27,11 @@ class TunnelConfig: ssh_options: str = "" -_DEFAULTS = TunnelConfig( - name="", host="", user="", local_port=0, remote_port=0 -) +_OPTIONAL_DEFAULTS = { + f.name: f.default + for f in dc_fields(TunnelConfig) + if f.name in ("port", "identity_file", "local_host", "remote_host", "ssh_options") +} def ensure_dirs(config_dir: Path) -> None: @@ -47,7 +48,7 @@ def load_tunnels(config_dir: Path) -> list[TunnelConfig]: if not tunnels_file.exists(): return [] data = yaml.safe_load(tunnels_file.read_text()) or {} - return [TunnelConfig(**entry) for entry in data.get("tunnels", [])] + return [TunnelConfig(**entry) for entry in (data.get("tunnels") or [])] def save_tunnels(config_dir: Path, tunnels: list[TunnelConfig]) -> None: @@ -55,8 +56,8 @@ def save_tunnels(config_dir: Path, tunnels: list[TunnelConfig]) -> None: for t in tunnels: d = asdict(t) # Omit optional fields that are at their default values - for key in ("port", "identity_file", "local_host", "remote_host", "ssh_options"): - if d[key] == getattr(_DEFAULTS, key): + for key, default in _OPTIONAL_DEFAULTS.items(): + if d[key] == default: del d[key] rows.append(d) tmp = config_dir / "tunnels.yaml.tmp" diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 80d9596..3979423 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -2,7 +2,7 @@ import pytest import click from autossh_mgr.config import ( TunnelConfig, ensure_dirs, load_tunnels, save_tunnels, get_tunnel -) +) # TunnelConfig kept for test_save_preserves_non_default_values def test_ensure_dirs_creates_structure(tmp_path): @@ -59,3 +59,22 @@ def test_get_config_dir_env_override(monkeypatch, tmp_path): monkeypatch.setenv("AUTOSSH_MGR_CONFIG_DIR", str(tmp_path)) from autossh_mgr.config import get_config_dir assert get_config_dir() == tmp_path + + +def test_save_preserves_non_default_values(config_dir): + tunnel = TunnelConfig( + name="custom", host="h.com", user="u", + local_port=80, remote_port=8080, + port=2222, + identity_file="~/.ssh/custom_key", + local_host="0.0.0.0", + remote_host="127.0.0.1", + ssh_options="-v", + ) + save_tunnels(config_dir, [tunnel]) + loaded = load_tunnels(config_dir)[0] + assert loaded.port == 2222 + assert loaded.identity_file == "~/.ssh/custom_key" + assert loaded.local_host == "0.0.0.0" + assert loaded.remote_host == "127.0.0.1" + assert loaded.ssh_options == "-v"