From 76ebf567e25d0b7f3ed365dd2da336a9b5768fad Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Wed, 21 Feb 2024 13:11:04 -0700 Subject: fix(ext/fs): make errors in tempfile creation clearer (#22498) When using a prefix or suffix containing an invalid filename character, it's not entirely clear where the errors come from. We make these errors more consistent across platforms. In addition, all permission prompts for tempfile and tempdir were printing the same API name. We also take the opportunity to make the tempfile random space larger by 2x (using a base32-encoded u64 rather than a hex-encoded u32). --- tests/unit/make_temp_test.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests/unit') diff --git a/tests/unit/make_temp_test.ts b/tests/unit/make_temp_test.ts index cbbae8dfe..2c771177b 100644 --- a/tests/unit/make_temp_test.ts +++ b/tests/unit/make_temp_test.ts @@ -155,3 +155,25 @@ Deno.test( } }, ); + +Deno.test( + { permissions: { read: true, write: true } }, + function makeTempInvalidCharacter() { + // Various control and ASCII characters that are disallowed on all platforms + for (const invalid of ["\0", "*", "\x9f"]) { + assertThrows(() => Deno.makeTempFileSync({ prefix: invalid })); + assertThrows(() => Deno.makeTempDirSync({ prefix: invalid })); + assertThrows(() => Deno.makeTempFileSync({ suffix: invalid })); + assertThrows(() => Deno.makeTempDirSync({ suffix: invalid })); + } + + // On Windows, files can't end with space or period + if (Deno.build.os === "windows") { + assertThrows(() => Deno.makeTempFileSync({ suffix: "." })); + assertThrows(() => Deno.makeTempFileSync({ suffix: " " })); + } else { + Deno.makeTempFileSync({ suffix: "." }); + Deno.makeTempFileSync({ suffix: " " }); + } + }, +); -- cgit v1.2.3