From 044b342de3dc52433b4f3632f4ba14d789e1f720 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Tue, 26 Jul 2022 17:28:16 +0200 Subject: [PATCH] Fixed random test failure due to asynchronous directory creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - we keep seeing random failures that complain about a directory already existing when trying to create it - the error arises from the `fs.copySync` in this `prepareContentFolder` function, because it tries to create the folder if it doesn't exist - it turns out we're using the asynchronous `fs.ensureDir` without an await just before, so it doesn't block on creating the folder - there's a veeeery small window where the code within `copySync` thinks the folder doesn't exist, `ensureDir` creates the folder, and then `copySync` tries to create the folder => 💥 - it looks like we're already `await`-ing `prepareContentFolder`, so we can just switch all the calls to the Promise-based ones and await them - the other `prepareContentFolder` uses the sync versions of the functions, but we can fix that in the future: https://github.com/TryGhost/Ghost/blob/7b7767d4837d15e2dcffc74fbdfc3f2c9ac8f87d/ghost/core/test/utils/e2e-utils.js#L73-L79 --- ghost/core/test/utils/e2e-framework.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ghost/core/test/utils/e2e-framework.js b/ghost/core/test/utils/e2e-framework.js index e7196c0b39..205b34f123 100644 --- a/ghost/core/test/utils/e2e-framework.js +++ b/ghost/core/test/utils/e2e-framework.js @@ -78,20 +78,20 @@ const startGhost = async (options = {}) => { * Slightly simplified copy-paste from e2e-utils. * @param {Object} options */ -const prepareContentFolder = ({contentFolder, redirectsFile = true, routesFile = true}) => { +const prepareContentFolder = async ({contentFolder, redirectsFile = true, routesFile = true}) => { const contentFolderForTests = contentFolder; - fs.ensureDir(contentFolderForTests); - fs.ensureDir(path.join(contentFolderForTests, 'data')); - fs.ensureDir(path.join(contentFolderForTests, 'themes')); - fs.ensureDir(path.join(contentFolderForTests, 'images')); - fs.ensureDir(path.join(contentFolderForTests, 'logs')); - fs.ensureDir(path.join(contentFolderForTests, 'adapters')); - fs.ensureDir(path.join(contentFolderForTests, 'settings')); + await fs.ensureDir(contentFolderForTests); + await fs.ensureDir(path.join(contentFolderForTests, 'data')); + await fs.ensureDir(path.join(contentFolderForTests, 'themes')); + await fs.ensureDir(path.join(contentFolderForTests, 'images')); + await fs.ensureDir(path.join(contentFolderForTests, 'logs')); + await fs.ensureDir(path.join(contentFolderForTests, 'adapters')); + await fs.ensureDir(path.join(contentFolderForTests, 'settings')); // Copy all themes into the new test content folder. Default active theme is always casper. // If you want to use a different theme, you have to set the active theme (e.g. stub) - fs.copySync( + await fs.copy( path.join(__dirname, 'fixtures', 'themes'), path.join(contentFolderForTests, 'themes') ); @@ -101,7 +101,7 @@ const prepareContentFolder = ({contentFolder, redirectsFile = true, routesFile = } if (routesFile) { - fs.copySync( + await fs.copy( path.join(__dirname, 'fixtures', 'settings', 'routes.yaml'), path.join(contentFolderForTests, 'settings', 'routes.yaml') );