From 0486aea51640f917b78f018c9a8e2baaf0b0d824 Mon Sep 17 00:00:00 2001 From: Yeaseen Date: Tue, 3 Dec 2024 04:41:37 -0700 Subject: [PATCH] test: update fs.rmSync test to use tmpdir as per review feedback --- test/parallel/test-fs-rmSync-special-char.js | 44 ++++++++------------ 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/test/parallel/test-fs-rmSync-special-char.js b/test/parallel/test-fs-rmSync-special-char.js index 2bba3355816ecf..fdee9c241d1dee 100644 --- a/test/parallel/test-fs-rmSync-special-char.js +++ b/test/parallel/test-fs-rmSync-special-char.js @@ -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(); \ No newline at end of file +// Ensure the file has been removed +assert.strictEqual(fs.existsSync(filePath), false, + 'The file should be removed successfully');