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():
54 first
= utils
.tmp_filename('test')
55 assert core
.exists(first
)
56 assert os
.path
.basename(first
).startswith('git-cola-test')
61 second
= utils
.tmp_filename('test')
62 assert core
.exists(second
)
63 assert os
.path
.basename(second
).startswith('git-cola-test')
67 assert first
!= second
70 def test_strip_one_abspath():
72 actual
= utils
.strip_one('/usr/bin/git')
73 assert expect
== actual
76 def test_strip_one_relpath():
78 actual
= utils
.strip_one('bin/git')
79 assert expect
== actual
82 def test_strip_one_nested_relpath():
84 actual
= utils
.strip_one('local/bin/git')
85 assert expect
== actual
88 def test_strip_one_basename():
90 actual
= utils
.strip_one('git')
91 assert expect
== actual
94 def test_select_directory():
95 filename
= utils
.tmp_filename('test')
97 expect
= os
.path
.dirname(filename
)
98 actual
= utils
.select_directory([filename
])
99 assert expect
== actual
104 def test_select_directory_prefers_directories():
105 filename
= utils
.tmp_filename('test')
108 actual
= utils
.select_directory([filename
, '.'])
109 assert expect
== actual