File: utils_test.py

package info (click to toggle)
git-cola 4.13.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 6,480 kB
  • sloc: python: 36,938; sh: 304; makefile: 223; xml: 100; tcl: 62
file content (110 lines) | stat: -rw-r--r-- 3,010 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
106
107
108
109
110
"""Tests the cola.utils module."""
import os

from cola import core
from cola import utils


def test_basename():
    """Test the utils.basename function."""
    assert utils.basename('bar') == 'bar'
    assert utils.basename('/bar') == 'bar'
    assert utils.basename('/bar ') == 'bar '
    assert utils.basename('foo/bar') == 'bar'
    assert utils.basename('/foo/bar') == 'bar'
    assert utils.basename('foo/foo/bar') == 'bar'
    assert utils.basename('/foo/foo/bar') == 'bar'
    assert utils.basename('/foo/foo//bar') == 'bar'
    assert utils.basename('////foo //foo//bar') == 'bar'


def test_dirname():
    """Test the utils.dirname function."""
    assert utils.dirname('bar') == ''
    assert utils.dirname('/bar') == ''
    assert utils.dirname('//bar') == ''
    assert utils.dirname('///bar') == ''
    assert utils.dirname('foo/bar') == 'foo'
    assert utils.dirname('foo//bar') == 'foo'
    assert utils.dirname('foo /bar') == 'foo '
    assert utils.dirname('/foo//bar') == '/foo'
    assert utils.dirname('/foo /bar') == '/foo '
    assert utils.dirname('//foo//bar') == '/foo'
    assert utils.dirname('///foo///bar') == '/foo'


def test_add_parents():
    """Test the utils.add_parents() function."""
    paths = {'foo///bar///baz'}
    path_set = utils.add_parents(paths)

    assert 'foo/bar/baz' in path_set
    assert 'foo/bar' in path_set
    assert 'foo' in path_set
    assert 'foo///bar///baz' not in path_set

    # Ensure that the original set is unchanged
    expect = {'foo///bar///baz'}
    assert expect == paths


def test_tmp_filename_gives_good_file():
    try:
        first = utils.tmp_filename('test')
        assert core.exists(first)
        assert os.path.basename(first).startswith('git-cola-test')
    finally:
        os.remove(first)

    try:
        second = utils.tmp_filename('test')
        assert core.exists(second)
        assert os.path.basename(second).startswith('git-cola-test')
    finally:
        os.remove(second)

    assert first != second


def test_strip_one_abspath():
    expect = 'bin/git'
    actual = utils.strip_one('/usr/bin/git')
    assert expect == actual


def test_strip_one_relpath():
    expect = 'git'
    actual = utils.strip_one('bin/git')
    assert expect == actual


def test_strip_one_nested_relpath():
    expect = 'bin/git'
    actual = utils.strip_one('local/bin/git')
    assert expect == actual


def test_strip_one_basename():
    expect = 'git'
    actual = utils.strip_one('git')
    assert expect == actual


def test_select_directory():
    filename = utils.tmp_filename('test')
    try:
        expect = os.path.dirname(filename)
        actual = utils.select_directory([filename])
        assert expect == actual
    finally:
        os.remove(filename)


def test_select_directory_prefers_directories():
    filename = utils.tmp_filename('test')
    try:
        expect = '.'
        actual = utils.select_directory([filename, '.'])
        assert expect == actual
    finally:
        os.remove(filename)