Skip to content

Commit

Permalink
Finialized asciidoc hyperlink check, added test
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-nelson committed May 12, 2024
1 parent f416134 commit 9ff040f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
36 changes: 36 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,39 @@ test("linkspector should add back the removed duplicates when returning the resu
expect(results[2].status).toBe("alive");
expect(results[3].status).toBe("error");
});

test(" linkspector should check hyperlinks in AsciiDoc files", async () => {
let hasErrorLinks = false;
let currentFile = ""; // Variable to store the current file name
let results = []; // Array to store the results if json is true

for await (const { file, result } of linkspector(
"./test/fixtures/asciidoc/hyperlinks/hyperlinksTest.yml",
cmd
)) {
currentFile = file;
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
});
}
if (linkStatusObj.status === "error") {
hasErrorLinks = true;
}
}
}

expect(hasErrorLinks).toBe(true);
expect(results.length).toBe(4);
expect(results[0].status).toBe("error");
expect(results[1].status).toBe("alive");
expect(results[2].status).toBe("error");
expect(results[3].status).toBe("alive");
});
18 changes: 18 additions & 0 deletions lib/update-linkstatus-obj.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/**
* Updates the link status object with the given AST nodes and existing link status.
*
* @param {Array} astNodes - The AST nodes to update the link status with.
* Each node is an object with properties `url`, `position`, `title`, and `children`.
*
* @param {Array} linkStatus - The existing link status to update.
* Each status is an object with properties `link`, `status`, `status_code`, `line_number`, `position`, `error_message`, `title`, and `children`.
*
* @returns {Array} The updated link status. Each status is an object with properties `link`, `status`, `status_code`, `line_number`, `position`, `error_message`, `title`, and `children`.
* The returned array is sorted by line number and start column in ascending order.
*/
"use strict";

function updateLinkStatusObj(astNodes, linkStatus) {
Expand Down Expand Up @@ -32,6 +44,12 @@ function updateLinkStatusObj(astNodes, linkStatus) {
});
}
});
updatedLinkStatus.sort((a, b) => {
if (a.position.start.line === b.position.start.line) {
return a.position.start.column - b.position.start.column;
}
return a.position.start.line - b.position.start.line;
});
return updatedLinkStatus;
}

Expand Down
3 changes: 0 additions & 3 deletions linkspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ export async function* linkspector(configFile, cmd) {
filesToCheck = modifiedFilesToCheck;
}

// Initialize an array to store link status objects
let linkStatusObjects = [];

// Process each file
for (const file of filesToCheck) {
const relativeFilePath = path.relative(process.cwd(), file);
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/asciidoc/hyperlinks/hyperlinksTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dirs:
- ./test/fixtures/asciidoc/hyperlinks/
fileExtensions:
- adoc
useGitIgnore: true
File renamed without changes.
1 change: 0 additions & 1 deletion test/fixtures/asciidoc/test2.adoc

This file was deleted.

0 comments on commit 9ff040f

Please sign in to comment.