def test_get_unique_name_integer(self): with temporary_path() as tmp: for _ in range(10): file_path = unique_path(tmp.joinpath("file.txt"), mode=UniqueMode.INTEGER) file_path.touch() self.assertEqual(len(os.listdir(tmp)), 10)
def test_basic_command(self): with temporary_path() as tmp: cmd = CommandRunner(cwd=tmp) if os.name == "nt": cmd.run(("mkdir", "test"), capture_output=False, shell=True) else: cmd.run(("mkdir", "test"), capture_output=False) self.assertTrue(tmp.joinpath("test").is_dir())
def test_get_unique_name_force(self): with temporary_path() as tmp: base_file = tmp.joinpath("file.txt") self.assertFalse(base_file.is_file()) file_path = unique_path(base_file, mode=UniqueMode.INTEGER, length=4, delimiter="_", force=True) self.assertEqual(file_path.name, "file_0001.txt")
def test_temp_path_readonly(self): with temporary_path() as tmp: ro_path = tmp.joinpath("ro-folder") ro_path.mkdir() ro_file = ro_path.joinpath("ro-file") with ro_file.open("w") as fp: fp.write("test") ro_file.chmod(stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) ro_path.chmod(stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) with self.assertRaises(PermissionError): with ro_file.open("w") as fp: fp.write("test") self.assertTrue(tmp.is_dir()) self.assertFalse(tmp.is_dir())
def test_basic_command_output(self): with temporary_path() as tmp: cmd = CommandRunner(cwd=tmp) if os.name == "nt": cmd.run(("mkdir", "test"), capture_output=True, shell=True) self.assertTrue(tmp.joinpath("test").is_dir()) output = cmd.run("dir", capture_output=True, shell=True) match_pattern = r"^.*?<DIR>\s+test$" else: cmd.run(("mkdir", "test"), capture_output=True) self.assertTrue(tmp.joinpath("test").is_dir()) output = cmd.run(("ls", "-l"), capture_output=True) match_pattern = r"^[d].*\s(?:test)$" matched = False for line in output.stdout.split("\n"): line = line.strip() if re.match(match_pattern, line): matched = True if not matched: self.fail(f"No matches in '{output.stdout}'")
def test_temp_path(self): with temporary_path() as tmp: self.assertTrue(tmp.is_dir()) self.assertFalse(tmp.is_dir())
def test_handle_remove_readonly(self): with temporary_path() as tmp: fake_path = tmp.joinpath("fake") with self.assertRaises(FileNotFoundError): shutil.rmtree(fake_path, onerror=handle_remove_readonly)
def test_get_unique_name_bad_mode(self): with temporary_path() as tmp: with self.assertRaises(NotImplementedError): # noinspection PyTypeChecker _ = unique_path(tmp.joinpath("file.txt"), mode=9999)