Skip to content

Commit

Permalink
refactor test command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vlasic committed Sep 28, 2021
1 parent d4cfb66 commit f49ad97
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
40 changes: 39 additions & 1 deletion cli/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func init() {
addCommandInvoke()
addCommandLogs()
addCommandNew()
addCommandTest()
addCommandWatch()
}

Expand Down Expand Up @@ -120,6 +121,30 @@ func addCommandNew() {
rootCmd.AddCommand(cmd)
}

func addCommandTest() {
cmd := &cobra.Command{
Use: "test",
Short: "Run project integration tests",
Long: `Run project integration tests
Project integration tests are pure Go test in [project-root]/test folder.
Mantil sets MANTIL_API_URL environment variable to point to the current
project api url and runs tests with 'go test -v'.
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
c := initTest(cmd, args)
if err := c.run(); err != nil {
log.Fatal(err)
}
},
}
cmd.Flags().StringP("run", "r", "", "run only tests with this pattern in name")
cmd.Flags().StringP("stage", "s", config.DefaultStageName, "stage name")
rootCmd.AddCommand(cmd)

}

func addCommandWatch() {
cmd := &cobra.Command{
Use: "watch",
Expand Down Expand Up @@ -227,6 +252,19 @@ func initNew(cmd *cobra.Command, args []string) *newCmd {
}
}

func initTest(cmd *cobra.Command, args []string) *testCmd {
p, path := getProject()
run := cmd.Flag("run").Value.String()
stageName, _ := cmd.Flags().GetString("stage")

return &testCmd{
project: p,
stageName: stageName,
repoPath: path,
runRegexp: run,
}
}

func initWatch(cmd *cobra.Command, args []string) *watchCmd {
p, path := getProject()
method := cmd.Flag("method").Value.String()
Expand All @@ -236,7 +274,7 @@ func initWatch(cmd *cobra.Command, args []string) *watchCmd {

stage := p.Stage(stageName)
if stage == nil {
log.Fatalf("stage %s not found")
log.Fatalf("stage %s not found", stageName)
}
awsClient := initialiseAWSSDK(p.Name, stage.Name)
account := getAccount(stageName)
Expand Down
57 changes: 24 additions & 33 deletions cli/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,33 @@ import (
"github.com/mantil-io/mantil.go/pkg/shell"
"github.com/mantil-io/mantil/cli/log"
"github.com/mantil-io/mantil/config"
"github.com/spf13/cobra"
)

var testCmd = &cobra.Command{
Use: "test",
Short: "Run project integration tests",
Long: `Run project integration tests
type testCmd struct {
project *config.Project
stageName string
repoPath string
runRegexp string
}

Project integration tests are pure Go test in [project-root]/test folder.
Mantil sets MANTIL_API_URL environment variable to point to the current
project api url and runs tests with 'go test -v'.
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
p, path := getProject()
run := cmd.Flag("run").Value.String()
shellArgs := []string{"go", "test", "-v"}
if run != "" {
shellArgs = append(shellArgs, "--run", run)
}
stageName, _ := cmd.Flags().GetString("stage")
err := shell.Exec(shell.ExecOptions{
Env: []string{"MANTIL_API_URL=" + p.RestEndpoint(stageName)},
Args: shellArgs,
WorkDir: path + "/test",
Logger: log.Info,
ShowShellCmd: false,
})
if err != nil {
log.Error(err)
}
},
func (c *testCmd) run() error {
err := shell.Exec(shell.ExecOptions{
Env: []string{"MANTIL_API_URL=" + c.project.RestEndpoint(c.stageName)},
Args: c.args(),
WorkDir: c.repoPath + "/test",
Logger: log.Info,
ShowShellCmd: false,
})
if err != nil {
return nil

This comment has been minimized.

Copy link
@ianic

ianic Sep 28, 2021

Member

return err ?

}
return nil
}

func init() {
testCmd.Flags().StringP("run", "r", "", "run only tests with this pattern in name")
testCmd.Flags().StringP("stage", "s", config.DefaultStageName, "stage name")
rootCmd.AddCommand(testCmd)
func (c *testCmd) args() []string {
args := []string{"go", "test", "-v"}
if c.runRegexp != "" {
args = append(args, "--run", c.runRegexp)
}
return args
}

0 comments on commit f49ad97

Please sign in to comment.