@@ -657,6 +657,36 @@ def test_file_with_prefix(self, temp_file: File):
657657 temp_file .with_prefix ("new_" )
658658 assert "new_test.txt" in temp_file .path
659659
660+ def test_file_with_name (self , temp_file : File ):
661+ """with_name() returns new File with different name."""
662+ original_dir = temp_file .dirname
663+ new_file = temp_file .with_name ("other.py" )
664+ assert new_file .basename == "other.py"
665+ assert new_file .dirname == original_dir
666+ assert new_file is not temp_file
667+
668+ def test_file_with_name_no_parent (self ):
669+ """with_name() works with no parent directory."""
670+ f = fs .File ("just_a_file.txt" )
671+ new_f = f .with_name ("renamed.py" )
672+ assert new_f .path == "renamed.py"
673+
674+ def test_file_with_stem (self , temp_file : File ):
675+ """with_stem() returns new File with different stem."""
676+ new_file = temp_file .with_stem ("different" )
677+ assert new_file .stem == "different"
678+ assert new_file .suffix == ".txt"
679+ assert new_file is not temp_file
680+
681+ def test_file_with_stem_no_extension (self , tmp_path : Path ):
682+ """with_stem() works with files without extension."""
683+ f = tmp_path / "noext"
684+ f .touch ()
685+ file = fs .File (str (f ))
686+ new_file = file .with_stem ("changed" )
687+ assert new_file .basename == "changed"
688+ assert new_file .suffix == ""
689+
660690
661691class TestFileRename :
662692 def test_file_rename (self , temp_file : File ):
@@ -1365,6 +1395,20 @@ def test_directory_to_str(self, temp_directory: fs.Directory):
13651395 assert isinstance (string_path , str )
13661396 assert string_path == temp_directory .path
13671397
1398+ def test_directory_with_name (self , temp_directory : fs .Directory ):
1399+ """with_name() returns new Directory with different name."""
1400+ original_parent = os .path .dirname (temp_directory .path )
1401+ new_dir = temp_directory .with_name ("otherdir" )
1402+ assert new_dir .basename == "otherdir"
1403+ assert os .path .dirname (new_dir .path ) == original_parent
1404+ assert new_dir is not temp_directory
1405+
1406+ def test_directory_with_name_no_parent (self ):
1407+ """with_name() works with no parent directory."""
1408+ d = fs .Directory ("mydir" )
1409+ new_d = d .with_name ("renamed" )
1410+ assert new_d .path == "renamed"
1411+
13681412
13691413class TestDirectoryAssertions :
13701414 def test_directory_should_exist (self , temp_directory : fs .Directory ):
0 commit comments