Merge pull request #1391 from davvid/macos/hotkeys
[git-cola.git] / test / settings_test.py
blobda6f606e8a7d1326318b9f9d2e432de602068cc4
1 """Test the cola.settings module"""
2 import os
4 import pytest
6 from cola.settings import Settings
8 from . import helper
11 @pytest.fixture(autouse=True)
12 def settings_fixture():
13 """Provide Settings that save into a temporary location to all tests"""
14 filename = helper.tmp_path('settings')
15 Settings.config_path = filename
17 yield Settings.read()
19 if os.path.exists(filename):
20 os.remove(filename)
23 def test_gui_save_restore(settings_fixture):
24 """Test saving and restoring gui state"""
25 settings = settings_fixture
26 settings.gui_state['test-gui'] = {'foo': 'bar'}
27 settings.save()
29 settings = Settings.read()
30 state = settings.gui_state.get('test-gui', {})
31 assert 'foo' in state
32 assert state['foo'] == 'bar'
35 def test_bookmarks_save_restore():
36 """Test the bookmark save/restore feature"""
37 # We automatically purge missing entries so we mock-out
38 # git.is_git_worktree() so that this bookmark is kept.
39 bookmark = {'path': '/tmp/python/thinks/this/exists', 'name': 'exists'}
41 def mock_verify(path):
42 return path == bookmark['path']
44 settings = Settings.read()
45 settings.add_bookmark(bookmark['path'], bookmark['name'])
46 settings.save()
48 settings = Settings.read(verify=mock_verify)
50 bookmarks = settings.bookmarks
51 assert len(settings.bookmarks) == 1
52 assert bookmark in bookmarks
54 settings.remove_bookmark(bookmark['path'], bookmark['name'])
55 bookmarks = settings.bookmarks
56 expect = 0
57 actual = len(bookmarks)
58 assert expect == actual
59 assert bookmark not in bookmarks
62 def test_bookmarks_removes_missing_entries():
63 """Test that missing entries are removed after a reload"""
64 # verify returns False so all entries will be removed.
65 bookmark = {'path': '.', 'name': 'does-not-exist'}
66 settings = Settings.read(verify=lambda x: False)
67 settings.add_bookmark(bookmark['path'], bookmark['name'])
68 settings.remove_missing_bookmarks()
69 settings.save()
71 settings = Settings.read()
72 bookmarks = settings.bookmarks
73 expect = 0
74 actual = len(bookmarks)
75 assert expect == actual
76 assert bookmark not in bookmarks
79 def test_rename_bookmark():
80 settings = Settings.read()
81 settings.add_bookmark('/tmp/repo', 'a')
82 settings.add_bookmark('/tmp/repo', 'b')
83 settings.add_bookmark('/tmp/repo', 'c')
85 settings.rename_bookmark('/tmp/repo', 'b', 'test')
87 expect = ['a', 'test', 'c']
88 actual = [i['name'] for i in settings.bookmarks]
89 assert expect == actual