~bzr-explorer-dev/bzr-explorer/trunk

« back to all changes in this revision

Viewing changes to tests/test_history_manager.py

Cope with broken history.dat files rather than failing on startup

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 
16
 
 
17
"""Tests for saving and restoring recent history of locations"""
 
18
 
 
19
import os
 
20
 
 
21
from bzrlib.plugins.explorer import tests
 
22
from bzrlib.plugins.explorer.lib import history_manager
 
23
from bzrlib.plugins.explorer.lib.extensions import accessories
 
24
 
 
25
 
 
26
class TestHistoryManager(tests.TestCaseInTempDir):
 
27
    """Tests for the HistoryManager class"""
 
28
 
 
29
    def setUp(self):
 
30
        super(TestHistoryManager, self).setUp()
 
31
        directory = accessories.wallet_dir()
 
32
        os.makedirs(directory)
 
33
        self.history_filename = os.path.join(directory, "history.dat")
 
34
 
 
35
    # TODO: add tests for correct basic operation here
 
36
 
 
37
    def test_no_history_file(self):
 
38
        history = history_manager.HistoryManager()
 
39
        self.assertLength(0, history.recent_locations())
 
40
        # Not having history.dat yet is normal so there should be no log spam
 
41
        self.assertNotContainsRe(self.get_log(), "history.dat")
 
42
 
 
43
    def test_broken_history_empty_file(self):
 
44
        open(self.history_filename, "wb").close()
 
45
        history = history_manager.HistoryManager()
 
46
        self.assertLength(0, history.recent_locations())
 
47
        self.assertContainsRe(self.get_log(), "explorer history.*history.dat")
 
48
 
 
49
    def test_broken_history_zeroed_file(self):
 
50
        f = open(self.history_filename, "wb")
 
51
        try:
 
52
            f.write("\x00" * 100)
 
53
        finally:
 
54
            f.close()
 
55
        history = history_manager.HistoryManager()
 
56
        self.assertLength(0, history.recent_locations())
 
57
        self.assertContainsRe(self.get_log(), "explorer history.*history.dat")