Skip to content

Commit

Permalink
Add jobcreate test and fix bug (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbenjam authored Dec 5, 2024
1 parent 497e300 commit 2dcde2f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ var jobCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(jobCmd)
jobCmd.AddCommand(jobListCmd, jobCreateCmd, jobCancelCmd)
jobCmd.AddCommand(jobListCmd, JobCreateCmd, jobCancelCmd)
}
16 changes: 9 additions & 7 deletions cmd/jobcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/renderinc/cli/pkg/tui/views"
)

var jobCreateCmd = &cobra.Command{
var JobCreateCmd = &cobra.Command{
Use: "create [serviceID]",
Short: "Create a new job for a service",
Args: cobra.MaximumNArgs(1),
Expand All @@ -24,10 +24,10 @@ var jobCreateCmd = &cobra.Command{
var InteractiveJobCreate = func(ctx context.Context, input *views.JobCreateInput, breadcrumb string) tea.Cmd {
return command.AddToStackFunc(
ctx,
jobCreateCmd,
JobCreateCmd,
breadcrumb,
input,
views.NewJobCreateView(ctx, input, jobCreateCmd, func(j *clientjob.Job) tea.Cmd {
views.NewJobCreateView(ctx, input, JobCreateCmd, views.CreateJob, func(j *clientjob.Job) tea.Cmd {
return InteractiveLogs(ctx, views.LogInput{
ResourceIDs: []string{j.Id},
Tail: true,
Expand All @@ -45,7 +45,9 @@ func interactiveJobCreate(cmd *cobra.Command, input *views.JobCreateInput) tea.C
"Create Job",
input,
views.NewServiceList(ctx, views.ServiceInput{
Types: []client.ServiceType{client.WebService, client.BackgroundWorker, client.PrivateService, client.CronJob},
Types: []client.ServiceType{
client.WebService, client.BackgroundWorker, client.PrivateService, client.CronJob,
},
}, func(ctx context.Context, r resource.Resource) tea.Cmd {
input.ServiceID = r.ID()
return InteractiveJobCreate(ctx, input, resource.BreadcrumbForResource(r))
Expand All @@ -62,7 +64,7 @@ func interactiveJobCreate(cmd *cobra.Command, input *views.JobCreateInput) tea.C
}

func init() {
jobCreateCmd.RunE = func(cmd *cobra.Command, args []string) error {
JobCreateCmd.RunE = func(cmd *cobra.Command, args []string) error {
var input views.JobCreateInput

err := command.ParseCommand(cmd, args, &input)
Expand All @@ -84,6 +86,6 @@ func init() {
return nil
}

jobCreateCmd.Flags().String("start-command", "", "The command to run for the job")
jobCreateCmd.Flags().String("plan-id", "", "The plan ID for the job (optional)")
JobCreateCmd.Flags().String("start-command", "", "The command to run for the job")
JobCreateCmd.Flags().String("plan-id", "", "The plan ID for the job (optional)")
}
1 change: 1 addition & 0 deletions pkg/command/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func FormValuesFromStruct(v any) FormValues {
case reflect.Ptr:
if elemField.IsNil() {
formValues[cliTag] = NewStringFormValue("")
break
}

switch field.Type.Elem().Kind() {
Expand Down
19 changes: 16 additions & 3 deletions pkg/tui/views/jobcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,27 @@ type JobCreateView struct {
formAction *tui.FormWithAction[*clientjob.Job]
}

func NewJobCreateView(ctx context.Context, input *JobCreateInput, cobraCmd *cobra.Command, action func(j *clientjob.Job) tea.Cmd) *JobCreateView {
form, _ := command.HuhForm(cobraCmd, input)
func NewJobCreateView(
ctx context.Context,
input *JobCreateInput,
cobraCmd *cobra.Command,
createJob func(ctx context.Context, input JobCreateInput) (*clientjob.Job, error),
action func(j *clientjob.Job) tea.Cmd,
) *JobCreateView {
form, values := command.HuhForm(cobraCmd, input)

return &JobCreateView{
formAction: tui.NewFormWithAction(
tui.NewFormAction(
action,
command.LoadCmd(ctx, CreateJob, *input),
func() tea.Msg {
var createJobInput JobCreateInput
err := command.StructFromFormValues(values, &createJobInput)
if err != nil {
return tui.ErrorMsg{Err: err}
}
return command.LoadCmd(ctx, createJob, createJobInput)()
},
),
form,
),
Expand Down
50 changes: 50 additions & 0 deletions pkg/tui/views/jobcreate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package views_test

import (
"context"
"testing"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/teatest"
"github.com/renderinc/cli/cmd"
clientjob "github.com/renderinc/cli/pkg/client/jobs"
"github.com/renderinc/cli/pkg/tui/testhelper"
"github.com/renderinc/cli/pkg/tui/views"
"github.com/stretchr/testify/require"
)

func TestJobCreate(t *testing.T) {
ctx := context.Background()

input := views.JobCreateInput{
ServiceID: "service-id",
}

var createJobInput views.JobCreateInput

createJob := func(ctx context.Context, input views.JobCreateInput) (*clientjob.Job, error) {
createJobInput = input
return &clientjob.Job{Id: "foo"}, nil
}

action := func(j *clientjob.Job) tea.Cmd {
return nil
}

m := views.NewJobCreateView(ctx, &input, cmd.JobCreateCmd, createJob, action)
tm := teatest.NewTestModel(t, testhelper.Stackify(m))

tm.Send(tea.WindowSizeMsg{Width: 80, Height: 80})

// Add start command
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("echo 'hello world'")})
tm.Send(tea.KeyMsg{Type: tea.KeyEnter})

require.Eventually(t, func() bool {
return createJobInput.StartCommand != nil && *createJobInput.StartCommand == "echo 'hello world'"
}, time.Second, time.Millisecond*10)

err := tm.Quit()
require.NoError(t, err)
}

0 comments on commit 2dcde2f

Please sign in to comment.