From 69930a0d39fbfee24e131c100b0046b638444a5b Mon Sep 17 00:00:00 2001 From: Peter Rice Date: Sat, 23 Aug 2025 16:43:02 -0400 Subject: [PATCH] back up profile's config.py when overwriting --- CHANGELOG.md | 1 + src/qbpm/profiles.py | 12 +++++++++--- tests/test_profiles.py | 11 +++++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd94c04..75096f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ qbpm now reads configuration options from a `$XDG_CONFIG_HOME/qbpm/config.toml`! ## other - support for symlinking `autoconfig.yml` in addition to or instead of sourcing `config.py` + - `qbpm new --overwrite`: back up existing config files by moving to e.g. `config.py.bak` - `contrib/qbpm.desktop`: add `MimeType` and `Keywords`, fix incorrect formatting of `Categories` - allow help text to be slightly wider to avoid awkward line breaks - macOS: fix detection of qutebrowser binary in /Applications diff --git a/src/qbpm/profiles.py b/src/qbpm/profiles.py index c75fcef..e11d338 100644 --- a/src/qbpm/profiles.py +++ b/src/qbpm/profiles.py @@ -45,6 +45,8 @@ def create_config( if not source.is_file(): return user_config = profile.root / "config" / "config.py" + if overwrite and user_config.exists(): + back_up(user_config) with user_config.open(mode="w" if overwrite else "x") as dest_config: out = partial(print, file=dest_config) out( @@ -70,12 +72,16 @@ def link_autoconfig( if not source.is_file() or dest.resolve() == source.resolve(): return if overwrite and dest.exists(): - backup = Path(str(dest) + ".bak") - info(f"backing up existing autoconfig to {backup}") - dest.replace(backup) + back_up(dest) dest.symlink_to(source) +def back_up(dest: Path) -> None: + backup = Path(str(dest) + ".bak") + info(f"backing up existing {dest.name} to {backup}") + dest.replace(backup) + + def check(profile: Profile) -> bool: if not profile.check_name(): return False diff --git a/tests/test_profiles.py b/tests/test_profiles.py index 3b7c3c9..cc10a61 100644 --- a/tests/test_profiles.py +++ b/tests/test_profiles.py @@ -70,14 +70,13 @@ def test_overwrite_config(tmp_path: Path): url = "http://example.com" config_dir = profile.root / "config" config_dir.mkdir(parents=True) + config = config_dir / "config.py" + backup = config_dir / "config.py.bak" profiles.create_config(profile, tmp_path, "") profiles.create_config(profile, tmp_path, "", url, True) - assert list(config_dir.iterdir()) == [config_dir / "config.py"] - with (config_dir / "config.py").open() as conf: - for line in conf: - if url in line: - return - raise AssertionError() + assert set(config_dir.iterdir()) == {config, backup} + assert url in config.read_text() + assert url not in backup.read_text() def test_link_autoconfig(tmp_path: Path): -- 2.51.2