Skip to content

Commit

Permalink
chore: rearrange test files
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-nelson committed Jul 1, 2024
1 parent b080721 commit d94d218
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 257 deletions.
250 changes: 0 additions & 250 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,6 @@ let cmd = {
json: true,
}

test('linkspector should check image links in Markdown file', 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/markdown/image/imageTest.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(false)
expect(results.length).toBe(1)
expect(results[0].link).toBe(
'https://commons.wikimedia.org/wiki/Main_Page#/media/File:Praia_do_Ribeiro_do_Cavalo2.jpg'
)
expect(results[0].status).toBe('alive')
})

test('linkspector should check relative links in Markdown file', 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/markdown/relative/.relativeTest.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(8)
expect(results[0].status).toBe('alive')
expect(results[1].status).toBe('alive')
expect(results[2].status).toBe('alive')
expect(results[3].status).toBe('alive')
expect(results[4].status).toBe('alive')
expect(results[5].status).toBe('alive')
expect(results[6].status).toBe('error')
expect(results[7].status).toBe('error')
})

test('linkspector should check top-level relative links in Markdown file', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
Expand Down Expand Up @@ -112,177 +36,3 @@ test('linkspector should check top-level relative links in Markdown file', async
expect(hasErrorLinks).toBe(false)
expect(results.length).toBe(21)
})

test('linkspector should add back the removed duplicates when returning the results', 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/markdown/duplicates/duplicateTest.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('alive')
expect(results[1].status).toBe('error')
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')
})

test('linkspector should check HTML encoded section links', 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/markdown/decoded-sections/.decodedTest.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(false)
expect(results.length).toBe(4)
expect(results[0].status).toBe('alive')
expect(results[1].status).toBe('alive')
expect(results[2].status).toBe('alive')
expect(results[3].status).toBe('alive')
})

test('linkspector should check HTML encoded section links and include anchor names', 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/markdown/with-html-anchors/.withHtmlAnchorsTest.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
}
}
}

// Test expectations for link checks
expect(hasErrorLinks).toBe(false)
expect(results.length).toBe(1)
expect(results[0].status).toBe('alive')
})

test('linkspector should check links to headings with inline bold, italic, code, link, and image elements', 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/markdown/headings/headingsTest.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(false)
expect(results.length).toBe(8)
})
20 changes: 14 additions & 6 deletions lib/update-linkstatus-obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ function updateLinkStatusObj(astNodes, linkStatus) {
const existingLink = linkStatus.find((link) => link.link === node.url)
if (existingLink) {
if (!existingLink.position) {
console.error(`ERROR: Markdown formatting error around link: ${existingLink.link}. Please check the file containing this link.`);
existingLink.position = { start: { line: 1, column: 1 }, end: { line: 1, column: 1 } };
console.error(
`ERROR: Markdown formatting error around link: ${existingLink.link}. Please check the file containing this link.`
)
existingLink.position = {
start: { line: 1, column: 1 },
end: { line: 1, column: 1 },
}
}

const existingPosition = existingLink.position;
const nodePosition = node.position || { start: { line: 1, column: 1 }, end: { line: 1, column: 1 } };
const existingPosition = existingLink.position
const nodePosition = node.position || {
start: { line: 1, column: 1 },
end: { line: 1, column: 1 },
}

if (
existingPosition.start.line !== nodePosition.start.line ||
Expand All @@ -35,7 +43,7 @@ function updateLinkStatusObj(astNodes, linkStatus) {
...existingLink,
line_number: nodePosition.start.line,
position: nodePosition,
});
})
}
} else {
updatedLinkStatus.push({
Expand All @@ -47,7 +55,7 @@ function updateLinkStatusObj(astNodes, linkStatus) {
error_message: null,
title: node.title,
children: node.children,
});
})
}
})
updatedLinkStatus.sort((a, b) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/validate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function validateConfig(config) {
return true
} catch (err) {
if (err instanceof ValidationError) {
console.error("Validation Error:", err.message)
console.error('Validation Error:', err.message)
throw err
} else if (err.message.includes('ENOENT: no such file or directory')) {
console.error('Error reading file:', err.message)
Expand Down
42 changes: 42 additions & 0 deletions test/fixtures/asciidoc/hyperlinks/asciidoc-hyperlinks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, test } from 'vitest'
import { linkspector } from './linkspector.js'

let cmd = {
json: true,
}

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')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, test } from 'vitest'
import { linkspector } from './linkspector.js'

let cmd = {
json: true,
}

test('linkspector should check HTML encoded section links', 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/markdown/decoded-sections/.decodedTest.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(false)
expect(results.length).toBe(4)
expect(results[0].status).toBe('alive')
expect(results[1].status).toBe('alive')
expect(results[2].status).toBe('alive')
expect(results[3].status).toBe('alive')
})
Loading

0 comments on commit d94d218

Please sign in to comment.