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/.github/workflows/scaffold.yml b/.github/workflows/scaffold.yml new file mode 100644 index 00000000..cb2cb81e --- /dev/null +++ b/.github/workflows/scaffold.yml @@ -0,0 +1,28 @@ +name: Run scaffold + +on: + pull_request: + types: [synchronize] + +jobs: + scaffold: + strategy: + fail-fast: true + matrix: + go-version: + - 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: 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/.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: 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..6ddd349c 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. @@ -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/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 88% rename from hrp/cmd/har2case.go rename to cli/hrp/cmd/har2case.go index 7512f241..c59ac313 100644 --- a/hrp/cmd/har2case.go +++ b/cli/hrp/cmd/har2case.go @@ -9,9 +9,9 @@ import ( // har2caseCmd represents the har2case command var har2caseCmd = &cobra.Command{ - Use: "har2case harPath...", - Short: "Convert HAR to json/yaml testcase files", - Long: `Convert HAR to json/yaml testcase files`, + Use: "har2case $har_path...", + 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) @@ -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 96% rename from hrp/cmd/run.go rename to cli/hrp/cmd/run.go index 29aa2486..23d3de9f 100644 --- a/hrp/cmd/run.go +++ b/cli/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 @@ -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/cli/hrp/cmd/scaffold.go b/cli/hrp/cmd/scaffold.go new file mode 100644 index 00000000..36a78f37 --- /dev/null +++ b/cli/hrp/cmd/scaffold.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" + + "github.com/httprunner/hrp/internal/scaffold" +) + +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 := scaffold.CreateScaffold(args[0]) + if err != nil { + os.Exit(1) + } + }, +} + +func init() { + 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/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/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 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/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..b9ca26d2 --- /dev/null +++ b/internal/scaffold/main.go @@ -0,0 +1,86 @@ +package scaffold + +import ( + "fmt" + "io/ioutil" + "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 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() + 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 + } + + // 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("path", 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("path", filePath).Msg("create file") + err := ioutil.WriteFile(filePath, []byte(data), 0o644) + if err != nil { + log.Error().Err(err).Msg("create file failed") + return err + } + return nil +} 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)