Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: JustinBeckwith/linkinator
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.11.0
Choose a base ref
...
head repository: JustinBeckwith/linkinator
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.11.1
Choose a head ref
  • 1 commit
  • 4 files changed
  • 1 contributor

Commits on Dec 26, 2020

  1. Copy the full SHA
    fee112b View commit details
Showing with 67 additions and 7 deletions.
  1. +27 −4 src/index.ts
  2. +5 −0 test/fixtures/nested/doll1/index.html
  3. +5 −0 test/fixtures/nested/doll2/index.html
  4. +30 −3 test/test.ts
31 changes: 27 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions test/fixtures/nested/doll1/index.html
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
@@ -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);
@@ -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);
@@ -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'});