Skip to content

Commit

Permalink
Sanitize project name before using it in directory name
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Mar 6, 2019
1 parent 3e6b5a4 commit 0d4a77f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/tasks/generateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ async function createTmpDirs(projectName) {
* @returns {Promise<Object>} Promise resolving with path of the temporary directory
*/
function createTmpDir(projectName) {
// Remove all non alpha-num characters from project name
const sanitizedProjectName = projectName.replace(/[^A-Za-z0-9]/g, "");
return new Promise((resolve, reject) => {
tmp.dir({
prefix: `ui5-tooling-tmp-jsdoc-${projectName}-`,
prefix: `ui5-tooling-tmp-jsdoc-${sanitizedProjectName}-`,
// keep: true,
unsafeCleanup: true
}, (err, path) => {
Expand Down
6 changes: 3 additions & 3 deletions test/lib/tasks/generateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ test.afterEach.always((t) => {
test.serial("createTmpDir successful", async (t) => {
t.context.tmpStub.callsArgWithAsync(1, undefined, "some/path");

const res = await generateJsdoc._createTmpDir("some.namespace");
const res = await generateJsdoc._createTmpDir("som$e.nam3/space"); // non alphanum characters get removed

t.deepEqual(t.context.tmpStub.callCount, 1, "Tmp dir is called once");
t.deepEqual(t.context.tmpStub.getCall(0).args[0].prefix, "ui5-tooling-tmp-jsdoc-some.namespace-");
t.deepEqual(t.context.tmpStub.getCall(0).args[0].prefix, "ui5-tooling-tmp-jsdoc-somenam3space-");
t.deepEqual(res, {path: "some/path"}, "Correct path returned");
});

Expand All @@ -30,7 +30,7 @@ test.serial("createTmpDir error", async (t) => {
const res = await t.throws(generateJsdoc._createTmpDir("some.namespace"));

t.deepEqual(t.context.tmpStub.callCount, 1, "Tmp dir is called once");
t.deepEqual(t.context.tmpStub.getCall(0).args[0].prefix, "ui5-tooling-tmp-jsdoc-some.namespace-");
t.deepEqual(t.context.tmpStub.getCall(0).args[0].prefix, "ui5-tooling-tmp-jsdoc-somenamespace-");
t.deepEqual(res, {message: "Dir creation failed"}, "Dir creation failed");
});

Expand Down

0 comments on commit 0d4a77f

Please sign in to comment.