mmcv/tests/test_path.py
Kai Chen 12e5913bb9
Remove supports for python 2.7 and bump version to 0.4.0 (#211)
* remove supports for python 2.7

* fix the unit test for python 3.5

* add python 3.8 in CI

* try ubuntu 18.04 as the environment
2020-03-16 15:15:35 +08:00

42 lines
1.2 KiB
Python

# Copyright (c) Open-MMLab. All rights reserved.
import os.path as osp
from pathlib import Path
import pytest
import mmcv
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')
assert hasattr(mmcv.fopen(Path(__file__)), 'read')
def test_check_file_exist():
mmcv.check_file_exist(__file__)
with pytest.raises(FileNotFoundError):
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):
list(mmcv.scandir(folder, 111))