feat: create scaffold project

This commit is contained in:
debugtalk
2022-01-08 22:09:08 +08:00
parent 2ec2b86c05
commit c6549f4012
7 changed files with 172 additions and 65 deletions

34
.github/workflows/scaffold.yml vendored Normal file
View File

@@ -0,0 +1,34 @@
name: Run scaffold
on:
pull_request:
types: [synchronize]
jobs:
scaffold:
strategy:
fail-fast: true
matrix:
go-version:
- 1.13.x
- 1.14.x
- 1.15.x
- 1.16.x
- 1.17.x
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Build hrp binary
run: make build
- name: Check hrp version
run: ./output/hrp -v
- name: Run start project
run: ./output/hrp startproject demo
- name: Run demo tests
run: ./output/hrp run demo/testcases/demo.json demo/testcases/demo.yaml

View File

@@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra"
"github.com/httprunner/hrp/internal/builtin"
"github.com/httprunner/hrp/internal/scaffold"
)
var scaffoldCmd = &cobra.Command{
@@ -16,7 +16,7 @@ var scaffoldCmd = &cobra.Command{
setLogLevel(logLevel)
},
Run: func(cmd *cobra.Command, args []string) {
err := builtin.CreateScaffold(args[0])
err := scaffold.CreateScaffold(args[0])
if err != nil {
os.Exit(1)
}

View File

@@ -1,9 +1,10 @@
# Release History
## v0.5.0 (2022-01-06)
## v0.5.0 (2022-01-08)
- feat: support creating and calling custom functions with [go plugin](https://pkg.go.dev/plugin)
- feat: install hrp with one shell command
- feat: add `startproject` sub-command for creating scaffold project
- feat: report GA event for loading go plugin
## v0.4.0 (2022-01-05)

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")
}

View File

@@ -1,11 +1,6 @@
package examples
package scaffold
import (
"fmt"
"testing"
"github.com/httprunner/hrp"
)
import "github.com/httprunner/hrp"
var demoTestCase = &hrp.TestCase{
Config: hrp.NewConfig("demo with complex mechanisms").
@@ -61,42 +56,17 @@ var demoTestCase = &hrp.TestCase{
},
}
var (
demoTestCaseJSONPath = "demo.json"
demoTestCaseYAMLPath = "demo.yaml"
)
// .gitignore
var demoIgnoreContent = `.env
reports/*
*.so
.vscode/
.idea/
.DS_Store
output/
`
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>
}
// .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
}