Skip to content

Commit 69930a0

Browse files
committed
back up profile's config.py when overwriting
1 parent e1d9317 commit 69930a0

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ qbpm now reads configuration options from a `$XDG_CONFIG_HOME/qbpm/config.toml`!
1717

1818
## other
1919
- support for symlinking `autoconfig.yml` in addition to or instead of sourcing `config.py`
20+
- `qbpm new --overwrite`: back up existing config files by moving to e.g. `config.py.bak`
2021
- `contrib/qbpm.desktop`: add `MimeType` and `Keywords`, fix incorrect formatting of `Categories`
2122
- allow help text to be slightly wider to avoid awkward line breaks
2223
- macOS: fix detection of qutebrowser binary in /Applications

src/qbpm/profiles.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def create_config(
4545
if not source.is_file():
4646
return
4747
user_config = profile.root / "config" / "config.py"
48+
if overwrite and user_config.exists():
49+
back_up(user_config)
4850
with user_config.open(mode="w" if overwrite else "x") as dest_config:
4951
out = partial(print, file=dest_config)
5052
out(
@@ -70,12 +72,16 @@ def link_autoconfig(
7072
if not source.is_file() or dest.resolve() == source.resolve():
7173
return
7274
if overwrite and dest.exists():
73-
backup = Path(str(dest) + ".bak")
74-
info(f"backing up existing autoconfig to {backup}")
75-
dest.replace(backup)
75+
back_up(dest)
7676
dest.symlink_to(source)
7777

7878

79+
def back_up(dest: Path) -> None:
80+
backup = Path(str(dest) + ".bak")
81+
info(f"backing up existing {dest.name} to {backup}")
82+
dest.replace(backup)
83+
84+
7985
def check(profile: Profile) -> bool:
8086
if not profile.check_name():
8187
return False

tests/test_profiles.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ def test_overwrite_config(tmp_path: Path):
7070
url = "http://example.com"
7171
config_dir = profile.root / "config"
7272
config_dir.mkdir(parents=True)
73+
config = config_dir / "config.py"
74+
backup = config_dir / "config.py.bak"
7375
profiles.create_config(profile, tmp_path, "")
7476
profiles.create_config(profile, tmp_path, "", url, True)
75-
assert list(config_dir.iterdir()) == [config_dir / "config.py"]
76-
with (config_dir / "config.py").open() as conf:
77-
for line in conf:
78-
if url in line:
79-
return
80-
raise AssertionError()
77+
assert set(config_dir.iterdir()) == {config, backup}
78+
assert url in config.read_text()
79+
assert url not in backup.read_text()
8180

8281

8382
def test_link_autoconfig(tmp_path: Path):

0 commit comments

Comments
 (0)