Skip to content

Commit fee112b

Browse files
authoredDec 26, 2020
fix: resolve glob paths based on server root (#218)
1 parent a8c0a43 commit fee112b

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed
 

‎src/index.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,40 @@ export class LinkChecker extends EventEmitter {
175175
);
176176
}
177177

178+
if (options.serverRoot) {
179+
options.serverRoot = path.normalize(options.serverRoot);
180+
}
181+
178182
// expand globs into paths
179183
if (!isUrlType) {
180184
const paths: string[] = [];
181-
for (const path of options.path) {
182-
const expandedPaths = await glob(path);
185+
for (const filePath of options.path) {
186+
// The glob path provided is relative to the serverRoot. For example,
187+
// if the serverRoot is test/fixtures/nested, and the glob is "*/*.html",
188+
// The glob needs to be calculated from the serverRoot directory.
189+
const fullPath = options.serverRoot
190+
? path.join(options.serverRoot, filePath)
191+
: filePath;
192+
const expandedPaths = await glob(fullPath);
183193
if (expandedPaths.length === 0) {
184194
throw new Error(
185-
`The provided glob "${path}" returned 0 results. The current working directory is "${process.cwd()}".`
195+
`The provided glob "${filePath}" returned 0 results. The current working directory is "${process.cwd()}".`
186196
);
187197
}
188-
paths.push(...expandedPaths);
198+
// After resolving the globs, the paths need to be returned to their
199+
// original form, without the serverRoot included in the path.
200+
for (let p of expandedPaths) {
201+
p = path.normalize(p);
202+
if (options.serverRoot) {
203+
const contractedPath = p
204+
.split(path.sep)
205+
.slice(options.serverRoot.split(path.sep).length)
206+
.join(path.sep);
207+
paths.push(contractedPath);
208+
} else {
209+
paths.push(p);
210+
}
211+
}
189212
}
190213
options.path = paths;
191214
}

‎test/fixtures/nested/doll1/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a href="http://fake.local/doll1">just follow a link</a>
4+
</body>
5+
</html>

‎test/fixtures/nested/doll2/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a href="http://fake.local/doll2">just follow a link</a>
4+
</body>
5+
</html>

‎test/test.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ describe('linkinator', () => {
304304
it('should allow overriding the server root', async () => {
305305
const results = await check({
306306
serverRoot: 'test/fixtures/markdown',
307-
markdown: true,
308307
path: 'README.md',
309308
});
310309
assert.strictEqual(results.links.length, 3);
@@ -385,7 +384,6 @@ describe('linkinator', () => {
385384
const consoleSpy = sinon.stub(console, 'log');
386385
const results = await check({
387386
path: 'test/fixtures/markdown/README.md',
388-
markdown: true,
389387
});
390388
assert.ok(results.passed);
391389
assert.ok(consoleSpy.calledOnce);
@@ -394,7 +392,6 @@ describe('linkinator', () => {
394392
it('should respect globs', async () => {
395393
const results = await check({
396394
path: 'test/fixtures/markdown/**/*.md',
397-
markdown: true,
398395
});
399396
assert.ok(results.passed);
400397
assert.strictEqual(results.links.length, 6);
@@ -446,6 +443,36 @@ describe('linkinator', () => {
446443
assert.ok(/Nock: Disallowed net connect for/.test(err.message));
447444
});
448445

446+
it('should respect server root with globs', async () => {
447+
const scope = nock('http://fake.local')
448+
.get('/doll1')
449+
.reply(200)
450+
.get('/doll2')
451+
.reply(200);
452+
const results = await check({
453+
serverRoot: 'test/fixtures/nested',
454+
path: '*/*.html',
455+
});
456+
assert.strictEqual(results.links.length, 4);
457+
assert.ok(results.passed);
458+
scope.done();
459+
});
460+
461+
it('should respect absolute server root', async () => {
462+
const scope = nock('http://fake.local')
463+
.get('/doll1')
464+
.reply(200)
465+
.get('/doll2')
466+
.reply(200);
467+
const results = await check({
468+
serverRoot: path.resolve('test/fixtures/nested'),
469+
path: '*/*.html',
470+
});
471+
assert.strictEqual(results.links.length, 4);
472+
assert.ok(results.passed);
473+
scope.done();
474+
});
475+
449476
it('should scan links in <meta content="URL"> tags', async () => {
450477
const scope = nock('http://fake.local').head('/').reply(200);
451478
const results = await check({path: 'test/fixtures/twittercard'});

0 commit comments

Comments
 (0)
Please sign in to comment.