From 2ebbf2b82fc8f05c4ce5137f89101861de2b5b31 Mon Sep 17 00:00:00 2001 From: Lyubo Kamenov Date: Thu, 17 Nov 2022 08:31:17 -0500 Subject: [PATCH] add ruby turbine command test --- cmd/meroxa/turbine/ruby/internal/cmd.go | 24 +++++++++++++++++ cmd/meroxa/turbine/ruby/internal/cmd_test.go | 27 ++++++++++++++++++++ cmd/meroxa/turbine/ruby/run.go | 17 ++++-------- 3 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 cmd/meroxa/turbine/ruby/internal/cmd.go create mode 100644 cmd/meroxa/turbine/ruby/internal/cmd_test.go diff --git a/cmd/meroxa/turbine/ruby/internal/cmd.go b/cmd/meroxa/turbine/ruby/internal/cmd.go new file mode 100644 index 000000000..9d1ef9799 --- /dev/null +++ b/cmd/meroxa/turbine/ruby/internal/cmd.go @@ -0,0 +1,24 @@ +package internal + +import ( + "fmt" + "os" + "os/exec" + "path" +) + +func NewTurbineCmd(appPath string, env map[string]string) *exec.Cmd { + cmd := exec.Command("ruby", []string{ + "-r", path.Join(appPath, "app"), + "-e", "Turbine.run", + }...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Dir = appPath + cmd.Env = os.Environ() + + for k, v := range env { + cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v)) + } + return cmd +} diff --git a/cmd/meroxa/turbine/ruby/internal/cmd_test.go b/cmd/meroxa/turbine/ruby/internal/cmd_test.go new file mode 100644 index 000000000..af5b96847 --- /dev/null +++ b/cmd/meroxa/turbine/ruby/internal/cmd_test.go @@ -0,0 +1,27 @@ +package internal_test + +import ( + "os" + "testing" + + "github.com/meroxa/cli/cmd/meroxa/turbine/ruby/internal" + "github.com/stretchr/testify/assert" +) + +func Test_NewTurbineCmd(t *testing.T) { + cmd := internal.NewTurbineCmd("/my/path", map[string]string{ + "foo": "bar", + "x": "y", + }) + + assert.Equal(t, cmd.Stdout, os.Stdout) + assert.Equal(t, cmd.Stderr, os.Stderr) + assert.Equal(t, cmd.Dir, "/my/path") + assert.Equal(t, cmd.Args, []string{ + "ruby", + "-r", "/my/path/app", + "-e", "Turbine.run", + }) + assert.Contains(t, cmd.Env, "foo=bar") + assert.Contains(t, cmd.Env, "x=y") +} diff --git a/cmd/meroxa/turbine/ruby/run.go b/cmd/meroxa/turbine/ruby/run.go index 841c5f85c..414bcae82 100644 --- a/cmd/meroxa/turbine/ruby/run.go +++ b/cmd/meroxa/turbine/ruby/run.go @@ -2,24 +2,17 @@ package turbinerb import ( "context" - "fmt" - "os" - "os/exec" - "path" + + "github.com/meroxa/cli/cmd/meroxa/turbine/ruby/internal" ) func (t *turbineRbCLI) Run(ctx context.Context) error { go t.runServer.Run(ctx) defer t.runServer.GracefulStop() - cmd := exec.Command("ruby", []string{ - "-r", path.Join(t.appPath, "app"), - "-e", "Turbine.run", - }...) - cmd.Env = append(os.Environ(), fmt.Sprintf("TURBINE_CORE_SERVER=%s", t.grpcListenAddress)) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - cmd.Dir = t.appPath + cmd := internal.NewTurbineCmd(t.appPath, map[string]string{ + "TURBINE_CORE_SERVER": t.grpcListenAddress, + }) if err := cmd.Start(); err != nil { t.logger.Errorf(ctx, err.Error())