diff --git a/.drone.star b/.drone.star index f34ba4b..bb32665 100644 --- a/.drone.star +++ b/.drone.star @@ -38,7 +38,7 @@ def testing(): 'name': 'test', 'image': 'golang:1.12', 'commands': [ - 'go test -cover ./...' + 'go test -race -coverprofile=coverage.txt -covermode=atomic ./...' ], } ], @@ -125,6 +125,23 @@ def notification(): 'type': 'docker', 'name': 'notification', 'steps': [ + { + 'name': 'coverage', + 'image': 'plugins/codevoc', + 'settings': { + 'token': { + 'from_secret': 'codecov_token', + }, + 'files':[ + 'coverage.txt' + ] + }, + 'when': { + 'status': [ + 'success', + ] + } + }, { 'name': 'matrix', 'image': 'plugins/matrix', @@ -143,13 +160,20 @@ def notification(): 'from_secret': 'matrix_username', }, }, + 'when': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**', + ], + } }, ], 'depends_on': [], 'trigger': { 'ref': [ 'refs/heads/master', - 'refs/tags/**' + 'refs/tags/**', + 'refs/pull/**', ], 'status': [ 'success', diff --git a/.drone.yml b/.drone.yml index b3b22e7..5ea467b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -16,7 +16,7 @@ steps: - name: test image: golang:1.12 commands: - - go test -cover ./... + - go test -race -coverprofile=coverage.txt -covermode=atomic ./... trigger: ref: @@ -92,6 +92,17 @@ platform: arch: amd64 steps: +- name: coverage + image: plugins/codevoc + settings: + files: + - coverage.txt + token: + from_secret: codecov_token + when: + status: + - success + - name: matrix image: plugins/matrix settings: @@ -104,11 +115,16 @@ steps: template: "Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}" username: from_secret: matrix_username + when: + ref: + - refs/heads/master + - refs/tags/** trigger: ref: - refs/heads/master - refs/tags/** + - refs/pull/** status: - success - failure @@ -118,6 +134,6 @@ depends_on: --- kind: signature -hmac: 84fb5966acf998f5efffbec5d0a279838b7912b578970668eb8fccd66c5ddc4e +hmac: b2a4aa46d907139e996670fcad212137ba51b1d0aba5a56b80644f5337f0617f ... diff --git a/.gitignore b/.gitignore index a3742dd..bfd2efd 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ /url-parser +coverage.txt diff --git a/README.md b/README.md index e5eb95a..ac92484 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # url-parser [![Build Status](https://img.shields.io/drone/build/xoxys/url-parser?logo=drone)](https://cloud.drone.io/xoxys/url-parser) +[![Codecov](https://img.shields.io/codecov/c/github/xoxys/url-parser)](https://codecov.io/gh/xoxys/url-parser) [![License: MIT](https://img.shields.io/github/license/xoxys/url-parser)](LICENSE) Inspired by [herloct/url-parser](https://github.com/herloct/url-parser), a simple command-line utility for parsing URLs. diff --git a/commands/commands.go b/commands/commands.go index 8e8b598..1a494ef 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -5,11 +5,10 @@ import ( "strings" "github.com/sirupsen/logrus" - "github.com/urfave/cli/v2" ) -func parseURL(c *cli.Context) *url.URL { - urlString := strings.TrimSpace(c.String("url")) +func parseURL(raw string) *url.URL { + urlString := strings.TrimSpace(raw) url, err := url.Parse(urlString) if err != nil { diff --git a/commands/commands_test.go b/commands/commands_test.go new file mode 100644 index 0000000..20b7a15 --- /dev/null +++ b/commands/commands_test.go @@ -0,0 +1,27 @@ +package commands + +import "testing" + +type TestParseData struct { + urlString string + expected string +} + +func TestParseURL(t *testing.T) { + urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment" + + tables := []TestParseData{ + TestParseData{ + urlString: urlString, + expected: urlString, + }, + } + + for _, table := range tables { + result := parseURL(urlString) + + if result.String() != table.expected { + t.Fatalf("URL `%v`, should be `%v`", result, table.expected) + } + } +} diff --git a/commands/password.go b/commands/password.go index 0ae2755..e46bd86 100644 --- a/commands/password.go +++ b/commands/password.go @@ -8,7 +8,7 @@ import ( // Password prints out the password part from url func Password(ctx *cli.Context) error { - parts := parseURL(ctx) + parts := parseURL(ctx.String("url")) if parts.User != nil { pw, _ := parts.User.Password() @@ -16,5 +16,6 @@ func Password(ctx *cli.Context) error { fmt.Println(pw) } } + return nil } diff --git a/commands/password_test.go b/commands/password_test.go new file mode 100644 index 0000000..63c8db0 --- /dev/null +++ b/commands/password_test.go @@ -0,0 +1,39 @@ +package commands + +import ( + "flag" + "strings" + "testing" + + "github.com/kami-zh/go-capturer" + "github.com/urfave/cli/v2" +) + +type TestPasswordData struct { + urlString string + expected string +} + +func TestPassword(t *testing.T) { + urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment" + + tables := []TestPasswordData{ + TestPasswordData{ + urlString: urlString, + expected: "pass", + }, + } + + for _, table := range tables { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + set.String("url", table.urlString, "test url") + + c := cli.NewContext(app, set, nil) + result := strings.TrimSpace(capturer.CaptureStdout(func() { Password(c) })) + + if result != table.expected { + t.Fatalf("URL password `%v`, should be `%v`", result, table.expected) + } + } +} diff --git a/commands/path.go b/commands/path.go index 7b997c7..f6997d1 100644 --- a/commands/path.go +++ b/commands/path.go @@ -21,7 +21,7 @@ func PathFlags() []cli.Flag { // Path prints out the path part from url func Path(ctx *cli.Context) error { - parts := parseURL(ctx) + parts := parseURL(ctx.String("url")) i := ctx.Int("path-index") if len(parts.Path) > 0 { diff --git a/commands/path_test.go b/commands/path_test.go new file mode 100644 index 0000000..f5f77d6 --- /dev/null +++ b/commands/path_test.go @@ -0,0 +1,47 @@ +package commands + +import ( + "flag" + "strings" + "testing" + + "github.com/kami-zh/go-capturer" + "github.com/urfave/cli/v2" +) + +type TestPathData struct { + urlString string + pathIndex int + expected string +} + +func TestPath(t *testing.T) { + urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment" + + tables := []TestPathData{ + TestPathData{ + urlString: urlString, + pathIndex: -1, + expected: "/path/to", + }, + TestPathData{ + urlString: urlString, + pathIndex: 0, + expected: "path", + }, + } + + for _, table := range tables { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + set.String("url", table.urlString, "test url") + set.Int("path-index", table.pathIndex, "index") + + c := cli.NewContext(app, set, nil) + result := strings.TrimSpace(capturer.CaptureStdout(func() { Path(c) })) + + if result != table.expected { + t.Fatalf("URL path `%v`, should be `%v`", result, table.expected) + } + } +} diff --git a/commands/run.go b/commands/run.go index 05ab789..dd174b8 100644 --- a/commands/run.go +++ b/commands/run.go @@ -8,7 +8,7 @@ import ( // Run default command and print out full url func Run(ctx *cli.Context) error { - parts := parseURL(ctx) + parts := parseURL(ctx.String("url")) if len(parts.String()) > 0 { fmt.Println(parts) diff --git a/commands/run_test.go b/commands/run_test.go new file mode 100644 index 0000000..12ee8d7 --- /dev/null +++ b/commands/run_test.go @@ -0,0 +1,39 @@ +package commands + +import ( + "flag" + "strings" + "testing" + + "github.com/kami-zh/go-capturer" + "github.com/urfave/cli/v2" +) + +type TestRunData struct { + urlString string + expected string +} + +func TestRun(t *testing.T) { + urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment" + + tables := []TestRunData{ + TestRunData{ + urlString: urlString, + expected: urlString, + }, + } + + for _, table := range tables { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + set.String("url", table.urlString, "test url") + + c := cli.NewContext(app, set, nil) + result := strings.TrimSpace(capturer.CaptureStdout(func() { Run(c) })) + + if result != table.expected { + t.Fatalf("URL `%v`, should be `%v`", result, table.expected) + } + } +} diff --git a/commands/scheme.go b/commands/scheme.go index 1d57e34..d8ddd2b 100644 --- a/commands/scheme.go +++ b/commands/scheme.go @@ -8,7 +8,7 @@ import ( // Scheme prints out the scheme part from the url func Scheme(ctx *cli.Context) error { - parts := parseURL(ctx) + parts := parseURL(ctx.String("url")) if len(parts.Scheme) > 0 { fmt.Println(parts.Scheme) diff --git a/commands/scheme_test.go b/commands/scheme_test.go new file mode 100644 index 0000000..ab40f25 --- /dev/null +++ b/commands/scheme_test.go @@ -0,0 +1,39 @@ +package commands + +import ( + "flag" + "strings" + "testing" + + "github.com/kami-zh/go-capturer" + "github.com/urfave/cli/v2" +) + +type TestSchemeData struct { + urlString string + expected string +} + +func TestScheme(t *testing.T) { + urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment" + + tables := []TestSchemeData{ + TestSchemeData{ + urlString: urlString, + expected: "postgres", + }, + } + + for _, table := range tables { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + set.String("url", table.urlString, "test url") + + c := cli.NewContext(app, set, nil) + result := strings.TrimSpace(capturer.CaptureStdout(func() { Scheme(c) })) + + if result != table.expected { + t.Fatalf("URL scheme `%v`, should be `%v`", result, table.expected) + } + } +} diff --git a/commands/user.go b/commands/user.go index d3eec56..bcd6d3f 100644 --- a/commands/user.go +++ b/commands/user.go @@ -8,7 +8,7 @@ import ( // User prints out the user part from url func User(ctx *cli.Context) error { - parts := parseURL(ctx) + parts := parseURL(ctx.String("url")) if parts.User != nil { if len(parts.User.Username()) > 0 { diff --git a/commands/user_test.go b/commands/user_test.go new file mode 100644 index 0000000..ec727a0 --- /dev/null +++ b/commands/user_test.go @@ -0,0 +1,39 @@ +package commands + +import ( + "flag" + "strings" + "testing" + + "github.com/kami-zh/go-capturer" + "github.com/urfave/cli/v2" +) + +type TestUserData struct { + urlString string + expected string +} + +func TestUser(t *testing.T) { + urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment" + + tables := []TestUserData{ + TestUserData{ + urlString: urlString, + expected: "user", + }, + } + + for _, table := range tables { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + set.String("url", table.urlString, "test url") + + c := cli.NewContext(app, set, nil) + result := strings.TrimSpace(capturer.CaptureStdout(func() { User(c) })) + + if result != table.expected { + t.Fatalf("URL user `%v`, should be `%v`", result, table.expected) + } + } +} diff --git a/go.mod b/go.mod index 2dece06..266f75d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/xoxys/url-parser go 1.13 require ( + github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d github.com/sirupsen/logrus v1.4.2 github.com/urfave/cli v1.22.2 github.com/urfave/cli/v2 v2.1.1 diff --git a/go.sum b/go.sum index d108ce7..4b7958e 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d h1:cVtBfNW5XTHiKQe7jDaDBSh/EVM4XLPutLAGboIXuM0= +github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=