3 from __future__
import absolute_import
, division
, unicode_literals
12 class ColaUtilsTestCase(unittest
.TestCase
):
13 """Tests the cola.utils module."""
15 def test_basename(self
):
16 """Test the utils.basename function."""
17 self
.assertEqual(utils
.basename('bar'), 'bar')
18 self
.assertEqual(utils
.basename('/bar'), 'bar')
19 self
.assertEqual(utils
.basename('/bar '), 'bar ')
20 self
.assertEqual(utils
.basename('foo/bar'), 'bar')
21 self
.assertEqual(utils
.basename('/foo/bar'), 'bar')
22 self
.assertEqual(utils
.basename('foo/foo/bar'), 'bar')
23 self
.assertEqual(utils
.basename('/foo/foo/bar'), 'bar')
24 self
.assertEqual(utils
.basename('/foo/foo//bar'), 'bar')
25 self
.assertEqual(utils
.basename('////foo //foo//bar'), 'bar')
27 def test_dirname(self
):
28 """Test the utils.dirname function."""
29 self
.assertEqual(utils
.dirname('bar'), '')
30 self
.assertEqual(utils
.dirname('/bar'), '')
31 self
.assertEqual(utils
.dirname('//bar'), '')
32 self
.assertEqual(utils
.dirname('///bar'), '')
33 self
.assertEqual(utils
.dirname('foo/bar'), 'foo')
34 self
.assertEqual(utils
.dirname('foo//bar'), 'foo')
35 self
.assertEqual(utils
.dirname('foo /bar'), 'foo ')
36 self
.assertEqual(utils
.dirname('/foo//bar'), '/foo')
37 self
.assertEqual(utils
.dirname('/foo /bar'), '/foo ')
38 self
.assertEqual(utils
.dirname('//foo//bar'), '/foo')
39 self
.assertEqual(utils
.dirname('///foo///bar'), '/foo')
41 def test_add_parents(self
):
42 """Test the utils.add_parents() function."""
43 paths
= set(['foo///bar///baz'])
44 path_set
= utils
.add_parents(paths
)
46 self
.assertTrue('foo/bar/baz' in path_set
)
47 self
.assertTrue('foo/bar' in path_set
)
48 self
.assertTrue('foo' in path_set
)
49 self
.assertFalse('foo///bar///baz' in path_set
)
51 # Ensure that the original set is unchanged
52 expect
= set(['foo///bar///baz'])
53 self
.assertEqual(expect
, paths
)
55 def test_tmp_filename_gives_good_file(self
):
56 first
= utils
.tmp_filename('test')
57 second
= utils
.tmp_filename('test')
59 self
.assertFalse(core
.exists(first
))
60 self
.assertFalse(core
.exists(second
))
62 self
.assertNotEqual(first
, second
)
63 self
.assertTrue(os
.path
.basename(first
).startswith('git-cola-test'))
64 self
.assertTrue(os
.path
.basename(second
).startswith('git-cola-test'))
66 def test_strip_one_abspath(self
):
68 actual
= utils
.strip_one('/usr/bin/git')
69 self
.assertEqual(expect
, actual
)
71 def test_strip_one_relpath(self
):
73 actual
= utils
.strip_one('bin/git')
74 self
.assertEqual(expect
, actual
)
76 def test_strip_one_nested_relpath(self
):
78 actual
= utils
.strip_one('local/bin/git')
79 self
.assertEqual(expect
, actual
)
81 def test_strip_one_basename(self
):
83 actual
= utils
.strip_one('git')
84 self
.assertEqual(expect
, actual
)
86 def test_select_directory(self
):
87 filename
= utils
.tmp_filename('test')
88 expect
= os
.path
.dirname(filename
)
89 actual
= utils
.select_directory([filename
])
90 self
.assertEqual(expect
, actual
)
92 def test_select_directory_prefers_directories(self
):
93 filename
= utils
.tmp_filename('test')
95 actual
= utils
.select_directory([filename
, '.'])
96 self
.assertEqual(expect
, actual
)
99 if __name__
== '__main__':