Skip to content

Commit

Permalink
feat: verify package.json has a version property
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Dec 31, 2017
1 parent 6123e84 commit 3c975df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions lib/get-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ const SemanticReleaseError = require('@semantic-release/error');
module.exports = async pkgRoot => {
try {
const pkg = await readPkg(pkgRoot);

if (!pkg.name) {
throw new SemanticReleaseError('No "name" found in package.json.', 'ENOPKGNAME');
}

if (!pkg.version) {
throw new SemanticReleaseError('No "version" found in package.json.', 'ENOPKGVERSION');
}

return pkg;
} catch (err) {
if (err.code === 'ENOENT') {
Expand Down
13 changes: 11 additions & 2 deletions test/get-pkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test.afterEach.always(() => {
process.chdir(cwd);
});

test.serial('Verify name and return parsed package.json', async t => {
test.serial('Verify name and version then return parsed package.json', async t => {
const pkg = {name: 'package', version: '0.0.0'};
await outputJson('./package.json', pkg);

Expand All @@ -25,7 +25,7 @@ test.serial('Verify name and return parsed package.json', async t => {
t.is(pkg.version, result.version);
});

test.serial('Verify name and return parsed package.json from a sub-directory', async t => {
test.serial('Verify name and version then return parsed package.json from a sub-directory', async t => {
const pkg = {name: 'package', version: '0.0.0'};
await outputJson('./dist/package.json', pkg);

Expand All @@ -50,6 +50,15 @@ test.serial('Throw error if missing package name', async t => {
t.is(error.code, 'ENOPKGNAME');
});

test.serial('Throw error if missing package version', async t => {
await outputJson('./package.json', {name: 'package'});

const error = await t.throws(getPkg());

t.is(error.name, 'SemanticReleaseError');
t.is(error.code, 'ENOPKGVERSION');
});

test.serial('Throw error if package.json is malformed', async t => {
await writeFile('./package.json', "{name: 'package',}");

Expand Down
14 changes: 7 additions & 7 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ test.serial(

test.serial('Verify npm auth and package', async t => {
Object.assign(process.env, npmRegistry.authEnv);
const pkg = {name: 'valid-token', publishConfig: {registry: npmRegistry.url}};
const pkg = {name: 'valid-token', version: '0.0.0-dev', publishConfig: {registry: npmRegistry.url}};
await outputJson('./package.json', pkg);
await t.notThrows(t.context.m.verifyConditions({}, {options: {}, logger: t.context.logger}));

Expand All @@ -127,7 +127,7 @@ test.serial('Verify npm auth and package', async t => {

test.serial('Verify npm auth and package from a sub-directory', async t => {
Object.assign(process.env, npmRegistry.authEnv);
const pkg = {name: 'valid-token', publishConfig: {registry: npmRegistry.url}};
const pkg = {name: 'valid-token', version: '0.0.0-dev', publishConfig: {registry: npmRegistry.url}};
await outputJson('./dist/package.json', pkg);
await t.notThrows(t.context.m.verifyConditions({pkgRoot: 'dist'}, {options: {}, logger: t.context.logger}));

Expand All @@ -139,7 +139,7 @@ test.serial('Verify npm auth and package from a sub-directory', async t => {
test.serial('Verify npm auth and package with "npm_config_registry" env var set by yarn', async t => {
Object.assign(process.env, npmRegistry.authEnv);
process.env.npm_config_registry = 'https://registry.yarnpkg.com'; // eslint-disable-line camelcase
const pkg = {name: 'valid-token', publishConfig: {registry: npmRegistry.url}};
const pkg = {name: 'valid-token', version: '0.0.0-dev', publishConfig: {registry: npmRegistry.url}};
await outputJson('./package.json', pkg);
await t.notThrows(t.context.m.verifyConditions({}, {options: {}, logger: t.context.logger}));

Expand All @@ -150,7 +150,7 @@ test.serial('Verify npm auth and package with "npm_config_registry" env var set

test.serial('Return nothing if no version if published', async t => {
Object.assign(process.env, npmRegistry.authEnv);
const pkg = {name: 'not-published', publishConfig: {registry: npmRegistry.url}};
const pkg = {name: 'not-published', version: '0.0.0-dev', publishConfig: {registry: npmRegistry.url}};
await outputJson('./package.json', pkg);
const nextRelease = await t.context.m.getLastRelease({}, {options: {}, logger: t.context.logger});

Expand Down Expand Up @@ -243,7 +243,7 @@ test('Throw SemanticReleaseError if publish "pkgRoot" option in getLastRelease i
});

test('Throw SemanticReleaseError if publish "npmPublish" option in verifyConditions is not a Boolean', async t => {
const pkg = {name: 'invalid-npmPublish', publishConfig: {registry: npmRegistry.url}};
const pkg = {name: 'invalid-npmPublish', version: '0.0.0-dev', publishConfig: {registry: npmRegistry.url}};
await outputJson('./package.json', pkg);
const npmPublish = 42;
const error = await t.throws(
Expand All @@ -261,7 +261,7 @@ test('Throw SemanticReleaseError if publish "npmPublish" option in verifyConditi
});

test('Throw SemanticReleaseError if publish "tarballDir" option in verifyConditions is not a String', async t => {
const pkg = {name: 'invalid-tarballDir', publishConfig: {registry: npmRegistry.url}};
const pkg = {name: 'invalid-tarballDir', version: '0.0.0-dev', publishConfig: {registry: npmRegistry.url}};
await outputJson('./package.json', pkg);
const tarballDir = 42;
const error = await t.throws(
Expand All @@ -279,7 +279,7 @@ test('Throw SemanticReleaseError if publish "tarballDir" option in verifyConditi
});

test('Throw SemanticReleaseError if publish "pkgRoot" option in verifyConditions is not a String', async t => {
const pkg = {name: 'invalid-pkgRoot', publishConfig: {registry: npmRegistry.url}};
const pkg = {name: 'invalid-pkgRoot', version: '0.0.0-dev', publishConfig: {registry: npmRegistry.url}};
await outputJson('./dist/package.json', pkg);
const pkgRoot = 42;
const error = await t.throws(
Expand Down

0 comments on commit 3c975df

Please sign in to comment.