def test_open(): with Story(['io.open', 'os.path.isdir']) as story: os.path.isdir(pth.Path('foo')) == False io.open(pth.Path('foo'), 'rb') == None with story.replay(strict=True): pth('foo')('rb')
def test_abspath_zip(): with pth.tmp() as tmp: pth.Path('tests/files/test.zip').copy(tmp) with tmp.cd: assert pth("test.zip").abs == tmp / "test.zip" assert pth("test.zip").abspath == tmp / "test.zip" assert (pth("test.zip") / "a.txt").abs == tmp / "test.zip" / "a.txt" assert (pth("test.zip") / "a.txt").abspath == tmp / "test.zip" / "a.txt"
def test_lchmod(): with Story(['os.lchmod', 'os.chmod']) as story: if PY33: os.chmod(pth.Path('foobar'), 0o666, follow_symlinks=False) == None else: os.lchmod(pth.Path('foobar'), 0o666) == None with story.replay(strict=True): pth('foobar').lchmod(0o666)
def test_link(): with Story(['os.link']) as story: if PY33: os.link(pth.Path('foo'), 'bar', follow_symlinks=True) == None else: os.link(pth.Path('foo'), 'bar') == None with story.replay(strict=True): pth('foo').link('bar')
def test_parents_zip(): z = pth('tests', 'files', 'test.zip').joinpath('b', 'c', 'd') assert z.parents == [ pth('tests', 'files', 'test.zip', 'b', 'c'), pth('tests', 'files', 'test.zip', 'b'), pth('tests', 'files', 'test.zip'), pth('tests', 'files'), 'tests', ]
def test_exists_zip(): with pth.tmp() as tmp: zp = pth.Path('tests/files/test.zip').copy(tmp) with tmp.cd: assert (pth("test.zip") / "a.txt").exists assert not (pth("test.zip") / "crappo").exists assert (pth(zp) / "a.txt").exists assert not (pth(zp) / "crappo").exists
def test_lchown(): with Story(['os.lchown', 'os.chown']) as story: if PY33: os.chown(pth.Path('foobar'), 123, 123, follow_symlinks=False) == None else: os.lchown(pth.Path('foobar'), 123, 123) == None with story.replay(strict=True): pth('foobar').lchown(123, 123)
def test_time_zip(): assert isinstance(pth('tests/files/test.zip').atime, float) assert isinstance(pth('tests/files/test.zip').ctime, float) assert isinstance(pth('tests/files/test.zip').mtime, float) a = pth('tests/files/test.zip') / 'a.txt' assert raises(NotImplementedError, lambda: a.atime) assert isinstance(a.ctime, float) assert raises(NotImplementedError, lambda: a.mtime)
def test_cd(): assert repr(pth('/bogus').cd) == "pth.WorkingDir('/bogus')" with Story(['zipfile.is_zipfile', 'os.chdir', 'os.getcwd', 'os.path.exists', 'os.stat']) as story: zipfile.is_zipfile('/bogus') == False # returns os.stat(pth.WorkingDir('/bogus')) == os.stat_result(( 17407, 2621441, 2049, 43, 0, 0, 3805184, 1406286835, 1408573505, 1408573505)) # returns os.getcwd() == '/current' # returns os.chdir(pth.WorkingDir('/bogus')) == None # returns with story.replay(strict=False): pth('/bogus').cd()
def test_open_missing(): with Story(['io.open', 'os.path.isdir']) as story: os.path.isdir(pth.Path('foo')) == False io.open(pth.Path('foo'), 'rb') ** IOError(errno.ENOENT, "Whatev") with story.replay(strict=True): raises(pth.PathMustBeFile, lambda: pth('foo')('rb'))
def test_cd_context_manager_call(): with Story(['os.stat', 'os.chdir', 'os.getcwd']) as story: os.stat(pth.WorkingDir('other')) == Namespace(st_mode=16893) os.getcwd() == '/foobar' # returns os.chdir(pth.WorkingDir('other')) == None # returns os.chdir(pth.Path('/foobar')) == None # returns with story.replay(strict=True): with pth('other').cd() as wd: assert wd == 'other' assert wd.previous == '/foobar'
def test_cd_call(): with Story(['os.stat', 'os.chdir', 'os.getcwd']) as story: os.stat(pth.WorkingDir('other')) == Namespace(st_mode=16893) os.getcwd() == '/foobar' # returns os.chdir(pth.WorkingDir('other')) == None # returns with story.replay(strict=True): cd = pth('other').cd assert cd == 'other' assert cd.previous is None cd()
def test_dirname(): assert pth("/my/abs/path").dir == "/my/abs" assert pth("/my/abs/path").dirname == "/my/abs" assert pth("path").dir == "" assert pth("path").dirname == "" assert pth("rel/path").dir == "rel" assert pth("rel/path").dirname == "rel"
def test_basename(): assert pth("/my/abs/path").name == "path" assert pth("/my/abs/path").basename == "path" assert pth("path").name == "path" assert pth("path").basename == "path" assert pth("rel/path").name == "path" assert pth("rel/path").basename == "path"
def test_list(): assert sorted(list(pth('tests', 'files').list)) == [ pth('tests', 'files', 'a'), pth('tests', 'files', 'b.txt'), pth('tests', 'files', 'test.zip'), pth('tests', 'files', 'trîcky-năme'), ] raises(pth.PathMustBeDirectory, next, pth('bogus-doesnt-exist').list)
def test_normpath(): p = pth('.', 'tEsts', '..', 'tests') assert p.normpath == os.path.normpath(p)
def test_join_zippath_abs(): assert pth('tests/files/test.zip') / '/etc' == '/etc'
def test_relpath_zip(): r = pth('tests/files') p = pth('tests/files/test.zip') q = p / 'a' assert q.rel(p) == q.relpath(p) == 'a' assert p.rel(r) == p.rel(r) == 'test.zip'
def test_normcase(): p = pth('.', 'tEsts', '..', 'tests') assert p.normcase == os.path.normcase(p)
def test_realpath(): p = pth('tests') assert p.real == p.realpath == os.path.realpath(p)
def test_relpath(): p = pth('tests') q = p / 'a' / 'b' assert q.rel(p) == q.relpath(p) == pth('a', 'b')
def test_readlink(): with Story(['os.readlink']) as story: os.readlink(pth.Path('foo')) == 'bar' with story.replay(strict=True): assert pth('foo').readlink == 'bar'
def test_samefile(): assert pth('.').same(pth().abs) assert pth('.').samefile(pth().abs)
def test_access(): with Story(['os.access']) as story: os.access(pth.Path('foo'), os.R_OK) == True with story.replay(strict=True): assert pth('foo').access(os.R_OK)
def test_abspath(): with pth.tmp().cd as tmp: assert pth("path").abs == tmp / "path" assert pth("path").abspath == tmp / "path"
def test_chflags_kwargs(): with Story(['os.lchflags']) as story: os.lchflags(pth.Path('foo'), stat.UF_NODUMP) == None with story.replay(strict=True): pth('foo').chflags(stat.UF_NODUMP, follow_symlinks=True)
def test_isexecutable_kwargs(): with Story(['os.access']) as story: os.access(pth.Path('foo'), os.R_OK | os.X_OK, follow_symlinks=True) == True with story.replay(strict=True): assert pth('foo').isexecutable(follow_symlinks=True)
def test_lexists(): assert pth("/").lexists assert not pth("bogus/doesn't/exist").lexists
def test_isexecutable(): with Story(['os.access']) as story: os.access(pth.Path('foo'), os.R_OK | os.X_OK) == True with story.replay(strict=True): assert pth('foo').isexecutable
def test_lchflags(): with Story(['os.chflags']) as story: os.lchflags(pth.Path('foo'), stat.UF_NODUMP) == None with story.replay(strict=True): pth('foo').lchflags(stat.UF_NODUMP)