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

import pytest

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

10

Kai Chen's avatar
Kai Chen committed
11
12
13
14
15
16
17
18
19
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
20
    assert hasattr(mmcv.fopen(Path(__file__)), 'read')
Kai Chen's avatar
Kai Chen committed
21
22


23
24
25
def test_check_file_exist():
    mmcv.check_file_exist(__file__)
    if sys.version_info > (3, 3):
Kai Chen's avatar
Kai Chen committed
26
        with pytest.raises(FileNotFoundError):  # noqa
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
            mmcv.check_file_exist('no_such_file.txt')
    else:
        with pytest.raises(IOError):
            mmcv.check_file_exist('no_such_file.txt')


def test_scandir():
    folder = osp.join(osp.dirname(__file__), 'data/for_scan')
    filenames = ['a.bin', '1.txt', '2.txt', '1.json', '2.json']

    assert set(mmcv.scandir(folder)) == set(filenames)
    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()
    with pytest.raises(TypeError):
        mmcv.scandir(folder, 111)