278 lines
7.5 KiB
Python
Executable File
278 lines
7.5 KiB
Python
Executable File
#!/usr/bin/env python
|
|
'''
|
|
onFileChange.py ~/deflDev/tests/test_path.py ~/deflDev/defl/_path_.py -- ~/deflDev/bin/pythonDeflDev.py - python ~/deflDev/tests/test_path.py
|
|
|
|
time find >/dev/null
|
|
time python3 -c 'import pathlib;pathlib.Path("**/*")'
|
|
'''
|
|
|
|
import defl
|
|
import os
|
|
import shlex
|
|
import pathlib
|
|
from defl import log, cl, Path
|
|
from defl.testing_ import Tester
|
|
|
|
tester = Tester(name=__file__)
|
|
|
|
|
|
def msg(a, b):
|
|
return f'{cl.yel}{a=}{cl.r} {cl.mag}{b=}{cl.r}'
|
|
|
|
|
|
@tester.add()
|
|
def test1():
|
|
a = str(Path('~'))
|
|
b = str(pathlib.Path("~"))
|
|
assert a != b, msg(a, b)
|
|
|
|
|
|
@tester.add()
|
|
def test2():
|
|
a = str(Path('~'))
|
|
b = os.environ['HOME']
|
|
assert a == b, msg(a, b)
|
|
|
|
|
|
@tester.add()
|
|
def test3():
|
|
p = '/a/b/c/d e'
|
|
a = shlex.quote(str(pathlib.Path(p)))
|
|
b = Path(p).quote()
|
|
assert a == b, msg(a, b)
|
|
|
|
|
|
@tester.add()
|
|
def test4():
|
|
a = repr(Path('a'))
|
|
b = 'λ(a)'
|
|
assert a == b, msg(a, b)
|
|
|
|
|
|
@tester.add()
|
|
def test5():
|
|
a = str(Path('a') / 'b/c/d/e')
|
|
b = 'a/b/c/d/e'
|
|
assert a == b, msg(a, b)
|
|
|
|
|
|
@tester.add()
|
|
def test6():
|
|
Path().is_file()
|
|
Path().is_dir()
|
|
|
|
|
|
@tester.add()
|
|
def test7():
|
|
a = Path('/tmp/').chdir()
|
|
assert isinstance(a, Path)
|
|
a = a.absolute().str
|
|
b = '/tmp'
|
|
assert a == b, msg(a, b)
|
|
|
|
|
|
@tester.add()
|
|
def test8():
|
|
a = Path('a')
|
|
b = Path('a')
|
|
assert a == b, msg(a, b)
|
|
|
|
|
|
@tester.add()
|
|
def test10():
|
|
a = Path('a')
|
|
b = pathlib.Path('a')
|
|
assert a == b, msg(a, b) + ' TODO SHOULD BE EQUAL'
|
|
|
|
|
|
@tester.add()
|
|
def test10():
|
|
a = Path('a')
|
|
b = Path('a')
|
|
assert a == b, msg(a, b)
|
|
|
|
|
|
@tester.add()
|
|
def properties():
|
|
a = Path('a')
|
|
a.suffix
|
|
a.s
|
|
a.str
|
|
a.json
|
|
|
|
|
|
pathTestRoot = Path('/tmp/pathTest')
|
|
createdFiles = []
|
|
createdLinks = []
|
|
createdDirs = [pathTestRoot]
|
|
|
|
|
|
def forceTouchAndAddToCreated(a: Path, /, *, link=False):
|
|
if a.parent not in createdDirs:
|
|
createdDirs.append(a.parent)
|
|
a.parent.mkdirForce()
|
|
if link:
|
|
createdLinks.append(a)
|
|
a.symlinkToForce(link)
|
|
else:
|
|
createdFiles.append(a)
|
|
a.touchForce()
|
|
|
|
|
|
# @tester.add()
|
|
def makeTestFiles():
|
|
if pathTestRoot.exists():
|
|
defl.RunCom('rm -rf /tmp/pathTest')
|
|
for i in 'abc':
|
|
for j in '123':
|
|
forceTouchAndAddToCreated(pathTestRoot / i / f"file_{i}_{j}")
|
|
forceTouchAndAddToCreated(pathTestRoot / i / j / f"file_{i}_{j}")
|
|
forceTouchAndAddToCreated(pathTestRoot / i / j / f"linkDir_{i}_{j}", link='/tmp/pathTest')
|
|
forceTouchAndAddToCreated(
|
|
pathTestRoot / i / j / f"linkFile_{i}_{j}",
|
|
link=pathTestRoot / i / j / f"file_{i}_{j}")
|
|
assert len(createdDirs) == 13
|
|
assert len(createdFiles) == 18
|
|
assert len(createdLinks) == 18
|
|
|
|
|
|
makeTestFiles()
|
|
import re
|
|
|
|
|
|
@tester.add()
|
|
def toCompile():
|
|
def ass(a, l):
|
|
assert len(a) == l
|
|
assert isinstance(a, list)
|
|
for i in a:
|
|
assert isinstance(i, re.Pattern)
|
|
ass(defl.castRegexList(None), l=0)
|
|
ass(defl.castRegexList('a'), l=1)
|
|
ass(defl.castRegexList(['a']), l=1)
|
|
ass(defl.castRegexList(re.compile(r'/file_._3$')), l=1)
|
|
ass(defl.castRegexList([re.compile(r'/file_._3$'), re.compile(r'/file_._3$')]), l=2)
|
|
ass(defl.castRegexList(['1', '2']), l=2)
|
|
ass(defl.castRegexList(['1', '2', None]), l=2)
|
|
ass(defl.castRegexList([None]), l=0)
|
|
|
|
|
|
@tester.add()
|
|
def chdir():
|
|
curdir = Path().absolute()
|
|
with pathTestRoot.chdirCM():
|
|
assert Path().absolute() == str(pathTestRoot), Path().absolute()
|
|
assert Path().absolute() == curdir
|
|
|
|
|
|
@tester.add()
|
|
def pathfilter1():
|
|
assert (pathTestRoot / 'c/3/file_c_3').relative_to(pathTestRoot) == 'c/3/file_c_3'
|
|
|
|
def argsAssert(assCount, pr=pathTestRoot, **args):
|
|
t = [x for x in pr.find(defl.PathFilter(**args))]
|
|
t = sorted(t)
|
|
t = [x.str for x in t]
|
|
assert len(t) == assCount, f"{cl.red}{args}{cl.r} ({cl.red}{len(t)}{cl.r}!={cl.grn}{assCount}{cl.r}): {t}"
|
|
|
|
argsAssert(18, file=True)
|
|
argsAssert(6, file=True, test=[lambda x: re.compile(r'/file_._3$').search(str(x))])
|
|
argsAssert(6, file=True, test=[defl.regSearchFactory('file_._3')])
|
|
argsAssert(12, file=True, test=['!', lambda x: re.compile(r'/file_._3$').search(str(x))])
|
|
argsAssert(12, file=True, test=['!', defl.regSearchFactory('file_._3')])
|
|
argsAssert(13, dir=True)
|
|
argsAssert(9, dir=True, link=True)
|
|
argsAssert(9, file=True, link=True)
|
|
argsAssert(18, dir=True, file=True, link=True)
|
|
argsAssert(1, dir=True, file=True, link=True, test=[lambda x: x == pathTestRoot / 'c/2/linkDir_c_2'])
|
|
argsAssert(18, link=True)
|
|
|
|
with pathTestRoot.chdirCM():
|
|
argsAssert(1, pr=Path('.'), dir=True, file=True, link=True, test=[lambda x: x == 'c/2/linkDir_c_2'])
|
|
|
|
with pathTestRoot.chdirCM():
|
|
res = Path('.').find(
|
|
pathFilter=defl.PathFilter(file=True, link=True, test=[
|
|
defl.regSearchFactory('linkDir_'), '|',
|
|
defl.regSearchFactory('linkFile_'), '|',
|
|
defl.regSearchFactory('a|b'), '|', None
|
|
]),
|
|
descendDirFilter=['!', defl.regSearchFactory('^b/')]
|
|
)
|
|
res = sorted([str(x) for x in res])
|
|
assert res == ['a/1/linkFile_a_1', 'a/2/linkFile_a_2', 'a/3/linkFile_a_3', 'c/1/linkFile_c_1', 'c/2/linkFile_c_2', 'c/3/linkFile_c_3'], res
|
|
|
|
|
|
def parts():
|
|
assert (a := pathlib.Path('/').parts) == ('/',), a
|
|
assert (a := Path('/').parts) == ('/',), a
|
|
assert (a := (Path('/') / 'aa/bb/').parts) == ('/', 'aa', 'bb'), a
|
|
assert (a := (Path('/') / 'aa/bb').parts) == ('/', 'aa', 'bb'), a
|
|
|
|
assert (a := pathlib.Path('.').parts) == (), a
|
|
assert (a := Path('.').parts) == (), a
|
|
assert (a := (Path('.') / 'aa/bb/').parts) == ('aa', 'bb'), a
|
|
assert (a := (Path('.') / 'aa/bb').parts) == ('aa', 'bb'), a
|
|
|
|
a = [
|
|
"Path('/aa/bb/cc/')",
|
|
"Path('/aa') / 'bb' / 'cc'",
|
|
"Path('/aa') / 'bb/cc'",
|
|
"Path('/') / 'aa/bb/cc'",
|
|
"Path('/', 'aa', 'bb', 'cc')",
|
|
"Path(['/', 'aa'], ['bb', 'cc'])",
|
|
"Path('/', ['aa', 'bb'], 'cc')",
|
|
"Path('/', 'aa/bb', ['cc'])",
|
|
"Path('/aa/bb', ['cc'])",
|
|
"Path('/aa/bb/cc', '')",
|
|
"Path('/aa/bb/cc/', '')",
|
|
"Path(['/', 'aa', 'bb', 'cc'])",
|
|
"Path(['/', 'aa', 'bb', 'cc'], '///')",
|
|
"Path(['///', 'aa', 'bb', 'cc'], '///')",
|
|
"Path(tuple(['/', 'aa', 'bb', 'cc']))",
|
|
]
|
|
for i in a:
|
|
ev = eval(i)
|
|
assert ev.parts == ('/', 'aa', 'bb', 'cc'), (i, ev.parts)
|
|
|
|
a = [
|
|
"Path('aa/bb/cc/')",
|
|
"Path('aa', 'bb', 'cc')",
|
|
"Path(tuple(['aa', 'bb', 'cc']))",
|
|
]
|
|
for i in a:
|
|
ev = eval(i)
|
|
assert ev.parts == ('aa', 'bb', 'cc'), (i, ev.parts)
|
|
|
|
a = [
|
|
"Path(['00/11///22/33'])",
|
|
"Path(['00'], '/11///22/33')",
|
|
"Path(['00/11///22///33'])",
|
|
"Path('00', '/', '11', '///', '/', '/', '/', '/', '/', '22', '/', '33')",
|
|
"Path(['00', '/', '11', '/', '/', '/', '///', '///', '/', '22', '/', '33', '///'])",
|
|
]
|
|
for i in a:
|
|
ev = eval(i)
|
|
assert ev.parts == ('00', '11', '22', '33'), (i, ev.parts)
|
|
|
|
a = Path('/00/a/22/33/00/b/22/33/00/c/22/33/00/d/22/33')
|
|
b = a.regFindIter('^.00/.*c/22')
|
|
assert [x for x in b][0].span() == (0, 30)
|
|
b = a.regFindIter('^(.*)$')
|
|
assert [x for x in b][0].span() == (0, 44)
|
|
b = a.regSub('^.00/?', '/')
|
|
assert str(b) == '/a/22/33/00/b/22/33/00/c/22/33/00/d/22/33'
|
|
b = a.regSub('.*/')
|
|
assert str(b) == '33'
|
|
b = a.regSearch('/22/33/00/b/22/33/00/c/22/33/00/d/22')
|
|
assert b
|
|
|
|
|
|
log.info(tester.run())
|
|
tester.exitWithStatus()
|
|
|
|
|
|
# makeTestFiles()
|
|
# pathfilter1()
|
|
# parts()
|