Skip to content

Commit

Permalink
fix: nodeshift should check that there is a name field in the package…
Browse files Browse the repository at this point in the history
….json
  • Loading branch information
lholmquist committed Jan 24, 2022
1 parent ba304f3 commit 2003a7c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/sample-project-no-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
5 changes: 5 additions & 0 deletions lib/config/nodeshift-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ async function setup (options = {}) {

logger.info(`using namespace ${config.namespace.name} at ${kubeConfig.getCurrentCluster().server}`);

// Check that the package.json has a name field first
if (!projectPackage.name) {
throw new Error('could not find required field "name" in package.json');
}

if (!projectPackage.name.match(/^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/)) {
throw new Error('"name" in package.json can only consist lower-case letters, numbers, and dashes. It must start with a letter and can\'t end with a -.');
}
Expand Down
45 changes: 45 additions & 0 deletions test/config-tests/nodeshift-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,51 @@ test('nodeshift-config no project Version', (t) => {
});
});

test('nodeshift-config no project name', (t) => {
const nodeshiftConfig = proxyquire('../../lib/config/nodeshift-config', {
'./login-config': {
doNodeshiftLogin: () => {
t.fail('This should not be called');
},
doNodeshiftLogout: () => {
t.fail('This should not be called');
return Promise.resolve();
},
checkForNodeshiftLogin: () => {
t.pass();
return Promise.resolve();
}
},
'openshift-rest-client': {
OpenshiftClient: () => {
return Promise.resolve({
kubeconfig: {
getCurrentContext: () => {
return 'nodey/ip/other';
},
getCurrentCluster: () => {
return { server: 'http://mock-cluster' };
},
getContexts: () => {
return [{ name: 'nodey/ip/other', namespace: 'test-namespace' }];
}
}
});
}
}
});

const options = {
projectLocation: './examples/sample-project-no-name'
};

nodeshiftConfig(options).catch((error) => {
console.log(error);
t.equal(error.message.includes('could not find required field "name" in package.json'), true);
t.end();
});
});

test('nodeshift-config no package.json', (t) => {
const nodeshiftConfig = proxyquire('../../lib/config/nodeshift-config', {
'./login-config': {
Expand Down

0 comments on commit 2003a7c

Please sign in to comment.