diff --git a/src/gitup/config.py b/src/gitup/config.py index c3e6bed..1124cbb 100644 --- a/src/gitup/config.py +++ b/src/gitup/config.py @@ -147,7 +147,11 @@ def clean_bookmarks(config_path=None): delete = [ path for path in bookmarks - if not (os.path.isdir(path) or glob(os.path.expanduser(path))) + if not ( + path.lstrip().startswith("#") + or os.path.isdir(path) + or glob(os.path.expanduser(path)) + ) ] if not delete: print("All of your bookmarks are valid.") diff --git a/src/gitup/test/test_bookmarks.py b/src/gitup/test/test_bookmarks.py index c051432..9f64ac2 100644 --- a/src/gitup/test/test_bookmarks.py +++ b/src/gitup/test/test_bookmarks.py @@ -3,6 +3,8 @@ # Copyright (C) 2011-2018 Ben Kurtovic # Released under the terms of the MIT License. See LICENSE for details. +import os + from gitup import config @@ -11,3 +13,27 @@ def test_empty_list(tmpdir, capsys): config.list_bookmarks(config_path) captured = capsys.readouterr() assert captured.out == "You have no bookmarks to display.\n" + + +def test_clean_bookmarks_preserves_comments(tmpdir, capsys): + """Comments in the bookmarks file must survive a cleanup.""" + repo_dir = tmpdir / "repo" + os.makedirs(os.fspath(repo_dir)) + missing_dir = tmpdir / "missing" + + config_path = tmpdir / "config" + comment = "# a comment line" + lines = [comment, os.fspath(repo_dir), os.fspath(missing_dir)] + with open(os.fspath(config_path), "wb") as handle: + handle.write("\n".join(lines).encode("utf8")) + + config.clean_bookmarks(config_path) + + remaining = config_path.read_binary().decode("utf8").split("\n") + assert comment in remaining + assert os.fspath(repo_dir) in remaining + assert os.fspath(missing_dir) not in remaining + + captured = capsys.readouterr() + assert "Deleted bookmarks:" in captured.out + assert os.fspath(missing_dir) in captured.out