Skip to content

Commit f0b9f3b

Browse files
committed
Adding unit test coverage
1 parent 0e6223f commit f0b9f3b

File tree

10 files changed

+87
-120
lines changed

10 files changed

+87
-120
lines changed

cmd/modern/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
package main
1313

1414
import (
15-
legacyCmd "github.com/microsoft/go-sqlcmd/cmd/sqlcmd"
1615
"github.com/microsoft/go-sqlcmd/internal"
1716
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
1817
"github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency"
@@ -23,8 +22,11 @@ import (
2322
"github.com/microsoft/go-sqlcmd/internal/pal"
2423
"github.com/microsoft/go-sqlcmd/pkg/sqlcmd"
2524
"github.com/spf13/cobra"
26-
"os"
2725
"path"
26+
27+
"os"
28+
29+
legacyCmd "github.com/microsoft/go-sqlcmd/cmd/sqlcmd"
2830
)
2931

3032
var rootCmd *Root

cmd/modern/root/install/mssql-base.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,22 @@
44
package install
55

66
import (
7-
"encoding/base64"
8-
"encoding/json"
97
"fmt"
10-
"github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency"
11-
"github.com/microsoft/go-sqlcmd/internal/tools"
12-
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest"
13-
"os"
148
"runtime"
159
"strings"
1610

1711
"github.com/microsoft/go-sqlcmd/cmd/modern/root/open"
18-
1912
"github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
2013
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
14+
"github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency"
2115
"github.com/microsoft/go-sqlcmd/internal/config"
2216
"github.com/microsoft/go-sqlcmd/internal/container"
2317
"github.com/microsoft/go-sqlcmd/internal/output"
2418
"github.com/microsoft/go-sqlcmd/internal/pal"
2519
"github.com/microsoft/go-sqlcmd/internal/secret"
2620
"github.com/microsoft/go-sqlcmd/internal/sql"
21+
"github.com/microsoft/go-sqlcmd/internal/tools"
22+
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest"
2723
"github.com/spf13/viper"
2824
)
2925

@@ -104,10 +100,18 @@ func (c *MssqlBase) AddFlags(
104100
Usage: "Context name (a default context name will be created if not provided)",
105101
})
106102

103+
// BUG(stuartpa): Make this a hidden flag so we don't break existing usage
107104
addFlag(cmdparser.FlagOptions{
108105
String: &c.defaultDatabase,
109106
Name: "user-database",
110107
Shorthand: "u",
108+
Usage: "[DEPRECATED use --database] Create a user database and set it as the default for login",
109+
})
110+
111+
addFlag(cmdparser.FlagOptions{
112+
String: &c.defaultDatabase,
113+
Name: "database",
114+
Shorthand: "d",
111115
Usage: "Create a user database and set it as the default for login",
112116
})
113117

@@ -219,25 +223,26 @@ func (c *MssqlBase) AddFlags(
219223
Usage: "Port (next available port from 1433 upwards used by default)",
220224
})
221225

226+
// BUG(stuartpa): Make this a hidden flag so we don't break existing usage
222227
addFlag(cmdparser.FlagOptions{
223228
String: &c.useDatabaseUrl,
224229
DefaultString: "",
225230
Name: "using",
226-
Usage: "Download and use database from .bak/.bacpac/.mdf/.7z URL",
231+
Usage: fmt.Sprintf("[DEPRECATED use --use] Download %q and use database", ingest.ValidFileExtensions()),
227232
})
228233

229234
addFlag(cmdparser.FlagOptions{
230235
String: &c.useDatabaseUrl,
231236
DefaultString: "",
232237
Name: "use",
233-
Usage: "Download and use database from .bak/.bacpac/.mdf/.7z URL",
238+
Usage: fmt.Sprintf("Download %q and use database", ingest.ValidFileExtensions()),
234239
})
235240

236241
addFlag(cmdparser.FlagOptions{
237242
String: &c.useMechanism,
238243
DefaultString: "",
239244
Name: "use-mechanism",
240-
Usage: "Mechanism to use to make --use database online (attach, restore, dacfx)",
245+
Usage: "Mechanism to use to bring database online (attach, restore, dacfx)",
241246
})
242247

243248
addFlag(cmdparser.FlagOptions{
@@ -321,7 +326,7 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
321326
[]string{
322327
fmt.Sprintf(
323328
"--using must be a path to a file with a %q extension",
324-
strings.Join(useDatabase.ValidFileExtensions(), ", "),
329+
ingest.ValidFileExtensions(),
325330
),
326331
},
327332
"%q is not a valid file extension for --using flag", useDatabase.UserProvidedFileExt())
@@ -500,17 +505,6 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
500505
output.Fatalf(tool.HowToInstall())
501506
}
502507

503-
if c.openFile != "" {
504-
args = append(args, c.openFile)
505-
506-
a := os.Args[1:]
507-
data, _ := json.Marshal(&a)
508-
509-
fmt.Printf("The URL for sharing this `sqlcmd create` is:\n\n")
510-
sEnc := base64.StdEncoding.EncodeToString(data)
511-
fmt.Printf("sqlcmd://%s\n", sEnc)
512-
}
513-
514508
_, err := tool.Run(args)
515509
c.CheckErr(err)
516510
}

cmd/modern/root/use.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Use struct {
2525
func (c *Use) DefineCommand(...cmdparser.CommandOptions) {
2626
options := cmdparser.CommandOptions{
2727
Use: "use",
28-
Short: "Download (into container) and use database",
28+
Short: fmt.Sprintf("Download database (into container) (%s)", ingest.ValidFileExtensions()),
2929
Examples: []cmdparser.ExampleOptions{
3030
{
3131
Description: "Download AdventureWorksLT into container for current context, set as default database",
@@ -46,9 +46,8 @@ func (c *Use) DefineCommand(...cmdparser.CommandOptions) {
4646
String: &c.useMechanism,
4747
DefaultString: "",
4848
Name: "use-mechanism",
49-
Usage: "Mechanism to use to make --use database online (attach, restore, dacfx)",
49+
Usage: "Mechanism to use to bring database online (attach, restore, dacfx)",
5050
})
51-
5251
}
5352

5453
func (c *Use) run() {
@@ -87,7 +86,7 @@ func (c *Use) run() {
8786
useDatabase.CopyToContainer(id)
8887

8988
if useDatabase.IsExtractionNeeded() {
90-
fmt.Println("Extracting database from file")
89+
output.Infof("Extracting files from %q", useDatabase.UrlFilename())
9190
useDatabase.Extract()
9291
}
9392

internal/uri/uri.go

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package uri
22

33
import (
4-
"path"
54
"path/filepath"
65
"strings"
76
)
@@ -14,30 +13,14 @@ func (u Uri) IsLocal() bool {
1413
}
1514
}
1615

16+
// actualUrl returns the url without the query string
1717
func (u Uri) ActualUrl() string {
18-
urlEndIdx := strings.LastIndex(u.uri, ".bak")
19-
if urlEndIdx == -1 {
20-
urlEndIdx = strings.LastIndex(u.uri, ".mdf")
21-
}
22-
if urlEndIdx != -1 {
23-
return u.uri[0:(urlEndIdx + 4)]
24-
}
25-
26-
if urlEndIdx == -1 {
27-
urlEndIdx = strings.LastIndex(u.uri, ".7z")
28-
if urlEndIdx != -1 {
29-
return u.uri[0:(urlEndIdx + 3)]
30-
}
31-
}
32-
33-
if urlEndIdx == -1 {
34-
urlEndIdx = strings.LastIndex(u.uri, ".bacpac")
35-
if urlEndIdx != -1 {
36-
return u.uri[0:(urlEndIdx + 7)]
37-
}
18+
terminator := strings.LastIndex(u.uri, ",")
19+
if terminator != -1 {
20+
return u.uri[0:terminator]
21+
} else {
22+
return u.uri
3823
}
39-
40-
return u.uri
4124
}
4225

4326
func (u Uri) Scheme() string {
@@ -66,50 +49,8 @@ func (u Uri) ParseDbName() string {
6649
panic("uri is empty")
6750
}
6851

69-
dbToken := path.Base(u.url.Path)
70-
if dbToken != "." && dbToken != "/" {
71-
lastIdx := strings.LastIndex(dbToken, ".bak")
72-
if lastIdx == -1 {
73-
lastIdx = strings.LastIndex(dbToken, ".mdf")
74-
}
75-
if lastIdx != -1 {
76-
//Get file name without extension
77-
fileName := dbToken[0:lastIdx]
78-
lastIdx += 5
79-
if lastIdx >= len(dbToken) {
80-
return fileName
81-
}
82-
//Return database name if it was specified
83-
return dbToken[lastIdx:]
84-
} else {
85-
lastIdx := strings.LastIndex(dbToken, ".bacpac")
86-
if lastIdx != -1 {
87-
//Get file name without extension
88-
fileName := dbToken[0:lastIdx]
89-
lastIdx += 8
90-
if lastIdx >= len(dbToken) {
91-
return fileName
92-
}
93-
//Return database name if it was specified
94-
return dbToken[lastIdx:]
95-
} else {
96-
lastIdx := strings.LastIndex(dbToken, ".7z")
97-
if lastIdx != -1 {
98-
//Get file name without extension
99-
fileName := dbToken[0:lastIdx]
100-
lastIdx += 4
101-
if lastIdx >= len(dbToken) {
102-
return fileName
103-
}
104-
//Return database name if it was specified
105-
return dbToken[lastIdx:]
106-
}
107-
}
108-
}
109-
}
110-
111-
fileName := filepath.Base(u.uri)
112-
return fileName[:len(fileName)-len(filepath.Ext(fileName))]
52+
// TODO: Reimplement
53+
return ""
11354
}
11455

11556
func (u Uri) GetDbNameAsIdentifier() string {

internal/uri/uri_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,41 @@ func TestExtractUrl(t *testing.T) {
2424
}
2525
}
2626

27+
func TestParseDbName(t *testing.T) {
28+
type test struct {
29+
inputURL string
30+
expectedURL string
31+
}
32+
33+
tests := []test{
34+
{"https://example.com/testdb.bak,myDbName", "myDbName"},
35+
{"https://example.com/testdb.bak", "testdb"},
36+
{"https://example.com/test.foo", "test"},
37+
{"https://example.com/test.foo,test", "test"},
38+
{"https://example.com/test.7z,tql_name", "tsql_name"},
39+
{"https://example.com/test.mdf,tsql_name?foo=bar", "tsql_name"},
40+
{"https://example.com/test.mdf,tsql_name#link?foo=bar", "tsql_name"},
41+
{"https://example.com/test.mdf?foo=bar", "test"},
42+
{"https://example.com/test.mdf#link?foo=bar", "test"},
43+
{"https://example.com/test,test", "test"},
44+
{"https://example.com,", ""},
45+
{"https://example.com", ""},
46+
{"test.7z,tql_name", "tsql_name"},
47+
{"test.mdf,tql_name", "tsql_name"},
48+
{"test.mdf", "test"},
49+
{"c:\test.mdf", "test"},
50+
{"c:\test.mdf,tsql_name", "tsql_name"},
51+
{"file://test.mdf,tsql_name", "tsql_name"},
52+
{"file://test.mdf", "test"},
53+
}
54+
55+
for _, testcase := range tests {
56+
u := NewUri(testcase.inputURL)
57+
assert.Equal(t, testcase.expectedURL, u.ParseDbName(),
58+
"Extracted DB Name does not match expected DB Name")
59+
}
60+
}
61+
2762
func TestGetDbNameIfExists(t *testing.T) {
2863

2964
type test struct {

pkg/mssqlcontainer/ingest/extract/7zip.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package extract
22

33
import (
4-
"fmt"
54
"github.com/microsoft/go-sqlcmd/internal/container"
65
"path/filepath"
76
"regexp"
8-
"strings"
97
)
108

119
type sevenZip struct {
@@ -43,8 +41,6 @@ func (e *sevenZip) Extract(srcFile string, destFolder string) (string, string) {
4341
"/var/opt/mssql/backup/" + srcFile,
4442
})
4543

46-
fmt.Println(stdout)
47-
4844
var mdfFile string
4945
var ldfFile string
5046

@@ -95,6 +91,5 @@ func extractPaths(input string) []string {
9591
for _, match := range matches {
9692
paths = append(paths, match[1])
9793
}
98-
fmt.Println("Path: " + strings.Join(paths, ", "))
9994
return paths
10095
}

pkg/mssqlcontainer/ingest/extract/factory.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package extract
22

33
import (
4-
"fmt"
54
"github.com/microsoft/go-sqlcmd/internal/container"
65
)
76

87
func NewExtractor(fileExtension string, controller *container.Controller) Extractor {
9-
fmt.Println("NewExtractor: " + fileExtension)
108
for _, extractor := range extractors {
119
for _, ext := range extractor.FileTypes() {
1210
if ext == fileExtension {

pkg/mssqlcontainer/ingest/factory.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package ingest
33
import (
44
"github.com/microsoft/go-sqlcmd/internal/container"
55
"github.com/microsoft/go-sqlcmd/internal/uri"
6+
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/extract"
67
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/location"
78
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
9+
"strings"
810
)
911

1012
func NewIngest(databaseUri string, controller *container.Controller, options IngestOptions) Ingest {
@@ -17,3 +19,17 @@ func NewIngest(databaseUri string, controller *container.Controller, options Ing
1719
mechanism: mechanism.NewMechanism(uri.FileExtension(), options.Mechanism, controller),
1820
}
1921
}
22+
23+
func ValidFileExtensions() string {
24+
var extensions []string
25+
26+
for _, m := range mechanism.FileTypes() {
27+
extensions = append(extensions, m)
28+
}
29+
30+
for _, e := range extract.FileTypes() {
31+
extensions = append(extensions, e)
32+
}
33+
34+
return strings.Join(extensions, ", ")
35+
}

0 commit comments

Comments
 (0)