From 5fdbf2613074d2fbddda2f615ee22aca92c7d749 Mon Sep 17 00:00:00 2001 From: Nathan Sarang-Walters Date: Mon, 10 Jun 2024 16:31:34 -0700 Subject: [PATCH] Support calling remove callback multiple times --- lib/tmp.js | 2 ++ test/inband-standard.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/tmp.js b/lib/tmp.js index 84aa1c5..73c8c02 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -376,6 +376,8 @@ function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCall } else { return removeFunction(fileOrDirName, next || function() {}); } + } else if (!sync) { + next(); } }; } diff --git a/test/inband-standard.js b/test/inband-standard.js index 3b5e5d5..18bfd36 100644 --- a/test/inband-standard.js +++ b/test/inband-standard.js @@ -92,6 +92,20 @@ function inbandStandardTests(testOpts, opts, isFile, beforeHook, sync = false) { throw err; } }.bind(topic)); + + it('should support removeCallback called multiple times', function () { + try { + this.topic.removeCallback(); + } catch (err) { + // important: remove file or dir unconditionally + try { + fs.rmSync(this.topic.name, { recursive: true }); + } catch (_ignored) { + // ignore + } + throw err; + } + }.bind(topic)); } else { it('should have a working removeCallback', function (done) { const self = this; @@ -106,6 +120,20 @@ function inbandStandardTests(testOpts, opts, isFile, beforeHook, sync = false) { done(); }); }.bind(topic)); + + it('should support removeCallback called multiple times', function (done) { + const self = this; + this.topic.removeCallback(function (err) { + if (err) return done(err); + try { + assertions.assertDoesNotExist(self.topic.name); + } catch (err) { + fs.rmSync(self.topic.name, { recursive: true }); + return done(err); + } + done(); + }); + }.bind(topic)); } }; }