Skip to content

Commit

Permalink
Add rule for repository.directory field (#106)
Browse files Browse the repository at this point in the history
* Add rule for repository.directory field

* Ensure that repository exists
  • Loading branch information
Ned Zimmerman authored and tclindner committed Mar 1, 2019
1 parent 6d2b71a commit aa3319d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/rules/require-repository-directory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const LintIssue = require('./../LintIssue');

const lintId = 'require-repository-directory';
const nodeName = 'repository';
const parentNodeMessage = 'repository is required';
const message = 'repository object missing directory property';
const ruleType = 'standard';

const lint = (packageJsonData, severity) => {
if (!packageJsonData.hasOwnProperty(nodeName)) {
return new LintIssue(lintId, severity, nodeName, parentNodeMessage);
}

if (!packageJsonData[nodeName].hasOwnProperty('directory')) {
return new LintIssue(lintId, severity, nodeName, message);
}

return true;
};

module.exports.lint = lint;
module.exports.ruleType = ruleType;
53 changes: 53 additions & 0 deletions test/unit/rules/require-repository-directory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const ruleModule = require('./../../../src/rules/require-repository-directory');

const {lint, ruleType} = ruleModule;

describe('require-repository-directory Unit Tests', () => {
describe('a rule type value should be exported', () => {
test('it should equal "standard"', () => {
expect(ruleType).toStrictEqual('standard');
});
});

describe('when package.json does not have parent node', () => {
test('false should be returned', () => {
const packageJsonData = {};
const response = lint(packageJsonData, 'error');

expect(response.lintId).toStrictEqual('require-repository-directory');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('repository');
expect(response.lintMessage).toStrictEqual('repository is required');
});
});

describe('when package.json has node', () => {
test('true should be returned', () => {
const packageJsonData = {
repository: {
url: 'https://github.com/packages/monorepo',
directory: 'packages/somepackage'
}
};
const response = lint(packageJsonData, 'error');

expect(response).toBeTruthy();
});
});

describe('when package.json does not have node', () => {
test('LintIssue object should be returned', () => {
const packageJsonData = {
repository: {
url: 'https://github.com/packages/monorepo'
}
};
const response = lint(packageJsonData, 'error');

expect(response.lintId).toStrictEqual('require-repository-directory');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('repository');
expect(response.lintMessage).toStrictEqual('repository object missing directory property');
});
});
});

0 comments on commit aa3319d

Please sign in to comment.