Browse Source

Refactor url_checker.go

renovate/configure
Yota Toyama 8 years ago
parent
commit
3f8dc77a24
  1. 24
      url_checker.go
  2. 28
      url_checker_test.go

24
url_checker.go

@ -22,20 +22,14 @@ func newURLChecker(t time.Duration, d string, s semaphore) urlChecker {
}
func (c urlChecker) Check(u string, f string) error {
u, err := c.resolveURL(u, f)
u, local, err := c.resolveURL(u, f)
if err != nil {
return err
}
uu, err := url.Parse(u)
if err != nil {
return err
}
if uu.Scheme == "" {
_, err := os.Stat(uu.Path)
if local {
_, err := os.Stat(u)
return err
}
@ -67,24 +61,24 @@ func (c urlChecker) CheckMany(us []string, f string, rc chan<- urlResult) {
close(rc)
}
func (c urlChecker) resolveURL(u string, f string) (string, error) {
func (c urlChecker) resolveURL(u string, f string) (string, bool, error) {
uu, err := url.Parse(u)
if err != nil {
return "", err
return "", false, err
}
if uu.Scheme != "" {
return u, nil
return u, false, nil
}
if !path.IsAbs(uu.Path) {
return path.Join(path.Dir(f), uu.Path), nil
return path.Join(path.Dir(f), uu.Path), true, nil
}
if c.documentRoot == "" {
return "", errors.New("document root directory is not specified")
return "", false, errors.New("document root directory is not specified")
}
return path.Join(c.documentRoot, uu.Path), nil
return path.Join(c.documentRoot, uu.Path), true, nil
}

28
url_checker_test.go

@ -47,21 +47,25 @@ func TestURLCheckerCheckMany(t *testing.T) {
func TestURLCheckerResolveURL(t *testing.T) {
f := newURLChecker(0, "", newSemaphore(1024))
for _, c := range []struct{ source, target string }{
{"foo", "foo"},
{"https://google.com", "https://google.com"},
for _, c := range []struct {
source, target string
local bool
}{
u, err := f.resolveURL(c.source, "foo.md")
{"foo", "foo", true},
{"https://google.com", "https://google.com", false},
} {
u, local, err := f.resolveURL(c.source, "foo.md")
assert.Equal(t, nil, err)
assert.Equal(t, c.target, u)
assert.Equal(t, c.local, local)
}
}
func TestURLCheckerResolveURLWithAbsolutePath(t *testing.T) {
f := newURLChecker(0, "", newSemaphore(1024))
u, err := f.resolveURL("/foo", "foo.md")
u, _, err := f.resolveURL("/foo", "foo.md")
assert.NotEqual(t, nil, err)
assert.Equal(t, "", u)
@ -70,14 +74,18 @@ func TestURLCheckerResolveURLWithAbsolutePath(t *testing.T) {
func TestURLCheckerResolveURLWithDocumentRoot(t *testing.T) {
f := newURLChecker(0, "foo", newSemaphore(1024))
for _, c := range []struct{ source, target string }{
{"foo", "foo"},
{"https://google.com", "https://google.com"},
{"/foo", "foo/foo"},
for _, c := range []struct {
source, target string
local bool
}{
{"foo", "foo", true},
{"https://google.com", "https://google.com", false},
{"/foo", "foo/foo", true},
} {
u, err := f.resolveURL(c.source, "foo.md")
u, local, err := f.resolveURL(c.source, "foo.md")
assert.Equal(t, nil, err)
assert.Equal(t, c.target, u)
assert.Equal(t, c.local, local)
}
}

Loading…
Cancel
Save