Skip to content

Commit

Permalink
Mock network call
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoConstantine committed Jul 10, 2024
1 parent 8ace3e2 commit ec95f56
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
10 changes: 10 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ import (
"testing"

"github.com/XiaoConstantine/mycli/pkg/commands/root"
"github.com/XiaoConstantine/mycli/pkg/commands/update"
"github.com/XiaoConstantine/mycli/pkg/iostreams"
"github.com/stretchr/testify/assert"
)

func TestRun(t *testing.T) {
// Save the original function and defer its restoration
originalCheckForUpdates := update.CheckForUpdatesFunc
defer func() { update.CheckForUpdatesFunc = originalCheckForUpdates }()

// Mock the CheckForUpdates function
update.CheckForUpdatesFunc = func(iostream *iostreams.IOStreams) (bool, string, error) {
return false, "v1.0.0", nil // No update available
}
// Define test cases
tests := []struct {
name string
Expand Down
7 changes: 2 additions & 5 deletions pkg/commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Run(args []string) ExitCode {
}

// Check for updates
hasUpdate, latestVersion, err := update.CheckForUpdates(iostream)
hasUpdate, latestVersion, err := update.CheckForUpdatesFunc(iostream)
if err != nil {
fmt.Fprintf(stderr, "Failed to check for updates: %s\n", err)
} else if hasUpdate {
Expand Down Expand Up @@ -157,10 +157,7 @@ func Run(args []string) ExitCode {
}
rootCmd.SetArgs([]string{selectedOption})
}
if err != nil {
fmt.Fprintf(stderr, "failed to create root command: %s\n", err)
return exitError
}

if _, err := rootCmd.ExecuteContextC(ctx); err != nil {
var pagerPipeError *iostreams.ErrClosedPagerPipe
var noResultsError utils.NoResultsError
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/spf13/cobra"
)

var CheckForUpdatesFunc = CheckForUpdates

func NewUpdateCmd(iostream *iostreams.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Expand Down

0 comments on commit ec95f56

Please sign in to comment.