mirror of
https://github.com/thegeeklab/url-parser.git
synced 2024-11-29 00:10:35 +00:00
add minimal testing
This commit is contained in:
parent
931dc63d59
commit
5862f84e3c
28
.drone.star
28
.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',
|
||||
|
20
.drone.yml
20
.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 }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> 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
|
||||
|
||||
...
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,3 +14,4 @@
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
/url-parser
|
||||
coverage.txt
|
||||
|
@ -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.
|
||||
|
@ -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 {
|
||||
|
27
commands/commands_test.go
Normal file
27
commands/commands_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
|
39
commands/password_test.go
Normal file
39
commands/password_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
47
commands/path_test.go
Normal file
47
commands/path_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
39
commands/run_test.go
Normal file
39
commands/run_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
39
commands/scheme_test.go
Normal file
39
commands/scheme_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
39
commands/user_test.go
Normal file
39
commands/user_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
1
go.mod
1
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
|
||||
|
2
go.sum
2
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=
|
||||
|
Loading…
Reference in New Issue
Block a user