Skip to content

Commit

Permalink
test: update fs.rmSync test to use tmpdir as per review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeaseen committed Dec 3, 2024
1 parent 573e5e6 commit 0486aea
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions test/parallel/test-fs-rmSync-special-char.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');

const fs = require('fs');
const path = require('path');
const assert = require('assert');
// This test ensures that fs.rmSync can handle UTF-8 characters
// in file paths without errors.

// Specify the path for the test file
const filePath = path.join(__dirname, '速.txt');
tmpdir.refresh(); // Prepare a clean temporary directory

// Create a file with the specified name
fs.writeFileSync(filePath, 'This is a test file containing special characters in the name.');
const filePath = path.join(tmpdir.path, '速.txt'); // Use tmpdir.path for the file location

// Function to try removing the file and verify the outcome
function testRemoveFile() {
try {
// Attempt to remove the file
fs.rmSync(filePath);

// Check if the file still exists to confirm it was removed
assert.ok(!fs.existsSync(filePath), 'The file should be removed.');
// Create a file with a name containing non-ASCII characters
fs.writeFileSync(filePath, 'This is a test file with special characters.');

console.log('File removed successfully.');
} catch (error) {
// Output an error if the removal fails
console.error('Failed to remove file:', error);

// Explicitly fail the test if an error is thrown
assert.fail('File removal should not throw an error.');
}
}
// Attempt to remove the file and assert no errors occur
assert.doesNotThrow(() => {
fs.rmSync(filePath);
}, 'fs.rmSync should not throw an error for non-ASCII file names');

// Run the test function
testRemoveFile();
// Ensure the file has been removed
assert.strictEqual(fs.existsSync(filePath), false,
'The file should be removed successfully');

0 comments on commit 0486aea

Please sign in to comment.