fix: clean up config.py unused imports, null-YAML safety, and add non-default roundtrip test

This commit is contained in:
2026-05-21 12:08:56 +08:00
parent 53133ccb67
commit 62414399b1
2 changed files with 29 additions and 9 deletions

View File

@@ -1,7 +1,6 @@
import os import os
from dataclasses import dataclass, asdict, field from dataclasses import dataclass, asdict, fields as dc_fields
from pathlib import Path from pathlib import Path
from typing import Optional
import click import click
import yaml import yaml
@@ -28,9 +27,11 @@ class TunnelConfig:
ssh_options: str = "" ssh_options: str = ""
_DEFAULTS = TunnelConfig( _OPTIONAL_DEFAULTS = {
name="", host="", user="", local_port=0, remote_port=0 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: def ensure_dirs(config_dir: Path) -> None:
@@ -47,7 +48,7 @@ def load_tunnels(config_dir: Path) -> list[TunnelConfig]:
if not tunnels_file.exists(): if not tunnels_file.exists():
return [] return []
data = yaml.safe_load(tunnels_file.read_text()) or {} 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: 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: for t in tunnels:
d = asdict(t) d = asdict(t)
# Omit optional fields that are at their default values # Omit optional fields that are at their default values
for key in ("port", "identity_file", "local_host", "remote_host", "ssh_options"): for key, default in _OPTIONAL_DEFAULTS.items():
if d[key] == getattr(_DEFAULTS, key): if d[key] == default:
del d[key] del d[key]
rows.append(d) rows.append(d)
tmp = config_dir / "tunnels.yaml.tmp" tmp = config_dir / "tunnels.yaml.tmp"

View File

@@ -2,7 +2,7 @@ import pytest
import click import click
from autossh_mgr.config import ( from autossh_mgr.config import (
TunnelConfig, ensure_dirs, load_tunnels, save_tunnels, get_tunnel 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): 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)) monkeypatch.setenv("AUTOSSH_MGR_CONFIG_DIR", str(tmp_path))
from autossh_mgr.config import get_config_dir from autossh_mgr.config import get_config_dir
assert get_config_dir() == tmp_path 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"