From 9f370c299f3b3c35eb8bb34bdcd7a0bfaccc776f Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sun, 4 Aug 2024 15:12:30 -0700 Subject: [PATCH] Fix incorrect behavior on package rename --- .eslintrc.js | 6 ++--- spec/publish-spec.js | 52 ++++++++++++++++++++++++++++++++++++++++++++ src/publish.js | 5 ++++- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 114226d6..dddd4a7e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,11 +2,11 @@ module.exports = { env: { browser: false, commonjs: true, - es2021: true + es2021: true, + node: true }, extends: [ "eslint:recommended", - "plugin:node/recommended" ], overrides: [], parserOptions: { @@ -18,8 +18,6 @@ module.exports = { asyncArrow: "always", named: "never" }], - "prettier/prettier": "off", - "ember-suave/lines-between-object-propertiess": "off", "no-empty": "off", "no-unused-vars": [ "error", { argsIgnorePattern: "^_" diff --git a/spec/publish-spec.js b/spec/publish-spec.js index 5f8cf95a..e30d511f 100644 --- a/spec/publish-spec.js +++ b/spec/publish-spec.js @@ -210,6 +210,7 @@ describe('apm publish', () => { spyOn(Publish.prototype, 'packageExists').andCallFake(() => { return Promise.resolve(true); }); + const packageToPublish = temp.mkdirSync('apm-test-package-'); const metadata = { name: 'test', @@ -243,4 +244,55 @@ describe('apm publish', () => { expect(callback.mostRecentCall.args[0]).toBeUndefined(); }); }); + + it('publishes successfully when the package exists and is being renamed', () => { + spyOn(Publish.prototype, 'packageExists').andCallFake((name) => { + // If we're renaming the package, we need to ask the API if the package's + // _old_ name exists. This mock will simulate what the API would say if + // we mistakenly ask it if the _new_ name exists. + return name === 'test'; + }); + let publishPackageSpy = spyOn(Publish.prototype, 'publishPackage').andCallThrough(); + let registerPackageSpy = spyOn(Publish.prototype, 'registerPackage').andCallThrough(); + + const packageToPublish = temp.mkdirSync('apm-test-package-'); + const metadata = { + name: 'test', + version: '1.0.0', + "repository": { + "type": "git", + "url": "https://github.com/pulsar-edit/foo" + }, + engines: { + atom: '1' + }, + dependencies: { + foo: '^5' + }, + devDependencies: { + abc: 'git://github.com/user/project.git', + abcd: 'latest', + } + }; + fs.writeFileSync(path.join(packageToPublish, 'package.json'), JSON.stringify(metadata)); + process.chdir(packageToPublish); + + childProcess.execSync('git init', { cwd: packageToPublish }); + childProcess.execSync('git remote add origin https://github.com/pulsar-edit/foo', { cwd: packageToPublish }); + + const callback = jasmine.createSpy('callback'); + apm.run(['publish', 'patch', '--rename', 'test-renamed'], callback); + waitsFor('waiting for publish to complete', 600000, () => callback.callCount === 1); + runs(() => { + expect(registerPackageSpy.calls.length).toBe(0); + expect(publishPackageSpy.calls.length).toBe(1); + expect( + publishPackageSpy.mostRecentCall?.args?.[2]?.rename + ).toBe('test-renamed'); + expect(requests.length).toBe(1); + expect(callback.mostRecentCall.args[0]).toBeUndefined(); + }); + + }); + }); diff --git a/src/publish.js b/src/publish.js index b7e897d6..3d2776dd 100644 --- a/src/publish.js +++ b/src/publish.js @@ -487,7 +487,10 @@ have published it.\ let doesPackageExist; try { - doesPackageExist = await this.packageExists(pack.name); + // If `originalName` is defined, it means we're renaming this package. + // We must therefore check if the package's _old_ name exists, not its + // _new_ name. + doesPackageExist = await this.packageExists(originalName ?? pack.name); } catch(error) { return error; }