@@ -2021,61 +2021,74 @@ fn test_split_non_utf8_paths() {
20212021
20222022#[ test]
20232023#[ cfg( target_os = "linux" ) ]
2024- fn test_split_non_utf8_prefix ( ) {
2025- use std:: os:: unix:: ffi:: OsStrExt ;
2024+ fn test_split_non_utf8_prefix_is_byte_preserving ( ) {
2025+ use std:: ffi:: OsStr ;
2026+ use std:: os:: unix:: ffi:: { OsStrExt , OsStringExt } ;
2027+
20262028 let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
2029+ at. write ( "input.txt" , "AB" ) ;
20272030
2028- at. write ( "input.txt" , "line1\n line2\n line3\n line4\n " ) ;
2031+ let invalid_prefix_bytes = b"p\xFF " ;
2032+ at. write ( "p�aa" , "keep-aa" ) ;
2033+ at. write ( "p�ab" , "keep-ab" ) ;
20292034
2030- let prefix = std:: ffi:: OsStr :: from_bytes ( b"\xFF \xFE " ) ;
2031- ucmd. arg ( "input.txt" ) . arg ( prefix) . succeeds ( ) ;
2035+ ucmd. args ( & [ "-b" , "1" , "input.txt" ] )
2036+ . arg ( OsStr :: from_bytes ( invalid_prefix_bytes) )
2037+ . succeeds ( ) ;
20322038
2033- // Check that split files were created (functionality works )
2034- // The actual filename may be converted due to lossy conversion, but the command should succeed
2035- let entries : Vec < _ > = fs :: read_dir ( at . as_string ( ) ) . unwrap ( ) . collect ( ) ;
2036- let split_files = entries
2037- . iter ( )
2038- . filter_map ( |e| e . as_ref ( ) . ok ( ) )
2039- . filter ( |entry| {
2040- let name = entry . file_name ( ) ;
2041- let name_str = name . to_string_lossy ( ) ;
2042- name_str . starts_with ( "�" ) || name_str . len ( ) > 2 // split files should exist
2039+ let mut produced_split_files : Vec < Vec < u8 > > = fs :: read_dir ( at . as_string ( ) )
2040+ . expect ( "temporary split directory should be readable" )
2041+ . filter_map ( |entry| {
2042+ let entry = entry . ok ( ) ? ;
2043+ let name = entry . file_name ( ) . into_vec ( ) ;
2044+ if name . starts_with ( invalid_prefix_bytes ) {
2045+ Some ( name )
2046+ } else {
2047+ None
2048+ }
20432049 } )
2044- . count ( ) ;
2045- assert ! (
2046- split_files > 0 ,
2047- "Expected at least one split file to be created"
2050+ . collect ( ) ;
2051+ produced_split_files. sort ( ) ;
2052+
2053+ assert_eq ! (
2054+ produced_split_files,
2055+ vec![ b"p\xFF aa" . to_vec( ) , b"p\xFF ab" . to_vec( ) ]
20482056 ) ;
2057+ assert_eq ! ( at. read( "p�aa" ) , "keep-aa" ) ;
2058+ assert_eq ! ( at. read( "p�ab" ) , "keep-ab" ) ;
20492059}
20502060
20512061#[ test]
20522062#[ cfg( target_os = "linux" ) ]
2053- fn test_split_non_utf8_additional_suffix ( ) {
2054- use std:: os :: unix :: ffi:: OsStrExt ;
2055- let ( at , mut ucmd ) = at_and_ucmd ! ( ) ;
2063+ fn test_split_non_utf8_additional_suffix_is_byte_preserving ( ) {
2064+ use std:: ffi:: OsStr ;
2065+ use std :: os :: unix :: ffi :: { OsStrExt , OsStringExt } ;
20562066
2057- at. write ( "input.txt" , "line1\n line2\n line3\n line4\n " ) ;
2067+ let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
2068+ at. write ( "input.txt" , "AB" ) ;
20582069
2059- let suffix = std :: ffi :: OsStr :: from_bytes ( b"\xFF \xFE " ) ;
2060- ucmd. args ( & [ "input.txt" , "--additional-suffix" ] )
2061- . arg ( suffix )
2070+ let suffix_bytes = b"\xFF \xFE " ;
2071+ ucmd. args ( & [ "-b" , "1" , " input.txt", "--additional-suffix" ] )
2072+ . arg ( OsStr :: from_bytes ( suffix_bytes ) )
20622073 . succeeds ( ) ;
20632074
2064- // Check that split files were created (functionality works )
2065- // The actual filename may be converted due to lossy conversion, but the command should succeed
2066- let entries : Vec < _ > = fs :: read_dir ( at . as_string ( ) ) . unwrap ( ) . collect ( ) ;
2067- let split_files = entries
2068- . iter ( )
2069- . filter_map ( |e| e . as_ref ( ) . ok ( ) )
2070- . filter ( |entry| {
2071- let name = entry . file_name ( ) ;
2072- let name_str = name . to_string_lossy ( ) ;
2073- name_str . ends_with ( "�" ) || name_str . starts_with ( 'x' ) // split files should exist
2075+ let mut produced_with_suffix : Vec < Vec < u8 > > = fs :: read_dir ( at . as_string ( ) )
2076+ . expect ( "temporary split directory should be readable" )
2077+ . filter_map ( |entry| {
2078+ let entry = entry . ok ( ) ? ;
2079+ let name = entry . file_name ( ) . into_vec ( ) ;
2080+ if name . starts_with ( b"xa" ) && name . ends_with ( suffix_bytes ) {
2081+ Some ( name )
2082+ } else {
2083+ None
2084+ }
20742085 } )
2075- . count ( ) ;
2076- assert ! (
2077- split_files > 0 ,
2078- "Expected at least one split file to be created"
2086+ . collect ( ) ;
2087+ produced_with_suffix. sort ( ) ;
2088+
2089+ assert_eq ! (
2090+ produced_with_suffix,
2091+ vec![ b"xaa\xFF \xFE " . to_vec( ) , b"xab\xFF \xFE " . to_vec( ) ]
20792092 ) ;
20802093}
20812094
@@ -2089,5 +2102,5 @@ fn test_split_directory_already_exists() {
20892102 ucmd. args ( & [ "file" ] )
20902103 . fails_with_code ( 1 )
20912104 . no_stdout ( )
2092- . stderr_is ( "split: xaa: Is a directory\n " ) ;
2105+ . stderr_is ( "split: ' xaa' : Is a directory\n " ) ;
20932106}
0 commit comments