github.com/bitrise-io/go-steputils/v2@v2.0.0-alpha.30/cache/compression/compression_test.go (about) 1 package compression 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 func TestAreAllPathsEmpty(t *testing.T) { 11 // Set up test dir structure 12 basePath := t.TempDir() 13 err := os.MkdirAll(filepath.Join(basePath, "empty_dir"), 0700) 14 if err != nil { 15 t.Fatalf(err.Error()) 16 } 17 err = os.MkdirAll(filepath.Join(basePath, "dir_with_dir_child", "nested_empty_dir"), 0700) 18 if err != nil { 19 t.Fatalf(err.Error()) 20 } 21 err = os.MkdirAll(filepath.Join(basePath, "first_level", "second_level"), 0700) 22 if err != nil { 23 t.Fatalf(err.Error()) 24 } 25 err = ioutil.WriteFile(filepath.Join(basePath, "first_level", "second_level", "nested_file.txt"), []byte("hello"), 0700) 26 if err != nil { 27 t.Fatalf(err.Error()) 28 } 29 30 tests := []struct { 31 name string 32 includePaths []string 33 want bool 34 }{ 35 { 36 name: "single empty dir", 37 includePaths: []string{ 38 filepath.Join(basePath, "empty_dir"), 39 }, 40 want: true, 41 }, 42 { 43 name: "dir with files", 44 includePaths: []string{ 45 filepath.Join(basePath, "first_level", "second_level", "nested_file.txt"), 46 }, 47 want: false, 48 }, 49 { 50 name: "empty dir within dir", 51 includePaths: []string{ 52 filepath.Join(basePath, "dir_with_dir_child"), 53 }, 54 want: false, 55 }, 56 { 57 name: "empty and non-empty dirs", 58 includePaths: []string{ 59 filepath.Join(basePath, "empty_dir"), 60 filepath.Join(basePath, "first_level"), 61 }, 62 want: false, 63 }, 64 { 65 name: "nonexistent dir", 66 includePaths: []string{ 67 filepath.Join(basePath, "this doesn't exist"), 68 }, 69 want: true, 70 }, 71 { 72 name: "file path", 73 includePaths: []string{ 74 filepath.Join(basePath, "this doesn't exist"), 75 filepath.Join(basePath, "empty_dir"), 76 filepath.Join(basePath, "first_level", "second_level", "nested_file.txt"), 77 }, 78 want: false, 79 }, 80 } 81 for _, tt := range tests { 82 t.Run(tt.name, func(t *testing.T) { 83 if got := AreAllPathsEmpty(tt.includePaths); got != tt.want { 84 t.Errorf("areAllPathsEmpty() = %v, want %v", got, tt.want) 85 } 86 }) 87 } 88 }