def test_mkdtemp(): fsn = mkdtemp() assert isinstance(fsn, fsnative) os.rmdir(fsn) fsn = mkdtemp(suffix="foo") fsn.endswith("foo") assert isinstance(fsn, fsnative) os.rmdir(fsn)
def test_canonicalise(self): from quodlibet.util.path import normalize_path as norm f, path = tempfile.mkstemp() path = os.path.realpath(path) # on osx tmp is a symlink os.close(f) path = norm(path) link_dir = mkdtemp() link = None if not is_win: link = os.path.join(link_dir, str(uuid.uuid4())) os.symlink(path, link) try: self.failUnlessEqual(norm(path, canonicalise=True), path) self.failUnlessEqual(norm(os.path.join(path, "foo", ".."), True), path) if link: self.failUnlessEqual(norm(link, True), path) # A symlink shouldn't be resolved unless asked for self.failIfEqual(norm(link, False), path) # And the other behaviour should also work unnormalised_path = os.path.join(link, "foo", "..") self.failUnlessEqual(norm(unnormalised_path, True), path) finally: if link: os.remove(link) os.remove(path) os.rmdir(link_dir)
def test_python_handling_broken_utf16(): # Create a file with an invalid utf-16 name. # Mainly to see how Python handles it tmp = mkdtemp() try: path = os.path.join(tmp, "foo") with open(path, "wb") as h: h.write(b"content") assert "foo" in os.listdir(tmp) if os.name == "nt": faulty = (path.encode("utf-16-le") + b"=\xd8\x01\xde" + b"=\xd8-\x00\x01\xde") buf = ctypes.create_string_buffer(faulty + b"\x00\x00") if winapi.MoveFileW(path, ctypes.cast(buf, ctypes.c_wchar_p)) == 0: raise ctypes.WinError() assert "foo" not in os.listdir(tmp) newpath = os.path.join(tmp, os.listdir(tmp)[0]) if not is_wine: # this is broken on wine.. assert newpath.encode("utf-16-le", _surrogatepass) == faulty with open(newpath, "rb") as h: assert h.read() == b"content" finally: shutil.rmtree(tmp)
def test_rename_other_dir(self): old_fn = self.quux["~filename"] new_dir = mkdtemp() self.quux.rename(os.path.join(new_dir, "foo")) assert not os.path.exists(old_fn) assert self.quux.exists() self.quux.rename(old_fn) assert self.quux.exists() os.rmdir(new_dir)
def test_manydeep(self): self.failUnless(not os.path.isdir("nonext")) t = mkdtemp() path = os.path.join(t, "nonext", "test", "test2", "test3") mkdir(path) try: self.failUnless(os.path.isdir(path)) finally: os.rmdir(path) path = os.path.dirname(path) os.rmdir(path) path = os.path.dirname(path) os.rmdir(path) path = os.path.dirname(path) os.rmdir(path) os.rmdir(t)