github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/testhelpers/osutil/file_helpers.go (about)

     1  package osutil
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  
     9  	"github.com/ActiveState/cli/internal/fileutils"
    10  )
    11  
    12  // CreateConfigFile will create a file in the config dir with the given file name.
    13  func CreateConfigFile(configPath string, fileName string, fileMode os.FileMode) (*os.File, error) {
    14  	filename := filepath.Join(configPath, fileName)
    15  	return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode)
    16  }
    17  
    18  // ReadConfigFile will read the contents of a file in the config dir.
    19  func ReadConfigFile(configPath, fileName string) (string, error) {
    20  	contents, err := os.ReadFile(filepath.Join(configPath, fileName))
    21  	return string(contents), err
    22  }
    23  
    24  // RemoveConfigFile will remove a file from the config dir with the given file name.
    25  func RemoveConfigFile(configPath, fileName string) error {
    26  	return os.Remove(filepath.Join(configPath, fileName))
    27  }
    28  
    29  // StatConfigFile returns the os.FileInfo for a file in the config dir.
    30  func StatConfigFile(configPath, fileName string) (os.FileInfo, error) {
    31  	return os.Stat(filepath.Join(configPath, fileName))
    32  }
    33  
    34  // GetTestDataDir returns the path to the caller's `testdata` directory.
    35  func GetTestDataDir() string {
    36  	callerPath := getCallerPath()
    37  	return filepath.Join(callerPath, "testdata")
    38  }
    39  
    40  // GetTestFile returns the path to the given fileName in the calling function's `testdata` directory.
    41  func GetTestFile(fileName ...string) string {
    42  	return filepath.Join(GetTestDataDir(), filepath.Join(fileName...))
    43  }
    44  
    45  // ReadTestFile will read the contents of a file from the `testdata` directory relative to the
    46  // path of the calling function file. This function assumes it is called directly from a function
    47  // in a file in the directory the `testdata` exists in.
    48  func ReadTestFile(fileName string) (string, error) {
    49  	contents, err := os.ReadFile(GetTestFile(fileName))
    50  	return string(contents), err
    51  }
    52  
    53  // CopyTestFileToConfigDir copies a file in a relatve `testdata` dir to the caller of this function
    54  // to the config dir as some target filename with some target FileMode.
    55  func CopyTestFileToConfigDir(configPath, testFileName, targetFileName string, targetFileMode os.FileMode) error {
    56  	testFileContent, err := ReadTestFile(testFileName)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	return os.WriteFile(filepath.Join(configPath, targetFileName), []byte(testFileContent), targetFileMode)
    61  }
    62  
    63  // getCallerPath returns the filesystem path of the caller to this func so long as it's not
    64  // in this file's directory.
    65  func getCallerPath() string {
    66  	_, currentFile, _, ok := runtime.Caller(0)
    67  	file := currentFile
    68  	skip := 1 // skip position
    69  
    70  	// find the file of the previous caller that is not in this file
    71  	for file == currentFile && ok {
    72  		_, file, _, ok = runtime.Caller(skip)
    73  		skip++
    74  	}
    75  
    76  	if file == "" || file == currentFile {
    77  		panic("Could not get caller")
    78  	}
    79  
    80  	return filepath.Dir(file)
    81  }
    82  
    83  // PrepareDir prepares a path for use in tests (ensures it exists and ensures the path is concistent)
    84  func PrepareDir(path string) string {
    85  	if path == "" {
    86  		return path
    87  	}
    88  
    89  	var err error
    90  	path, err = fileutils.PrepareDir(path)
    91  	if err != nil {
    92  		panic(fmt.Sprintf("PrepareDir error: %v", err))
    93  	}
    94  
    95  	return path
    96  }