Skip to content

Commit

Permalink
test: add input file validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Lobzov committed Jan 29, 2024
1 parent c06a49b commit 78d3140
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 31 deletions.
75 changes: 49 additions & 26 deletions asciidoc-link-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ const { argv } = require('process');
const fs = require('fs');
const readline = require('readline');

// regular expression for http- and https-link extraction
const pattern = /\bhttps?:\/\/[^\s|^[]+\b/g;

async function asciidocLinkCheck(file) {

async function asciidocLinkCheck(file, pattern) {
let exitCode;

// 0 - valid link code, -1 - invalid link code
const exitCode = await checkFileLinks(file, pattern);
if ( file === undefined ) {

// -2 - file is undefined
exitCode = -2;
} else {

if ( file.split('.').at(-1) === "adoc" ) {

// 0 - all links are valid, -1 - one of the links is invalid
exitCode = await checkFileLinks(file);
} else {

// -3 - is not adoc file
exitCode = -3;
}
}
console.log(`Exit code: ${ exitCode }`);
return exitCode;
}

/*
Expand Down Expand Up @@ -54,40 +67,50 @@ async function logCheckLinkResult(row, index, link) {
check file links function,
return file links positions and their availability status
*/
async function checkFileLinks(file, pattern) {
async function checkFileLinks(file) {

const readableStream = fs.createReadStream(file);
try {

const readlineInterface = readline.createInterface({
input: readableStream,
output: process.stdout,
terminal: false
});
// regular expression for http- and https-link extraction
const pattern = /\bhttps?:\/\/[^\s|^[]+\b/g;

let lineNumber = 0;
let match;
const readableStream = fs.createReadStream(file);

// 0 - valid link code, -1 - invalid link code
let exitCode = 0, res;
const readlineInterface = readline.createInterface({
input: readableStream,
output: process.stdout,
terminal: false
});

let lineNumber = 0;
let match;

for await (const line of readlineInterface) {
// 0 - valid link code, -1 - invalid link code
let exitCode = 0, res;

lineNumber++;
for await (const line of readlineInterface) {

while ((match = pattern.exec(line)) !== null) {
lineNumber++;

res = await logCheckLinkResult(lineNumber, match.index + 1, match[0]);
if (res === -1) {
exitCode = res;
while ((match = pattern.exec(line)) !== null) {

res = await logCheckLinkResult(lineNumber, match.index + 1, match[0]);
if (res === -1) {
exitCode = res;
}
}
}
}

return exitCode;
return exitCode;
} catch (err) {

// -4 - no such file
return -4
}
}


asciidocLinkCheck(argv[2], pattern);
asciidocLinkCheck(argv[2]);


module.exports = { checkLink, logCheckLinkResult, checkFileLinks, pattern };
module.exports = { checkLink, logCheckLinkResult, checkFileLinks, asciidocLinkCheck };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asciidoc-link-checker",
"version": "0.0.11",
"version": "0.0.12",
"description": "Module checks links availability in an asciidoc file",
"license": "MIT",
"author": {
Expand Down
27 changes: 23 additions & 4 deletions test/asciidoc-link-checker.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { checkLink, logCheckLinkResult, checkFileLinks, pattern } = require('../asciidoc-link-checker');
const { checkLink, logCheckLinkResult, checkFileLinks, asciidocLinkCheck } = require('../asciidoc-link-checker');


describe('test checkLink function', () => {
Expand Down Expand Up @@ -49,17 +49,36 @@ describe('test logCheckLinkResult function', () => {
describe('test checkFileLinks function', () => {

it('test valid links adoc file', async () => {
const exitCode = await checkFileLinks('./test/data/valid-links-file.adoc', pattern);
const exitCode = await checkFileLinks('./test/data/valid-links-file.adoc');
expect(exitCode).toBe(0);
});

it('test invalid links adoc file', async () => {
const exitCode = await checkFileLinks('./test/data/invalid-links-file.adoc', pattern);
const exitCode = await checkFileLinks('./test/data/invalid-links-file.adoc');
expect(exitCode).toBe(-1);
});

it('test mixed links adoc file', async () => {
const exitCode = await checkFileLinks('./test/data/mixed-links-file.adoc', pattern);
const exitCode = await checkFileLinks('./test/data/mixed-links-file.adoc');
expect(exitCode).toBe(-1);
});
});


describe('test asciidocLinkCheck function', () => {

it('test undefined file processing', async () => {
const exitCode = await asciidocLinkCheck();
expect(exitCode).toBe(-2);
});

it('test non-adoc file processing', async () => {
const exitCode = await asciidocLinkCheck('./test/data/txt-file.txt');
expect(exitCode).toBe(-3);
});

it('test empty file processing', async () => {
const exitCode = await asciidocLinkCheck('./test/data/empty-file.adoc');
expect(exitCode).toBe(-4);
});
});

0 comments on commit 78d3140

Please sign in to comment.