diff --git a/src/brackets.js b/src/brackets.js index 4b36cc894..fa3a6bc9f 100644 --- a/src/brackets.js +++ b/src/brackets.js @@ -220,7 +220,7 @@ define(function (require, exports, module) { if (!params.get("skipSampleProjectLoad") && !prefs.getValue("afterFirstLaunch")) { prefs.setValue("afterFirstLaunch", "true"); if (ProjectManager.isWelcomeProjectPath(initialProjectPath)) { - brackets.appFileSystem.resolve(initialProjectPath + "/index.html", function (err, file) { + brackets.appFileSystem.resolve(initialProjectPath + "index.html", function (err, file) { if (!err) { var promise = CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, { fullPath: file.fullPath }); promise.then(deferred.resolve, deferred.reject); diff --git a/src/extensions/default/JavaScriptCodeHints/ScopeManager.js b/src/extensions/default/JavaScriptCodeHints/ScopeManager.js index 97459eb85..51f40da57 100644 --- a/src/extensions/default/JavaScriptCodeHints/ScopeManager.js +++ b/src/extensions/default/JavaScriptCodeHints/ScopeManager.js @@ -1444,6 +1444,12 @@ define(function (require, exports, module) { function handleProjectOpen(projectRootPath) { initPreferences(projectRootPath); } + + + /** Used to avoid timing bugs in unit tests */ + function _readyPromise() { + return deferredPreferences; + } exports.getBuiltins = getBuiltins; exports.getResolvedPath = getResolvedPath; @@ -1456,5 +1462,6 @@ define(function (require, exports, module) { exports.requestParameterHint = requestParameterHint; exports.handleProjectClose = handleProjectClose; exports.handleProjectOpen = handleProjectOpen; + exports._readyPromise = _readyPromise; }); diff --git a/src/extensions/default/JavaScriptCodeHints/unittests.js b/src/extensions/default/JavaScriptCodeHints/unittests.js index 4b4917343..b5c4228f5 100644 --- a/src/extensions/default/JavaScriptCodeHints/unittests.js +++ b/src/extensions/default/JavaScriptCodeHints/unittests.js @@ -22,7 +22,7 @@ */ /*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50, regexp: true */ -/*global define, describe, xdescribe, it, xit, expect, beforeEach, afterEach, waitsFor, runs, $, brackets, waitsForDone, spyOn */ +/*global define, describe, it, xit, expect, beforeEach, afterEach, waitsFor, runs, $, brackets, waits, waitsForDone, spyOn */ define(function (require, exports, module) { "use strict"; @@ -424,7 +424,7 @@ define(function (require, exports, module) { } } - function setupTest(path, primePump) { + function setupTest(path, primePump) { // FIXME: primePump argument ignored even though used below DocumentManager.getDocumentForPath(path).done(function (doc) { testDoc = doc; }); @@ -437,6 +437,8 @@ define(function (require, exports, module) { runs(function () { testEditor = createMockEditor(testDoc); preTestText = testDoc.getText(); + + waitsForDone(ScopeManager._readyPromise()); }); } @@ -740,11 +742,13 @@ define(function (require, exports, module) { middle = { line: 6, ch: 3 }, end = { line: 6, ch: 8 }, endplus = { line: 6, ch: 12 }; - - testDoc.replaceRange("A1.prop", start, start); - testEditor.setCursorPos(middle); - var hintObj = expectHints(JSCodeHints.jsHintProvider); - selectHint(JSCodeHints.jsHintProvider, hintObj, "propA"); + + runs(function () { + testDoc.replaceRange("A1.prop", start, start); + testEditor.setCursorPos(middle); + var hintObj = expectHints(JSCodeHints.jsHintProvider); + selectHint(JSCodeHints.jsHintProvider, hintObj, "propA"); + }); runs(function () { expect(testEditor.getCursorPos()).toEqual(end); @@ -1407,9 +1411,7 @@ define(function (require, exports, module) { }); - // TODO: FileSystem - there is something wrong with the tern paths, - // causing an infinite recursion in directory scanning. - xdescribe("JavaScript Code Hinting without modules", function () { + describe("JavaScript Code Hinting without modules", function () { var testPath = extensionPath + "/unittest-files/non-module-test-files/app.js"; ScopeManager.handleProjectOpen(extensionPath + "/unittest-files/non-module-test-files/"); @@ -1433,9 +1435,7 @@ define(function (require, exports, module) { }); }); - // TODO: FileSystem - there is something wrong with the tern paths, - // causing an infinite recursion in directory scanning. - xdescribe("JavaScript Code Hinting with modules", function () { + describe("JavaScript Code Hinting with modules", function () { var testPath = extensionPath + "/unittest-files/module-test-files/module_tests.js"; beforeEach(function () { diff --git a/src/extensions/default/UrlCodeHints/main.js b/src/extensions/default/UrlCodeHints/main.js index da6e8e3d8..3c6f59247 100644 --- a/src/extensions/default/UrlCodeHints/main.js +++ b/src/extensions/default/UrlCodeHints/main.js @@ -141,7 +141,7 @@ define(function (require, exports, module) { // code hints show the same strings that are inserted into text, // so strings in list will be encoded. wysiwyg, baby! - unfiltered.push(encodeURI(entryStr + (entry.isDirectory() ? "/" : ""))); + unfiltered.push(encodeURI(entryStr)); } }); diff --git a/src/filesystem/Directory.js b/src/filesystem/Directory.js index 3e43e5e07..c789f3548 100644 --- a/src/filesystem/Directory.js +++ b/src/filesystem/Directory.js @@ -80,7 +80,7 @@ define(function (require, exports, module) { var len = stats ? stats.length : 0; for (i = 0; i < len; i++) { - entryPath = this.fullPath + "/" + contents[i]; + entryPath = this.fullPath + contents[i]; // Note: not all entries necessarily have associated stats. // For now, silently ignore such entries. diff --git a/src/filesystem/FileSystem.js b/src/filesystem/FileSystem.js index 60fffa7ee..da76d93e8 100644 --- a/src/filesystem/FileSystem.js +++ b/src/filesystem/FileSystem.js @@ -100,6 +100,28 @@ define(function (require, exports, module) { return !name.match(_exclusionListRegEx); }; + /** + * Returns a canonical version of the path, with no duplicated /es and with + * directories guaranteed to end in a trailing /. + * @param {!string} path + * @param {boolean=} isDirectory + * @return {!string} + */ + function _normalizePath(path, isDirectory) { + if (isDirectory) { + // Make sure path DOES include trailing slash + if (path[path.length - 1] !== "/") { + path += "/"; + } + } + + // Remove duplicated "/"es + path = path.replace(/\/{2,}/, "/"); + + // TODO: normalize out ".."s ? + return path; + } + /** * Return a File object for the specified path. * @@ -108,6 +130,7 @@ define(function (require, exports, module) { * @return {File} The File object. This file may not yet exist on disk. */ FileSystem.prototype.getFileForPath = function (path) { + path = _normalizePath(path, false); var file = this._index.getEntry(path); if (!file) { @@ -140,11 +163,7 @@ define(function (require, exports, module) { * @return {Directory} The Directory object. This directory may not yet exist on disk. */ FileSystem.prototype.getDirectoryForPath = function (path) { - // Make sure path doesn't include trailing slash - if (path[path.length - 1] === "/") { - path = path.substr(0, path.length - 1); - } - + path = _normalizePath(path, true); var directory = this._index.getEntry(path); if (!directory) { @@ -162,6 +181,9 @@ define(function (require, exports, module) { * @param {function (err, object)} callback */ FileSystem.prototype.resolve = function (path, callback) { + // No need to normalize path here: assume underlying stat() does it internally, + // and it will be normalized anyway when ingested by get*ForPath() afterward + this._impl.stat(path, function (err, stat) { var item; @@ -365,14 +387,6 @@ define(function (require, exports, module) { * @param {string} rootPath The new project root. */ FileSystem.prototype.setProjectRoot = function (rootPath) { - - // Remove trailing "/" from path - if (rootPath && rootPath.length > 1) { - if (rootPath[rootPath.length - 1] === "/") { - rootPath = rootPath.substr(0, rootPath.length - 1); - } - } - // Clear file index this._index.clear(); diff --git a/src/filesystem/FileSystemEntry.js b/src/filesystem/FileSystemEntry.js index ee045714e..7581a9af0 100644 --- a/src/filesystem/FileSystemEntry.js +++ b/src/filesystem/FileSystemEntry.js @@ -51,7 +51,15 @@ define(function (require, exports, module) { set: function (val) { throw new Error("Cannot set fullPath"); } }, "name": { - get: function () { return this._path.split("/").pop(); }, + get: function () { + // TODO: cache this result in a member? + var parts = this._path.split("/"); + if (this.isDirectory()) { + return parts[parts.length - 2]; + } else { + return parts[parts.length - 1]; + } + }, set: function (val) { throw new Error("Cannot set name"); } }, "id": { diff --git a/src/project/ProjectManager.js b/src/project/ProjectManager.js index d114bc8a4..ae611dafd 100644 --- a/src/project/ProjectManager.js +++ b/src/project/ProjectManager.js @@ -332,7 +332,7 @@ define(function (require, exports, module) { */ function makeProjectRelativeIfPossible(absPath) { if (isWithinProject(absPath)) { - return absPath.slice(_projectRoot.fullPath.length + 1); + return absPath.slice(_projectRoot.fullPath.length); } return absPath; } @@ -376,8 +376,9 @@ define(function (require, exports, module) { if (entry.fullPath) { fullPath = entry.fullPath; - // Truncate project path prefix and trailing slash - shortPath = fullPath.slice(projectPathLength + 1); + // Truncate project path prefix (inlcuding its last slash) AND remove trailing slash suffix + // So "/foo/bar/projroot/abc/xyz/" -> "abc/xyz" + shortPath = fullPath.slice(projectPathLength, -1); // Determine depth of the node by counting path separators. // Children at the root have depth of zero @@ -999,7 +1000,6 @@ define(function (require, exports, module) { }); }); } - return result.promise(); } @@ -1336,7 +1336,7 @@ define(function (require, exports, module) { errorCleanup(); }; - var newItemPath = selectionEntry.fullPath + "/" + data.rslt.name; + var newItemPath = selectionEntry.fullPath + data.rslt.name; _fileSystem.resolve(newItemPath, function (err, item) { if (!err) { diff --git a/test/spec/ProjectManager-test.js b/test/spec/ProjectManager-test.js index 32764305a..d7924764f 100644 --- a/test/spec/ProjectManager-test.js +++ b/test/spec/ProjectManager-test.js @@ -125,7 +125,6 @@ define(function (require, exports, module) { ); }); - // TODO: FileSystem - figure out why this doesn't pass it("should fail when a file already exists", function () { var didCreate = false, gotError = false; @@ -370,7 +369,7 @@ define(function (require, exports, module) { expect(stat.isDirectory()).toBe(true); rootFolderEntry = ProjectManager.getSelectedItem(); - expect(rootFolderEntry.fullPath).toBe(testPath + "/toDelete"); + expect(rootFolderEntry.fullPath).toBe(testPath + "/toDelete/"); }); // Create a sub folder @@ -384,7 +383,7 @@ define(function (require, exports, module) { runs(function () { var newFolder; - newFolderName += "/toDelete1"; + newFolderName += "toDelete1/"; newFolder = fileSystem.getDirectoryForPath(newFolderName); complete = false; newFolder.stat(function (err, _stat) { @@ -537,7 +536,7 @@ define(function (require, exports, module) { it("should reselect previously selected file when made visible again", function () { var promise, initialFile = testPath + "/file.js", - folder = testPath + "/directory", + folder = testPath + "/directory/", fileInFolder = testPath + "/directory/file.js"; runs(function () { @@ -569,7 +568,7 @@ define(function (require, exports, module) { it("should deselect after opening file hidden in tree, but select when made visible again", function () { var promise, initialFile = testPath + "/file.js", - folder = testPath + "/directory", + folder = testPath + "/directory/", fileInFolder = testPath + "/directory/file.js"; runs(function () { diff --git a/test/spec/SpecRunnerUtils.js b/test/spec/SpecRunnerUtils.js index df9ececeb..5393ebf65 100644 --- a/test/spec/SpecRunnerUtils.js +++ b/test/spec/SpecRunnerUtils.js @@ -582,7 +582,7 @@ define(function (require, exports, module) { var result = _testWindow.brackets.test.ProjectManager.openProject(path); // wait for file system to finish loading - waitsForDone(result, "ProjectManager.openProject()"); + waitsForDone(result, "ProjectManager.openProject()", 10000); }); } @@ -651,7 +651,7 @@ define(function (require, exports, module) { return path; } - return fullPath + "/" + path; + return fullPath + path; } if (Array.isArray(paths)) { @@ -843,58 +843,59 @@ define(function (require, exports, module) { // create the destination folder destDir.create(function (err) { - if (!err) { - source.getContents(function (err, contents) { - if (!err) { - // copy all children of this directory - var copyChildrenPromise = Async.doInParallel( - contents, - function copyChild(child) { - var childDestination = destination + "/" + child.name, - promise; - - if (child.isDirectory()) { - promise = copyDirectoryEntry(child, childDestination, options); - } else { - promise = copyFileEntry(child, childDestination, options); - - if (parseOffsets) { - // save offset data for each file path - promise.done(function (destinationEntry, offsets, text) { - options.infos[childDestination] = { - offsets : offsets, - fileEntry : destinationEntry, - text : text - }; - }); - } - } - - return promise; - } - ); - - copyChildrenPromise.then(deferred.resolve, deferred.reject); - } else { - deferred.reject(err); - } - }); - } else { + if (err && err !== Error.ALREADY_EXISTS) { deferred.reject(); + return; } - - deferred.always(function () { - // remove destination path prefix - if (removePrefix && options.infos) { - var shortKey; - Object.keys(options.infos).forEach(function (key) { - shortKey = key.substr(destination.length + 1); - options.infos[shortKey] = options.infos[key]; - }); + + source.getContents(function (err, contents) { + if (!err) { + // copy all children of this directory + var copyChildrenPromise = Async.doInParallel( + contents, + function copyChild(child) { + var childDestination = destination + "/" + child.name, + promise; + + if (child.isDirectory()) { + promise = copyDirectoryEntry(child, childDestination, options); + } else { + promise = copyFileEntry(child, childDestination, options); + + if (parseOffsets) { + // save offset data for each file path + promise.done(function (destinationEntry, offsets, text) { + options.infos[childDestination] = { + offsets : offsets, + fileEntry : destinationEntry, + text : text + }; + }); + } + } + + return promise; + } + ); + + copyChildrenPromise.then(deferred.resolve, deferred.reject); + } else { + deferred.reject(err); } }); }); + deferred.always(function () { + // remove destination path prefix + if (removePrefix && options.infos) { + var shortKey; + Object.keys(options.infos).forEach(function (key) { + shortKey = key.substr(destination.length + 1); + options.infos[shortKey] = options.infos[key]; + }); + } + }); + return deferred.promise(); }