Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to name test cases #903

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/hooks/connection/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func capture(db platform.TestCaseDB, req *http.Request, resp *http.Response, log
// err = db.Insert(httpMock, getDeps())
err = db.WriteTestcase(&models.TestCase{
Version: models.V1Beta2,
Name: "",
Name: pkg.ToYamlHttpHeader(req.Header)["Keploy-Test-Name"],
Kind: models.HTTP,
Created: time.Now().Unix(),
HttpReq: models.HttpReq{
Expand Down
30 changes: 22 additions & 8 deletions pkg/platform/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@
// createYamlFile is used to create the yaml file along with the path directory (if does not exists)
func createYamlFile(path string, fileName string, Logger *zap.Logger) (bool, error) {
// checks id the yaml exists
if _, err := os.Stat(filepath.Join(path, fileName+".yaml")); err != nil {
yamlPath, err := ValidatePath(filepath.Join(path, fileName + ".yaml"))
if err != nil {
return false, err
}
if _, err := os.Stat(yamlPath); err != nil {
// creates the path director if does not exists
err = os.MkdirAll(filepath.Join(path), fs.ModePerm)
if err != nil {
Logger.Error("failed to create a directory for the yaml file", zap.Error(err), zap.Any("path directory", path), zap.Any("yaml", fileName))
return false, err
}

// create the yaml file
_, err := os.Create(filepath.Join(path, fileName+".yaml"))
_, err := os.Create(yamlPath)
if err != nil {
Logger.Error("failed to create a yaml file", zap.Error(err), zap.Any("path directory", path), zap.Any("yaml", fileName))
return false, err
}

Expand Down Expand Up @@ -122,9 +126,15 @@
if err != nil {
return err
}
file, err := os.OpenFile(filepath.Join(path, fileName+".yaml"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)

yamlPath, err := ValidatePath(filepath.Join(path, fileName+".yaml"))
if err != nil {
return err
}

file, err := os.OpenFile(yamlPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
ys.Logger.Error("failed to open the created yaml file", zap.Error(err), zap.Any("yaml file name", fileName))
ys.Logger.Error("failed to open the created yaml file", zap.Error(err), zap.Any("yaml file name", fileName))
Fixed Show fixed Hide fixed
return err
}

Expand All @@ -134,14 +144,14 @@
}
d, err := yamlLib.Marshal(&doc)
if err != nil {
ys.Logger.Error("failed to marshal the recorded calls into yaml", zap.Error(err), zap.Any("yaml file name", fileName))
ys.Logger.Error("failed to marshal the recorded calls into yaml", zap.Error(err), zap.Any("yaml file name", fileName))
return err
}
data = append(data, d...)

_, err = file.Write(data)
if err != nil {
ys.Logger.Error("failed to write the yaml document", zap.Error(err), zap.Any("yaml file name", fileName))
ys.Logger.Error("failed to write the yaml document", zap.Error(err), zap.Any("yaml file name", fileName))
Fixed Show fixed Hide fixed
return err
}
defer file.Close()
Expand All @@ -159,7 +169,11 @@
if err != nil {
return err
}
tcsName = fmt.Sprintf("test-%v", lastIndx)
if tc.Name == "" {
tcsName = fmt.Sprintf("test-%v", lastIndx)
} else {
tcsName = tc.Name
}
} else {
tcsName = ys.TcsName
}
Expand All @@ -178,7 +192,7 @@
ys.Logger.Error("failed to write testcase yaml file", zap.Error(err))
return err
}
ys.Logger.Info("🟠 Keploy has captured test cases for the user's application.", zap.String("path", ys.TcsPath), zap.String("testcase name", tcsName))

// write the mock yamls
// mockName := fmt.Sprintf("mock-%v", lastIndx)
Expand Down Expand Up @@ -374,4 +388,4 @@

return configMocks, tcsMocks, nil

}
}
Loading