Skip to content

Commit e1d7415

Browse files
committed
PR feedback
1 parent 1944095 commit e1d7415

32 files changed

+134
-278
lines changed

cmd/modern/root/open/ads.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/microsoft/go-sqlcmd/internal/config"
1111
"github.com/microsoft/go-sqlcmd/internal/container"
1212
"github.com/microsoft/go-sqlcmd/internal/tools"
13+
"runtime"
14+
"strings"
1315
)
1416

1517
// Ads implements the `sqlcmd open ads` command. It opens
@@ -70,19 +72,27 @@ func (c *Ads) ensureContainerIsRunning(endpoint sqlconfig.Endpoint) {
7072
}
7173

7274
// launchAds launches the Azure Data Studio using the specified server and username.
73-
func (c *Ads) launchAds(localhost string, port int, username string) {
75+
func (c *Ads) launchAds(host string, port int, username string) {
7476
output := c.Output()
7577
args := []string{
7678
"-r",
7779
fmt.Sprintf(
7880
"--server=%s", fmt.Sprintf(
7981
"%s,%d",
80-
localhost,
82+
host,
8183
port)),
8284
}
8385

8486
if username != "" {
85-
args = append(args, fmt.Sprintf("--user=%s", username))
87+
88+
// Here's a fun SQL Server behavior - it allows you to create database
89+
// and login names that include the " character. SSMS escapes those
90+
// with \" when invoking ADS on the command line, we do the same here
91+
args = append(args, fmt.Sprintf("--user=%s", strings.Replace(username, `"`, `\"`, -1)))
92+
} else {
93+
if runtime.GOOS == "windows" {
94+
args = append(args, "--integrated")
95+
}
8696
}
8797

8898
tool := tools.NewTool("ads")

cmd/modern/root/open/ads_darwin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
66
)
77

8+
// Type Ads is used to implement the "open ads" which launches Azure
9+
// Data Studio and establishes a connection to the SQL Server for the current
10+
// context
811
type Ads struct {
912
cmdparser.Cmd
1013
}

cmd/modern/root/open/ads_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
66
)
77

8+
// Type Ads is used to implement the "open ads" which launches Azure
9+
// Data Studio and establishes a connection to the SQL Server for the current
10+
// context
811
type Ads struct {
912
cmdparser.Cmd
1013
}

cmd/modern/root/open/ads_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"github.com/microsoft/go-sqlcmd/internal/secret"
99
)
1010

11+
// Type Ads is used to implement the "open ads" which launches Azure
12+
// Data Studio and establishes a connection to the SQL Server for the current
13+
// context
1114
type Ads struct {
1215
cmdparser.Cmd
1316

cmd/modern/root/open/ads_windows_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,8 @@ func TestPersistCredentialForAds(t *testing.T) {
3232

3333
// Test if the correct target name is generated
3434
expectedTargetName := "Microsoft.SqlTools|itemtype:Profile|id:providerName:MSSQL|applicationName:azdata|authenticationType:SqlLogin|database:|server:localhost,1433|user:testuser"
35-
if ads.credential.TargetName != expectedTargetName {
36-
t.Errorf("Expected target name to be %s, got %s", expectedTargetName, ads.credential.TargetName)
37-
}
38-
39-
// Test if the correct username is set
40-
if ads.credential.UserName != user.BasicAuth.Username {
41-
t.Errorf("Expected username to be %s, got %s", user.BasicAuth.Username, ads.credential.UserName)
42-
}
35+
assert.Equal(t, ads.credential.TargetName, expectedTargetName, "Expected target name to be %s, got %s", expectedTargetName, ads.credential.TargetName)
36+
assert.Equal(t, ads.credential.UserName, user.BasicAuth.Username, "Expected username to be %s, got %s", user.BasicAuth.Username, ads.credential.UserName)
4337

4438
// Test if the password is decoded correctly
4539
decodedPassword := secret.DecodeAsUtf16(user.BasicAuth.Password, user.BasicAuth.PasswordEncrypted)

cmd/modern/root/start.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111

1212
type Start struct {
1313
cmdparser.Cmd
14-
15-
errorLogEntryToWaitFor string
1614
}
1715

1816
func (c *Start) DefineCommand(...cmdparser.CommandOptions) {
@@ -28,12 +26,6 @@ func (c *Start) DefineCommand(...cmdparser.CommandOptions) {
2826
}
2927

3028
c.Cmd.DefineCommand(options)
31-
c.AddFlag(cmdparser.FlagOptions{
32-
String: &c.errorLogEntryToWaitFor,
33-
DefaultString: "Recovery is complete",
34-
Name: "errorlog-wait-line",
35-
Usage: "Line in errorlog to wait for before exiting this process",
36-
})
3729
}
3830

3931
func (c *Start) run() {
@@ -56,17 +48,6 @@ func (c *Start) run() {
5648
)
5749
err := controller.ContainerStart(id)
5850
c.CheckErr(err)
59-
60-
// BUG(stuartpa): Even if we block here for the final entry in the errorlog
61-
// "Recovery Complete", a sqlcmd query will still fail for a few seconds
62-
// with:
63-
// sqlcmd query "SELECT @@version"
64-
// EOF
65-
// Error: EOF
66-
// I'm not sure what else to block here for before returning to ensure
67-
// a `sqlcmd query` will work
68-
controller.ContainerWaitForLogEntry(
69-
id, c.errorLogEntryToWaitFor)
7051
} else {
7152
output.FatalfWithHintExamples([][]string{
7253
{"Create new context with a sql container ", "sqlcmd create mssql"},

internal/config/config_test.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ func TestFindUniqueUserName(t *testing.T) {
176176
}
177177
for _, tt := range tests {
178178
t.Run(tt.name, func(t *testing.T) {
179-
if gotUniqueUserName := FindUniqueUserName(tt.args.name); gotUniqueUserName != tt.wantUniqueUserName {
180-
t.Errorf("FindUniqueUserName() = %v, want %v", gotUniqueUserName, tt.wantUniqueUserName)
181-
}
179+
gotUniqueUserName := FindUniqueUserName(tt.args.name)
180+
assert.Equal(t, gotUniqueUserName, tt.wantUniqueUserName, "FindUniqueUserName() = %v, want %v", gotUniqueUserName, tt.wantUniqueUserName)
182181
})
183182
}
184183
}
@@ -194,9 +193,8 @@ func TestGetUser(t *testing.T) {
194193
}
195194
for _, tt := range tests {
196195
t.Run(tt.name, func(t *testing.T) {
197-
if gotUser := GetUser(tt.args.name); !reflect.DeepEqual(gotUser, tt.wantUser) {
198-
t.Errorf("GetUser() = %v, want %v", gotUser, tt.wantUser)
199-
}
196+
gotUser := GetUser(tt.args.name)
197+
assert.Truef(t, reflect.DeepEqual(gotUser, tt.wantUser), "GetUser() = %v, want %v", gotUser, tt.wantUser)
200198
})
201199
}
202200
}
@@ -228,9 +226,8 @@ func TestUserExists(t *testing.T) {
228226
}
229227
for _, tt := range tests {
230228
t.Run(tt.name, func(t *testing.T) {
231-
if gotExists := UserNameExists(tt.args.name); gotExists != tt.wantExists {
232-
t.Errorf("UserNameExists() = %v, want %v", gotExists, tt.wantExists)
233-
}
229+
gotExists := UserNameExists(tt.args.name)
230+
assert.Equal(t, gotExists, tt.wantExists, "UserNameExists() = %v, want %v", gotExists, tt.wantExists)
234231
})
235232
}
236233
}
@@ -246,9 +243,8 @@ func TestUserNameExists(t *testing.T) {
246243
}
247244
for _, tt := range tests {
248245
t.Run(tt.name, func(t *testing.T) {
249-
if gotExists := UserNameExists(tt.args.name); gotExists != tt.wantExists {
250-
t.Errorf("UserNameExists() = %v, want %v", gotExists, tt.wantExists)
251-
}
246+
gotExists := UserNameExists(tt.args.name)
247+
assert.Equal(t, gotExists, tt.wantExists, "UserNameExists() = %v, want %v", gotExists, tt.wantExists)
252248
})
253249
}
254250
}
@@ -264,9 +260,8 @@ func Test_userOrdinal(t *testing.T) {
264260
}
265261
for _, tt := range tests {
266262
t.Run(tt.name, func(t *testing.T) {
267-
if gotOrdinal := userOrdinal(tt.args.name); gotOrdinal != tt.wantOrdinal {
268-
t.Errorf("userOrdinal() = %v, want %v", gotOrdinal, tt.wantOrdinal)
269-
}
263+
gotOrdinal := userOrdinal(tt.args.name)
264+
assert.Equal(t, gotOrdinal, tt.wantOrdinal, "userOrdinal() = %v, want %v", gotOrdinal, tt.wantOrdinal)
270265
})
271266
}
272267
}

internal/config/endpoint-container_test.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package config
55

66
import (
77
. "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
8+
"github.com/stretchr/testify/assert"
89
"strings"
910
"testing"
1011
)
@@ -14,33 +15,21 @@ import (
1415
func TestCurrentContextEndpointHasContainer(t *testing.T) {
1516
Clean()
1617

17-
defer func() {
18-
if r := recover(); r == nil {
19-
t.Errorf("The code did not panic")
20-
}
21-
}()
18+
defer func() { assert.NotNil(t, recover(), "The code did not panic as expected") }()
2219
CurrentContextEndpointHasContainer()
2320
}
2421

2522
func TestGetContainerId(t *testing.T) {
2623
Clean()
2724

28-
defer func() {
29-
if r := recover(); r == nil {
30-
t.Errorf("The code did not panic")
31-
}
32-
}()
25+
defer func() { assert.NotNil(t, recover(), "The code did not panic as expected") }()
3326
ContainerId()
3427
}
3528

3629
func TestGetContainerId2(t *testing.T) {
3730
Clean()
3831

39-
defer func() {
40-
if r := recover(); r == nil {
41-
t.Errorf("The code did not panic")
42-
}
43-
}()
32+
defer func() { assert.NotNil(t, recover(), "The code did not panic as expected") }()
4433

4534
AddEndpoint(Endpoint{
4635
AssetDetails: &AssetDetails{},
@@ -66,11 +55,7 @@ func TestGetContainerId2(t *testing.T) {
6655

6756
func TestGetContainerId3(t *testing.T) {
6857
Clean()
69-
defer func() {
70-
if r := recover(); r == nil {
71-
t.Errorf("The code did not panic")
72-
}
73-
}()
58+
defer func() { assert.NotNil(t, recover(), "The code did not panic as expected") }()
7459

7560
AddEndpoint(Endpoint{
7661
AssetDetails: &AssetDetails{

internal/config/endpoint_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
package config
55

6-
import "testing"
6+
import (
7+
"github.com/stretchr/testify/assert"
8+
"testing"
9+
)
710

811
func TestEndpointExists(t *testing.T) {
9-
defer func() {
10-
if r := recover(); r == nil {
11-
t.Errorf("The code did not panic")
12-
}
13-
}()
12+
defer func() { assert.NotNil(t, recover(), "The code did not panic as expected") }()
1413
EndpointExists("")
1514
}

internal/config/error_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ package config
55

66
import (
77
"errors"
8+
"github.com/stretchr/testify/assert"
89
"testing"
910
)
1011

1112
func Test_checkErr(t *testing.T) {
12-
defer func() {
13-
if r := recover(); r == nil {
14-
t.Errorf("The code did not panic")
15-
}
16-
}()
13+
defer func() { assert.NotNil(t, recover(), "The code did not panic as expected") }()
1714
checkErr(errors.New("verify error handler"))
1815
}

0 commit comments

Comments
 (0)