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
|
import os.path
from boltons import fileutils
from boltons.fileutils import FilePerms, iter_find_files
from boltons.strutils import removeprefix
BOLTONS_PATH = os.path.dirname(os.path.abspath(fileutils.__file__))
def test_fileperms():
up = FilePerms()
up.other = ''
up.user = 'xrw'
up.group = 'rrrwx'
try:
up.other = 'nope'
except ValueError:
# correctly raised ValueError on invalid chars
pass
assert repr(up) == "FilePerms(user='rwx', group='rwx', other='')"
assert up.user == 'rwx'
assert oct(int(up)) == '0o770'
assert int(FilePerms()) == 0
def test_iter_find_files():
def _to_baseless_list(paths):
return [removeprefix(p, BOLTONS_PATH).lstrip(os.path.sep) for p in paths]
assert 'fileutils.py' in _to_baseless_list(iter_find_files(BOLTONS_PATH, patterns=['*.py']))
boltons_parent = os.path.dirname(BOLTONS_PATH)
assert 'fileutils.py' in _to_baseless_list(iter_find_files(boltons_parent, patterns=['*.py']))
assert 'fileutils.py' not in _to_baseless_list(iter_find_files(boltons_parent, patterns=['*.py'], max_depth=0))
def test_rotate_file_no_rotation(tmp_path):
file_path = tmp_path / 'test_file.txt'
fileutils.rotate_file(file_path)
assert not file_path.exists()
def test_rotate_file_one_rotation(tmp_path):
file_path = tmp_path / 'test_file.txt'
file_path.write_text('test content')
assert file_path.exists()
fileutils.rotate_file(file_path)
assert not file_path.exists()
assert (tmp_path / 'test_file.1.txt').exists()
def test_rotate_file_full_rotation(tmp_path):
file_path = tmp_path / 'test_file.txt'
file_path.write_text('test content 0')
for i in range(1, 5):
cur_path = tmp_path / f'test_file.{i}.txt'
cur_path.write_text(f'test content {i}')
assert cur_path.exists()
fileutils.rotate_file(file_path, keep=5)
assert not file_path.exists()
for i in range(1, 5):
cur_path = tmp_path / f'test_file.{i}.txt'
assert cur_path.read_text() == f'test content {i-1}'
assert not (tmp_path / 'test_file.5.txt').exists()
def test_rotate_file_full_rotation_no_ext(tmp_path):
file_path = tmp_path / 'test_file'
file_path.write_text('test content 0')
for i in range(1, 5):
cur_path = tmp_path / f'test_file.{i}'
cur_path.write_text(f'test content {i}')
assert cur_path.exists()
fileutils.rotate_file(file_path, keep=5)
assert not file_path.exists()
for i in range(1, 5):
cur_path = tmp_path / f'test_file.{i}'
assert cur_path.read_text() == f'test content {i-1}'
assert not (tmp_path / 'test_file.5').exists()
|