From 91562bbd6300b5fba4101154cca006231c224fdb Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Sat, 7 Dec 2024 23:29:25 +0800 Subject: [PATCH] chore: update test coverage --- .github/workflows/ci.yml | 9 +++++---- internal/utils/console_test.go | 12 +----------- internal/utils/flags/project_ref.go | 5 +++-- internal/utils/flags/project_ref_test.go | 7 +++++-- internal/utils/prompt.go | 4 ++-- 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0f4078158..ce32d176b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,12 +21,13 @@ jobs: # Required by: internal/utils/credentials/keyring_test.go - uses: t1m0thyj/unlock-keyring@v1 - run: | - go run gotest.tools/gotestsum -- ./... -race -v -count=1 \ - -coverpkg ./cmd/...,./internal/... -coverprofile=coverage.out + go run gotest.tools/gotestsum -- -race -v -count=1 -coverprofile=coverage.out \ + `go list ./... | grep -Ev 'cmd|docs|examples|pkg/api|tools'` - - uses: shogo82148/actions-goveralls@v1 + - uses: coverallsapp/github-action@v2 with: - path-to-profile: coverage.out + file: coverage.out + format: golang lint: name: Lint diff --git a/internal/utils/console_test.go b/internal/utils/console_test.go index 5fbd53db5..f7662d2d1 100644 --- a/internal/utils/console_test.go +++ b/internal/utils/console_test.go @@ -2,11 +2,9 @@ package utils import ( "context" - "os" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "github.com/supabase/cli/v2/internal/testing/fstest" ) @@ -34,15 +32,7 @@ func TestPromptYesNo(t *testing.T) { func TestPromptText(t *testing.T) { t.Run("defaults on timeout", func(t *testing.T) { - // Setup stdin - r, _, err := os.Pipe() - require.NoError(t, err) - // Replace stdin - oldStdin := os.Stdin - defer func() { - os.Stdin = oldStdin - }() - os.Stdin = r + t.Cleanup(fstest.MockStdin(t, "")) c := NewConsole() // Run test val, err := c.PromptText(context.Background(), "test") diff --git a/internal/utils/flags/project_ref.go b/internal/utils/flags/project_ref.go index e8f675a16..6b39b02ac 100644 --- a/internal/utils/flags/project_ref.go +++ b/internal/utils/flags/project_ref.go @@ -6,6 +6,7 @@ import ( "fmt" "os" + tea "github.com/charmbracelet/bubbletea" "github.com/go-errors/errors" "github.com/spf13/afero" "github.com/spf13/viper" @@ -26,7 +27,7 @@ func ParseProjectRef(ctx context.Context, fsys afero.Fs) error { return errors.New(utils.ErrNotLinked) } -func PromptProjectRef(ctx context.Context, title string) error { +func PromptProjectRef(ctx context.Context, title string, opts ...tea.ProgramOption) error { resp, err := utils.GetSupabase().V1ListAllProjectsWithResponse(ctx) if err != nil { return errors.Errorf("failed to retrieve projects: %w", err) @@ -41,7 +42,7 @@ func PromptProjectRef(ctx context.Context, title string) error { Details: fmt.Sprintf("name: %s, org: %s, region: %s", project.Name, project.OrganizationId, project.Region), } } - choice, err := utils.PromptChoice(ctx, title, items) + choice, err := utils.PromptChoice(ctx, title, items, opts...) if err != nil { return err } diff --git a/internal/utils/flags/project_ref_test.go b/internal/utils/flags/project_ref_test.go index ca910e23d..ee12de886 100644 --- a/internal/utils/flags/project_ref_test.go +++ b/internal/utils/flags/project_ref_test.go @@ -4,8 +4,10 @@ import ( "context" "net/http" "os" + "strings" "testing" + tea "github.com/charmbracelet/bubbletea" "github.com/go-errors/errors" "github.com/h2non/gock" "github.com/spf13/afero" @@ -69,6 +71,7 @@ func TestProjectPrompt(t *testing.T) { t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) t.Run("validates prompt input", func(t *testing.T) { + input := tea.WithInput(strings.NewReader("\r")) // Setup mock api defer gock.OffAll() gock.New(utils.DefaultApiHost). @@ -80,9 +83,9 @@ func TestProjectPrompt(t *testing.T) { OrganizationId: "test-org", }}) // Run test - err := PromptProjectRef(context.Background(), "") + err := PromptProjectRef(context.Background(), "", input) // Check error - assert.ErrorContains(t, err, "failed to prompt choice:") + assert.NoError(t, err) assert.Empty(t, apitest.ListUnmatchedRequests()) }) diff --git a/internal/utils/prompt.go b/internal/utils/prompt.go index 8ff67d97e..5589f49d6 100644 --- a/internal/utils/prompt.go +++ b/internal/utils/prompt.go @@ -103,7 +103,7 @@ func (m model) View() string { } // Prompt user to choose from a list of items, returns the chosen index. -func PromptChoice(ctx context.Context, title string, items []PromptItem) (PromptItem, error) { +func PromptChoice(ctx context.Context, title string, items []PromptItem, opts ...tea.ProgramOption) (PromptItem, error) { // Create list items var listItems []list.Item for _, v := range items { @@ -126,7 +126,7 @@ func PromptChoice(ctx context.Context, title string, items []PromptItem) (Prompt // Create our model ctx, cancel := context.WithCancel(ctx) initial := model{cancel: cancel, list: l} - prog := tea.NewProgram(initial) + prog := tea.NewProgram(initial, opts...) state, err := prog.Run() if err != nil { return initial.choice, errors.Errorf("failed to prompt choice: %w", err)