Skip to content

Commit

Permalink
Merge pull request #116 from savetheclocktower/bug-fixes
Browse files Browse the repository at this point in the history
Fix bugs found in `publish` after 1.112 release
  • Loading branch information
DeeDeeG committed Dec 18, 2023
2 parents 957acbd + 1ead662 commit f660a7a
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 27 deletions.
117 changes: 116 additions & 1 deletion spec/publish-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,64 @@ const fs = require('fs-plus');
const temp = require('temp');
const express = require('express');
const http = require('http');
const childProcess = require('child_process');
const apm = require('../src/apm-cli');
const Publish = require('../src/publish');
const Command = require('../src/command');

describe('apm publish', () => {
let server;

let requests;
beforeEach(() => {
delete process.env.ATOM_PACKAGES_URL;
spyOnToken();
silenceOutput();

spyOn(Command.prototype, 'spawn').andCallFake(
(command, args, optionsOrCallback, callbackOrMissing) => {
const [callback, options] = callbackOrMissing == null
? [optionsOrCallback]
: [callbackOrMissing, optionsOrCallback];

callback(0, '', '');
}
);

spyOn(Publish.prototype, 'waitForTagToBeAvailable').andCallFake(
() => {
return Promise.resolve();
}
);

spyOn(Publish.prototype, 'versionPackage').andCallFake(
(version) => {
return Promise.resolve('0.0.1');
}
);

requests = [];
const app = express();

app.post('/api/packages', (req, res) => {
requests.push(req);
res.sendStatus(201);
});

app.post('/api/packages/:packageName/versions', (req, res) => {
requests.push(req);
res.sendStatus(201);
});

server = http.createServer(app);
let live = false;
server.listen(3000, '127.0.0.1', () => {
process.env.ATOM_HOME = temp.mkdirSync('apm-home-dir-');
process.env.ATOM_API_URL = 'http://localhost:3000/api';
process.env.ATOM_RESOURCE_PATH = temp.mkdirSync('atom-resource-path-');
live = true;
setTimeout(() => {
live = true;
}, 3000);
});
waitsFor(() => live);
});
Expand Down Expand Up @@ -128,4 +170,77 @@ describe('apm publish', () => {
expect(callback.mostRecentCall.args[0].message).toBe('The bar dev dependency range in the package.json file is invalid: 1,3');
});
});

it('publishes successfully when new', () => {
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'], callback);
waitsFor('waiting for publish to complete', 600000, () => callback.callCount === 1);
runs(() => {
expect(requests.length).toBe(2);
expect(callback.mostRecentCall.args[0]).toBeUndefined();
});
});

it('publishes successfully when package exists', () => {
spyOn(Publish.prototype, 'packageExists').andCallFake(() => {
return Promise.resolve(true);
});
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'], callback);
waitsFor('waiting for publish to complete', 600000, () => callback.callCount === 1);
runs(() => {
expect(requests.length).toBe(1);
expect(callback.mostRecentCall.args[0]).toBeUndefined();
});
});
});
8 changes: 4 additions & 4 deletions src/apm.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ module.exports = {
if (process.env.ATOM_RESOURCE_PATH) {
return void process.nextTick(() => resolve(process.env.ATOM_RESOURCE_PATH));
}

if (asarPath) { // already calculated
return void process.nextTick(() => resolve(asarPath));
}

let apmFolder = path.resolve(__dirname, '..');
let appFolder = path.dirname(apmFolder);
if ((path.basename(apmFolder) === 'ppm') && (path.basename(appFolder) === 'app')) {
Expand All @@ -43,7 +43,7 @@ module.exports = {
return void process.nextTick(() => resolve(asarPath));
}
}

apmFolder = path.resolve(__dirname, '..', '..', '..');
appFolder = path.dirname(apmFolder);
if ((path.basename(apmFolder) === 'ppm') && (path.basename(appFolder) === 'app')) {
Expand All @@ -52,7 +52,7 @@ module.exports = {
return void process.nextTick(() => resolve(asarPath));
}
}

switch (process.platform) {
case 'darwin':
return child_process.exec('mdfind "kMDItemCFBundleIdentifier == \'dev.pulsar-edit.pulsar\'"', (error, stdout, _stderr) => {
Expand Down
50 changes: 28 additions & 22 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ have published it.\
return new Promise((resolve, reject) => {
this.fork(this.atomNpmPath, versionArgs, (code, stderr, stdout) => {
stderr ??= '';
stdout ??= '';
stdout ??= '';
if (code === 0) {
this.logSuccess();
resolve(stdout.trim());
Expand Down Expand Up @@ -114,28 +114,33 @@ have published it.\
};

return new Promise((resolve, _reject) => {
const requestTags = () => void request.get(requestSettings, (_error, response, tags) => {
tags ??= [];
if (response?.statusCode === 200) {
if (tags.find(elem => elem.name === tag) != null) {
resolve();
return;
const requestTags = () => {
request.get(
requestSettings,
(_error, response, tags) => {
tags ??= [];
if (response?.statusCode === 200) {
if (tags.some(t => t.name === tag)) {
resolve();
return;
}
}
if (--retryCount <= 0) {
return void resolve();
}
}
}
if (--retryCount <= 0) {
return void resolve();
}

)
setTimeout(requestTags, interval);
});
};
requestTags();
});
}

// Does the given package already exist in the registry?
//
// packageName - The string package name to check.
//
// return value - A Promise that can reject with an error or resolve to
// return value - A Promise that can reject with an error or resolve to
// a boolean value.
async packageExists(packageName) {
const token = await Login.getTokenOrLogin();
Expand Down Expand Up @@ -169,7 +174,7 @@ have published it.\
}

const exists = await this.packageExists(pack.name);
if (exists) { return Promise.reject(); }
if (exists) { return Promise.resolve(false); }

const repository = Packages.getRepository(pack);

Expand All @@ -181,7 +186,7 @@ have published it.\

try {
const token = await Login.getTokenOrLogin();

const requestSettings = {
url: config.getAtomPackagesUrl(),
json: true,
Expand Down Expand Up @@ -221,7 +226,7 @@ have published it.\
//
// return value - A Promise that rejects with an error or resolves without a value.
async createPackageVersion(packageName, tag, options) {
const token = await Login.getTokenOrLogin();
const token = await Login.getTokenOrLogin();
const requestSettings = {
url: `${config.getAtomPackagesUrl()}/${packageName}/versions`,
json: true,
Expand Down Expand Up @@ -324,7 +329,7 @@ have published it.\

// Rename package if necessary
async renamePackage(pack, name) {
if (name?.length <= 0) {
if ((name ?? '').length <= 0) {
// Just fall through if the name is empty
return; // error or return value?
}
Expand Down Expand Up @@ -376,7 +381,7 @@ have published it.\
let packageName, semverRange;
if (!pack) { return; }

const isValidRange = function(semverRange) {
const isValidRange = function (semverRange) {
if (semver.validRange(semverRange)) { return true; }

try {
Expand Down Expand Up @@ -440,10 +445,11 @@ have published it.\
if (rename?.length > 0) { originalName = pack.name; }

let firstTimePublishing;
let tag;
try {
firstTimePublishing = await this.registerPackage(pack);
await this.renamePackage(pack, rename);
const tag = await this.versionPackage(version);
tag = await this.versionPackage(version);
await this.pushVersion(tag, pack);
} catch (error) {
return error;
Expand All @@ -456,7 +462,7 @@ have published it.\
rename = pack.name;
pack.name = originalName;
}

try {
await this.publishPackage(pack, tag, {rename});
} catch (error) {
Expand All @@ -472,7 +478,7 @@ have published it.\
} catch (error) {
return error;
}

try {
await this.publishPackage(pack, tag);
} catch (error) {
Expand Down

0 comments on commit f660a7a

Please sign in to comment.