git-cola v3.10.1
[git-cola.git] / test / utils_test.py
blob1fc36366c9ac4569221d907e88e1bcb27e091c44
1 #!/usr/bin/env python
2 from __future__ import absolute_import, division, unicode_literals
4 import os
5 import unittest
7 from cola import core
8 from cola import utils
11 class ColaUtilsTestCase(unittest.TestCase):
12 """Tests the cola.utils module."""
14 def test_basename(self):
15 """Test the utils.basename function."""
16 self.assertEqual(utils.basename('bar'), 'bar')
17 self.assertEqual(utils.basename('/bar'), 'bar')
18 self.assertEqual(utils.basename('/bar '), 'bar ')
19 self.assertEqual(utils.basename('foo/bar'), 'bar')
20 self.assertEqual(utils.basename('/foo/bar'), 'bar')
21 self.assertEqual(utils.basename('foo/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')
26 def test_dirname(self):
27 """Test the utils.dirname function."""
28 self.assertEqual(utils.dirname('bar'), '')
29 self.assertEqual(utils.dirname('/bar'), '')
30 self.assertEqual(utils.dirname('//bar'), '')
31 self.assertEqual(utils.dirname('///bar'), '')
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 ')
37 self.assertEqual(utils.dirname('//foo//bar'), '/foo')
38 self.assertEqual(utils.dirname('///foo///bar'), '/foo')
40 def test_add_parents(self):
41 """Test the utils.add_parents() function."""
42 paths = set(['foo///bar///baz'])
43 path_set = utils.add_parents(paths)
45 self.assertTrue('foo/bar/baz' in path_set)
46 self.assertTrue('foo/bar' in path_set)
47 self.assertTrue('foo' in path_set)
48 self.assertFalse('foo///bar///baz' in path_set)
50 # Ensure that the original set is unchanged
51 expect = set(['foo///bar///baz'])
52 self.assertEqual(expect, paths)
54 def test_tmp_filename_gives_good_file(self):
55 first = utils.tmp_filename('test')
56 second = utils.tmp_filename('test')
58 self.assertFalse(core.exists(first))
59 self.assertFalse(core.exists(second))
61 self.assertNotEqual(first, second)
62 self.assertTrue(os.path.basename(first).startswith('git-cola-test'))
63 self.assertTrue(os.path.basename(second).startswith('git-cola-test'))
65 def test_strip_one_abspath(self):
66 expect = 'bin/git'
67 actual = utils.strip_one('/usr/bin/git')
68 self.assertEqual(expect, actual)
70 def test_strip_one_relpath(self):
71 expect = 'git'
72 actual = utils.strip_one('bin/git')
73 self.assertEqual(expect, actual)
75 def test_strip_one_nested_relpath(self):
76 expect = 'bin/git'
77 actual = utils.strip_one('local/bin/git')
78 self.assertEqual(expect, actual)
80 def test_strip_one_basename(self):
81 expect = 'git'
82 actual = utils.strip_one('git')
83 self.assertEqual(expect, actual)
85 def test_select_directory(self):
86 filename = utils.tmp_filename('test')
87 expect = os.path.dirname(filename)
88 actual = utils.select_directory([filename])
89 self.assertEqual(expect, actual)
91 def test_select_directory_prefers_directories(self):
92 filename = utils.tmp_filename('test')
93 expect = '.'
94 actual = utils.select_directory([filename, '.'])
95 self.assertEqual(expect, actual)
98 if __name__ == '__main__':
99 unittest.main()