@@ -121,6 +121,60 @@ fn test_cp_duplicate_files() {
121121 assert_eq ! ( at. read( TEST_COPY_TO_FOLDER_FILE ) , "Hello, World!\n " ) ;
122122}
123123
124+ #[ test]
125+ fn test_cp_duplicate_folder ( ) {
126+ let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
127+ ucmd. arg ( "-r" )
128+ . arg ( TEST_COPY_FROM_FOLDER )
129+ . arg ( TEST_COPY_FROM_FOLDER )
130+ . arg ( TEST_COPY_TO_FOLDER )
131+ . succeeds ( )
132+ . stderr_contains ( format ! (
133+ "source directory '{TEST_COPY_FROM_FOLDER}' specified more than once"
134+ ) ) ;
135+ assert ! ( at. dir_exists( format!( "{TEST_COPY_TO_FOLDER}/{TEST_COPY_FROM_FOLDER}" ) . as_str( ) ) ) ;
136+ }
137+
138+ #[ test]
139+ fn test_cp_duplicate_files_normalized_path ( ) {
140+ let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
141+ ucmd. arg ( TEST_HELLO_WORLD_SOURCE )
142+ . arg ( format ! ( "./{TEST_HELLO_WORLD_SOURCE}" ) )
143+ . arg ( TEST_COPY_TO_FOLDER )
144+ . succeeds ( )
145+ . stderr_contains ( format ! (
146+ "source file './{TEST_HELLO_WORLD_SOURCE}' specified more than once"
147+ ) ) ;
148+ assert_eq ! ( at. read( TEST_COPY_TO_FOLDER_FILE ) , "Hello, World!\n " ) ;
149+ }
150+
151+ #[ test]
152+ fn test_cp_duplicate_files_with_plain_backup ( ) {
153+ let ( _, mut ucmd) = at_and_ucmd ! ( ) ;
154+ ucmd. arg ( TEST_HELLO_WORLD_SOURCE )
155+ . arg ( TEST_HELLO_WORLD_SOURCE )
156+ . arg ( TEST_COPY_TO_FOLDER )
157+ . arg ( "--backup" )
158+ . fails ( )
159+ // cp would skip duplicate src check and fail when it tries to overwrite the "just-created" file.
160+ . stderr_contains (
161+ "will not overwrite just-created 'hello_dir/hello_world.txt' with 'hello_world.txt" ,
162+ ) ;
163+ }
164+
165+ #[ test]
166+ fn test_cp_duplicate_files_with_numbered_backup ( ) {
167+ let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
168+ // cp would skip duplicate src check and succeeds
169+ ucmd. arg ( TEST_HELLO_WORLD_SOURCE )
170+ . arg ( TEST_HELLO_WORLD_SOURCE )
171+ . arg ( TEST_COPY_TO_FOLDER )
172+ . arg ( "--backup=numbered" )
173+ . succeeds ( ) ;
174+ at. file_exists ( TEST_COPY_TO_FOLDER_FILE ) ;
175+ at. file_exists ( format ! ( "{TEST_COPY_TO_FOLDER}.~1~" ) ) ;
176+ }
177+
124178#[ test]
125179fn test_cp_same_file ( ) {
126180 let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
0 commit comments