Skip to content

Commit

Permalink
fix: resolve glob paths based on server root (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Dec 26, 2020
1 parent a8c0a43 commit fee112b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
31 changes: 27 additions & 4 deletions src/index.ts
Expand Up @@ -175,17 +175,40 @@ export class LinkChecker extends EventEmitter {
);
}

if (options.serverRoot) {
options.serverRoot = path.normalize(options.serverRoot);
}

// expand globs into paths
if (!isUrlType) {
const paths: string[] = [];
for (const path of options.path) {
const expandedPaths = await glob(path);
for (const filePath of options.path) {
// The glob path provided is relative to the serverRoot. For example,
// if the serverRoot is test/fixtures/nested, and the glob is "*/*.html",
// The glob needs to be calculated from the serverRoot directory.
const fullPath = options.serverRoot
? path.join(options.serverRoot, filePath)
: filePath;
const expandedPaths = await glob(fullPath);
if (expandedPaths.length === 0) {
throw new Error(
`The provided glob "${path}" returned 0 results. The current working directory is "${process.cwd()}".`
`The provided glob "${filePath}" returned 0 results. The current working directory is "${process.cwd()}".`
);
}
paths.push(...expandedPaths);
// After resolving the globs, the paths need to be returned to their
// original form, without the serverRoot included in the path.
for (let p of expandedPaths) {
p = path.normalize(p);
if (options.serverRoot) {
const contractedPath = p
.split(path.sep)
.slice(options.serverRoot.split(path.sep).length)
.join(path.sep);
paths.push(contractedPath);
} else {
paths.push(p);
}
}
}
options.path = paths;
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/nested/doll1/index.html
@@ -0,0 +1,5 @@
<html>
<body>
<a href="http://fake.local/doll1">just follow a link</a>
</body>
</html>
5 changes: 5 additions & 0 deletions test/fixtures/nested/doll2/index.html
@@ -0,0 +1,5 @@
<html>
<body>
<a href="http://fake.local/doll2">just follow a link</a>
</body>
</html>
33 changes: 30 additions & 3 deletions test/test.ts
Expand Up @@ -304,7 +304,6 @@ describe('linkinator', () => {
it('should allow overriding the server root', async () => {
const results = await check({
serverRoot: 'test/fixtures/markdown',
markdown: true,
path: 'README.md',
});
assert.strictEqual(results.links.length, 3);
Expand Down Expand Up @@ -385,7 +384,6 @@ describe('linkinator', () => {
const consoleSpy = sinon.stub(console, 'log');
const results = await check({
path: 'test/fixtures/markdown/README.md',
markdown: true,
});
assert.ok(results.passed);
assert.ok(consoleSpy.calledOnce);
Expand All @@ -394,7 +392,6 @@ describe('linkinator', () => {
it('should respect globs', async () => {
const results = await check({
path: 'test/fixtures/markdown/**/*.md',
markdown: true,
});
assert.ok(results.passed);
assert.strictEqual(results.links.length, 6);
Expand Down Expand Up @@ -446,6 +443,36 @@ describe('linkinator', () => {
assert.ok(/Nock: Disallowed net connect for/.test(err.message));
});

it('should respect server root with globs', async () => {
const scope = nock('http://fake.local')
.get('/doll1')
.reply(200)
.get('/doll2')
.reply(200);
const results = await check({
serverRoot: 'test/fixtures/nested',
path: '*/*.html',
});
assert.strictEqual(results.links.length, 4);
assert.ok(results.passed);
scope.done();
});

it('should respect absolute server root', async () => {
const scope = nock('http://fake.local')
.get('/doll1')
.reply(200)
.get('/doll2')
.reply(200);
const results = await check({
serverRoot: path.resolve('test/fixtures/nested'),
path: '*/*.html',
});
assert.strictEqual(results.links.length, 4);
assert.ok(results.passed);
scope.done();
});

it('should scan links in <meta content="URL"> tags', async () => {
const scope = nock('http://fake.local').head('/').reply(200);
const results = await check({path: 'test/fixtures/twittercard'});
Expand Down

0 comments on commit fee112b

Please sign in to comment.