Skip to content

Commit

Permalink
Merge pull request #135 from savetheclocktower/fix-publish-rename
Browse files Browse the repository at this point in the history
Fix incorrect behavior on package rename
  • Loading branch information
DeeDeeG authored Aug 10, 2024
2 parents a5930b4 + 9f370c2 commit d9bcff1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
6 changes: 2 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: "^_"
Expand Down
52 changes: 52 additions & 0 deletions spec/publish-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
});

});

});
5 changes: 4 additions & 1 deletion src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d9bcff1

Please sign in to comment.