From 26387e890e9d19d8e2842cd98055bd70de7d8d62 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Wed, 19 May 2021 19:59:20 +0530 Subject: [PATCH] Fix `clilog` file names for link errors (#31) * Fix clilog file name Signed-off-by: Saswata Mukherjee * Implement suggestions Signed-off-by: Saswata Mukherjee --- pkg/mdformatter/linktransformer/link.go | 13 ++++++++-- pkg/mdformatter/linktransformer/link_test.go | 25 ++++++++++++++------ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/pkg/mdformatter/linktransformer/link.go b/pkg/mdformatter/linktransformer/link.go index aef85dd..561fc34 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 { + return errors.Wrap(err, "resolve working dir") + } + path, err := filepath.Rel(base, ctx.Filepath) + if err != nil { + return errors.Wrap(err, "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..4770634 100644 --- a/pkg/mdformatter/linktransformer/link_test.go +++ b/pkg/mdformatter/linktransformer/link_test.go @@ -183,6 +183,11 @@ 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") + filePath := "/repo/docs/test/invalid-local-links.md" + wdir, err := os.Getwd() + testutil.Ok(t, err) + relDirPath, err := filepath.Rel(wdir, tmpDir) + testutil.Ok(t, err) 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 +201,23 @@ 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", + tmpDir+filePath, relDirPath+filePath, tmpDir, relDirPath+filePath, tmpDir, relDirPath+filePath, tmpDir, relDirPath+filePath, 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)) + filePath := "/repo/docs/test/invalid-link.md" + wdir, err := os.Getwd() + testutil.Ok(t, err) + relDirPath, err := filepath.Rel(wdir, tmpDir) + testutil.Ok(t, err) diff, err := mdformatter.IsFormatted(context.TODO(), logger, []string{testFile}) testutil.Ok(t, err) @@ -216,7 +227,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: %v%v: \"https://bwplotka.dev/does-not-exists\" not accessible; status code 404: Not Found", tmpDir, filePath, relDirPath, filePath), err.Error()) }) t.Run("check 404 link, ignored", func(t *testing.T) {