Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Feb 9, 2022
1 parent c9c1ea1 commit f69de04
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/find/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ export function config(ctx) {

if (info && info.type !== 'gist') {
if (info.type in viewPaths) {
urlConfig.prefix = '/' + info.path() + '/' + viewPaths[info.type] + '/'
if (info.committish) {
urlConfig.prefix += info.committish + '/'
urlConfig.prefix =
new URL(info.browse({treepath: viewPaths[info.type]})).pathname + '/'
if (!info.committish) {
urlConfig.prefix += viewPaths[info.type] + '/'
}
}

Expand Down
11 changes: 10 additions & 1 deletion lib/find/find-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,20 @@ function urlToPath(value, config, type) {

value = url.pathname.slice(config.urlConfig.prefix.length)

// Drop the first path segment (the branch) if the prefix is
// branch-less (matches every branch). Make an educated guess
// whether the prefix is branch-less: Currently every
// hosted-git-info browsetemplate/pathtemplate has <= 2 segments
// (`${user}/${project}`), plus e.g. "blob" and surrounding slashes
// -> 5.
//
// Things get interesting here: branches: `foo/bar/baz` could be `baz` on
// the `foo/bar` branch, or, `baz` in the `bar` directory on the `foo`
// branch.
// Currently, we’re ignoring this and just not supporting branches.
value = value.split(slash).slice(1).join(slash)
if (config.urlConfig.prefix.split('/').length <= 5) {
value = value.split(slash).slice(1).join(slash)
}

return normalize(
path.resolve(config.root, value + (type === 'image' ? '' : url.hash)),
Expand Down
51 changes: 51 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,3 +1006,54 @@ test('remark-validate-links', async (t) => {

t.end()
})

test('links to other branches', async (t) => {
const {stderr} = await exec(
[
bin,
'--no-config',
'--no-ignore',
'--use',
'"../../index.js=repository:\\"wooorm/test#main\\""',
'--use',
'../sort.js',
'github.md',
'examples/github.md'
].join(' ')
)
t.deepEqual(
strip(stderr),
[
'examples/github.md',
' 5:37-5:51 warning Link to unknown heading: `world` missing-heading remark-validate-links',
' 15:34-15:91 warning Link to unknown file: `../world.md` missing-file remark-validate-links',
' 19:10-19:29 warning Link to unknown file: `../world.md` missing-file remark-validate-links',
' 29:10-29:33 warning Link to unknown heading in `../github.md`: `world` missing-heading-in-file remark-validate-links',
' 31:10-31:71 warning Link to unknown heading in `../github.md`: `world` missing-heading-in-file remark-validate-links',
' 35:10-35:32 warning Link to unknown file: `../world.md` missing-file remark-validate-links',
' 35:10-35:32 warning Link to unknown heading in `../world.md`: `hello` missing-heading-in-file remark-validate-links',
' 37:10-37:70 warning Link to unknown file: `../world.md` missing-file remark-validate-links',
' 37:10-37:70 warning Link to unknown heading in `../world.md`: `hello` missing-heading-in-file remark-validate-links',
'',
'github.md',
' 5:37-5:51 warning Link to unknown heading: `world` missing-heading remark-validate-links',
' 21:34-21:100 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
' 23:34-23:82 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
' 27:10-27:37 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
' 29:10-29:35 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
' 41:10-41:41 warning Link to unknown heading in `examples/github.md`: `world` missing-heading-in-file remark-validate-links',
' 43:10-43:39 warning Link to unknown heading in `examples/github.md`: `world` missing-heading-in-file remark-validate-links',
' 45:10-45:80 warning Link to unknown heading in `examples/github.md`: `world` missing-heading-in-file remark-validate-links',
' 49:10-49:40 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
' 49:10-49:40 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links',
' 51:10-51:38 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
' 51:10-51:38 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links',
' 53:10-53:79 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links',
' 53:10-53:79 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links',
'',
'⚠ 23 warnings',
''
].join('\n'),
'should ignore links to other branches'
)
})

0 comments on commit f69de04

Please sign in to comment.