|
12 | 12 |
|
13 | 13 | from upath import UnsupportedOperation |
14 | 14 | from upath import UPath |
| 15 | +from upath._protocol import get_upath_protocol |
15 | 16 | from upath._stat import UPathStatResult |
16 | 17 | from upath.types import StatResultType |
17 | 18 |
|
@@ -275,31 +276,101 @@ def test_readlink(self): |
275 | 276 | self.path.readlink() |
276 | 277 |
|
277 | 278 | def test_rename(self): |
278 | | - upath = self.path.joinpath("file1.txt") |
279 | | - target = upath.parent.joinpath("file1_renamed.txt") |
280 | | - moved = upath.rename(target) |
281 | | - assert target == moved |
282 | | - assert not upath.exists() |
283 | | - assert moved.exists() |
284 | | - # reverse with an absolute path as str |
285 | | - back = moved.rename(upath.path) |
286 | | - assert back == upath |
287 | | - assert not moved.exists() |
288 | | - assert back.exists() |
289 | | - |
290 | | - def test_rename2(self): |
291 | | - upath = self.path.joinpath("folder1/file2.txt") |
292 | | - target = "file2_renamed.txt" |
293 | | - moved = upath.rename(target) |
294 | | - target_path = upath.parent.joinpath(target).resolve() |
295 | | - assert target_path == moved |
296 | | - assert not upath.exists() |
297 | | - assert moved.exists() |
298 | | - # reverse with a relative path as UPath |
299 | | - back = moved.rename(UPath("file2.txt")) |
300 | | - assert back == upath |
301 | | - assert not moved.exists() |
302 | | - assert back.exists() |
| 279 | + p_source = self.path.joinpath("file1.txt") |
| 280 | + p_target = self.path.joinpath("file1_renamed.txt") |
| 281 | + |
| 282 | + p_moved = p_source.rename(p_target) |
| 283 | + assert p_target == p_moved |
| 284 | + assert not p_source.exists() |
| 285 | + assert p_moved.exists() |
| 286 | + |
| 287 | + p_revert = p_moved.rename(p_source) |
| 288 | + assert p_revert == p_source |
| 289 | + assert not p_moved.exists() |
| 290 | + assert p_revert.exists() |
| 291 | + |
| 292 | + @pytest.fixture |
| 293 | + def supports_cwd(self): |
| 294 | + # intentionally called on the instance to support ProxyUPath().cwd() |
| 295 | + try: |
| 296 | + self.path.cwd() |
| 297 | + except UnsupportedOperation: |
| 298 | + return False |
| 299 | + else: |
| 300 | + return True |
| 301 | + |
| 302 | + @pytest.mark.parametrize( |
| 303 | + "target_factory", |
| 304 | + [ |
| 305 | + lambda obj, name: name, |
| 306 | + lambda obj, name: UPath(name), |
| 307 | + lambda obj, name: Path(name), |
| 308 | + lambda obj, name: obj.joinpath(name).relative_to(obj), |
| 309 | + ], |
| 310 | + ids=[ |
| 311 | + "str_relative", |
| 312 | + "plain_upath_relative", |
| 313 | + "plain_path_relative", |
| 314 | + "self_upath_relative", |
| 315 | + ], |
| 316 | + ) |
| 317 | + def test_rename_with_target_relative( |
| 318 | + self, request, monkeypatch, supports_cwd, target_factory, tmp_path |
| 319 | + ): |
| 320 | + source = self.path.joinpath("folder1/file2.txt") |
| 321 | + target = target_factory(self.path, "file2_renamed.txt") |
| 322 | + |
| 323 | + source_text = source.read_text() |
| 324 | + if supports_cwd: |
| 325 | + cid = request.node.callspec.id |
| 326 | + cwd = tmp_path.joinpath(cid) |
| 327 | + cwd.mkdir(parents=True, exist_ok=True) |
| 328 | + monkeypatch.chdir(cwd) |
| 329 | + |
| 330 | + t = source.rename(target) |
| 331 | + assert (t.protocol == UPath(target).protocol) or UPath( |
| 332 | + target |
| 333 | + ).protocol == "" |
| 334 | + assert (t.path == UPath(target).path) or ( |
| 335 | + t.path == UPath(target).absolute().path |
| 336 | + ) |
| 337 | + assert t.exists() |
| 338 | + assert t.read_text() == source_text |
| 339 | + |
| 340 | + else: |
| 341 | + with pytest.raises(UnsupportedOperation): |
| 342 | + source.rename(target) |
| 343 | + |
| 344 | + @pytest.mark.parametrize( |
| 345 | + "target_factory", |
| 346 | + [ |
| 347 | + lambda obj, name: obj.joinpath(name).absolute().as_posix(), |
| 348 | + lambda obj, name: UPath(obj.absolute().joinpath(name).path), |
| 349 | + lambda obj, name: Path(obj.absolute().joinpath(name).path), |
| 350 | + lambda obj, name: obj.absolute().joinpath(name), |
| 351 | + ], |
| 352 | + ids=[ |
| 353 | + "str_absolute", |
| 354 | + "plain_upath_absolute", |
| 355 | + "plain_path_absolute", |
| 356 | + "self_upath_absolute", |
| 357 | + ], |
| 358 | + ) |
| 359 | + def test_rename_with_target_absolute(self, target_factory): |
| 360 | + from upath._chain import Chain |
| 361 | + from upath._chain import FSSpecChainParser |
| 362 | + |
| 363 | + source = self.path.joinpath("folder1/file2.txt") |
| 364 | + target = target_factory(self.path, "file2_renamed.txt") |
| 365 | + |
| 366 | + source_text = source.read_text() |
| 367 | + t = source.rename(target) |
| 368 | + assert get_upath_protocol(target) in {t.protocol, ""} |
| 369 | + assert t.path == Chain.from_list( |
| 370 | + FSSpecChainParser().unchain(str(target)) |
| 371 | + ).active_path.replace("\\", "/") |
| 372 | + assert t.exists() |
| 373 | + assert t.read_text() == source_text |
303 | 374 |
|
304 | 375 | def test_replace(self): |
305 | 376 | pass |
|
0 commit comments