Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions general/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ const (
)

type LoginCommand struct {
serverId string
}

func NewLoginCommand() *LoginCommand {
return &LoginCommand{}
}

func (lc *LoginCommand) SetServerId(serverId string) *LoginCommand {
lc.serverId = serverId
return lc
}

func (lc *LoginCommand) Run() error {
if lc.serverId != "" {
return existingServerLogin(lc.serverId)
}
configurations, err := config.GetAllServersConfigs()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add tests

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for taking the time to review my pr, I have added tests for the lines that I added. There were no tests at all for that part of the code, that's why I didn't add any tests

if err != nil {
return err
Expand Down
35 changes: 35 additions & 0 deletions general/login/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package login

import (
"testing"

"github.com/jfrog/jfrog-cli-core/v2/utils/config"
utilsTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSetServerId(t *testing.T) {
lc := NewLoginCommand()
result := lc.SetServerId("my-server")
assert.Equal(t, "my-server", lc.serverId)
// Verify fluent API returns the same instance
assert.Same(t, lc, result)
}

func TestRunWithNonExistentServerId(t *testing.T) {
cleanUp, err := utilsTests.SetJfrogHome()
require.NoError(t, err)
defer cleanUp()

// At least one server must exist so GetConfig actually looks up by ID
err = config.SaveServersConf([]*config.ServerDetails{
{ServerId: "other-server", Url: "https://example.jfrog.io/"},
})
require.NoError(t, err)

lc := NewLoginCommand().SetServerId("non-existent-server")
err = lc.Run()
assert.ErrorContains(t, err, "non-existent-server")
}

Loading