-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_functions.go
More file actions
147 lines (131 loc) · 3.26 KB
/
test_functions.go
File metadata and controls
147 lines (131 loc) · 3.26 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
)
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorWhite = "\033[37m"
)
func coloredOutput(color, text string) string {
return color + text + colorReset
}
func writeToFile(fname, contents string) error {
f, err := os.Create(fname)
if err != nil {
return err
}
_, err = fmt.Fprint(f, contents)
if err != nil {
_ = f.Close()
return err
}
err = f.Close()
return err
}
func appendToFile(fname, content string) error {
// Open the file in append mode. Create the file if it doesn't exist.
f, err := os.OpenFile(fname, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
// Write the content to the file.
if _, err := f.WriteString(content); err != nil {
return err
}
return nil
}
func checkBinaries() error {
binaries := []string{"p4", "p4d", "p4broker"}
for _, bin := range binaries {
path, err := exec.LookPath(bin)
if err != nil {
return fmt.Errorf(colorRed+"Required executable '%s' not found in PATH"+colorReset, bin)
}
fmt.Printf(colorGreen+"Found: %s -> %s\n"+colorReset, bin, path)
}
return nil
}
func (p4t *P4Test) ensureDirectories() {
for _, d := range []string{p4t.serverRoot, p4t.brokerRoot, p4t.clientRoot} {
err := os.MkdirAll(d, 0777)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create %s: %v", d, err)
}
}
}
func getFunctionName() string {
pc, _, _, ok := runtime.Caller(1)
if !ok {
return "unknown"
}
fn := runtime.FuncForPC(pc)
fullFuncName := fn.Name()
// Split the full function name by the dot and return the last part
parts := strings.Split(fullFuncName, ".")
if len(parts) > 0 {
return parts[len(parts)-1] // Last part is the actual function name
}
return "unknown"
}
type P4ConfigType string
const (
DefaultP4Config P4ConfigType = "default"
BrokerP4Config P4ConfigType = "broker"
CustomP4Config P4ConfigType = "custom"
)
// Function to set different Perforce configurations
func setP4Config(configType P4ConfigType, p4Test *P4Test) {
switch configType {
case DefaultP4Config:
//os.Setenv("PATH", newPath)
os.Setenv("P4CONFIG", ".p4config")
os.Setenv("P4ROOT", p4Test.serverRoot)
os.Setenv("P4LOG", p4Test.serverLog)
os.Setenv("P4USER", "perforce")
os.Setenv("P4PORT", p4Test.port)
os.Setenv("P4PASSWD", p4Test.p4Passwd)
os.Setenv("P4TICKETS", p4Test.serverRoot)
os.Setenv("P4TRUST", p4Test.serverRoot)
// ... other default settings ...
case BrokerP4Config:
os.Setenv("P4PORT", p4Test.bport)
// ... settings specific to broker configuration ...
case CustomP4Config:
os.Setenv("P4USER", "noodles")
os.Setenv("P4PASSWD", "noodles")
// ... custom settings ...
}
// Note: Add more cases as needed for different configurations
}
func logEnvironmentVariables(title string) {
if BrokerDebug {
fmt.Printf("\n--- %s ---\n", title)
for _, env := range os.Environ() {
fmt.Println(env)
}
}
}
func isDirectoryEmpty(dir string) (bool, error) {
f, err := os.Open(dir)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1) // Or Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err
}