def test_find_ignore(tmp): # first do a control to make sure we find 'sub2' res = find('data', item_type='both', direction='down', path='tests/', regex=True, first_only=False) ok = False for path in res: if re.match('(.*)sub2(.*)', path): ok = True break assert ok is True # then try and ignore it res = find('data', item_type='both', direction='down', path='tests/', regex=True, first_only=False, ignore=['(.*)sub2(.*)']) assert isinstance(res, list) for path in res: assert not re.match('(.*)sub2(.*)', path) ok = True for path in res: if re.match('(.*)sub2(.*)', path): ok = False break assert ok is True # coverage res = find('data', item_type='both', direction='down', path='tests/', first_only=True, ignore=['(.*)data(.*)']) assert res is None
def test_find_item_type(tmp): # item_type file res = find('data', item_type='file', direction='both', path='tests/') assert os.path.isfile(res) # item_type directory res = find('data', item_type='directory', direction='both', path='tests') assert os.path.isdir(res) # item_type both res = find('data', item_type='both', first_only=False, direction='both', path='tests') found_a_file = False found_a_dir = False for item in res: if os.path.isfile(item): found_a_file = True elif os.path.isdir(item): found_a_dir = True assert found_a_file assert found_a_dir # same thing with first_only true (coverage) res = find('data', item_type='both', first_only=True, direction='down', path='tests/') # direction bogus (raises error) msg = "Argument 'item_type' must be one of" with raises(BackpedalArgumentError, match=msg): res = find('data', item_type='bogus')
def test_find_regex(tmp): res = find('dat', direction='down', path='tests/', regex=False) assert res is None res = find('(.*)dat(.*)', direction='down', path='tests/', regex=True, first_only=False) assert isinstance(res, list) for path in res: assert re.match('(.*)data', path) # coverage res = find('(.*)dat(.*)', direction='down', path='tests/', regex=True) re.match('(.*)data/sub2/data', res)
def test_find_first_only(tmp): # first_only True res = find('README.md', first_only=True, path='tests/data/sub1/a') assert re.match('(.*)data/sub1/a/README.md', res) # first_only False res = find('README.md', first_only=False, path='tests/data/sub1/a') assert isinstance(res, list) for path in res: assert re.match('(.*)README.md', path) # first_only bogus msg = "Argument 'first_only' must be one of" with raises(BackpedalArgumentError, match=msg): res = find('README.md', first_only='bogus')
def test_find_direction(tmp): # direction up res = find('README.md', direction='up', path='tests/data/sub1/a') assert re.match('(.*)data/sub1/a/README.md', res) # direction down res = find('README.md', direction='down', path='tests/data') assert re.match('(.*)data/sub1/a/README.md', res) # direction both res = find('README.md', direction='both', path='tests/data') assert re.match('(.*)README.md', res) # direction bogus (raises error) msg = "Argument 'direction' must be one of" with raises(BackpedalArgumentError, match=msg): res = find('README.md', direction='bogus')
import backpedal backpedal.DEBUG = True def log(msg): print() print('-' * 78) print(msg) print('-' * 78) log('Find the first matching file starting from `.`') res = backpedal.find('README.md') print('RES > %s' % res) log('Find all matching files starting from sub1/a/b/c:') res = backpedal.find('README.md', path='sub1/a/b/c', first_only=False) print('RES > %s' % res) log('Look for directory but peddle down instead of up') res = backpedal.find('data', direction='down', item_type='directory') print('RES > %s' % res) log('Look for files and directories in both directions') res = backpedal.find('data', direction='both', item_type='both', first_only=False) print('RES > %s' % res)
def test_find(tmp): res = find('README.md') assert re.match('(.*)README.md', res)
def test_find_none(tmp): res = find('bogus_file_does_not_exist') assert res is None