Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized test case execution time && Tests to verify the api endpoint. #58

Merged
merged 11 commits into from
Dec 30, 2024
8 changes: 4 additions & 4 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ program
console.error(error);
}
});

function initCommand(options) {
//Changing sync to async to optimize further
async function initCommand(options) {
const selectedTemplate = options.template || "basic"; // Default to 'basic' if no template is specified

if (!templates[selectedTemplate]) {
Expand Down Expand Up @@ -108,7 +108,7 @@ function initCommand(options) {

const copySpinner = createSpinner("Creating server files...").start();
try {
fs.copySync(templatePath, destinationPath);
await fs.copy(templatePath, destinationPath);

copySpinner.success({ text: "Created server files successfully." });
} catch (err) {
Expand All @@ -133,7 +133,7 @@ function initCommand(options) {
const addDependencies = createSpinner("Adding dependency packages...").start();
try {
const packageJsonPath = path.join(targetDir, "package.json");
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJsonContent = await fs.readJSON(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
packageJson.dependencies = packageJson.dependencies || {};

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
"main": "index.js",
"scripts": {
"dev": "node bin/index.js init",
"test": "jest"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
},
"author": "Abhinav Ramakrishnan, Ashwin Narayanan S",
"license": "ISC",
"bin": "./bin/index.js",
"dependencies": {
"chalk": "^5.3.0",
"commander": "^12.1.0",
"express": "^4.21.2",
"figlet": "^1.7.0",
"fs-extra": "^11.2.0",
"nanospinner": "^1.1.0"
"nanospinner": "^1.1.0",
"supertest": "^7.0.0"
},
"devDependencies": {
"jest": "^29.7.0"
}
}
}
4 changes: 4 additions & 0 deletions templates/basic/server.js
Ashrockzzz2003 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ app.get("/", (req, res) => {
return res.send("Hello World!");
});

if (process.env.NODE_ENV !== "test") {
app.listen(port, () => {
console.log(`Example app listening on port ${port}.`);
});
}

export default app;
10 changes: 10 additions & 0 deletions templates/basic/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import app from "./server.js";
import request from "supertest";

describe("API Endpoints", () => {
it("should return Hello World on GET /", async () => {
const res = await request(app).get("/");
expect(res.statusCode).toEqual(200);
expect(res.text).toBe("Hello World!");
});
});
5 changes: 3 additions & 2 deletions test/help.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
import { exec as execCallback } from 'child_process';
import { promisify } from 'util';
const exec = promisify(execCallback);

const help = `Usage: qse [options] [command]

Expand Down
45 changes: 28 additions & 17 deletions test/init.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
import { createHash } from 'node:crypto';
import path from 'node:path';
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs';
import { fileURLToPath } from 'url';
import { exec as execCallback } from 'child_process';
import { promisify } from 'util';

const exec = promisify(execCallback);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const tempDir = path.join(__dirname, 'temp');

function initTempDirectory() {
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true });
if (existsSync(tempDir)) {
rmSync(tempDir, { recursive: true });
}
fs.mkdirSync(tempDir);
mkdirSync(tempDir);
}

function clearTempDirectory() {
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true });
if (existsSync(tempDir)) {
rmSync(tempDir, { recursive: true });
}
}

function traverseDirectory(dirName, hash) {
const files = fs.readdirSync(dirName);
const files = readdirSync(dirName);

for (const file of files) {
const filePath = path.join(dirName, file);
const stats = fs.statSync(filePath);
const stats = statSync(filePath);
if (stats.isDirectory()) {
traverseDirectory(filePath, hash);
} else {
const data = fs.readFileSync(filePath);
const data = readFileSync(filePath);
hash.update(data);
}
}
Expand All @@ -36,19 +40,19 @@ function traverseDirectory(dirName, hash) {
// Ignore node_modules and package-lock.json
// and compute the SHA256 hash.
function computeSHA256Hash(dirName) {
const hash = crypto.createHash('sha256');
const files = fs.readdirSync(dirName);
const hash = createHash('sha256');
const files = readdirSync(dirName);

for (const file of files) {
if (file === 'node_modules' || file === 'package-lock.json' || file === 'package.json') {
continue;
}
const filePath = path.join(dirName, file);
const stats = fs.statSync(filePath);
const stats = statSync(filePath);
if (stats.isDirectory()) {
traverseDirectory(filePath, hash);
} else {
const data = fs.readFileSync(filePath);
const data = readFileSync(filePath);
hash.update(data);
}
}
Expand All @@ -66,9 +70,16 @@ describe('init', () => {
});

test('no templates passed, should default to basic', async () => {

console.time('Hash Calculation');
const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'basic'));
console.timeEnd('Hash Calculation');
console.time('Command Execution');
await exec(`node ../../bin/index.js init`, { cwd: tempDir });
console.timeEnd('Command Execution');
console.time('Hash Verification');
const commandHash = computeSHA256Hash(tempDir);
console.timeEnd('Hash Verification');
expect(commandHash).toEqual(originalHash);
Ashrockzzz2003 marked this conversation as resolved.
Show resolved Hide resolved
})

Expand Down
5 changes: 3 additions & 2 deletions test/list.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
import { exec as execCallback } from 'child_process';
import { promisify } from 'util';
const exec = promisify(execCallback);

const list = `Available Commands:
- -v, --version: Prints current qse version
Expand Down
7 changes: 4 additions & 3 deletions test/version.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
const packageJson = require('../package.json')
import packageJson from '../package.json';
import { exec as execCallback } from 'child_process';
import { promisify } from 'util';
const exec = promisify(execCallback);

describe('Version Command', () => {
test('package.json has version', () => {
Expand Down