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