github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/fs/utils_test.go (about) 1 // Package fs provides mountpath and FQN abstractions and methods to resolve/map stored content 2 /* 3 * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package fs_test 6 7 import ( 8 "fmt" 9 "os" 10 "testing" 11 12 "github.com/NVIDIA/aistore/fs" 13 "github.com/NVIDIA/aistore/tools" 14 "github.com/NVIDIA/aistore/tools/tassert" 15 ) 16 17 func TestIsDirEmpty(t *testing.T) { 18 tests := []tools.DirTreeDesc{ 19 {Dirs: 0, Depth: 1, Empty: true}, 20 {Dirs: 0, Depth: 1, Empty: false}, 21 22 {Dirs: 50, Depth: 1, Empty: true}, 23 {Dirs: 50, Depth: 1, Empty: false}, 24 25 {Dirs: 50, Depth: 8, Empty: true}, 26 {Dirs: 2000, Depth: 2, Empty: true}, 27 28 {Dirs: 3000, Depth: 2, Empty: false}, 29 } 30 31 for _, test := range tests { 32 testName := fmt.Sprintf("dirs=%d#depth=%d#empty=%t", test.Dirs, test.Depth, test.Empty) 33 t.Run(testName, func(t *testing.T) { 34 topDirName, _ := tools.PrepareDirTree(t, test) 35 defer os.RemoveAll(topDirName) 36 37 _, empty, err := fs.IsDirEmpty(topDirName) 38 tassert.CheckFatal(t, err) 39 tassert.Errorf( 40 t, empty == test.Empty, 41 "expected directory to be empty=%t, got: empty=%t", test.Empty, empty, 42 ) 43 }) 44 } 45 } 46 47 func TestIsDirEmptyNonExist(t *testing.T) { 48 _, _, err := fs.IsDirEmpty("/this/dir/does/not/exist") 49 tassert.Fatalf(t, err != nil, "expected error") 50 } 51 52 func BenchmarkIsDirEmpty(b *testing.B) { 53 benches := []tools.DirTreeDesc{ 54 {Dirs: 0, Depth: 1, Empty: true}, 55 {Dirs: 0, Depth: 1, Empty: false}, 56 57 {Dirs: 50, Depth: 1, Empty: true}, 58 {Dirs: 50, Depth: 1, Empty: false}, 59 {Dirs: 50, Depth: 8, Empty: true}, 60 {Dirs: 50, Depth: 8, Empty: false}, 61 62 {Dirs: 2000, Depth: 3, Empty: true}, 63 {Dirs: 2000, Depth: 3, Empty: false}, 64 65 {Dirs: 3000, Depth: 1, Empty: true}, 66 {Dirs: 3000, Depth: 1, Empty: false}, 67 {Dirs: 3000, Depth: 3, Empty: true}, 68 {Dirs: 3000, Depth: 3, Empty: false}, 69 } 70 71 for _, bench := range benches { 72 benchName := fmt.Sprintf("dirs=%d#depth=%d#empty=%t", bench.Dirs, bench.Depth, bench.Empty) 73 b.Run(benchName, func(b *testing.B) { 74 topDirName, _ := tools.PrepareDirTree(b, bench) 75 defer os.RemoveAll(topDirName) 76 77 b.ResetTimer() 78 for range b.N { 79 _, empty, err := fs.IsDirEmpty(topDirName) 80 tassert.CheckFatal(b, err) 81 tassert.Errorf( 82 b, empty == bench.Empty, 83 "expected directory to be empty=%t, got: empty=%t", bench.Empty, empty, 84 ) 85 } 86 }) 87 } 88 }