def test_chdir(self): with TestServer(): with open_may("localhost", user="******", passwd="passwd") as may: # make directories may.makedirs("example1/example11", exist_ok=True) may.makedirs("example1/example12", exist_ok=True) may.makedirs("example1/example12/example121", exist_ok=True) # change dir and check may.chdir("example1") self.assertEqual(may.curdir(), Path("/example1")) may.chdir("example12") self.assertEqual(may.curdir(), Path("/example1/example12")) may.chdir("example121") self.assertEqual(may.curdir(), Path("/example1/example12/example121")) with self.assertRaises(FileNotFoundError): may.chdir("unknown-directory") may.chdir("/") self.assertEqual(may.curdir(), Path("/"))
def test_open(self): with TestServer(): with open_may("localhost", user="******", passwd="passwd") as may: # write then read with may.open("example.txt", "w") as stream: stream.write(TESTDATA) with may.open("example.txt", "r") as stream: self.assertEqual(stream.read(), TESTDATA) # try read unexists file with self.assertRaises(FileNotFoundError): with may.open("unexists-file.txt", "r"): pass # try read directory may.mkdir("exampledir") with self.assertRaises(PermissionError): with may.open("exampledir", "w"): pass with self.assertRaises(PermissionError): with may.open("exampledir", "r"): pass
def test_rename(self): with TestServer(): with open_may("localhost", user="******", passwd="passwd") as may: # make directory and file may.mkdir("exampledir") with may.open("example.txt", "w"): pass # rename files may.rename("exampledir", "exampledir2") may.rename("example.txt", "example2.txt") with self.assertRaises(SameFileError): may.rename("exampledir", "exampledir") with self.assertRaises(SameFileError): may.rename("example.txt", "example.txt") # check the structure files = list(may.iterdir()) self.assertEqual(len(files), 2) self.assertIn(Path("/exampledir2"), files) self.assertIn(Path("/example2.txt"), files)
def test_makedirs(self): with TestServer(): with open_may("localhost", user="******", passwd="passwd") as may: # make directory may.makedirs("example1", exist_ok=True) may.makedirs("example2/example21", exist_ok=True) may.makedirs("example2/example22", exist_ok=True) # try make directory (always raise error) with self.assertRaises(FileExistsError): may.makedirs("example1", exist_ok=False) with self.assertRaises(FileExistsError): may.makedirs("example2/example21", exist_ok=False) with self.assertRaises(FileExistsError): may.makedirs("example2/example22", exist_ok=False) # check directory rootfiles = list(may.iterdir("")) self.assertEqual(len(rootfiles), 2) self.assertIn(Path("/example1"), rootfiles) self.assertIn(Path("/example2"), rootfiles) example1files = list(may.iterdir("example1")) self.assertEqual(len(example1files), 0) example2files = list(may.iterdir("example2")) self.assertEqual(len(example2files), 2) self.assertIn(Path("/example2/example21"), example2files) self.assertIn(Path("/example2/example22"), example2files)
def test_removedirs(self): with TestServer(): with open_may("localhost", user="******", passwd="passwd") as may: # make directories and files # # not-a-directory.txt # example1 => # example11 => # example111 # example112 # example12 => # keep.txt # example13 may.makedirs("example1/example11", exist_ok=True) may.makedirs("example1/example11/example111", exist_ok=True) may.makedirs("example1/example11/example112", exist_ok=True) may.makedirs("example1/example12", exist_ok=True) may.makedirs("example1/example13", exist_ok=True) with may.open("not-a-directory.txt", "w"): pass with may.open("example1/example12/keep.txt", "w"): pass # remove directories may.removedirs("example1") # directory with self.assertRaises(NotADirectoryError): # file may.removedirs("not-a-directory.txt") with self.assertRaises(FileNotFoundError): # unexists may.removedirs("example2") # check structures rootfiles = list(may.iterdir("/")) self.assertEqual(len(rootfiles), 2) self.assertIn(Path("/not-a-directory.txt"), rootfiles) self.assertIn(Path("/example1"), rootfiles) example1files = list(may.iterdir("/example1")) self.assertEqual(len(example1files), 1) self.assertIn(Path("/example1/example12"), example1files) example12files = list(may.iterdir("/example1/example12")) self.assertEqual(len(example12files), 1) self.assertIn( Path("/example1/example12/keep.txt"), example12files)
def test_upload_and_download(self): with TestServer(): with open_may("localhost", user="******", passwd="passwd") as may: # upload tempfileup = NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) tempfileup.write(TESTDATA) tempfileup.close() may.upload(tempfileup.name, Path(tempfileup.name).name) # download tempfiledown = NamedTemporaryFile(delete=False) tempfiledown.close() may.download(Path(tempfileup.name).name, tempfiledown.name) with open(tempfiledown.name, "r") as stream: self.assertEqual(TESTDATA, stream.read())