From a224497fa0b64d8adb744c727a34f0851495228f Mon Sep 17 00:00:00 2001 From: Pathurs Date: Wed, 26 Jun 2019 03:59:44 +1000 Subject: [PATCH] fix: skip tarball move if config is `cwd` --- lib/prepare.js | 9 ++++++++- test/prepare.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/prepare.js b/lib/prepare.js index 9bc65c51..d2910c6f 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -35,6 +35,13 @@ module.exports = async ({tarballDir, pkgRoot}, {cwd, env, stdout, stderr, nextRe ); const tarball = (await packResult).stdout.split('\n').pop(); - await move(path.resolve(cwd, tarball), path.resolve(cwd, tarballDir.trim(), tarball)); + const tarballSource = path.resolve(cwd, tarball); + const tarballDestination = path.resolve(cwd, tarballDir.trim(), tarball); + + // Only move the tarball if we need to + // Fixes: https://github.com/semantic-release/npm/issues/169 + if (tarballSource !== tarballDestination) { + await move(tarballSource, tarballDestination); + } } }; diff --git a/test/prepare.test.js b/test/prepare.test.js index 74d56890..361d11e0 100644 --- a/test/prepare.test.js +++ b/test/prepare.test.js @@ -226,3 +226,29 @@ test('Create the package in the "tarballDir" directory', async t => { // Verify the logger has been called with the version updated t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]); }); + +test('Only move the created tarball if the "tarballDir" directory is not the CWD', async t => { + const cwd = tempy.directory(); + const packagePath = path.resolve(cwd, 'package.json'); + const pkg = {name: 'my-pkg', version: '0.0.0-dev'}; + await outputJson(packagePath, pkg); + + await prepare( + {tarballDir: '.'}, + { + cwd, + env: {}, + stdout: t.context.stdout, + stderr: t.context.stderr, + nextRelease: {version: '1.0.0'}, + logger: t.context.logger, + } + ); + + // Verify package.json has been updated + t.is((await readJson(packagePath)).version, '1.0.0'); + + t.true(await pathExists(path.resolve(cwd, `${pkg.name}-1.0.0.tgz`))); + // Verify the logger has been called with the version updated + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]); +});