feat: create scaffold project

This commit is contained in:
debugtalk
2022-01-08 22:09:08 +08:00
parent 288346bfd8
commit 1c8df8ecfd
7 changed files with 172 additions and 65 deletions

View File

@@ -1,17 +0,0 @@
package builtin
import (
"fmt"
"github.com/httprunner/hrp/internal/ga"
)
func CreateScaffold(projectName string) error {
// report event
ga.SendEvent(ga.EventTracking{
Category: "Scaffold",
Action: "hrp startproject",
})
return fmt.Errorf("not implemented")
}

72
internal/scaffold/demo.go Normal file
View File

@@ -0,0 +1,72 @@
package scaffold
import "github.com/httprunner/hrp"
var demoTestCase = &hrp.TestCase{
Config: hrp.NewConfig("demo with complex mechanisms").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{ // global level variables
"n": 5,
"a": 12.3,
"b": 3.45,
"varFoo1": "${gen_random_string($n)}",
"varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
}),
TestSteps: []hrp.IStep{
hrp.NewStep("transaction 1 start").StartTransaction("tran1"), // start transaction
hrp.NewStep("get with params").
WithVariables(map[string]interface{}{ // step level variables
"n": 3, // inherit config level variables if not set in step level, a/varFoo1
"b": 34.5, // override config level variable if existed, n/b/varFoo2
"varFoo2": "${max($a, $b)}", // 34.5; override variable b and eval again
}).
GET("/get").
WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}). // request with params
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). // request with headers
Extract().
WithJmesPath("body.args.foo1", "varFoo1"). // extract variable with jmespath
Validate().
AssertEqual("status_code", 200, "check response status code"). // validate response status code
AssertStartsWith("headers.\"Content-Type\"", "application/json", ""). // validate response header
AssertLengthEqual("body.args.foo1", 5, "check args foo1"). // validate response body with jmespath
AssertLengthEqual("$varFoo1", 5, "check args foo1"). // assert with extracted variable from current step
AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
hrp.NewStep("transaction 1 end").EndTransaction("tran1"), // end transaction
hrp.NewStep("post json data").
POST("/post").
WithBody(map[string]interface{}{
"foo1": "$varFoo1", // reference former extracted variable
"foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here
}).
Validate().
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.json.foo1", 5, "check args foo1").
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
hrp.NewStep("post form data").
POST("/post").
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
WithBody(map[string]interface{}{
"foo1": "$varFoo1", // reference former extracted variable
"foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here
}).
Validate().
AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.form.foo1", 5, "check args foo1").
AssertEqual("body.form.foo2", "12.3", "check args foo2"), // form data will be converted to string
},
}
// .gitignore
var demoIgnoreContent = `.env
reports/*
*.so
.vscode/
.idea/
.DS_Store
output/
`
// .env
var demoEnvContent = `USERNAME=debugtalk
"PASSWORD=123456
`

View File

@@ -0,0 +1,48 @@
package scaffold
import (
"fmt"
"testing"
"github.com/httprunner/hrp"
)
var (
demoTestCaseJSONPath = "../../examples/demo.json"
demoTestCaseYAMLPath = "../../examples/demo.yaml"
)
func TestGenDemoTestCase(t *testing.T) {
tCase, _ := demoTestCase.ToTCase()
err := tCase.Dump2JSON(demoTestCaseJSONPath)
if err != nil {
t.Fail()
}
err = tCase.Dump2YAML(demoTestCaseYAMLPath)
if err != nil {
t.Fail()
}
}
func Example_demo() {
err := hrp.NewRunner(nil).Run(demoTestCase) // hrp.Run(demoTestCase)
fmt.Println(err)
// Output:
// <nil>
}
func Example_jsonDemo() {
testCase := &hrp.TestCasePath{Path: demoTestCaseJSONPath}
err := hrp.NewRunner(nil).Run(testCase) // hrp.Run(testCase)
fmt.Println(err)
// Output:
// <nil>
}
func Example_yamlDemo() {
testCase := &hrp.TestCasePath{Path: demoTestCaseYAMLPath}
err := hrp.NewRunner(nil).Run(testCase) // hrp.Run(testCase)
fmt.Println(err)
// Output:
// <nil>
}

71
internal/scaffold/main.go Normal file
View File

@@ -0,0 +1,71 @@
package scaffold
import (
"fmt"
"os"
"path"
"github.com/httprunner/hrp/internal/ga"
"github.com/rs/zerolog/log"
)
func CreateScaffold(projectName string) error {
// report event
ga.SendEvent(ga.EventTracking{
Category: "Scaffold",
Action: "hrp startproject",
})
// check if projectName exists
if _, err := os.Stat(projectName); err == nil {
log.Warn().Str("projectName", projectName).
Msg("project name already exists, please specify a new one.")
return fmt.Errorf("project name already exists")
}
log.Info().Str("projectName", projectName).Msg("create new scaffold project")
// create project folder
createFolder(projectName)
createFolder(path.Join(projectName, "har"))
createFolder(path.Join(projectName, "testcases"))
createFolder(path.Join(projectName, "reports"))
// create demo testcases
tCase, _ := demoTestCase.ToTCase()
err := tCase.Dump2JSON(path.Join(projectName, "testcases", "demo.json"))
if err != nil {
log.Error().Err(err).Msg("create demo.json testcase failed")
return err
}
err = tCase.Dump2YAML(path.Join(projectName, "testcases", "demo.yaml"))
if err != nil {
log.Error().Err(err).Msg("create demo.yml testcase failed")
return err
}
createFile(path.Join(projectName, ".gitignore"), demoIgnoreContent)
createFile(path.Join(projectName, ".env"), demoEnvContent)
return nil
}
func createFolder(folderPath string) error {
log.Info().Str("folderPath", folderPath).Msg("create folder")
err := os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
log.Error().Err(err).Msg("create folder failed")
return err
}
return nil
}
func createFile(filePath string, data string) error {
log.Info().Str("filePath", filePath).Msg("create file")
err := os.WriteFile(filePath, []byte(data), 0o644)
if err != nil {
log.Error().Err(err).Msg("create file failed")
return err
}
return nil
}