agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
139 lines (115 loc) • 4.93 kB
JavaScript
/**
* @file File System Utils test bridge
* @description Bridge test file to import from file-system module
*/
const qtests = require('qtests');
const fs = require('fs');
const path = require('path');
const { fileExists, readFileContent, getFileExtension, isFileType, createTempFile, resolveProjectPath } = require('./file-system');
async function runTests() {
console.log('\n=== Testing File System Utilities ===');
const results = { total: 0, passed: 0 };
// Test fileExists function
results.total++;
try {
const exists = fileExists('./package.json');
qtests.assert(typeof exists === 'boolean', 'fileExists should return boolean');
qtests.assert(exists === true, 'package.json should exist');
const notExists = fileExists('./nonexistent-file.txt');
qtests.assert(notExists === false, 'Non-existent file should return false');
console.log('✓ fileExists correctly checks file existence');
results.passed++;
} catch (error) {
console.log(`✗ fileExists test failed: ${error.message}`);
}
// Test readFileContent function
results.total++;
try {
const content = readFileContent('./package.json');
qtests.assert(typeof content === 'string', 'readFileContent should return string');
qtests.assert(content.includes('name'), 'package.json should contain name field');
console.log('✓ readFileContent correctly reads file contents');
results.passed++;
} catch (error) {
console.log(`✗ readFileContent test failed: ${error.message}`);
}
// Test readFileContent error handling
results.total++;
try {
qtests.assert(typeof readFileContent === 'function', 'readFileContent should be function');
try {
readFileContent('./nonexistent-file.txt');
console.log('✗ readFileContent error handling test failed: Should throw on missing file');
} catch (readError) {
console.log('✓ readFileContent correctly handles missing files');
results.passed++;
}
} catch (error) {
console.log(`✗ readFileContent error handling test failed: ${error.message}`);
}
// Test getFileExtension function
results.total++;
try {
const ext1 = getFileExtension('./test.js');
qtests.assert(ext1 === '.js', 'Should extract .js extension');
const ext2 = getFileExtension('./package.json');
qtests.assert(ext2 === '.json', 'Should extract .json extension');
console.log('✓ getFileExtension correctly extracts file extensions');
results.passed++;
} catch (error) {
console.log(`✗ getFileExtension test failed: ${error.message}`);
}
// Test isFileType function
results.total++;
try {
const isJs = isFileType('./test.js', '.js');
qtests.assert(isJs === true, 'Should match .js file type');
const isNotJs = isFileType('./test.json', '.js');
qtests.assert(isNotJs === false, 'Should not match different file type');
console.log('✓ isFileType correctly checks file types');
results.passed++;
} catch (error) {
console.log(`✗ isFileType test failed: ${error.message}`);
}
// Test temp file operations
results.total++;
try {
const tempPath = createTempFile('test content', '.txt');
qtests.assert(typeof tempPath === 'string', 'createTempFile should return path');
qtests.assert(fileExists(tempPath), 'Temp file should exist');
const content = readFileContent(tempPath);
qtests.assert(content === 'test content', 'Temp file should have correct content');
// Cleanup
fs.unlinkSync(tempPath);
console.log('✓ createTempFile correctly creates temporary files');
results.passed++;
} catch (error) {
console.log(`✗ temp file operations test failed: ${error.message}`);
}
// Test resolveProjectPath function
results.total++;
try {
const resolved = resolveProjectPath('package.json');
qtests.assert(typeof resolved === 'string', 'resolveProjectPath should return string');
qtests.assert(path.isAbsolute(resolved), 'Should return absolute path');
qtests.assert(resolved.includes('package.json'), 'Should include filename');
console.log('✓ resolveProjectPath correctly resolves project paths');
results.passed++;
} catch (error) {
console.log(`✗ resolveProjectPath test failed: ${error.message}`);
}
// Test error handling
results.total++;
try {
qtests.assert(typeof readFileContent === 'function', 'Functions should be available');
console.log('✓ All file system utilities are properly exported');
results.passed++;
} catch (error) {
console.log(`✗ Error handling test failed: ${error.message}`);
}
console.log(`=== File System Utilities Test Results ===`);
console.log(`Tests passed: ${results.passed}/${results.total}`);
console.log(`Success rate: ${((results.passed / results.total) * 100).toFixed(1)}%\n`);
return results;
}
module.exports = { runTests };