From e596bddda74facbd1a660f3417cd336484dbce15 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 11:42:39 +0800 Subject: [PATCH 01/10] feat: init scaffold --- hrp/cmd/har2case.go | 2 +- hrp/cmd/run.go | 2 +- hrp/cmd/scaffold.go | 28 ++++++++++++++++++++++++++++ internal/builtin/scaffold.go | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 hrp/cmd/scaffold.go create mode 100644 internal/builtin/scaffold.go diff --git a/hrp/cmd/har2case.go b/hrp/cmd/har2case.go index 7512f241..4269dbee 100644 --- a/hrp/cmd/har2case.go +++ b/hrp/cmd/har2case.go @@ -9,7 +9,7 @@ import ( // har2caseCmd represents the har2case command var har2caseCmd = &cobra.Command{ - Use: "har2case harPath...", + Use: "har2case $har_path...", Short: "Convert HAR to json/yaml testcase files", Long: `Convert HAR to json/yaml testcase files`, Args: cobra.MinimumNArgs(1), diff --git a/hrp/cmd/run.go b/hrp/cmd/run.go index 29aa2486..274c7424 100644 --- a/hrp/cmd/run.go +++ b/hrp/cmd/run.go @@ -10,7 +10,7 @@ import ( // runCmd represents the run command var runCmd = &cobra.Command{ - Use: "run path...", + Use: "run $path...", Short: "run API test", Long: `run yaml/json testcase files for API test`, Example: ` $ hrp run demo.json # run specified json testcase file diff --git a/hrp/cmd/scaffold.go b/hrp/cmd/scaffold.go new file mode 100644 index 00000000..2e2fed45 --- /dev/null +++ b/hrp/cmd/scaffold.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" + + "github.com/httprunner/hrp/internal/builtin" +) + +var scaffoldCmd = &cobra.Command{ + Use: "startproject $project_name", + Short: "Create a scaffold project", + Args: cobra.ExactValidArgs(1), + PreRun: func(cmd *cobra.Command, args []string) { + setLogLevel(logLevel) + }, + Run: func(cmd *cobra.Command, args []string) { + err := builtin.CreateScaffold(args[0]) + if err != nil { + os.Exit(1) + } + }, +} + +func init() { + RootCmd.AddCommand(scaffoldCmd) +} diff --git a/internal/builtin/scaffold.go b/internal/builtin/scaffold.go new file mode 100644 index 00000000..0a57f2aa --- /dev/null +++ b/internal/builtin/scaffold.go @@ -0,0 +1,17 @@ +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") +} From 2598691c69a4fa775b8bc1c05639a1637f53a712 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 12:35:54 +0800 Subject: [PATCH 02/10] fix: report GA event with value to indicate failure --- internal/ga/client_test.go | 4 ++-- internal/ga/events.go | 2 +- plugin.go | 14 ++++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/internal/ga/client_test.go b/internal/ga/client_test.go index a1e1cae4..d1c29a72 100644 --- a/internal/ga/client_test.go +++ b/internal/ga/client_test.go @@ -8,7 +8,7 @@ func TestSendEvents(t *testing.T) { event := EventTracking{ Category: "unittest", Action: "SendEvents", - Value: "123", + Value: 123, } err := gaClient.SendEvent(event) if err != nil { @@ -21,7 +21,7 @@ func TestStructToUrlValues(t *testing.T) { Category: "unittest", Action: "convert", Label: "v0.3.0", - Value: "123", + Value: 123, } val := structToUrlValues(event) if val.Encode() != "ea=convert&ec=unittest&el=v0.3.0&ev=123" { diff --git a/internal/ga/events.go b/internal/ga/events.go index 2044d196..99e91179 100644 --- a/internal/ga/events.go +++ b/internal/ga/events.go @@ -17,7 +17,7 @@ type EventTracking struct { Category string `form:"ec"` // Required. Event Category. Action string `form:"ea"` // Required. Event Action. Label string `form:"el"` // Optional. Event label, used as version. - Value string `form:"ev"` // Optional. Event value, must be digits, "123" + Value int `form:"ev"` // Optional. Event value, must be non-negative integer } func (e EventTracking) StartTiming(variable string) UserTimingTracking { diff --git a/plugin.go b/plugin.go index 6a5cf1f7..7748e207 100644 --- a/plugin.go +++ b/plugin.go @@ -35,10 +35,16 @@ func (p *parser) loadPlugin(path string) error { } // report event for loading go plugin - go ga.SendEvent(ga.EventTracking{ - Category: "LoadGoPlugin", - Action: "plugin.Open", - }) + defer func() { + event := ga.EventTracking{ + Category: "LoadGoPlugin", + Action: "plugin.Open", + } + if err != nil { + event.Value = 1 // failed + } + go ga.SendEvent(event) + }() // load plugin plugins, err := plugin.Open(pluginPath) From 2ec2b86c05c026dd0ceb2547462db93adc0f8a76 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 15:10:37 +0800 Subject: [PATCH 03/10] refactor: commands --- .github/workflows/release.yml | 2 +- Makefile | 2 +- README.md | 4 ++-- {hrp => cli}/README.md | 0 {hrp => cli/hrp}/cmd/boom.go | 2 +- {docs => cli/hrp}/cmd/doc_test.go | 6 ++---- {hrp => cli/hrp}/cmd/har2case.go | 2 +- {hrp => cli/hrp}/cmd/root.go | 10 +++++----- {hrp => cli/hrp}/cmd/run.go | 2 +- {hrp => cli/hrp}/cmd/scaffold.go | 2 +- {hrp => cli/hrp}/main.go | 2 +- {hrp => cli}/scripts/build.sh | 4 ++-- {hrp => cli}/scripts/install.sh | 2 +- docs/cmd/hrp.md | 3 ++- docs/cmd/hrp_boom.md | 2 +- docs/cmd/hrp_har2case.md | 4 ++-- docs/cmd/hrp_run.md | 4 ++-- docs/cmd/hrp_startproject.md | 19 +++++++++++++++++++ 18 files changed, 45 insertions(+), 27 deletions(-) rename {hrp => cli}/README.md (100%) rename {hrp => cli/hrp}/cmd/boom.go (98%) rename {docs => cli/hrp}/cmd/doc_test.go (53%) rename {hrp => cli/hrp}/cmd/har2case.go (97%) rename {hrp => cli/hrp}/cmd/root.go (91%) rename {hrp => cli/hrp}/cmd/run.go (98%) rename {hrp => cli/hrp}/cmd/scaffold.go (93%) rename {hrp => cli/hrp}/main.go (81%) rename {hrp => cli}/scripts/build.sh (78%) rename {hrp => cli}/scripts/install.sh (98%) create mode 100644 docs/cmd/hrp_startproject.md diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f4f12fde..297b913c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,7 +27,7 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} goos: ${{ matrix.goos }} goarch: ${{ matrix.goarch }} - project_path: "./hrp" # go build ./hrp/main.go + project_path: "./cli/hrp" # go build ./cli/hrp/main.go binary_name: "hrp" ldflags: "-s -w" extra_files: LICENSE README.md docs/CHANGELOG.md diff --git a/Makefile b/Makefile index f9133fbb..b75eb2cc 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ test: ## run unit tests .PHONY: build build: ## build hrp cli tool @echo "[info] build hrp cli tool" - @. hrp/scripts/build.sh + @. cli/scripts/build.sh .PHONY: help help: ## print make commands diff --git a/README.md b/README.md index 4ec957ac..e1daa8a5 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,13 @@ See [CHANGELOG]. You can install `hrp` with one shell command, which will download the latest version's released binary and install to the current system. ```bash -$ curl -sL https://raw.githubusercontent.com/httprunner/hrp/main/hrp/scripts/install.sh | bash +$ curl -sL https://raw.githubusercontent.com/httprunner/hrp/main/cli/scripts/install.sh | bash ``` If you are a golang developer, you can also install `hrp` with `go get`. ```bash -$ go get -u github.com/httprunner/hrp/hrp +$ go get -u github.com/httprunner/hrp/cli/hrp ``` Since installed, you will get a `hrp` command with multiple sub-commands. diff --git a/hrp/README.md b/cli/README.md similarity index 100% rename from hrp/README.md rename to cli/README.md diff --git a/hrp/cmd/boom.go b/cli/hrp/cmd/boom.go similarity index 98% rename from hrp/cmd/boom.go rename to cli/hrp/cmd/boom.go index b19c239a..35a7d6a9 100644 --- a/hrp/cmd/boom.go +++ b/cli/hrp/cmd/boom.go @@ -55,7 +55,7 @@ var ( ) func init() { - RootCmd.AddCommand(boomCmd) + rootCmd.AddCommand(boomCmd) boomCmd.Flags().Int64Var(&maxRPS, "max-rps", 0, "Max RPS that boomer can generate, disabled by default.") boomCmd.Flags().StringVar(&requestIncreaseRate, "request-increase-rate", "-1", "Request increase rate, disabled by default.") diff --git a/docs/cmd/doc_test.go b/cli/hrp/cmd/doc_test.go similarity index 53% rename from docs/cmd/doc_test.go rename to cli/hrp/cmd/doc_test.go index 2ab82020..0f254e29 100644 --- a/docs/cmd/doc_test.go +++ b/cli/hrp/cmd/doc_test.go @@ -4,13 +4,11 @@ import ( "testing" "github.com/spf13/cobra/doc" - - "github.com/httprunner/hrp/hrp/cmd" ) -// run this test to generate markdown docs +// run this test to generate markdown docs for hrp command func TestGenMarkdownTree(t *testing.T) { - err := doc.GenMarkdownTree(cmd.RootCmd, "./") + err := doc.GenMarkdownTree(rootCmd, "../../../docs/cmd") if err != nil { t.Fatal(err) } diff --git a/hrp/cmd/har2case.go b/cli/hrp/cmd/har2case.go similarity index 97% rename from hrp/cmd/har2case.go rename to cli/hrp/cmd/har2case.go index 4269dbee..4e08ea41 100644 --- a/hrp/cmd/har2case.go +++ b/cli/hrp/cmd/har2case.go @@ -52,7 +52,7 @@ var ( ) func init() { - RootCmd.AddCommand(har2caseCmd) + rootCmd.AddCommand(har2caseCmd) har2caseCmd.Flags().BoolVarP(&genJSONFlag, "to-json", "j", false, "convert to JSON format (default)") har2caseCmd.Flags().BoolVarP(&genYAMLFlag, "to-yaml", "y", false, "convert to JSON format") har2caseCmd.Flags().StringVarP(&outputDir, "output-dir", "d", "", "specify output directory, default to the same dir with har file") diff --git a/hrp/cmd/root.go b/cli/hrp/cmd/root.go similarity index 91% rename from hrp/cmd/root.go rename to cli/hrp/cmd/root.go index 0b60831a..916e6db8 100644 --- a/hrp/cmd/root.go +++ b/cli/hrp/cmd/root.go @@ -11,8 +11,8 @@ import ( "github.com/httprunner/hrp/internal/version" ) -// RootCmd represents the base command when called without any subcommands -var RootCmd = &cobra.Command{ +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ Use: "hrp", Short: "One-stop solution for HTTP(S) testing.", Long: ` @@ -47,10 +47,10 @@ var ( // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - RootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level") - RootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format") + rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level") + rootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format") - if err := RootCmd.Execute(); err != nil { + if err := rootCmd.Execute(); err != nil { os.Exit(1) } } diff --git a/hrp/cmd/run.go b/cli/hrp/cmd/run.go similarity index 98% rename from hrp/cmd/run.go rename to cli/hrp/cmd/run.go index 274c7424..23d3de9f 100644 --- a/hrp/cmd/run.go +++ b/cli/hrp/cmd/run.go @@ -45,7 +45,7 @@ var ( ) func init() { - RootCmd.AddCommand(runCmd) + rootCmd.AddCommand(runCmd) runCmd.Flags().BoolVar(&continueOnFailure, "continue-on-failure", false, "continue running next step when failure occurs") runCmd.Flags().BoolVarP(&silentFlag, "silent", "s", false, "disable logging request & response details") runCmd.Flags().StringVarP(&proxyUrl, "proxy-url", "p", "", "set proxy url") diff --git a/hrp/cmd/scaffold.go b/cli/hrp/cmd/scaffold.go similarity index 93% rename from hrp/cmd/scaffold.go rename to cli/hrp/cmd/scaffold.go index 2e2fed45..d70db7eb 100644 --- a/hrp/cmd/scaffold.go +++ b/cli/hrp/cmd/scaffold.go @@ -24,5 +24,5 @@ var scaffoldCmd = &cobra.Command{ } func init() { - RootCmd.AddCommand(scaffoldCmd) + rootCmd.AddCommand(scaffoldCmd) } diff --git a/hrp/main.go b/cli/hrp/main.go similarity index 81% rename from hrp/main.go rename to cli/hrp/main.go index c3c613f1..b39dc828 100644 --- a/hrp/main.go +++ b/cli/hrp/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/httprunner/hrp/hrp/cmd" + "github.com/httprunner/hrp/cli/hrp/cmd" "github.com/httprunner/hrp/internal/sentry" ) diff --git a/hrp/scripts/build.sh b/cli/scripts/build.sh similarity index 78% rename from hrp/scripts/build.sh rename to cli/scripts/build.sh index 60f0f41a..37678976 100644 --- a/hrp/scripts/build.sh +++ b/cli/scripts/build.sh @@ -5,7 +5,7 @@ # Usage: # $ make build # or -# $ bash hrp/scripts/build.sh +# $ bash cli/scripts/build.sh set -e set -x @@ -15,7 +15,7 @@ mkdir -p "output" bin_path="output/hrp" # build -go build -ldflags '-s -w' -o "$bin_path" hrp/main.go +go build -ldflags '-s -w' -o "$bin_path" cli/hrp/main.go # check output and version ls -lh "$bin_path" diff --git a/hrp/scripts/install.sh b/cli/scripts/install.sh similarity index 98% rename from hrp/scripts/install.sh rename to cli/scripts/install.sh index 038e0493..b76f0adc 100644 --- a/hrp/scripts/install.sh +++ b/cli/scripts/install.sh @@ -1,6 +1,6 @@ #!/bin/bash # install hrp with one shell command -# curl -sL https://raw.githubusercontent.com/httprunner/hrp/main/hrp/scripts/install.sh | bash +# curl -sL https://raw.githubusercontent.com/httprunner/hrp/main/cli/scripts/install.sh | bash set -e diff --git a/docs/cmd/hrp.md b/docs/cmd/hrp.md index cb05f56f..93352005 100644 --- a/docs/cmd/hrp.md +++ b/docs/cmd/hrp.md @@ -31,5 +31,6 @@ Copyright 2021 debugtalk * [hrp boom](hrp_boom.md) - run load test with boomer * [hrp har2case](hrp_har2case.md) - Convert HAR to json/yaml testcase files * [hrp run](hrp_run.md) - run API test +* [hrp startproject](hrp_startproject.md) - Create a scaffold project -###### Auto generated by spf13/cobra on 7-Jan-2022 +###### Auto generated by spf13/cobra on 8-Jan-2022 diff --git a/docs/cmd/hrp_boom.md b/docs/cmd/hrp_boom.md index f81e3723..36e4851c 100644 --- a/docs/cmd/hrp_boom.md +++ b/docs/cmd/hrp_boom.md @@ -38,4 +38,4 @@ hrp boom [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 7-Jan-2022 +###### Auto generated by spf13/cobra on 8-Jan-2022 diff --git a/docs/cmd/hrp_har2case.md b/docs/cmd/hrp_har2case.md index da27b051..79315570 100644 --- a/docs/cmd/hrp_har2case.md +++ b/docs/cmd/hrp_har2case.md @@ -7,7 +7,7 @@ Convert HAR to json/yaml testcase files Convert HAR to json/yaml testcase files ``` -hrp har2case harPath... [flags] +hrp har2case $har_path... [flags] ``` ### Options @@ -23,4 +23,4 @@ hrp har2case harPath... [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 7-Jan-2022 +###### Auto generated by spf13/cobra on 8-Jan-2022 diff --git a/docs/cmd/hrp_run.md b/docs/cmd/hrp_run.md index 5564d2e4..20523b8d 100644 --- a/docs/cmd/hrp_run.md +++ b/docs/cmd/hrp_run.md @@ -7,7 +7,7 @@ run API test run yaml/json testcase files for API test ``` -hrp run path... [flags] +hrp run $path... [flags] ``` ### Examples @@ -31,4 +31,4 @@ hrp run path... [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 7-Jan-2022 +###### Auto generated by spf13/cobra on 8-Jan-2022 diff --git a/docs/cmd/hrp_startproject.md b/docs/cmd/hrp_startproject.md new file mode 100644 index 00000000..c22b9f47 --- /dev/null +++ b/docs/cmd/hrp_startproject.md @@ -0,0 +1,19 @@ +## hrp startproject + +Create a scaffold project + +``` +hrp startproject $project_name [flags] +``` + +### Options + +``` + -h, --help help for startproject +``` + +### SEE ALSO + +* [hrp](hrp.md) - One-stop solution for HTTP(S) testing. + +###### Auto generated by spf13/cobra on 8-Jan-2022 From c6549f4012aada2a6b79924788f73854ac151837 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 22:09:08 +0800 Subject: [PATCH 04/10] feat: create scaffold project --- .github/workflows/scaffold.yml | 34 +++++++++ cli/hrp/cmd/scaffold.go | 4 +- docs/CHANGELOG.md | 3 +- internal/builtin/scaffold.go | 17 ----- .../demo_test.go => internal/scaffold/demo.go | 60 ++++------------ internal/scaffold/demo_test.go | 48 +++++++++++++ internal/scaffold/main.go | 71 +++++++++++++++++++ 7 files changed, 172 insertions(+), 65 deletions(-) create mode 100644 .github/workflows/scaffold.yml delete mode 100644 internal/builtin/scaffold.go rename examples/demo_test.go => internal/scaffold/demo.go (76%) create mode 100644 internal/scaffold/demo_test.go create mode 100644 internal/scaffold/main.go diff --git a/.github/workflows/scaffold.yml b/.github/workflows/scaffold.yml new file mode 100644 index 00000000..7beb1444 --- /dev/null +++ b/.github/workflows/scaffold.yml @@ -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 diff --git a/cli/hrp/cmd/scaffold.go b/cli/hrp/cmd/scaffold.go index d70db7eb..94c4fd51 100644 --- a/cli/hrp/cmd/scaffold.go +++ b/cli/hrp/cmd/scaffold.go @@ -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) } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ae6f24e7..e7c708e6 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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) diff --git a/internal/builtin/scaffold.go b/internal/builtin/scaffold.go deleted file mode 100644 index 0a57f2aa..00000000 --- a/internal/builtin/scaffold.go +++ /dev/null @@ -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") -} diff --git a/examples/demo_test.go b/internal/scaffold/demo.go similarity index 76% rename from examples/demo_test.go rename to internal/scaffold/demo.go index bb41544e..83753467 100644 --- a/examples/demo_test.go +++ b/internal/scaffold/demo.go @@ -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: - // -} - -func Example_jsonDemo() { - testCase := &hrp.TestCasePath{Path: demoTestCaseJSONPath} - err := hrp.NewRunner(nil).Run(testCase) // hrp.Run(testCase) - fmt.Println(err) - // Output: - // -} - -func Example_yamlDemo() { - testCase := &hrp.TestCasePath{Path: demoTestCaseYAMLPath} - err := hrp.NewRunner(nil).Run(testCase) // hrp.Run(testCase) - fmt.Println(err) - // Output: - // -} +// .env +var demoEnvContent = `USERNAME=debugtalk +"PASSWORD=123456 +` diff --git a/internal/scaffold/demo_test.go b/internal/scaffold/demo_test.go new file mode 100644 index 00000000..870ef7bd --- /dev/null +++ b/internal/scaffold/demo_test.go @@ -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: + // +} + +func Example_jsonDemo() { + testCase := &hrp.TestCasePath{Path: demoTestCaseJSONPath} + err := hrp.NewRunner(nil).Run(testCase) // hrp.Run(testCase) + fmt.Println(err) + // Output: + // +} + +func Example_yamlDemo() { + testCase := &hrp.TestCasePath{Path: demoTestCaseYAMLPath} + err := hrp.NewRunner(nil).Run(testCase) // hrp.Run(testCase) + fmt.Println(err) + // Output: + // +} diff --git a/internal/scaffold/main.go b/internal/scaffold/main.go new file mode 100644 index 00000000..32838335 --- /dev/null +++ b/internal/scaffold/main.go @@ -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 +} From 853a456b1240b8a7fa2ea2d2698dd1c2344af3fb Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 22:21:42 +0800 Subject: [PATCH 05/10] change: udpate docs --- README.md | 11 ++++++----- cli/hrp/cmd/har2case.go | 4 ++-- cli/hrp/cmd/scaffold.go | 2 +- internal/scaffold/main.go | 3 ++- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e1daa8a5..6ddd349c 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,12 @@ Usage: hrp [command] Available Commands: - boom run load test with boomer - completion generate the autocompletion script for the specified shell - har2case Convert HAR to json/yaml testcase files - help Help about any command - run run API test + boom run load test with boomer + completion generate the autocompletion script for the specified shell + har2case convert HAR to json/yaml testcase files + help Help about any command + run run API test + startproject create a scaffold project Flags: -h, --help help for hrp diff --git a/cli/hrp/cmd/har2case.go b/cli/hrp/cmd/har2case.go index 4e08ea41..c59ac313 100644 --- a/cli/hrp/cmd/har2case.go +++ b/cli/hrp/cmd/har2case.go @@ -10,8 +10,8 @@ import ( // har2caseCmd represents the har2case command var har2caseCmd = &cobra.Command{ Use: "har2case $har_path...", - Short: "Convert HAR to json/yaml testcase files", - Long: `Convert HAR to json/yaml testcase files`, + Short: "convert HAR to json/yaml testcase files", + Long: `convert HAR to json/yaml testcase files`, Args: cobra.MinimumNArgs(1), PreRun: func(cmd *cobra.Command, args []string) { setLogLevel(logLevel) diff --git a/cli/hrp/cmd/scaffold.go b/cli/hrp/cmd/scaffold.go index 94c4fd51..36a78f37 100644 --- a/cli/hrp/cmd/scaffold.go +++ b/cli/hrp/cmd/scaffold.go @@ -10,7 +10,7 @@ import ( var scaffoldCmd = &cobra.Command{ Use: "startproject $project_name", - Short: "Create a scaffold project", + Short: "create a scaffold project", Args: cobra.ExactValidArgs(1), PreRun: func(cmd *cobra.Command, args []string) { setLogLevel(logLevel) diff --git a/internal/scaffold/main.go b/internal/scaffold/main.go index 32838335..2f2b7528 100644 --- a/internal/scaffold/main.go +++ b/internal/scaffold/main.go @@ -2,6 +2,7 @@ package scaffold import ( "fmt" + "io/ioutil" "os" "path" @@ -62,7 +63,7 @@ func createFolder(folderPath string) error { func createFile(filePath string, data string) error { log.Info().Str("filePath", filePath).Msg("create file") - err := os.WriteFile(filePath, []byte(data), 0o644) + err := ioutil.WriteFile(filePath, []byte(data), 0o644) if err != nil { log.Error().Err(err).Msg("create file failed") return err From 2887e278352c9c16673ab35ff5f828a6a9d3055e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 22:31:02 +0800 Subject: [PATCH 06/10] change: trigger github action for scaffold --- .github/workflows/scaffold.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/scaffold.yml b/.github/workflows/scaffold.yml index 7beb1444..a2f50707 100644 --- a/.github/workflows/scaffold.yml +++ b/.github/workflows/scaffold.yml @@ -1,6 +1,7 @@ name: Run scaffold on: + push: pull_request: types: [synchronize] From b83aec93c722734bc97bc3120065dab1c8d00b21 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 22:41:33 +0800 Subject: [PATCH 07/10] change: scaffold action --- .github/workflows/scaffold.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/scaffold.yml b/.github/workflows/scaffold.yml index a2f50707..5c0adcac 100644 --- a/.github/workflows/scaffold.yml +++ b/.github/workflows/scaffold.yml @@ -11,10 +11,6 @@ jobs: 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 }} @@ -27,8 +23,6 @@ jobs: 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 From df8a6590fa6cf7605d603b3a95a1fe6dac4ecab0 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 22:53:32 +0800 Subject: [PATCH 08/10] change: scaffold action --- internal/scaffold/main.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/internal/scaffold/main.go b/internal/scaffold/main.go index 2f2b7528..b9ca26d2 100644 --- a/internal/scaffold/main.go +++ b/internal/scaffold/main.go @@ -26,11 +26,19 @@ func CreateScaffold(projectName string) error { 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 project folders + if err := createFolder(projectName); err != nil { + return err + } + if err := createFolder(path.Join(projectName, "har")); err != nil { + return err + } + if err := createFolder(path.Join(projectName, "testcases")); err != nil { + return err + } + if err := createFolder(path.Join(projectName, "reports")); err != nil { + return err + } // create demo testcases tCase, _ := demoTestCase.ToTCase() @@ -45,14 +53,20 @@ func CreateScaffold(projectName string) error { return err } - createFile(path.Join(projectName, ".gitignore"), demoIgnoreContent) - createFile(path.Join(projectName, ".env"), demoEnvContent) + // create .gitignore + if err := createFile(path.Join(projectName, ".gitignore"), demoIgnoreContent); err != nil { + return err + } + // create .env + if err := createFile(path.Join(projectName, ".env"), demoEnvContent); err != nil { + return err + } return nil } func createFolder(folderPath string) error { - log.Info().Str("folderPath", folderPath).Msg("create folder") + log.Info().Str("path", folderPath).Msg("create folder") err := os.MkdirAll(folderPath, os.ModePerm) if err != nil { log.Error().Err(err).Msg("create folder failed") @@ -62,7 +76,7 @@ func createFolder(folderPath string) error { } func createFile(filePath string, data string) error { - log.Info().Str("filePath", filePath).Msg("create file") + log.Info().Str("path", filePath).Msg("create file") err := ioutil.WriteFile(filePath, []byte(data), 0o644) if err != nil { log.Error().Err(err).Msg("create file failed") From cc21e7d2c03e1c158ec312a735fa594014c690c6 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 22:55:08 +0800 Subject: [PATCH 09/10] change: scaffold action --- .github/workflows/scaffold.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/scaffold.yml b/.github/workflows/scaffold.yml index 5c0adcac..cb2cb81e 100644 --- a/.github/workflows/scaffold.yml +++ b/.github/workflows/scaffold.yml @@ -1,7 +1,6 @@ name: Run scaffold on: - push: pull_request: types: [synchronize] From 2445df8d6ca05b521b88aad2d34e4b21e6d72c83 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 8 Jan 2022 22:57:32 +0800 Subject: [PATCH 10/10] change: unittest action --- .github/workflows/unittest.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml index d0680739..80f4e312 100644 --- a/.github/workflows/unittest.yml +++ b/.github/workflows/unittest.yml @@ -1,7 +1,6 @@ name: Run unittests on: - push: pull_request: types: [synchronize] schedule: