views.main: Handle the help menu in the main view
[git-cola.git] / test / test_cola_models_classic.py
blobf65e450b598a5ce0cc18cb7d7136b5d770ba74aa
1 """Covers interfaces used by the classic view."""
3 import helper
5 from cola.models.main import MainModel
7 class MainModelObserver(object):
8 """Helper class for observing changes to the model."""
9 def __init__(self, model):
10 self.paths = None
11 model.add_message_observer(model.message_paths_staged,
12 self.observe_paths)
13 model.add_message_observer(model.message_paths_unstaged,
14 self.observe_paths)
15 model.add_message_observer(model.message_paths_reverted,
16 self.observe_paths)
18 def observe_paths(self, paths=None):
19 """React to the 'paths_staged' message."""
20 self.paths = paths
23 class ClassicModelTestCase(helper.TestCase):
24 """Tests interfaces used by the classic view."""
26 def setup_baseline_repo(self, commit=True):
27 """Create a baseline repo for testing."""
28 self.shell("""
29 git init >/dev/null &&
30 touch the-file &&
31 git add the-file
32 """)
33 if commit:
34 self.shell("git commit -s -m'Initial commit' >/dev/null")
36 def test_everything(self):
37 """Test the MainModel.everything() method."""
38 self.setup_baseline_repo()
39 self.shell('touch other-file')
40 model = MainModel()
41 everything = model.everything()
42 self.assertTrue('the-file' in everything)
43 self.assertTrue('other-file' in everything)
45 def test_stage_paths(self):
46 """Test a simple usage of MainModel.stage_paths."""
47 self.setup_baseline_repo()
48 self.shell('echo change > the-file')
50 model = MainModel()
51 observer = MainModelObserver(model)
52 model.stage_paths(['the-file'])
54 self.assertTrue('the-file' in observer.paths)
56 def test_stage_paths_subdir(self):
57 """Test stage_paths() in a subdirectory."""
58 self.setup_baseline_repo()
59 self.shell("""
60 mkdir -p foo/bar &&
61 touch foo/bar/baz &&
62 git add foo/bar/baz &&
63 echo change > foo/bar/baz
64 """)
66 model = MainModel()
67 observer = MainModelObserver(model)
68 model.stage_paths(['foo'])
70 self.assertTrue('foo' in observer.paths)
71 self.assertTrue('foo/bar' in observer.paths)
72 self.assertTrue('foo/bar/baz' in observer.paths)
74 self.shell('echo change >> foo/bar/baz')
76 model.stage_paths(['foo/bar/baz'])
78 self.assertTrue('foo/bar/baz' in observer.paths)
79 self.assertTrue('foo/bar' in observer.paths)
80 self.assertTrue('foo' in observer.paths)
82 def test_stage_paths_untracked(self):
83 """Test stage_paths() with an untracked file."""
84 self.setup_baseline_repo()
85 self.shell("""
86 mkdir -p foo/bar &&
87 touch foo/bar/baz
88 """)
90 model = MainModel()
91 observer = MainModelObserver(model)
92 model.stage_paths(['foo'])
94 self.assertTrue('foo' in observer.paths)
95 self.assertTrue('foo/bar' in observer.paths)
96 self.assertTrue('foo/bar/baz' in observer.paths)
97 self.assertTrue('foo/bar/baz' in model.staged)
98 self.assertTrue('foo/bar/baz' not in model.modified)
99 self.assertTrue('foo/bar/baz' not in model.untracked)
101 def test_unstage_paths(self):
102 """Test a simple usage of unstage_paths()."""
103 self.setup_baseline_repo()
104 self.shell("""
105 echo change > the-file &&
106 git add the-file
107 """)
109 model = MainModel()
110 observer = MainModelObserver(model)
111 model.unstage_paths(['the-file'])
113 self.assertTrue('the-file' in observer.paths)
114 self.assertTrue('the-file' not in model.staged)
115 self.assertTrue('the-file' in model.modified)
117 def test_unstage_paths_init(self):
118 """Test unstage_paths() on the root commit."""
119 self.setup_baseline_repo(commit=False)
120 model = MainModel()
121 observer = MainModelObserver(model)
122 model.unstage_paths(['the-file'])
124 self.assertTrue('the-file' in observer.paths)
125 self.assertTrue('the-file' not in model.staged)
126 self.assertTrue('the-file' in model.untracked)
128 def test_unstage_paths_subdir(self):
129 """Test unstage_paths() in a subdirectory."""
130 self.setup_baseline_repo()
131 self.shell("""
132 mkdir -p foo/bar &&
133 touch foo/bar/baz &&
134 git add foo/bar/baz
135 """)
137 model = MainModel()
138 observer = MainModelObserver(model)
139 model.unstage_paths(['foo'])
141 self.assertTrue('foo' in observer.paths)
142 self.assertTrue('foo/bar' in observer.paths)
143 self.assertTrue('foo/bar/baz' in observer.paths)
144 self.assertTrue('foo/bar/baz' in model.untracked)
145 self.assertTrue('foo/bar/baz' not in model.staged)
147 def test_revert_paths(self):
148 """Test a simple use of 'revert_paths'."""
149 self.setup_baseline_repo()
150 self.shell('echo change > the-file')
152 model = MainModel()
153 observer = MainModelObserver(model)
154 model.revert_paths(['the-file'])
156 self.assertTrue('the-file' in observer.paths)
157 self.assertTrue('the-file' not in model.staged)
158 self.assertTrue('the-file' not in model.modified)
160 def test_revert_paths_subdir(self):
161 self.setup_baseline_repo()
162 self.shell("""
163 mkdir -p foo/bar &&
164 touch foo/bar/baz &&
165 git add foo/bar/baz &&
166 git commit -m'Changed foo/bar/baz' >/dev/null &&
167 echo change > foo/bar/baz
168 """)
170 model = MainModel()
171 observer = MainModelObserver(model)
172 model.revert_paths(['foo'])
174 self.assertTrue('foo' in observer.paths)
175 self.assertTrue('foo/bar' in observer.paths)
176 self.assertTrue('foo/bar/baz' in observer.paths)
177 self.assertTrue('foo/bar/baz' not in model.modified)
178 self.assertTrue('foo/bar/baz' not in model.staged)