Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #10072 from adobe/dangoor/9910-project-tree-root-dir
Browse files Browse the repository at this point in the history
Ensure that the baseDir for createNewItem is within the project.
  • Loading branch information
RaymondLim committed Dec 4, 2014
2 parents f016607 + 3a4d454 commit 23a2556
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/project/ProjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,13 +1022,7 @@ define(function (require, exports, module) {
* filename.
*/
function createNewItem(baseDir, initialName, skipRename, isFolder) {
if (typeof baseDir === "string") {
if (_.last(baseDir) !== "/") {
baseDir += "/";
}
} else {
baseDir = baseDir.fullPath;
}
baseDir = model.getDirectoryInProject(baseDir);

if (skipRename) {
return model.createAtPath(baseDir + initialName, isFolder);
Expand Down
24 changes: 24 additions & 0 deletions src/project/ProjectModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,30 @@ define(function (require, exports, module) {
}
return absPath;
};

/**
* Returns a valid directory within the project, either the path (or Directory object)
* provided or the project root.
*
* @param {string|Directory} path Directory path to verify against the project
* @return {string} A directory path within the project.
*/
ProjectModel.prototype.getDirectoryInProject = function (path) {
if (path && typeof path === "string") {
if (_.last(path) !== "/") {
path += "/";
}
} else if (path && path.isDirectory) {
path = path.fullPath;
} else {
path = null;
}

if (!path || (typeof path !== "string") || !this.isWithinProject(path)) {
path = this.projectRoot.fullPath;
}
return path;
};

/**
* @private
Expand Down
21 changes: 21 additions & 0 deletions test/spec/ProjectModel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ define(function (require, exports, module) {
it("won't create a relative path to a file outside the project", function () {
expect(pm.makeProjectRelativeIfPossible("/some/other/project/README.md")).toBe("/some/other/project/README.md");
});

it("will return a directory within the project", function () {
expect(pm.getDirectoryInProject("/foo/bar/project/baz/")).toBe("/foo/bar/project/baz/");
expect(pm.getDirectoryInProject("/foo/bar/project/baz")).toBe("/foo/bar/project/baz/");
expect(pm.getDirectoryInProject({
fullPath: "/foo/bar/project/foo2/",
isDirectory: true
})).toBe("/foo/bar/project/foo2/");
});

it("will default to project root when getDirectoryInProject", function () {
expect(pm.getDirectoryInProject()).toBe("/foo/bar/project/");
expect(pm.getDirectoryInProject(null)).toBe("/foo/bar/project/");
expect(pm.getDirectoryInProject("")).toBe("/foo/bar/project/");
expect(pm.getDirectoryInProject({
isFile: true,
isDirectory: false,
fullPath: "/foo/bar/project/README.txt"
})).toBe("/foo/bar/project/");
expect(pm.getDirectoryInProject("/other/project/")).toBe("/foo/bar/project/");
});
});

describe("All Files Cache", function () {
Expand Down

0 comments on commit 23a2556

Please sign in to comment.