@@ -524,20 +524,23 @@ def test_model_checkpoint_link_checkpoint(tmp_path):
524
524
ModelCheckpoint ._link_checkpoint (trainer , filepath = str (file ), linkpath = str (link ))
525
525
assert os .path .islink (link )
526
526
assert os .path .realpath (link ) == str (file )
527
+ assert not os .path .isabs (os .readlink (link ))
527
528
528
529
# link exists (is a file)
529
530
new_file1 = tmp_path / "new_file1"
530
531
new_file1 .touch ()
531
532
ModelCheckpoint ._link_checkpoint (trainer , filepath = str (new_file1 ), linkpath = str (link ))
532
533
assert os .path .islink (link )
533
534
assert os .path .realpath (link ) == str (new_file1 )
535
+ assert not os .path .isabs (os .readlink (link ))
534
536
535
537
# link exists (is a link)
536
538
new_file2 = tmp_path / "new_file2"
537
539
new_file2 .touch ()
538
540
ModelCheckpoint ._link_checkpoint (trainer , filepath = str (new_file2 ), linkpath = str (link ))
539
541
assert os .path .islink (link )
540
542
assert os .path .realpath (link ) == str (new_file2 )
543
+ assert not os .path .isabs (os .readlink (link ))
541
544
542
545
# link exists (is a folder)
543
546
folder = tmp_path / "folder"
@@ -547,13 +550,15 @@ def test_model_checkpoint_link_checkpoint(tmp_path):
547
550
ModelCheckpoint ._link_checkpoint (trainer , filepath = str (folder ), linkpath = str (folder_link ))
548
551
assert os .path .islink (folder_link )
549
552
assert os .path .realpath (folder_link ) == str (folder )
553
+ assert not os .path .isabs (os .readlink (folder_link ))
550
554
551
555
# link exists (is a link to a folder)
552
556
new_folder = tmp_path / "new_folder"
553
557
new_folder .mkdir ()
554
558
ModelCheckpoint ._link_checkpoint (trainer , filepath = str (new_folder ), linkpath = str (folder_link ))
555
559
assert os .path .islink (folder_link )
556
560
assert os .path .realpath (folder_link ) == str (new_folder )
561
+ assert not os .path .isabs (os .readlink (folder_link ))
557
562
558
563
# simulate permission error on Windows (creation of symbolic links requires privileges)
559
564
file = tmp_path / "win_file"
@@ -565,6 +570,22 @@ def test_model_checkpoint_link_checkpoint(tmp_path):
565
570
assert os .path .isfile (link ) # fall back to copying instead of linking
566
571
567
572
573
+ def test_model_checkpoint_link_checkpoint_relative_path (tmp_path , monkeypatch ):
574
+ """Test that linking a checkpoint works with relative paths."""
575
+ trainer = Mock ()
576
+ monkeypatch .chdir (tmp_path )
577
+
578
+ folder = Path ("x/z/z" )
579
+ folder .mkdir (parents = True )
580
+ file = folder / "file"
581
+ file .touch ()
582
+ link = folder / "link"
583
+ ModelCheckpoint ._link_checkpoint (trainer , filepath = str (file .absolute ()), linkpath = str (link .absolute ()))
584
+ assert os .path .islink (link )
585
+ assert Path (os .readlink (link )) == file .relative_to (folder )
586
+ assert not os .path .isabs (os .readlink (link ))
587
+
588
+
568
589
def test_invalid_top_k (tmpdir ):
569
590
"""Make sure that a MisconfigurationException is raised for a negative save_top_k argument."""
570
591
with pytest .raises (MisconfigurationException , match = r".*Must be >= -1" ):
0 commit comments