Merge pull request #1333 from kurtmckee/use-pre-commit
[git-cola.git] / test / utils_test.py
blob0cc6b40e9afc9ba962f6689a9dac8d92c0740a90
1 """Tests the cola.utils module."""
2 import os
4 from cola import core
5 from cola import utils
8 def test_basename():
9 """Test the utils.basename function."""
10 assert utils.basename('bar') == 'bar'
11 assert utils.basename('/bar') == 'bar'
12 assert utils.basename('/bar ') == 'bar '
13 assert utils.basename('foo/bar') == 'bar'
14 assert utils.basename('/foo/bar') == 'bar'
15 assert utils.basename('foo/foo/bar') == 'bar'
16 assert utils.basename('/foo/foo/bar') == 'bar'
17 assert utils.basename('/foo/foo//bar') == 'bar'
18 assert utils.basename('////foo //foo//bar') == 'bar'
21 def test_dirname():
22 """Test the utils.dirname function."""
23 assert utils.dirname('bar') == ''
24 assert utils.dirname('/bar') == ''
25 assert utils.dirname('//bar') == ''
26 assert utils.dirname('///bar') == ''
27 assert utils.dirname('foo/bar') == 'foo'
28 assert utils.dirname('foo//bar') == 'foo'
29 assert utils.dirname('foo /bar') == 'foo '
30 assert utils.dirname('/foo//bar') == '/foo'
31 assert utils.dirname('/foo /bar') == '/foo '
32 assert utils.dirname('//foo//bar') == '/foo'
33 assert utils.dirname('///foo///bar') == '/foo'
36 def test_add_parents():
37 """Test the utils.add_parents() function."""
38 paths = {'foo///bar///baz'}
39 path_set = utils.add_parents(paths)
41 assert 'foo/bar/baz' in path_set
42 assert 'foo/bar' in path_set
43 assert 'foo' in path_set
44 assert 'foo///bar///baz' not in path_set
46 # Ensure that the original set is unchanged
47 expect = {'foo///bar///baz'}
48 assert expect == paths
51 def test_tmp_filename_gives_good_file():
52 try:
53 first = utils.tmp_filename('test')
54 assert core.exists(first)
55 assert os.path.basename(first).startswith('git-cola-test')
56 finally:
57 os.remove(first)
59 try:
60 second = utils.tmp_filename('test')
61 assert core.exists(second)
62 assert os.path.basename(second).startswith('git-cola-test')
63 finally:
64 os.remove(second)
66 assert first != second
69 def test_strip_one_abspath():
70 expect = 'bin/git'
71 actual = utils.strip_one('/usr/bin/git')
72 assert expect == actual
75 def test_strip_one_relpath():
76 expect = 'git'
77 actual = utils.strip_one('bin/git')
78 assert expect == actual
81 def test_strip_one_nested_relpath():
82 expect = 'bin/git'
83 actual = utils.strip_one('local/bin/git')
84 assert expect == actual
87 def test_strip_one_basename():
88 expect = 'git'
89 actual = utils.strip_one('git')
90 assert expect == actual
93 def test_select_directory():
94 filename = utils.tmp_filename('test')
95 try:
96 expect = os.path.dirname(filename)
97 actual = utils.select_directory([filename])
98 assert expect == actual
99 finally:
100 os.remove(filename)
103 def test_select_directory_prefers_directories():
104 filename = utils.tmp_filename('test')
105 try:
106 expect = '.'
107 actual = utils.select_directory([filename, '.'])
108 assert expect == actual
109 finally:
110 os.remove(filename)