github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/testhelpers/e2e/dirs.go (about) 1 package e2e 2 3 import ( 4 "os" 5 "path/filepath" 6 ) 7 8 // Dirs represents directories that are temporarily created for this end-to-end testing session 9 type Dirs struct { 10 Base string 11 // Config is where configuration files are stored 12 Config string 13 // Cache is the directory where cached files including downloaded artifacts are stored 14 Cache string 15 // Bin is the directory where executables are stored 16 Bin string 17 // Work is the working directory where the activestate.yaml file would live, and that is the PWD for tested console processes 18 Work string 19 // DefaultBin is the bin directory for our default installation 20 DefaultBin string 21 // SockRoot is the directory for the state service's socket file 22 SockRoot string 23 // HomeDir is used as the test user's home directory 24 HomeDir string 25 // TempDir is the directory where temporary files are stored 26 TempDir string 27 } 28 29 // NewDirs creates all temporary directories 30 func NewDirs(base string) (*Dirs, error) { 31 if base == "" { 32 tmpDir, err := os.MkdirTemp("", "") 33 if err != nil { 34 return nil, err 35 } 36 base = tmpDir 37 } 38 39 cache := filepath.Join(base, "cache") 40 config := filepath.Join(base, "config") 41 bin := filepath.Join(base, "bin") 42 work := filepath.Join(base, "work") 43 defaultBin := filepath.Join(base, "cache", "bin") 44 sockRoot := filepath.Join(base, "sock") 45 homeDir := filepath.Join(base, "home") 46 tempDir := filepath.Join(base, "temp") 47 48 subdirs := []string{config, cache, bin, work, defaultBin, sockRoot, homeDir, tempDir} 49 for _, subdir := range subdirs { 50 if err := os.MkdirAll(subdir, 0700); err != nil { 51 return nil, err 52 } 53 } 54 55 dirs := Dirs{ 56 Base: base, 57 Config: config, 58 Cache: cache, 59 Bin: bin, 60 Work: work, 61 DefaultBin: defaultBin, 62 SockRoot: sockRoot, 63 HomeDir: homeDir, 64 TempDir: tempDir, 65 } 66 67 return &dirs, nil 68 } 69 70 // Close removes the temporary directories 71 func (d *Dirs) Close() error { 72 subdirs := []string{d.Bin, d.Config, d.Work, d.Cache} 73 for _, subdir := range subdirs { 74 if err := os.RemoveAll(subdir); err != nil { 75 return err 76 } 77 } 78 return nil 79 }