Skip to content

Commit

Permalink
test: add input parameters 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 22555a3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
32 changes: 24 additions & 8 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,7 +67,10 @@ 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) {

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

const readableStream = fs.createReadStream(file);

Expand Down Expand Up @@ -87,7 +103,7 @@ async function checkFileLinks(file, pattern) {
}


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 checkFileLinks 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');
console.log(exitCode);
expect(exitCode).toBe(-3);
});

// TODO
it('test empty file processing', async () => {
//...
});
});
1 change: 1 addition & 0 deletions test/data/txt-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some text

0 comments on commit 22555a3

Please sign in to comment.