From c6fb846db1f91bc682e32ba1f22b87dddc30efdb Mon Sep 17 00:00:00 2001 From: vishnudas Date: Sat, 18 Nov 2023 01:06:48 -0500 Subject: [PATCH] Add tests for src/index.js --- .eslintrc.js | 3 +- package-lock.json | 75 ++++++++++++++++++++++++++++ package.json | 1 + src/index.js | 94 ++++++++++++++++++++--------------- test/processDirectory.test.js | 34 +++++++++++++ test/test.js | 10 ++-- 6 files changed, 173 insertions(+), 44 deletions(-) create mode 100644 test/processDirectory.test.js diff --git a/.eslintrc.js b/.eslintrc.js index b49a7fd..b4c95e6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,7 +2,8 @@ module.exports = { "env": { "browser": true, "commonjs": true, - "es2021": true + "es2021": true, + "node": true }, "extends": [ "eslint:recommended", diff --git a/package-lock.json b/package-lock.json index 8240d50..c582995 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "yargs": "^17.7.2" }, "devDependencies": { + "assert": "^2.1.0", "eslint": "^8.52.0", "eslint-plugin-react": "^7.33.2", "mocha": "^10.2.0", @@ -347,6 +348,19 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/assert": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/assert/-/assert-2.1.0.tgz", + "integrity": "sha512-eLHpSK/Y4nhMJ07gDaAzoX/XAKS8PSaojml3M0DM4JpV1LAi5JOJ/p6H/XWrl8L+DzVEvVCW1z3vWAaB9oTsQw==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.2", + "is-nan": "^1.3.2", + "object-is": "^1.1.5", + "object.assign": "^4.1.4", + "util": "^0.12.5" + } + }, "node_modules/asynciterator.prototype": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/asynciterator.prototype/-/asynciterator.prototype-1.0.0.tgz", @@ -1395,6 +1409,22 @@ "node": ">= 0.4" } }, + "node_modules/is-arguments": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.1.1.tgz", + "integrity": "sha512-8Q7EARjzEnKpt/PCD7e1cgUS0a6X8u5tdSiMqXhojOdoV9TsMsiO+9VLC5vAmO8N7/GmXn7yjR8qnA6bVAEzfA==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.2", + "has-tostringtag": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-array-buffer": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/is-array-buffer/-/is-array-buffer-3.0.2.tgz", @@ -1568,6 +1598,22 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-nan": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/is-nan/-/is-nan-1.3.2.tgz", + "integrity": "sha512-E+zBKpQ2t6MEo1VsonYmluk9NxGrbzpeeLC2xIViuO2EjU2xsXsBPwTr3Ykv9l08UYEVEdWeRZNouaZqF6RN0w==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.0", + "define-properties": "^1.1.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-negative-zero": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/is-negative-zero/-/is-negative-zero-2.0.2.tgz", @@ -2112,6 +2158,22 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/object-is": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.1.5.tgz", + "integrity": "sha512-3cyDsyHgtmi7I7DfSSI2LDp6SK2lwvtbg0p0R1e0RvTqF5ceGx+K2dfSjm1bKDMVCFEDAQvy+o8c6a7VujOddw==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.2", + "define-properties": "^1.1.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/object-keys": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", @@ -2918,6 +2980,19 @@ "punycode": "^2.1.0" } }, + "node_modules/util": { + "version": "0.12.5", + "resolved": "https://registry.npmjs.org/util/-/util-0.12.5.tgz", + "integrity": "sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==", + "dev": true, + "dependencies": { + "inherits": "^2.0.3", + "is-arguments": "^1.0.4", + "is-generator-function": "^1.0.7", + "is-typed-array": "^1.1.3", + "which-typed-array": "^1.1.2" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index bb5d57e..6754c14 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "yargs": "^17.7.2" }, "devDependencies": { + "assert": "^2.1.0", "eslint": "^8.52.0", "eslint-plugin-react": "^7.33.2", "mocha": "^10.2.0", diff --git a/src/index.js b/src/index.js index a5e31e3..bbb6900 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,12 @@ -const fs = require("fs"); -const path = require("path"); -const yargs = require("yargs"); -const toml = require("toml"); +const fs = require('fs'); +const path = require('path'); +const yargs = require('yargs'); +const toml = require('toml'); // Function to read the content of a file function readFile(filePath) { try { - return fs.readFileSync(filePath, "utf8"); + return fs.readFileSync(filePath, 'utf8'); } catch (error) { console.error(`Error reading file: ${error.message}`); process.exit(1); @@ -34,20 +34,30 @@ function processFile(inputFilePath, outputDir, lang) { console.log(`Converted ${inputFilePath} to HTML.`); } -module.exports = { - processFile // Ensure processFile is included in the exported object -}; + +// Function to process all .txt and .md files in a directory +function processDirectory(inputDir, outputDir, lang) { + const files = fs.readdirSync(inputDir); + + files.forEach((file) => { + const filePath = path.join(inputDir, file); + + if (fs.lstatSync(filePath).isFile() && file.match(/\.(txt|md)$/)) { + processFile(filePath, outputDir, lang); + } + }); +} function generateHTML(fileContent, lang) { const [title, ...paragraphs] = fileContent.split(/\n{1,}/); // Split by one or more newline characters - const titleHtml = title ? `${title}

${title}

` : ""; + const titleHtml = title ? `${title}

${title}

` : ''; const paragraphsHtml = paragraphs .map((paragraph) => { // Support for bold text using double asterisks - paragraph = paragraph.replace(/\*\*(.*?)\*\*/g, "$1"); + paragraph = paragraph.replace(/\*\*(.*?)\*\*/g, '$1'); return `

${paragraph}

`; }) - .join("\n"); + .join('\n'); return ` @@ -62,7 +72,6 @@ function generateHTML(fileContent, lang) { `; } - function processConfigFile(configPath) { const configContent = readFile(configPath); return toml.parse(configContent); @@ -71,37 +80,37 @@ function processConfigFile(configPath) { // Main command-line tool logic function main() { yargs - .scriptName("til") - .usage("$0 [args]") + .scriptName('til') + .usage('$0 [args]') .command( - "process [input]", - "Process .txt and .md file(s) and generate HTML", + 'process [input]', + 'Process .txt and .md file(s) and generate HTML', (yargs) => { yargs - .positional("input", { - describe: "Input .txt file or folder", - type: "string", + .positional('input', { + describe: 'Input .txt file or folder', + type: 'string', }) - .option("output", { - alias: "o", - describe: "Output directory", - type: "string", + .option('output', { + alias: 'o', + describe: 'Output directory', + type: 'string', }) - .option("lang", { - alias: "l", - describe: "language for lang attribute on elements", - type: "string", - default: "en-CA", + .option('lang', { + alias: 'l', + describe: 'language for lang attribute on elements', + type: 'string', + default: 'en-CA', }) - .option("config", { - alias: "c", - describe: "Path to TOML configuration file", - type: "string", + .option('config', { + alias: 'c', + describe: 'Path to TOML configuration file', + type: 'string', }); }, (argv) => { const inputPath = argv.input; - const outputDir = argv.output || "til"; // Use 'til' if not specified + let outputDir = argv.output || 'til'; // Use 'til' if not specified let lang = argv.lang; // Check if a config file is provided @@ -130,17 +139,24 @@ function main() { processFile(inputPath, outputDir, lang); } else { console.error( - "Invalid input. Please provide a valid .txt or .md file or folder." + 'Invalid input. Please provide a valid .txt or .md file or folder.' ); process.exit(1); // Exit with a non-zero exit code } } ) - .demandCommand(1, "You need to specify a command.") - .version("v", "Show the tool's name and version", `til v1.0.0`) - .alias("v", "version") - .help("h", "Show a useful help message") - .alias("h", "help").argv; + .demandCommand(1, 'You need to specify a command.') + .version('v', 'Show the tool\'s name and version', `til v1.0.0`) + .alias('v', 'version') + .help('h', 'Show a useful help message') + .alias('h', 'help').argv; } main(); + +// Export the processFile function +module.exports = { + processFile, + processDirectory, + // other exports if any +}; diff --git a/test/processDirectory.test.js b/test/processDirectory.test.js new file mode 100644 index 0000000..d895a59 --- /dev/null +++ b/test/processDirectory.test.js @@ -0,0 +1,34 @@ +/* eslint-env mocha */ + +const assert = require('assert'); +const fs = require('fs'); +const { processDirectory } = require('../src/index'); + +describe('processDirectory Function Test Suite', function () { + const outputDir = './output'; // Set an output directory for testing + const lang = 'en-US'; // Set a language for testing + + before(function () { + // Create the output directory if it doesn't exist + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir); + } + }); + + it('Processes all .txt and .md files in a directory', function () { + const inputDir = './src'; // Provide a test directory for input + + // Run the function + processDirectory(inputDir, outputDir, lang); + + // Verify if the output files were created + const files = fs.readdirSync(outputDir); + const fileExists = files.some(file => file.endsWith('.html')); + + // Assertion to check if at least one HTML file exists in the output directory + assert.strictEqual(fileExists, true); + }); + + // Add more test cases as needed +}); + diff --git a/test/test.js b/test/test.js index 1f44f51..9080162 100644 --- a/test/test.js +++ b/test/test.js @@ -1,10 +1,12 @@ +/* eslint-env mocha */ + const assert = require('assert'); const fs = require('fs'); const path = require('path'); -const { processFile } = require('../src/index'); +const { processFile } = require('../src/index.js'); // Adjust the path accordingly -describe('processFile Function Test Suite', function() { - it('Generates HTML and writes to output directory', function() { +describe('processFile Function Test Suite', function () { + it('Generates HTML and writes to output directory', function () { const inputFilePath = './src/till-1.txt'; // Provide a test file for input const outputDir = './output'; // Set an output directory for testing const lang = 'en-US'; // Set a language for testing @@ -15,7 +17,7 @@ describe('processFile Function Test Suite', function() { } // Run the function - processFile(inputFilePath, outputDir, lang); + processFile(inputFilePath, outputDir, lang); // Use processFile function // Verify if the output file was created const expectedOutputFilePath = path.join(outputDir, 'till-1.html');