Browse Source

Merge branch 'relative-path'

Close #4.
renovate/configure
Yota Toyama 8 years ago
parent
commit
5c8f951d01
  1. 36
      examples/relative_path.feature
  2. 4
      file_checker.go
  3. 4
      file_checker_test.go
  4. 26
      url_checker.go

36
examples/relative_path.feature

@ -0,0 +1,36 @@
Feature: Relative paths
Scenario: Check a markdown file
Given a file named "foo.md" with:
"""
[bar](bar.md)
"""
And a file named "bar.md" with ""
When I successfully run `liche -v foo.md`
Then the stderr should contain "OK"
Scenario: Check a directory
Given a file named "foo.md" with:
"""
[bar](bar)
"""
And a directory named "bar"
When I successfully run `liche -v foo.md`
Then the stderr should contain "OK"
Scenario: Check an image
Given a file named "foo.md" with:
"""
![foo](foo.png)
"""
And a file named "foo.png" with ""
When I successfully run `liche -v foo.md`
Then the stderr should contain "OK"
Scenario: Check a non-existent markdown file
Given a file named "foo.md" with:
"""
[bar](bar.md)
"""
When I run `liche foo.md`
Then the exit status should be 1
And the stderr should contain "ERROR"

4
file_checker.go

@ -31,7 +31,7 @@ func (c fileChecker) Check(f string) ([]urlResult, error) {
rc := make(chan urlResult, len(us))
rs := make([]urlResult, 0, len(us))
go c.urlChecker.CheckMany(us, rc)
go c.urlChecker.CheckMany(us, f, rc)
for r := range rc {
rs = append(rs, r)
@ -120,7 +120,7 @@ func extractURLs(n *html.Node) []string {
func isURL(s string) bool {
u, err := url.Parse(s)
return err == nil && (u.Scheme == "http" || u.Scheme == "https")
return err == nil && (u.Scheme == "" || u.Scheme == "http" || u.Scheme == "https")
}
func isHTMLFile(f string) bool {

4
file_checker_test.go

@ -49,11 +49,11 @@ func TestURLParse(t *testing.T) {
}
func TestIsURL(t *testing.T) {
for _, s := range []string{"http://google.com", "https://google.com"} {
for _, s := range []string{"http://google.com", "https://google.com", "file-path"} {
assert.True(t, isURL(s))
}
for _, s := range []string{"", "file-path"} {
for _, s := range []string{"ftp://foo.com", "file://file-path"} {
assert.False(t, isURL(s))
}
}

26
url_checker.go

@ -1,6 +1,9 @@
package main
import (
"net/url"
"os"
"path"
"sync"
"time"
@ -16,7 +19,17 @@ func newURLChecker(t time.Duration, s semaphore) urlChecker {
return urlChecker{t, s}
}
func (c urlChecker) Check(u string) error {
func (c urlChecker) Check(u string, f string) error {
uu, err := url.Parse(u)
if err != nil {
return err
}
if uu.Scheme == "" {
return checkRelativePath(u, f)
}
c.semaphore.Request()
defer c.semaphore.Release()
@ -25,18 +38,18 @@ func (c urlChecker) Check(u string) error {
return err
}
_, _, err := fasthttp.GetTimeout(nil, u, c.timeout)
_, _, err = fasthttp.GetTimeout(nil, u, c.timeout)
return err
}
func (c urlChecker) CheckMany(us []string, rc chan<- urlResult) {
func (c urlChecker) CheckMany(us []string, f string, rc chan<- urlResult) {
wg := sync.WaitGroup{}
for _, s := range us {
wg.Add(1)
go func(s string) {
rc <- urlResult{s, c.Check(s)}
rc <- urlResult{s, c.Check(s, f)}
wg.Done()
}(s)
}
@ -44,3 +57,8 @@ func (c urlChecker) CheckMany(us []string, rc chan<- urlResult) {
wg.Wait()
close(rc)
}
func checkRelativePath(p string, f string) error {
_, err := os.Stat(path.Join(path.Dir(f), p))
return err
}

Loading…
Cancel
Save