pyproject.toml: transfer more metadata from setup.cfg
[git-cola.git] / test / utils_test.py
blob8009bcbcd595f1c1a361477d1a5fb252ab8cc779
1 """Tests the cola.utils module."""
2 from __future__ import absolute_import, division, print_function, unicode_literals
3 import os
5 from cola import core
6 from cola import utils
9 def test_basename():
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'
22 def test_dirname():
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 try:
54 first = utils.tmp_filename('test')
55 assert core.exists(first)
56 assert os.path.basename(first).startswith('git-cola-test')
57 finally:
58 os.remove(first)
60 try:
61 second = utils.tmp_filename('test')
62 assert core.exists(second)
63 assert os.path.basename(second).startswith('git-cola-test')
64 finally:
65 os.remove(second)
67 assert first != second
70 def test_strip_one_abspath():
71 expect = 'bin/git'
72 actual = utils.strip_one('/usr/bin/git')
73 assert expect == actual
76 def test_strip_one_relpath():
77 expect = 'git'
78 actual = utils.strip_one('bin/git')
79 assert expect == actual
82 def test_strip_one_nested_relpath():
83 expect = 'bin/git'
84 actual = utils.strip_one('local/bin/git')
85 assert expect == actual
88 def test_strip_one_basename():
89 expect = 'git'
90 actual = utils.strip_one('git')
91 assert expect == actual
94 def test_select_directory():
95 filename = utils.tmp_filename('test')
96 try:
97 expect = os.path.dirname(filename)
98 actual = utils.select_directory([filename])
99 assert expect == actual
100 finally:
101 os.remove(filename)
104 def test_select_directory_prefers_directories():
105 filename = utils.tmp_filename('test')
106 try:
107 expect = '.'
108 actual = utils.select_directory([filename, '.'])
109 assert expect == actual
110 finally:
111 os.remove(filename)