diff --git a/pkg/mdformatter/linktransformer/link.go b/pkg/mdformatter/linktransformer/link.go index aef85dd..a9d4e94 100644 --- a/pkg/mdformatter/linktransformer/link.go +++ b/pkg/mdformatter/linktransformer/link.go @@ -201,14 +201,23 @@ func (v *validator) Close(ctx mdformatter.SourceContext) error { }) merr := merrors.New() + base, err := os.Getwd() + if err != nil { + merr.Add(errors.Wrapf(err, "could not resolve working dir")) + } + path, err := filepath.Rel(base, ctx.Filepath) + if err != nil { + merr.Add(errors.Wrapf(err, "could not find relative path")) + } + for _, k := range keys { f := v.destFutures[k] if err := f.resultFn(); err != nil { if f.cases == 1 { - merr.Add(err) + merr.Add(errors.Wrapf(err, "%v", path)) continue } - merr.Add(errors.Wrapf(err, "(%v occurrences)", f.cases)) + merr.Add(errors.Wrapf(err, "%v (%v occurrences)", path, f.cases)) } } return merr.Err() diff --git a/pkg/mdformatter/linktransformer/link_test.go b/pkg/mdformatter/linktransformer/link_test.go index 0a9395d..4ad926f 100644 --- a/pkg/mdformatter/linktransformer/link_test.go +++ b/pkg/mdformatter/linktransformer/link_test.go @@ -183,6 +183,8 @@ func TestValidator_TransformDestination(t *testing.T) { t.Run("check invalid local links", func(t *testing.T) { testFile := filepath.Join(tmpDir, "repo", "docs", "test", "invalid-local-links.md") + rel := "../../../../../../.." + tmpDirPath := tmpDir + "/repo/docs/test/invalid-local-links.md" testutil.Ok(t, ioutil.WriteFile(testFile, []byte(`# yolo [1](.) [2](#not-yolo) [3](../test2/invalid-local-links.md) [4](../test/invalid-local-links.md#not-yolo) [5](../test/doc.md) @@ -196,17 +198,19 @@ func TestValidator_TransformDestination(t *testing.T) { MustNewValidator(logger, regexp.MustCompile(`^$`), anchorDir), )) testutil.NotOk(t, err) - testutil.Equals(t, fmt.Sprintf("%v/repo/docs/test/invalid-local-links.md: 4 errors: "+ - "link ../test2/invalid-local-links.md, normalized to: %v/repo/docs/test2/invalid-local-links.md: file not found; "+ - "link ../test/invalid-local-links.md#not-yolo, normalized to: link %v/repo/docs/test/invalid-local-links.md#not-yolo, existing ids: [yolo]: file exists, but does not have such id; "+ - "link ../test/doc.md, normalized to: %v/repo/docs/test/doc.md: file not found; "+ - "link #not-yolo, normalized to: link %v/repo/docs/test/invalid-local-links.md#not-yolo, existing ids: [yolo]: file exists, but does not have such id", - tmpDir, tmpDir, tmpDir, tmpDir, tmpDir), err.Error()) + + testutil.Equals(t, fmt.Sprintf("%v: 4 errors: "+ + "%v: link ../test2/invalid-local-links.md, normalized to: %v/repo/docs/test2/invalid-local-links.md: file not found; "+ + "%v: link ../test/invalid-local-links.md#not-yolo, normalized to: link %v/repo/docs/test/invalid-local-links.md#not-yolo, existing ids: [yolo]: file exists, but does not have such id; "+ + "%v: link ../test/doc.md, normalized to: %v/repo/docs/test/doc.md: file not found; "+ + "%v: link #not-yolo, normalized to: link %v/repo/docs/test/invalid-local-links.md#not-yolo, existing ids: [yolo]: file exists, but does not have such id", + tmpDirPath, rel+tmpDirPath, tmpDir, rel+tmpDirPath, tmpDir, rel+tmpDirPath, tmpDir, rel+tmpDirPath, tmpDir), err.Error()) }) t.Run("check 404 link", func(t *testing.T) { testFile := filepath.Join(tmpDir, "repo", "docs", "test", "invalid-link.md") testutil.Ok(t, ioutil.WriteFile(testFile, []byte("https://bwplotka.dev/does-not-exists\n"), os.ModePerm)) + tmpDirPath := tmpDir + "/repo/docs/test/invalid-link.md" diff, err := mdformatter.IsFormatted(context.TODO(), logger, []string{testFile}) testutil.Ok(t, err) @@ -216,7 +220,7 @@ func TestValidator_TransformDestination(t *testing.T) { MustNewValidator(logger, regexp.MustCompile(`^$`), anchorDir), )) testutil.NotOk(t, err) - testutil.Equals(t, tmpDir+"/repo/docs/test/invalid-link.md: \"https://bwplotka.dev/does-not-exists\" not accessible; status code 404: Not Found", err.Error()) + testutil.Equals(t, fmt.Sprintf("%v: ../../../../../../..%v: \"https://bwplotka.dev/does-not-exists\" not accessible; status code 404: Not Found", tmpDirPath, tmpDirPath), err.Error()) }) t.Run("check 404 link, ignored", func(t *testing.T) {