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
8 changes: 5 additions & 3 deletions pkg/sqlcmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ func connectCommand(s *Sqlcmd, args []string, line uint) error {
}

commandArgs := strings.Fields(args[0])
// A bare ":connect" still yields one empty argument.
if len(commandArgs) == 0 {
return InvalidCommandError("CONNECT", line)
}

// Parse flags
flags := flag.NewFlagSet("connect", flag.ContinueOnError)
Expand Down Expand Up @@ -499,9 +503,7 @@ func connectCommand(s *Sqlcmd, args []string, line uint) error {
connect.AuthenticationMethod = *authenticationMethod

// Set server name as the first positional argument
if len(commandArgs) > 0 {
connect.ServerName, _ = resolveArgumentVariables(s, []rune(commandArgs[0]), false)
}
connect.ServerName, _ = resolveArgumentVariables(s, []rune(commandArgs[0]), false)

// If no user name is provided we switch to integrated auth
_ = s.ConnectDb(&connect, s.lineIo == nil)
Expand Down
24 changes: 24 additions & 0 deletions pkg/sqlcmd/connect_command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package sqlcmd

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConnectCommandWithoutServerName(t *testing.T) {
for _, line := range []string{":connect", ":CONNECT ", ":connect -- no server"} {
t.Run(line, func(t *testing.T) {
s := New(nil, "", InitializeVariables(false))
cmd, args := s.Cmd.matchCommand(line)
require.NotNil(t, cmd, "line should match a command")
var err error
assert.NotPanics(t, func() { err = cmd.action(s, args, 1) })
assert.EqualError(t, err, InvalidCommandError("CONNECT", 1).Error())
})
}
}