44// file that was distributed with this source code.
55
66// spell-checker:ignore (paths) atim sublink subwords azerty azeaze xcwww azeaz amaz azea qzerty tazerty tsublink testfile1 testfile2 filelist fpath testdir testfile
7- // spell-checker:ignore selfref ELOOP
7+ // spell-checker:ignore selfref ELOOP smallfile
88#[ cfg( not( windows) ) ]
99use regex:: Regex ;
1010
@@ -197,45 +197,71 @@ fn test_du_soft_link() {
197197 let ts = TestScenario :: new ( util_name ! ( ) ) ;
198198 let at = & ts. fixtures ;
199199
200- at. symlink_file ( SUB_FILE , SUB_LINK ) ;
200+ // Create the directory and file structure explicitly for this test
201+ at. mkdir_all ( "subdir/links" ) ;
202+ at. write ( "subdir/links/subwords.txt" , & "hello world\n " . repeat ( 100 ) ) ;
203+ at. symlink_file ( "subdir/links/subwords.txt" , "subdir/links/sublink.txt" ) ;
201204
202- let result = ts. ucmd ( ) . arg ( SUB_DIR_LINKS ) . succeeds ( ) ;
205+ let result = ts. ucmd ( ) . arg ( "subdir/links" ) . succeeds ( ) ;
203206
204207 #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
205208 {
206- let result_reference = unwrap_or_return ! ( expected_result( & ts, & [ SUB_DIR_LINKS ] ) ) ;
209+ let result_reference = unwrap_or_return ! ( expected_result( & ts, & [ "subdir/links" ] ) ) ;
207210 if result_reference. succeeded ( ) {
208211 assert_eq ! ( result. stdout_str( ) , result_reference. stdout_str( ) ) ;
209212 return ;
210213 }
211214 }
212- du_soft_link ( result. stdout_str ( ) ) ;
213- }
214215
215- #[ cfg( target_vendor = "apple" ) ]
216- fn du_soft_link ( s : & str ) {
217- // 'macos' host variants may have `du` output variation for soft links
218- assert ! ( ( s == "12\t subdir/links\n " ) || ( s == "16\t subdir/links\n " ) ) ;
219- }
220- #[ cfg( target_os = "windows" ) ]
221- fn du_soft_link ( s : & str ) {
222- assert_eq ! ( s, "8\t subdir/links\n " ) ;
223- }
224- #[ cfg( target_os = "freebsd" ) ]
225- fn du_soft_link ( s : & str ) {
226- assert_eq ! ( s, "16\t subdir/links\n " ) ;
227- }
228- #[ cfg( all(
229- not( target_vendor = "apple" ) ,
230- not( target_os = "windows" ) ,
231- not( target_os = "freebsd" )
232- ) ) ]
233- fn du_soft_link ( s : & str ) {
234- // MS-WSL linux has altered expected output
235- if uucore:: os:: is_wsl_1 ( ) {
236- assert_eq ! ( s, "8\t subdir/links\n " ) ;
237- } else {
238- assert_eq ! ( s, "16\t subdir/links\n " ) ;
216+ let s = result. stdout_str ( ) ;
217+ println ! ( "Output: {s}" ) ;
218+
219+ // Helper closure to assert output matches one of the valid sizes
220+ #[ cfg( any( target_vendor = "apple" , target_os = "windows" , target_os = "freebsd" ) ) ]
221+ let assert_valid_size = |output : & str , valid_sizes : & [ & str ] | {
222+ assert ! (
223+ valid_sizes. contains( & output) ,
224+ "Expected one of {valid_sizes:?}, got {output}"
225+ ) ;
226+ } ;
227+
228+ #[ cfg( target_vendor = "apple" ) ]
229+ {
230+ // 'macos' host variants may have `du` output variation for soft links
231+ let valid_sizes = [
232+ "8\t subdir/links\n " ,
233+ "12\t subdir/links\n " ,
234+ "16\t subdir/links\n " ,
235+ ] ;
236+ assert_valid_size ( s, & valid_sizes) ;
237+ }
238+
239+ #[ cfg( target_os = "windows" ) ]
240+ {
241+ let valid_sizes = [ "4\t subdir/links\n " , "8\t subdir/links\n " ] ;
242+ assert_valid_size ( s, & valid_sizes) ;
243+ }
244+
245+ #[ cfg( target_os = "freebsd" ) ]
246+ {
247+ // FreeBSD may have different block allocations depending on filesystem
248+ // Accept both common sizes
249+ let valid_sizes = [ "12\t subdir/links\n " , "16\t subdir/links\n " ] ;
250+ assert_valid_size ( & s, & valid_sizes) ;
251+ }
252+
253+ #[ cfg( all(
254+ not( target_vendor = "apple" ) ,
255+ not( target_os = "windows" ) ,
256+ not( target_os = "freebsd" )
257+ ) ) ]
258+ {
259+ // MS-WSL linux has altered expected output
260+ if uucore:: os:: is_wsl_1 ( ) {
261+ assert_eq ! ( s, "8\t subdir/links\n " ) ;
262+ } else {
263+ assert_eq ! ( s, "16\t subdir/links\n " ) ;
264+ }
239265 }
240266}
241267
@@ -814,12 +840,18 @@ fn test_du_no_exec_permission() {
814840#[ cfg( not( target_os = "openbsd" ) ) ]
815841fn test_du_one_file_system ( ) {
816842 let ts = TestScenario :: new ( util_name ! ( ) ) ;
843+ let at = & ts. fixtures ;
844+
845+ // Create the directory structure explicitly for this test
846+ at. mkdir_all ( "subdir/deeper/deeper_dir" ) ;
847+ at. write ( "subdir/deeper/deeper_dir/deeper_words.txt" , "hello world" ) ;
848+ at. write ( "subdir/deeper/words.txt" , "world" ) ;
817849
818- let result = ts. ucmd ( ) . arg ( "-x" ) . arg ( SUB_DIR ) . succeeds ( ) ;
850+ let result = ts. ucmd ( ) . arg ( "-x" ) . arg ( "subdir/deeper" ) . succeeds ( ) ;
819851
820852 #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
821853 {
822- let result_reference = unwrap_or_return ! ( expected_result( & ts, & [ "-x" , SUB_DIR ] ) ) ;
854+ let result_reference = unwrap_or_return ! ( expected_result( & ts, & [ "-x" , "subdir/deeper" ] ) ) ;
823855 if result_reference. succeeded ( ) {
824856 assert_eq ! ( result. stdout_str( ) , result_reference. stdout_str( ) ) ;
825857 return ;
@@ -832,16 +864,26 @@ fn test_du_one_file_system() {
832864#[ cfg( not( target_os = "openbsd" ) ) ]
833865fn test_du_threshold ( ) {
834866 let ts = TestScenario :: new ( util_name ! ( ) ) ;
867+ let at = & ts. fixtures ;
868+
869+ // Create the directory structure explicitly for this test
870+ at. mkdir_all ( "subdir/links" ) ;
871+ at. mkdir_all ( "subdir/deeper/deeper_dir" ) ;
872+ // Create files with specific sizes to test threshold
873+ at. write ( "subdir/links/bigfile.txt" , & "x" . repeat ( 10000 ) ) ; // ~10K file
874+ at. write ( "subdir/deeper/deeper_dir/smallfile.txt" , "small" ) ; // small file
835875
836876 let threshold = if cfg ! ( windows) { "7K" } else { "10K" } ;
837877
838878 ts. ucmd ( )
879+ . arg ( "--apparent-size" )
839880 . arg ( format ! ( "--threshold={threshold}" ) )
840881 . succeeds ( )
841882 . stdout_contains ( "links" )
842883 . stdout_does_not_contain ( "deeper_dir" ) ;
843884
844885 ts. ucmd ( )
886+ . arg ( "--apparent-size" )
845887 . arg ( format ! ( "--threshold=-{threshold}" ) )
846888 . succeeds ( )
847889 . stdout_does_not_contain ( "links" )
0 commit comments