-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_helpers_test.go
113 lines (95 loc) · 2.77 KB
/
t_helpers_test.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
)
func wrapSnapTestError(test snapTest, errMsg string) string {
return wrapTestError(test.description, test.cmdOpts, errMsg)
}
func wrapUploadTestError(test uploadTest, errMsg string) string {
return wrapTestError(test.description, test.cmdOpts, errMsg)
}
func wrapTestError(description string, cmdOpts interface{}, errMsg string) string {
msg := fmt.Sprintf("(%s) [%+v] %s", description, cmdOpts, errMsg)
logrus.Warnf(msg)
return msg
}
func cleanupTestDir(testDir string) (err error) {
// remove test dir
logrus.Infof("Cleaning up")
err = os.RemoveAll(testDir)
if err != nil {
err = fmt.Errorf("could not remove test dir: %s", err)
return
}
logrus.Infof("Test Dir removed")
return
}
// ensureTestDir ensures that a test directory exists
// relativeDirName is relative the directory in which the go file exists
func ensureTestDir(relativeDirName string) (pwd string, dir string, err error) {
// get the working directory
pwd, err = os.Getwd()
if err != nil {
err = fmt.Errorf("could not get pwd: %s", pwd)
return
}
logrus.Infof("PWD %s", pwd)
// what is the directory?
dir = fmt.Sprintf("%s/%s", pwd, relativeDirName)
logrus.Infof("TMP DIR %s", dir)
// remove the directory if it exists
err = cleanupTestDir(dir)
if err != nil {
err = fmt.Errorf("could not create test temp dir (%s): %s", dir, err)
return
}
// ensure the temp directory exists
err = os.MkdirAll(dir, 0700)
// err = os.MkdirAll(dir, testRootCmdOpts.FileCreateMode)
if err != nil {
err = fmt.Errorf("could not create test temp dir (%s): %s", dir, err)
return
}
logrus.Infof("TMP DIR Created")
return
}
func copyFile(existingRelativePath, newRelativePath string) (bytesCopied int64, err error) {
// get the absolute path of existing file
existingAbsPath, err := filepath.Abs(existingRelativePath)
if err != nil {
err = fmt.Errorf("could not get absoltue path for existing file: %s", existingRelativePath)
return
}
// get the absolute path of new file
newAbsPath, err := filepath.Abs(newRelativePath)
if err != nil {
err = fmt.Errorf("could not get absoltue path for new file: %s", newRelativePath)
return
}
// open the existing file to get its data
existingFile, err := os.Open(existingAbsPath)
if err != nil {
err = fmt.Errorf("could not open existing file: %s", existingAbsPath)
return
}
defer existingFile.Close()
// Create new file
newFile, err := os.Create(newAbsPath)
if err != nil {
err = fmt.Errorf("could not create new file: %s", newAbsPath)
return
}
defer newFile.Close()
// copy the data to the new file
bytesCopied, err = io.Copy(newFile, existingFile)
if err != nil {
err = fmt.Errorf("could not copy data new file: %s", newAbsPath)
return
}
// done
return
}