File: test_pathutils.py

package info (click to toggle)
mozjs78 78.15.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 739,892 kB
  • sloc: javascript: 1,344,214; cpp: 1,215,708; python: 526,544; ansic: 433,835; xml: 118,736; sh: 26,176; asm: 16,664; makefile: 11,537; yacc: 4,486; perl: 2,564; ada: 1,681; lex: 1,414; pascal: 1,139; cs: 879; exp: 499; java: 164; ruby: 68; sql: 45; csh: 35; sed: 18; lisp: 2
file content (144 lines) | stat: -rw-r--r-- 3,978 bytes parent folder | download | duplicates (6)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
from fnmatch import fnmatch

import mozunit
import pytest

from mozlint import pathutils

here = os.path.abspath(os.path.dirname(__file__))
root = os.path.join(here, 'filter')


def assert_paths(a, b):
    def normalize(p):
        if not os.path.isabs(p):
            p = os.path.join(root, p)
        return os.path.normpath(p)
    assert set(map(normalize, a)) == set(map(normalize, b))


@pytest.mark.parametrize('test', (
    {
        'paths': ['a.js', 'subdir1/subdir3/d.js'],
        'include': ['.'],
        'exclude': ['subdir1'],
        'expected': ['a.js'],
    },
    {
        'paths': ['a.js', 'subdir1/subdir3/d.js'],
        'include': ['subdir1/subdir3'],
        'exclude': ['subdir1'],
        'expected': ['subdir1/subdir3/d.js'],
    },
    {
        'paths': ['.'],
        'include': ['.'],
        'exclude': ['**/c.py', 'subdir1/subdir3'],
        'extensions': ['py'],
        'expected': ['.'],
        'expected_exclude': ['subdir2/c.py', 'subdir1/subdir3'],
    },
    {
        'paths': ['a.py', 'a.js', 'subdir1/b.py', 'subdir2/c.py', 'subdir1/subdir3/d.py'],
        'include': ['.'],
        'exclude': ['**/c.py', 'subdir1/subdir3'],
        'extensions': ['py'],
        'expected': ['a.py', 'subdir1/b.py'],
    },
    {
        'paths': ['a.py', 'a.js', 'subdir2'],
        'include': ['.'],
        'exclude': [],
        'extensions': ['py'],
        'expected': ['a.py', 'subdir2'],
    },
    {
        'paths': ['subdir1'],
        'include': ['.'],
        'exclude': ['subdir1/subdir3'],
        'extensions': ['py'],
        'expected': ['subdir1'],
        'expected_exclude': ['subdir1/subdir3'],
    },
    {
        'paths': ['docshell'],
        'include': ['docs'],
        'exclude': [],
        'expected': [],
    },
    {
        'paths': ['does/not/exist'],
        'include': ['.'],
        'exclude': [],
        'expected': [],
    }
))
def test_filterpaths(test):
    expected = test.pop('expected')
    expected_exclude = test.pop('expected_exclude', [])

    paths, exclude = pathutils.filterpaths(root, **test)
    assert_paths(paths, expected)
    assert_paths(exclude, expected_exclude)


@pytest.mark.parametrize('test', (
    {
        'paths': ['subdir1/b.js'],
        'config': {
            'exclude': ['subdir1'],
            'extensions': 'js',
        },
        'expected': [],
    },
    {
        'paths': ['subdir1/subdir3'],
        'config': {
            'exclude': ['subdir1'],
            'extensions': 'js',
        },
        'expected': [],
    },
))
def test_expand_exclusions(test):
    expected = test.pop('expected', [])

    paths = list(pathutils.expand_exclusions(test['paths'], test['config'], root))
    assert_paths(paths, expected)


@pytest.mark.parametrize('paths,expected', [
    (['subdir1/*'], ['subdir1']),
    (['subdir2/*'], ['subdir2']),
    (['subdir1/*.*', 'subdir1/subdir3/*', 'subdir2/*'], ['subdir1', 'subdir2']),
    ([root + '/*', 'subdir1/*.*', 'subdir1/subdir3/*', 'subdir2/*'], [root]),
    (['subdir1/b.py', 'subdir1/subdir3'], ['subdir1/b.py', 'subdir1/subdir3']),
    (['subdir1/b.py', 'subdir1/b.js'], ['subdir1/b.py', 'subdir1/b.js']),
    (['subdir1/subdir3'], ['subdir1/subdir3']),
    (['foo', 'foobar', ], ['foo', 'foobar']),
])
def test_collapse(paths, expected):
    os.chdir(root)

    inputs = []
    for path in paths:
        base, name = os.path.split(path)
        if '*' in name:
            for n in os.listdir(base):
                if not fnmatch(n, name):
                    continue
                inputs.append(os.path.join(base, n))
        else:
            inputs.append(path)

    print("inputs: {}".format(inputs))
    assert_paths(pathutils.collapse(inputs), expected)


if __name__ == '__main__':
    mozunit.main()