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
|
package result
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
)
// On windows the pty package is failing to obtain stderr text when a test fails,
// so this is our workaround: when a test fails, it writes the result to a file,
// and then the test runner reads the file and checks the result
const PathEnvVar = "LAZYGIT_INTEGRATION_TEST_RESULT_PATH"
type IntegrationTestResult struct {
Success bool
Message string
}
func LogFailure(message string) error {
return writeResult(failure(message))
}
func LogSuccess() error {
return writeResult(success())
}
func failure(message string) IntegrationTestResult {
return IntegrationTestResult{Success: false, Message: message}
}
func success() IntegrationTestResult {
return IntegrationTestResult{Success: true}
}
func writeResult(result IntegrationTestResult) error {
resultPath := os.Getenv(PathEnvVar)
if resultPath == "" {
return fmt.Errorf("Environment variable %s not set", PathEnvVar)
}
file, err := os.Create(resultPath)
if err != nil {
return fmt.Errorf("Error creating file: %w", err)
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(result)
if err != nil {
return fmt.Errorf("Error encoding JSON: %w", err)
}
return nil
}
// Reads the result file stored by the lazygit test, and deletes the file
func ReadResult(resultPath string) (IntegrationTestResult, error) {
file, err := os.Open(resultPath)
if err != nil {
return IntegrationTestResult{}, fmt.Errorf("Error creating file: %w", err)
}
decoder := json.NewDecoder(file)
var result IntegrationTestResult
err = decoder.Decode(&result)
if err != nil {
file.Close()
return IntegrationTestResult{}, fmt.Errorf("Error decoding JSON: %w", err)
}
file.Close()
_ = os.Remove(resultPath)
return result, nil
}
func SetResultPathEnvVar(cmd *exec.Cmd, resultPath string) {
cmd.Env = append(
cmd.Env,
fmt.Sprintf("%s=%s", PathEnvVar, resultPath),
)
}
func GetResultPath() string {
return filepath.Join(os.TempDir(), fmt.Sprintf("lazygit_result_%s.json", generateRandomString(10)))
}
func generateRandomString(length int) string {
buffer := make([]byte, length)
_, err := rand.Read(buffer)
if err != nil {
panic(fmt.Sprintf("Could not generate random string: %s", err))
}
randomString := base64.URLEncoding.EncodeToString(buffer)
return randomString[:length]
}
|