forked from git-cola/git-cola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.py
100 lines (79 loc) · 3.54 KB
/
utils_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
from __future__ import absolute_import, division, unicode_literals
import os
import unittest
from cola import core
from cola import utils
class ColaUtilsTestCase(unittest.TestCase):
"""Tests the cola.utils module."""
def test_basename(self):
"""Test the utils.basename function."""
self.assertEqual(utils.basename('bar'), 'bar')
self.assertEqual(utils.basename('/bar'), 'bar')
self.assertEqual(utils.basename('/bar '), 'bar ')
self.assertEqual(utils.basename('foo/bar'), 'bar')
self.assertEqual(utils.basename('/foo/bar'), 'bar')
self.assertEqual(utils.basename('foo/foo/bar'), 'bar')
self.assertEqual(utils.basename('/foo/foo/bar'), 'bar')
self.assertEqual(utils.basename('/foo/foo//bar'), 'bar')
self.assertEqual(utils.basename('////foo //foo//bar'), 'bar')
def test_dirname(self):
"""Test the utils.dirname function."""
self.assertEqual(utils.dirname('bar'), '')
self.assertEqual(utils.dirname('/bar'), '')
self.assertEqual(utils.dirname('//bar'), '')
self.assertEqual(utils.dirname('///bar'), '')
self.assertEqual(utils.dirname('foo/bar'), 'foo')
self.assertEqual(utils.dirname('foo//bar'), 'foo')
self.assertEqual(utils.dirname('foo /bar'), 'foo ')
self.assertEqual(utils.dirname('/foo//bar'), '/foo')
self.assertEqual(utils.dirname('/foo /bar'), '/foo ')
self.assertEqual(utils.dirname('//foo//bar'), '/foo')
self.assertEqual(utils.dirname('///foo///bar'), '/foo')
def test_add_parents(self):
"""Test the utils.add_parents() function."""
paths = set(['foo///bar///baz'])
path_set = utils.add_parents(paths)
self.assertTrue('foo/bar/baz' in path_set)
self.assertTrue('foo/bar' in path_set)
self.assertTrue('foo' in path_set)
self.assertFalse('foo///bar///baz' in path_set)
# Ensure that the original set is unchanged
expect = set(['foo///bar///baz'])
self.assertEqual(expect, paths)
def test_tmp_filename_gives_good_file(self):
first = utils.tmp_filename('test')
second = utils.tmp_filename('test')
self.assertFalse(core.exists(first))
self.assertFalse(core.exists(second))
self.assertNotEqual(first, second)
self.assertTrue(os.path.basename(first).startswith('git-cola-test'))
self.assertTrue(os.path.basename(second).startswith('git-cola-test'))
def test_strip_one_abspath(self):
expect = 'bin/git'
actual = utils.strip_one('/usr/bin/git')
self.assertEqual(expect, actual)
def test_strip_one_relpath(self):
expect = 'git'
actual = utils.strip_one('bin/git')
self.assertEqual(expect, actual)
def test_strip_one_nested_relpath(self):
expect = 'bin/git'
actual = utils.strip_one('local/bin/git')
self.assertEqual(expect, actual)
def test_strip_one_basename(self):
expect = 'git'
actual = utils.strip_one('git')
self.assertEqual(expect, actual)
def test_select_directory(self):
filename = utils.tmp_filename('test')
expect = os.path.dirname(filename)
actual = utils.select_directory([filename])
self.assertEqual(expect, actual)
def test_select_directory_prefers_directories(self):
filename = utils.tmp_filename('test')
expect = '.'
actual = utils.select_directory([filename, '.'])
self.assertEqual(expect, actual)
if __name__ == '__main__':
unittest.main()