Browse Source

Fix absolute path resolution

renovate/configure
Yota Toyama 8 years ago
parent
commit
cd9d305cdd
  1. 19
      examples/html.feature
  2. 33
      url_checker.go
  3. 6
      url_checker_test.go

19
examples/html.feature

@ -76,3 +76,22 @@ Feature: HTML
"""
When I run `liche foo.html`
Then the exit status should be 1
Scenario: Set document root to a sub directory
Given a directory named "sub"
And a file named "sub/foo.html" with:
"""
<!DOCTYPE html>
<html>
<head>
<title>My title</title>
</head>
<body>
<div>
<a href="/foo.html">Google</a>
</div>
</body>
</html>
"""
When I successfully run `liche --document-root sub sub/foo.html`
Then the stderr should contain exactly ""

33
url_checker.go

@ -5,7 +5,6 @@ import (
"net/url"
"os"
"path"
"strings"
"sync"
"time"
@ -23,7 +22,7 @@ func newURLChecker(t time.Duration, d string, s semaphore) urlChecker {
}
func (c urlChecker) Check(u string, f string) error {
u, err := c.resolveURL(u)
u, err := c.resolveURL(u, f)
if err != nil {
return err
@ -36,7 +35,8 @@ func (c urlChecker) Check(u string, f string) error {
}
if uu.Scheme == "" {
return checkRelativePath(u, f)
_, err := os.Stat(uu.Path)
return err
}
c.semaphore.Request()
@ -67,19 +67,24 @@ func (c urlChecker) CheckMany(us []string, f string, rc chan<- urlResult) {
close(rc)
}
func (c urlChecker) resolveURL(u string) (string, error) {
abs := strings.HasPrefix(u, "/")
func (c urlChecker) resolveURL(u string, f string) (string, error) {
uu, err := url.Parse(u)
if abs && c.documentRoot != "" {
return path.Join(c.documentRoot, u), nil
} else if abs {
return "", errors.New("document root directory is not specified")
if err != nil {
return "", err
}
return u, nil
}
if uu.Scheme != "" {
return u, nil
}
func checkRelativePath(p string, f string) error {
_, err := os.Stat(path.Join(path.Dir(f), p))
return err
if !path.IsAbs(uu.Path) {
return path.Join(path.Dir(f), uu.Path), nil
}
if c.documentRoot == "" {
return "", errors.New("document root directory is not specified")
}
return path.Join(c.documentRoot, uu.Path), nil
}

6
url_checker_test.go

@ -51,7 +51,7 @@ func TestURLCheckerResolveURL(t *testing.T) {
{"foo", "foo"},
{"https://google.com", "https://google.com"},
} {
u, err := f.resolveURL(c.source)
u, err := f.resolveURL(c.source, "foo.md")
assert.Equal(t, nil, err)
assert.Equal(t, c.target, u)
@ -61,7 +61,7 @@ func TestURLCheckerResolveURL(t *testing.T) {
func TestURLCheckerResolveURLWithAbsolutePath(t *testing.T) {
f := newURLChecker(0, "", newSemaphore(1024))
u, err := f.resolveURL("/foo")
u, err := f.resolveURL("/foo", "foo.md")
assert.NotEqual(t, nil, err)
assert.Equal(t, "", u)
@ -75,7 +75,7 @@ func TestURLCheckerResolveURLWithDocumentRoot(t *testing.T) {
{"https://google.com", "https://google.com"},
{"/foo", "foo/foo"},
} {
u, err := f.resolveURL(c.source)
u, err := f.resolveURL(c.source, "foo.md")
assert.Equal(t, nil, err)
assert.Equal(t, c.target, u)

Loading…
Cancel
Save