Skip to content

Commit

Permalink
fix: add explicit check for folder permissions to create a project
Browse files Browse the repository at this point in the history
  • Loading branch information
kakha urigashvili authored and RonWang committed Dec 8, 2020
1 parent ff33a67 commit 647c826
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/view/prompt-view.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const inquirer = require('inquirer');

const stringUtils = require('@src/utils/string-utils');
Expand All @@ -21,6 +22,11 @@ function getProjectFolderName(defaultName, callback) {
if (!input || stringUtils.filterNonAlphanumeric(input) === '') {
return 'Project folder name should consist of alphanumeric character(s) plus "-" only.';
}
try {
fs.accessSync(process.cwd(), fs.constants.W_OK);
} catch (error) {
return `No write access inside of the folder: ${process.cwd()}.`;
}
return true;
}
}]).then((answer) => {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/view/prompt-view-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { expect } = require('chai');
const fs = require('fs');
const inquirer = require('inquirer');
const sinon = require('sinon');

Expand All @@ -19,6 +20,7 @@ function validateInquirerConfig(stub, expectedConfig) {
describe('View test - prompt view test', () => {
const TEST_ERROR = 'init error';
let infoStub;
let accessSyncStub;

describe('# validate ui.getProjectFolderName', () => {
beforeEach(() => {
Expand All @@ -27,6 +29,7 @@ describe('View test - prompt view test', () => {
sinon.stub(Messenger, 'getInstance').returns({
info: infoStub
});
accessSyncStub = sinon.stub(fs, 'accessSync');
});

afterEach(() => {
Expand Down Expand Up @@ -96,5 +99,18 @@ describe('View test - prompt view test', () => {
done();
});
});
it('| user does not have read/write to project folder, expect return error message string', (done) => {
// setup
const TEST_DEFAULT_NAME = 'some-folder';
inquirer.prompt.resolves({ projectFolderName: TEST_DEFAULT_NAME });
accessSyncStub.throws('Some error');
// call
ui.getProjectFolderName(TEST_DEFAULT_NAME, () => {
// verify
expect(inquirer.prompt.args[0][0][0].validate(TEST_DEFAULT_NAME))
.include('No write access inside of the folder');
done();
});
});
});
});

0 comments on commit 647c826

Please sign in to comment.