1 from __future__
import absolute_import
, division
, unicode_literals
9 class ColaUtilsTestCase(unittest
.TestCase
):
10 """Tests the cola.utils module."""
12 def test_basename(self
):
13 """Test the utils.basename function."""
14 self
.assertEqual(utils
.basename('bar'), 'bar')
15 self
.assertEqual(utils
.basename('/bar'), 'bar')
16 self
.assertEqual(utils
.basename('/bar '), 'bar ')
17 self
.assertEqual(utils
.basename('foo/bar'), 'bar')
18 self
.assertEqual(utils
.basename('/foo/bar'), 'bar')
19 self
.assertEqual(utils
.basename('foo/foo/bar'), 'bar')
20 self
.assertEqual(utils
.basename('/foo/foo/bar'), 'bar')
21 self
.assertEqual(utils
.basename('/foo/foo//bar'), 'bar')
22 self
.assertEqual(utils
.basename('////foo //foo//bar'), 'bar')
24 def test_dirname(self
):
25 """Test the utils.dirname function."""
26 self
.assertEqual(utils
.dirname('bar'), '')
27 self
.assertEqual(utils
.dirname('/bar'), '')
28 self
.assertEqual(utils
.dirname('//bar'), '')
29 self
.assertEqual(utils
.dirname('///bar'), '')
30 self
.assertEqual(utils
.dirname('foo/bar'), 'foo')
31 self
.assertEqual(utils
.dirname('foo//bar'), 'foo')
32 self
.assertEqual(utils
.dirname('foo /bar'), 'foo ')
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')
38 def test_add_parents(self
):
39 """Test the utils.add_parents() function."""
40 paths
= set(['foo///bar///baz'])
41 path_set
= utils
.add_parents(paths
)
43 self
.assertTrue('foo/bar/baz' in path_set
)
44 self
.assertTrue('foo/bar' in path_set
)
45 self
.assertTrue('foo' in path_set
)
46 self
.assertFalse('foo///bar///baz' in path_set
)
48 # Ensure that the original set is unchanged
49 expect
= set(['foo///bar///baz'])
50 self
.assertEqual(expect
, paths
)
52 def test_tmp_filename_gives_good_file(self
):
53 first
= utils
.tmp_filename('test')
54 second
= utils
.tmp_filename('test')
56 self
.assertFalse(core
.exists(first
))
57 self
.assertFalse(core
.exists(second
))
59 self
.assertNotEqual(first
, second
)
60 self
.assertTrue(os
.path
.basename(first
).startswith('git-cola-test'))
61 self
.assertTrue(os
.path
.basename(second
).startswith('git-cola-test'))
63 def test_strip_one_abspath(self
):
65 actual
= utils
.strip_one('/usr/bin/git')
66 self
.assertEqual(expect
, actual
)
68 def test_strip_one_relpath(self
):
70 actual
= utils
.strip_one('bin/git')
71 self
.assertEqual(expect
, actual
)
73 def test_strip_one_nested_relpath(self
):
75 actual
= utils
.strip_one('local/bin/git')
76 self
.assertEqual(expect
, actual
)
78 def test_strip_one_basename(self
):
80 actual
= utils
.strip_one('git')
81 self
.assertEqual(expect
, actual
)
83 def test_select_directory(self
):
84 filename
= utils
.tmp_filename('test')
85 expect
= os
.path
.dirname(filename
)
86 actual
= utils
.select_directory([filename
])
87 self
.assertEqual(expect
, actual
)
89 def test_select_directory_prefers_directories(self
):
90 filename
= utils
.tmp_filename('test')
92 actual
= utils
.select_directory([filename
, '.'])
93 self
.assertEqual(expect
, actual
)