agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
88 lines (78 loc) • 4.13 kB
JavaScript
// 🔗 Tests: scanDirectory → getAllFiles, walkDirectorySync → getAllFilesSync
const { testHelpers } = require('qtests'); // helper utilities for environment isolation
const fs = require('fs'); // file system operations
const path = require('path'); // path utilities
const os = require('os'); // OS utilities for temp directories
const { getAllFiles, getAllFilesSync, scanDirectory, walkDirectorySync } = require('./directoryScanner'); // functions under test
/**
* Run tests for directory scanning utilities
* @returns {Promise<{total:number, passed:number}>} aggregate results
*/
async function runTests() {
const results = { total: 0, passed: 0 }; // track totals
console.log('=== Testing Directory Scanner Utilities ==='); // log start
await testHelpers.withSavedEnv(async () => { // preserve environment state
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ds-test-')); // create temp directory
const subDir = path.join(tmpDir, 'sub'); // nested directory path
const ignoreDir = path.join(tmpDir, 'node_modules'); // directory that should be ignored
fs.mkdirSync(subDir); // make nested directory
fs.mkdirSync(ignoreDir); // make ignored directory
const filesToCreate = [ // list of files to create
path.join(tmpDir, 'root.js'),
path.join(subDir, 'child.ts'),
path.join(ignoreDir, 'ignored.js'),
path.join(tmpDir, 'note.txt')
];
filesToCreate.forEach(file => fs.writeFileSync(file, 'console.log(`test`);')); // write dummy files
const expected = [ // expected result files
path.join(tmpDir, 'root.js'),
path.join(subDir, 'child.ts')
];
// Test getAllFiles
results.total++; // increment total tests
try {
const asyncFiles = await getAllFiles(tmpDir); // run async scan
if (asyncFiles.sort().join() === expected.sort().join()) { // compare results
console.log('✓ getAllFiles collected expected files'); // log success
results.passed++; // increment passes
} else {
console.log('✗ getAllFiles returned unexpected results', asyncFiles); // log mismatch
}
} catch (error) {
console.log(`✗ getAllFiles failed: ${error.message}`); // log failure
}
// Test getAllFilesSync
results.total++; // increment total tests
try {
const syncFiles = getAllFilesSync(tmpDir); // run sync scan
if (syncFiles.sort().join() === expected.sort().join()) { // compare results
console.log('✓ getAllFilesSync collected expected files'); // log success
results.passed++; // increment passes
} else {
console.log('✗ getAllFilesSync returned unexpected results', syncFiles); // log mismatch
}
} catch (error) {
console.log(`✗ getAllFilesSync failed: ${error.message}`); // log failure
}
// Test helper functions directly
results.total++; // increment total tests
try {
const asyncHelperFiles = []; // accumulator for async helper
await scanDirectory(tmpDir, asyncHelperFiles, ['.js', '.ts', '.jsx', '.tsx'], ['node_modules', '.git', 'dist', 'build']); // run async helper
const syncHelperFiles = []; // accumulator for sync helper
walkDirectorySync(tmpDir, syncHelperFiles, ['.js', '.ts', '.jsx', '.tsx'], ['node_modules', '.git', 'dist', 'build']); // run sync helper
const helperCombined = [...asyncHelperFiles, ...syncHelperFiles]; // combine results for comparison
if (helperCombined.sort().join() === [...expected, ...expected].sort().join()) { // check combined results
console.log('✓ helper functions collected expected files'); // log success
results.passed++; // increment passes
} else {
console.log('✗ helper functions returned unexpected results', helperCombined); // log mismatch
}
} catch (error) {
console.log(`✗ helper functions failed: ${error.message}`); // log failure
}
fs.rmSync(tmpDir, { recursive: true, force: true }); // clean up temp directory
});
return results; // return aggregated results
}
module.exports = { runTests }; // export for test runner