1 """Covers interfaces used by the classic view."""
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
):
11 model
.add_message_observer(model
.message_paths_staged
,
13 model
.add_message_observer(model
.message_paths_unstaged
,
15 model
.add_message_observer(model
.message_paths_reverted
,
18 def observe_paths(self
, paths
=None):
19 """React to the 'paths_staged' message."""
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."""
29 git init >/dev/null &&
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')
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')
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()
62 git add foo/bar/baz &&
63 echo change > foo/bar/baz
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()
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()
105 echo change > the-file &&
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)
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()
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')
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()
165 git add foo/bar/baz &&
166 git commit -m'Changed foo/bar/baz' >/dev/null &&
167 echo change > foo/bar/baz
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
)