diff --git a/test/common-tap.js b/test/common-tap.js index 83a61f4bdbef..d8dc8a10d870 100644 --- a/test/common-tap.js +++ b/test/common-tap.js @@ -60,29 +60,31 @@ const find = require('which').sync('find') require('tap').teardown(() => { // work around windows folder locking process.chdir(returnCwd) - try { - if (isSudo) { - // running tests as sudo. ensure we didn't leave any root-owned - // files in the cache by mistake. - const args = [ commonCache, '-uid', '0' ] - const found = spawnSync(find, args) - const output = found && found.stdout && found.stdout.toString() - if (output.length) { - const er = new Error('Root-owned files left in cache!') - er.testName = main - er.files = output.trim().split('\n') - throw er + process.on('exit', () => { + try { + if (isSudo) { + // running tests as sudo. ensure we didn't leave any root-owned + // files in the cache by mistake. + const args = [ commonCache, '-uid', '0' ] + const found = spawnSync(find, args) + const output = found && found.stdout && found.stdout.toString() + if (output.length) { + const er = new Error('Root-owned files left in cache!') + er.testName = main + er.files = output.trim().split('\n') + throw er + } + } + if (!process.env.NO_TEST_CLEANUP) { + rimraf.sync(exports.pkg) + rimraf.sync(commonCache) + } + } catch (e) { + if (process.platform !== 'win32') { + throw e } } - if (!process.env.NO_TEST_CLEANUP) { - rimraf.sync(exports.pkg) - rimraf.sync(commonCache) - } - } catch (e) { - if (process.platform !== 'win32') { - throw e - } - } + }) }) var port = exports.port = 15443 + testId diff --git a/test/tap/meta-test-flaky-root-ownership-test.js b/test/tap/meta-test-flaky-root-ownership-test.js new file mode 100644 index 000000000000..24dd9e3d95dd --- /dev/null +++ b/test/tap/meta-test-flaky-root-ownership-test.js @@ -0,0 +1,20 @@ +const t = require('tap') +if (!process.getuid || process.getuid() !== 0 || !process.env.SUDO_UID || !process.env.SUDO_GID) { + t.pass('this test only runs in sudo mode') + t.end() + process.exit(0) +} + +const common = require('../common-tap.js') +const fs = require('fs') +const mkdirp = require('mkdirp') +mkdirp.sync(common.cache + '/root/owned') +fs.writeFileSync(common.cache + '/root/owned/file.txt', 'should be chowned') +const chown = require('chownr') + +// this will fire after t.teardown() but before process.on('exit') +setTimeout(() => { + chown.sync(common.cache, +process.env.SUDO_UID, +process.env.SUDO_GID) +}, 100) + +t.pass('this is fine')