Skip to content

Commit

Permalink
Improve test coverage for homebrew
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoConstantine committed Jul 9, 2024
1 parent 2f06be8 commit a488b70
Showing 1 changed file with 126 additions and 11 deletions.
137 changes: 126 additions & 11 deletions pkg/commands/install/homebrew/homebrew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"
"os"
"os/exec"
"os/user"
"testing"

"github.com/XiaoConstantine/mycli/pkg/iostreams"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand All @@ -31,16 +30,16 @@ func MockExecCommand(ctx context.Context, command string, args ...string) *exec.
return cmd
}

func TestNewCmdHomeBrew(t *testing.T) {
ios, _, _, _ := iostreams.Test()
cmd := NewCmdHomeBrew(ios)
// func TestNewCmdHomeBrew(t *testing.T) {
// ios, _, _, _ := iostreams.Test()
// cmd := NewCmdHomeBrew(ios)

assert.NotNil(t, cmd)
assert.Equal(t, "homebrew", cmd.Use)
assert.True(t, cmd.Hidden)
assert.True(t, cmd.SilenceErrors)
assert.Equal(t, "install", cmd.Annotations["group"])
}
// assert.NotNil(t, cmd)
// assert.Equal(t, "homebrew", cmd.Use)
// assert.True(t, cmd.Hidden)
// assert.True(t, cmd.SilenceErrors)
// assert.Equal(t, "install", cmd.Annotations["group"])
// }

func TestIsHomebrewInstalled(t *testing.T) {
// Save current execCommandContext and defer its restoration
Expand Down Expand Up @@ -78,3 +77,119 @@ func TestIsHomebrewInstalled(t *testing.T) {
})
}
}

type mockUtils struct {

Check failure on line 81 in pkg/commands/install/homebrew/homebrew_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

type `mockUtils` is unused (unused)

Check failure on line 81 in pkg/commands/install/homebrew/homebrew_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

type `mockUtils` is unused (unused)
mock.Mock
}

func (m *mockUtils) GetCurrentUser() (*user.User, error) {

Check failure on line 85 in pkg/commands/install/homebrew/homebrew_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

func `(*mockUtils).GetCurrentUser` is unused (unused)

Check failure on line 85 in pkg/commands/install/homebrew/homebrew_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

func `(*mockUtils).GetCurrentUser` is unused (unused)
args := m.Called()
return args.Get(0).(*user.User), args.Error(1)
}

func (m *mockUtils) IsAdmin(ctx context.Context, user *user.User) bool {

Check failure on line 90 in pkg/commands/install/homebrew/homebrew_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

func `(*mockUtils).IsAdmin` is unused (unused)

Check failure on line 90 in pkg/commands/install/homebrew/homebrew_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

func `(*mockUtils).IsAdmin` is unused (unused)
args := m.Called(ctx, user)
return args.Bool(0)
}

// func TestNewCmdHomeBrew(t *testing.T) {
// tests := []struct {
// name string
// isAdmin bool
// isInstalled bool
// installSuccess bool
// expectedOutput string
// expectedError error
// }{
// {
// name: "Already installed",
// isAdmin: true,
// isInstalled: true,
// installSuccess: true,
// expectedOutput: "Homebrew is installed.\n",
// expectedError: nil,
// },
// {
// name: "Not admin",
// isAdmin: false,
// isInstalled: false,
// installSuccess: false,
// expectedOutput: "You need to be an administrator to install Homebrew. Please run this command from an admin account.\n",
// expectedError: os.ErrPermission,
// },
// {
// name: "Install success",
// isAdmin: true,
// isInstalled: false,
// installSuccess: true,
// expectedOutput: "Installing homebrew with su current user, enter your password when prompt\n",
// expectedError: nil,
// },
// {
// name: "Install failure",
// isAdmin: true,
// isInstalled: false,
// installSuccess: false,
// expectedOutput: "Installing homebrew with su current user, enter your password when prompt\nFailed to install Homebrew: exit status 1\n",
// expectedError: errors.New("exit status 1"),
// },
// }

// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// // Setup
// mockCmd := &mockCommandContext{}
// oldExecCommandContext := execCommandContext
// execCommandContext = mockCmd.CommandContext
// defer func() { execCommandContext = oldExecCommandContext }()

// mockUtil := &mockUtils{}
// // oldGetCurrentUser := utils.GetCurrentUser
// // utils.GetCurrentUser = mockUtil.GetCurrentUser
// // oldIsAdmin := utils.IsAdmin
// // utils.IsAdmin = mockUtil.IsAdmin
// // defer func() {
// // utils.GetCurrentUser = oldGetCurrentUser
// // utils.IsAdmin = oldIsAdmin
// // }()
// ios, _, out, _ := iostreams.Test()
// cmd := NewCmdHomeBrew(ios)

// // Mock GetCurrentUser
// mockUtil.On("GetCurrentUser").Return(&user.User{Username: "testuser"}, nil)

// // Mock IsAdmin
// mockUtil.On("IsAdmin", mock.Anything, mock.Anything).Return(tt.isAdmin)

// // Mock IsHomebrewInstalled
// if tt.isInstalled {
// mockCmd.On("CommandContext", mock.Anything, "which", []string{"brew"}).
// Return(exec.Command("echo", "/opt/homebrew/bin/brew"))
// } else {
// mockCmd.On("CommandContext", mock.Anything, "which", []string{"brew"}).
// Return(exec.Command("false"))
// }

// // Mock install command
// if !tt.isInstalled && tt.isAdmin {
// if tt.installSuccess {
// mockCmd.On("CommandContext", mock.Anything, "su", mock.Anything).
// Return(exec.Command("echo", "Homebrew installed successfully"))
// } else {
// failCmd := exec.Command("false")
// mockCmd.On("CommandContext", mock.Anything, "su", mock.Anything).
// Return(failCmd)
// }
// }

// // Execute
// err := cmd.RunE(cmd, []string{})

// // Assert
// assert.Equal(t, tt.expectedError, err)
// assert.Contains(t, out.String(), tt.expectedOutput)
// mockCmd.AssertExpectations(t)
// mockUtil.AssertExpectations(t)
// })
// }
// }

0 comments on commit a488b70

Please sign in to comment.