-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
historyConfig.go
168 lines (148 loc) · 4.1 KB
/
historyConfig.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package fs
import (
"errors"
"fmt"
"gopkg.in/yaml.v3"
"io"
"os"
"path/filepath"
)
type HistCfg struct {
TcPath string `json:"tc_path" yaml:"tc_path"`
MockPath string `json:"mock_path" yaml:"mock_path"`
AppPath string `json:"app_path" yaml:"app_path"`
TestRuns map[string][]string `json:"test_runs" yaml:"test_runs"`
}
func NewHistCfgFS() *HistCfg {
return &HistCfg{
TcPath: "",
MockPath: "",
AppPath: "",
TestRuns: map[string][]string{},
}
}
func (hc *HistCfg) CaptureTestsEvent(tc_path, mock_path, app_path, test_run_path, test_run_id string) error {
HistCfg := HistCfg{
TcPath: tc_path,
AppPath: app_path,
MockPath: mock_path,
TestRuns: map[string][]string{
test_run_path: {test_run_id},
},
}
err := SetHistory(&HistCfg)
if err != nil {
return err
}
return nil
}
// Todo : optimize this function
func (hc *HistCfg) CapturedRecordEvents(tc_path, mock_path, app_path string) error {
HistCfg := HistCfg{
TcPath: tc_path,
MockPath: mock_path,
AppPath: app_path,
}
err := SetHistory(&HistCfg)
if err != nil {
return err
}
return nil
}
func SetHistory(hc *HistCfg) error {
currentHistory := make(map[string][]HistCfg)
currentHistory["histCfg"] = append(currentHistory["histCfg"], *hc)
path := UserHomeDir(true)
fileName := "histCfg.yaml"
filePath := filepath.Join(path, fileName)
// Check if the file exists; if not, create it
if _, err := os.Stat(filePath); os.IsNotExist(err) {
_, err := CreateMockFile(path, "histCfg")
if err != nil {
return fmt.Errorf("failed to create file %s. error: %s", fileName, err.Error())
}
}
// Read the existing content of the file
exstingData, err := os.ReadFile(filePath)
if len(exstingData) == 0 {
Write(filePath, currentHistory)
return nil
}
if err != nil {
return fmt.Errorf("failed to read existing content from yaml file. error: %s", err.Error())
}
totalHist, err := ParseBytes(exstingData, currentHistory)
if err != nil {
return fmt.Errorf("failed to parse bytes. error: %s", err.Error())
}
Write(filePath, totalHist)
return nil
}
// UI can be rendered by fetching this method
func (hc *HistCfg) GetHistory() error {
var (
path = UserHomeDir(true)
history map[string][]HistCfg
)
file, err := os.OpenFile(filepath.Join(path, "histCfg.yaml"), os.O_RDONLY, os.ModePerm)
defer file.Close()
decoder := yaml.NewDecoder(file)
err = decoder.Decode(&history)
if errors.Is(err, io.EOF) {
return fmt.Errorf("failed to decode the HistCfg yaml. error: %v", err.Error())
}
return nil
}
func Write(filePath string, data map[string][]HistCfg) error {
d, err := yaml.Marshal(&data)
if err != nil {
return fmt.Errorf("failed to marshal document to yaml. error: %s", err.Error())
}
err = os.WriteFile(filePath, d, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to write histCfg in yaml file. Please check the Unix permissions error: %s", err.Error())
}
return nil
}
func ParseBytes(data []byte, hc map[string][]HistCfg) (map[string][]HistCfg, error) {
var exstingData map[string][]HistCfg
err := yaml.Unmarshal(data, &exstingData)
if err != nil {
return nil, fmt.Errorf("failed to read existing content from yaml file. error: %s", err.Error())
}
if err != nil {
return nil, fmt.Errorf("failed to Unmarshal document to yaml. error: %s", err.Error())
}
var prev = exstingData["histCfg"]
var current = hc["histCfg"][0]
var flag = false
for i, v := range prev {
if v.TcPath == current.TcPath && v.MockPath == current.MockPath {
// iterate over all testrun path
f := false
for j := range prev[i].TestRuns {
if _, ok := current.TestRuns[j]; ok {
prev[i].TestRuns[j] = append(current.TestRuns[j], v.TestRuns[j]...)
f = true
}
}
// test run path is new and not available in history
if !f {
for k, v := range current.TestRuns {
prev[i].TestRuns[k] = v
}
}
//for appending after record for the first time
if len(prev[i].TestRuns) == 0 {
prev[i].TestRuns = current.TestRuns
}
flag = true
break
}
}
if !flag {
prev = append(prev, current)
}
exstingData["histCfg"] = prev
return exstingData, nil
}