nodejs/Integration/fs-module/script.js
2025-01-08 12:49:33 +01:00

41 lines
1.2 KiB
JavaScript

const fs = require('fs');
const path = require('path');
// Test file path
const testFile = path.join(__dirname, 'test-file.txt');
try {
// Step 1: Write to a file
console.log('Writing to file...');
const data = 'Hello, TMT fs test!';
fs.writeFileSync(testFile, data);
if (fs.existsSync(testFile)) {
console.log('✔ File created successfully.');
} else {
throw new Error('✘ Failed to create file.');
}
// Step 2: Read from the file
console.log('Reading from file...');
const fileContent = fs.readFileSync(testFile, 'utf8');
if (fileContent === data) {
console.log('✔ File content matches expected.');
} else {
throw new Error('✘ File content mismatch.');
}
// Step 3: Delete the file
console.log('Deleting file...');
fs.unlinkSync(testFile);
if (!fs.existsSync(testFile)) {
console.log('✔ File deleted successfully.');
} else {
throw new Error('✘ Failed to delete file.');
}
console.log('All tests passed!');
process.exit(0); // Success exit code
} catch (error) {
console.error(error.message);
process.exit(1); // Failure exit code
}