test_path.py 2.67 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
2
import os.path as osp
Kai Chen's avatar
Kai Chen committed
3
from pathlib import Path
4
5
6

import pytest

Kai Chen's avatar
Kai Chen committed
7
8
import mmcv

9

Kai Chen's avatar
Kai Chen committed
10
11
12
13
14
15
16
17
18
def test_is_filepath():
    assert mmcv.is_filepath(__file__)
    assert mmcv.is_filepath('abc')
    assert mmcv.is_filepath(Path('/etc'))
    assert not mmcv.is_filepath(0)


def test_fopen():
    assert hasattr(mmcv.fopen(__file__), 'read')
Kai Chen's avatar
Kai Chen committed
19
    assert hasattr(mmcv.fopen(Path(__file__)), 'read')
Kai Chen's avatar
Kai Chen committed
20
21


22
23
def test_check_file_exist():
    mmcv.check_file_exist(__file__)
24
25
    with pytest.raises(FileNotFoundError):
        mmcv.check_file_exist('no_such_file.txt')
26
27
28


def test_scandir():
29
    folder = osp.join(osp.dirname(osp.dirname(__file__)), 'data/for_scan')
30
    filenames = ['a.bin', '1.txt', '2.txt', '1.json', '2.json', '3.TXT']
31
    assert set(mmcv.scandir(folder)) == set(filenames)
Xintao's avatar
Xintao committed
32
    assert set(mmcv.scandir(Path(folder))) == set(filenames)
33
34
35
36
37
38
39
    assert set(mmcv.scandir(folder, '.txt')) == set(
        [filename for filename in filenames if filename.endswith('.txt')])
    assert set(mmcv.scandir(folder, ('.json', '.txt'))) == set([
        filename for filename in filenames
        if filename.endswith(('.txt', '.json'))
    ])
    assert set(mmcv.scandir(folder, '.png')) == set()
Xintao's avatar
Xintao committed
40

41
42
    # path of sep is `\\` in windows but `/` in linux, so osp.join should be
    # used to join string for compatibility
Xintao's avatar
Xintao committed
43
    filenames_recursive = [
44
        'a.bin', '1.txt', '2.txt', '1.json', '2.json', '3.TXT',
45
46
        osp.join('sub', '1.json'),
        osp.join('sub', '1.txt'), '.file'
Xintao's avatar
Xintao committed
47
    ]
48
49
50
51
52
    # .file starts with '.' and is a file so it will not be scanned
    assert set(mmcv.scandir(folder, recursive=True)) == set(
        [filename for filename in filenames_recursive if filename != '.file'])
    assert set(mmcv.scandir(Path(folder), recursive=True)) == set(
        [filename for filename in filenames_recursive if filename != '.file'])
Xintao's avatar
Xintao committed
53
54
55
56
    assert set(mmcv.scandir(folder, '.txt', recursive=True)) == set([
        filename for filename in filenames_recursive
        if filename.endswith('.txt')
    ])
57
58
59
60
61
62
63
64
65
66
67
68
69
    assert set(
        mmcv.scandir(folder, '.TXT', recursive=True,
                     case_sensitive=False)) == set([
                         filename for filename in filenames_recursive
                         if filename.endswith(('.txt', '.TXT'))
                     ])
    assert set(
        mmcv.scandir(
            folder, ('.TXT', '.JSON'), recursive=True,
            case_sensitive=False)) == set([
                filename for filename in filenames_recursive
                if filename.endswith(('.txt', '.json', '.TXT'))
            ])
Xintao's avatar
Xintao committed
70
71
    with pytest.raises(TypeError):
        list(mmcv.scandir(123))
72
    with pytest.raises(TypeError):
73
        list(mmcv.scandir(folder, 111))