Skip to content

Commit

Permalink
Fix same link in different file error (#26)
Browse files Browse the repository at this point in the history
* Fix same link issue

Signed-off-by: Saswata Mukherjee <[email protected]>

* Change to const

Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode authored May 14, 2021
1 parent b3ebc08 commit c09ec55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/mdformatter/linktransformer/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var (
IDNotFoundErr = LookupError(errors.New("file exists, but does not have such id"))
)

const (
originalURLKey = "originalURLKey"
)

type chain struct {
chain []mdformatter.LinkTransformer
}
Expand Down Expand Up @@ -150,15 +154,20 @@ func NewValidator(logger log.Logger, except *regexp.Regexp, anchorDir string) (m
}); err != nil {
return nil, err
}
v.c.OnRequest(func(request *colly.Request) {
v.rMu.Lock()
defer v.rMu.Unlock()
request.Ctx.Put(originalURLKey, request.URL.String())
})
v.c.OnScraped(func(response *colly.Response) {
v.rMu.Lock()
defer v.rMu.Unlock()
v.remoteLinks[response.Request.URL.String()] = nil
v.remoteLinks[response.Ctx.Get(originalURLKey)] = nil
})
v.c.OnError(func(response *colly.Response, err error) {
v.rMu.Lock()
defer v.rMu.Unlock()
v.remoteLinks[response.Request.URL.String()] = errors.Wrapf(err, "%q not accessible; status code %v", response.Request.URL.String(), response.StatusCode)
v.remoteLinks[response.Ctx.Get(originalURLKey)] = errors.Wrapf(err, "%q not accessible; status code %v", response.Request.URL.String(), response.StatusCode)
})
return v, nil
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/mdformatter/linktransformer/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ func TestValidator_TransformDestination(t *testing.T) {
testutil.Equals(t, 0, len(diff), diff.String())
})

t.Run("check valid but same link in diff files", func(t *testing.T) {
testFile := filepath.Join(tmpDir, "repo", "docs", "test", "valid-link.md")
testFile2 := filepath.Join(tmpDir, "repo", "docs", "test", "valid-link2.md")
testutil.Ok(t, ioutil.WriteFile(testFile, []byte("https://bwplotka.dev/about\n"), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(testFile2, []byte("https://bwplotka.dev/about\n"), os.ModePerm))

diff, err := mdformatter.IsFormatted(context.TODO(), logger, []string{testFile, testFile2})
testutil.Ok(t, err)
testutil.Equals(t, 0, len(diff), diff.String())

diff, err = mdformatter.IsFormatted(context.TODO(), logger, []string{testFile, testFile2}, mdformatter.WithLinkTransformer(
MustNewValidator(logger, regexp.MustCompile(`^$`), anchorDir),
))
testutil.Ok(t, err)
testutil.Equals(t, 0, len(diff), diff.String())
})

t.Run("check valid local links", func(t *testing.T) {
testFile := filepath.Join(tmpDir, "repo", "docs", "test", "valid-local-links.md")
testutil.Ok(t, ioutil.WriteFile(testFile, []byte(`# yolo
Expand Down

0 comments on commit c09ec55

Please sign in to comment.