1 """Tests the cola.utils module."""
2 from __future__
import absolute_import
, division
, print_function
, unicode_literals
10 """Test the utils.basename function."""
11 assert utils
.basename('bar') == 'bar'
12 assert utils
.basename('/bar') == 'bar'
13 assert utils
.basename('/bar ') == 'bar '
14 assert utils
.basename('foo/bar') == 'bar'
15 assert utils
.basename('/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'
19 assert utils
.basename('////foo //foo//bar') == 'bar'
23 """Test the utils.dirname function."""
24 assert utils
.dirname('bar') == ''
25 assert utils
.dirname('/bar') == ''
26 assert utils
.dirname('//bar') == ''
27 assert utils
.dirname('///bar') == ''
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'
34 assert utils
.dirname('///foo///bar') == '/foo'
37 def test_add_parents():
38 """Test the utils.add_parents() function."""
39 paths
= set(['foo///bar///baz'])
40 path_set
= utils
.add_parents(paths
)
42 assert 'foo/bar/baz' in path_set
43 assert 'foo/bar' in path_set
44 assert 'foo' in path_set
45 assert 'foo///bar///baz' not in path_set
47 # Ensure that the original set is unchanged
48 expect
= set(['foo///bar///baz'])
49 assert expect
== paths
52 def test_tmp_filename_gives_good_file():
53 first
= utils
.tmp_filename('test')
54 second
= utils
.tmp_filename('test')
56 assert not core
.exists(first
)
57 assert not core
.exists(second
)
58 assert first
!= second
59 assert os
.path
.basename(first
).startswith('git-cola-test')
60 assert os
.path
.basename(second
).startswith('git-cola-test')
63 def test_strip_one_abspath():
65 actual
= utils
.strip_one('/usr/bin/git')
66 assert expect
== actual
69 def test_strip_one_relpath():
71 actual
= utils
.strip_one('bin/git')
72 assert expect
== actual
75 def test_strip_one_nested_relpath():
77 actual
= utils
.strip_one('local/bin/git')
78 assert expect
== actual
81 def test_strip_one_basename():
83 actual
= utils
.strip_one('git')
84 assert expect
== actual
87 def test_select_directory():
88 filename
= utils
.tmp_filename('test')
89 expect
= os
.path
.dirname(filename
)
90 actual
= utils
.select_directory([filename
])
91 assert expect
== actual
94 def test_select_directory_prefers_directories():
95 filename
= utils
.tmp_filename('test')
97 actual
= utils
.select_directory([filename
, '.'])
98 assert expect
== actual