Bug 1402279 - Part 1 - Move the DownloadPaths module to the jsdownloads folder. r=mak draft
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Fri, 22 Sep 2017 15:45:39 +0100
changeset 670527 7505715770e8fa383a42e0f8a18258c08e8e0f3a
parent 669596 7e962631ba4298bcefa571008661983d77c3e652
child 670528 d17ef98796d5a4fcac899c6319045eec02b0633e
push id81653
push userpaolo.mozmail@amadzone.org
push dateTue, 26 Sep 2017 15:05:31 +0000
reviewersmak
bugs1402279
milestone58.0a1
Bug 1402279 - Part 1 - Move the DownloadPaths module to the jsdownloads folder. r=mak MozReview-Commit-ID: 91ZqKef6NIE
toolkit/components/jsdownloads/src/DownloadPaths.jsm
toolkit/components/jsdownloads/src/moz.build
toolkit/components/jsdownloads/test/unit/test_DownloadPaths.js
toolkit/components/jsdownloads/test/unit/xpcshell.ini
toolkit/mozapps/downloads/DownloadPaths.jsm
toolkit/mozapps/downloads/moz.build
toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js
toolkit/mozapps/downloads/tests/unit/xpcshell.ini
rename from toolkit/mozapps/downloads/DownloadPaths.jsm
rename to toolkit/components/jsdownloads/src/DownloadPaths.jsm
--- a/toolkit/mozapps/downloads/DownloadPaths.jsm
+++ b/toolkit/components/jsdownloads/src/DownloadPaths.jsm
@@ -1,57 +1,46 @@
-/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
-/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+/**
+ * Provides methods for giving names and paths to files being downloaded.
+ */
+
+"use strict";
+
 this.EXPORTED_SYMBOLS = [
   "DownloadPaths",
 ];
 
-/**
- * This module provides the DownloadPaths object which contains methods for
- * giving names and paths to files being downloaded.
- *
- * List of methods:
- *
- * nsIFile
- * createNiceUniqueFile(nsIFile aLocalFile)
- *
- * [string base, string ext]
- * splitBaseNameAndExtension(string aLeafName)
- */
-
-const Cc = Components.classes;
-const Ci = Components.interfaces;
-const Cu = Components.utils;
-const Cr = Components.results;
+const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
 this.DownloadPaths = {
   /**
    * Creates a uniquely-named file starting from the name of the provided file.
    * If a file with the provided name already exists, the function attempts to
    * create nice alternatives, like "base(1).ext" (instead of "base-1.ext").
    *
    * If a unique name cannot be found, the function throws the XPCOM exception
    * NS_ERROR_FILE_TOO_BIG. Other exceptions, like NS_ERROR_FILE_ACCESS_DENIED,
    * can also be expected.
    *
-   * @param aTemplateFile
+   * @param templateFile
    *        nsIFile whose leaf name is going to be used as a template. The
    *        provided object is not modified.
-   * @returns A new instance of an nsIFile object pointing to the newly
-   *          created empty file. On platforms that support permission bits, the
-   *          file is created with permissions 644.
+   *
+   * @return A new instance of an nsIFile object pointing to the newly created
+   *         empty file. On platforms that support permission bits, the file is
+   *         created with permissions 644.
    */
-  createNiceUniqueFile: function DP_createNiceUniqueFile(aTemplateFile) {
+  createNiceUniqueFile(templateFile) {
     // Work on a clone of the provided template file object.
-    var curFile = aTemplateFile.clone().QueryInterface(Ci.nsIFile);
-    var [base, ext] = DownloadPaths.splitBaseNameAndExtension(curFile.leafName);
+    let curFile = templateFile.clone().QueryInterface(Ci.nsIFile);
+    let [base, ext] = DownloadPaths.splitBaseNameAndExtension(curFile.leafName);
     // Try other file names, for example "base(1).txt" or "base(1).tar.gz",
     // only if the file name initially set already exists.
     for (let i = 1; i < 10000 && curFile.exists(); i++) {
       curFile.leafName = base + "(" + i + ")" + ext;
     }
     // At this point we hand off control to createUnique, which will create the
     // file with the name we chose, if it is valid. If not, createUnique will
     // attempt to modify it again, for example it will shorten very long names
@@ -59,31 +48,32 @@ this.DownloadPaths = {
     // nsIFile.create would result in NS_ERROR_FILE_NOT_FOUND. This can result
     // very rarely in strange names like "base(9999).tar-1.gz" or "ba-1.gz".
     curFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
     return curFile;
   },
 
   /**
    * Separates the base name from the extension in a file name, recognizing some
-   *  double extensions like ".tar.gz".
+   * double extensions like ".tar.gz".
    *
-   * @param aLeafName
+   * @param leafName
    *        The full leaf name to be parsed. Be careful when processing names
    *        containing leading or trailing dots or spaces.
-   * @returns [base, ext]
-   *          The base name of the file, which can be empty, and its extension,
-   *          which always includes the leading dot unless it's an empty string.
-   *          Concatenating the two items always results in the original name.
+   *
+   * @return [base, ext]
+   *         The base name of the file, which can be empty, and its extension,
+   *         which always includes the leading dot unless it's an empty string.
+   *         Concatenating the two items always results in the original name.
    */
-  splitBaseNameAndExtension: function DP_splitBaseNameAndExtension(aLeafName) {
+  splitBaseNameAndExtension(leafName) {
     // The following regular expression is built from these key parts:
     //  .*?                      Matches the base name non-greedily.
     //  \.[A-Z0-9]{1,3}          Up to three letters or numbers preceding a
     //                           double extension.
     //  \.(?:gz|bz2|Z)           The second part of common double extensions.
     //  \.[^.]*                  Matches any extension or a single trailing dot.
-    var [, base, ext] = /(.*?)(\.[A-Z0-9]{1,3}\.(?:gz|bz2|Z)|\.[^.]*)?$/i
-                        .exec(aLeafName);
+    let [, base, ext] = /(.*?)(\.[A-Z0-9]{1,3}\.(?:gz|bz2|Z)|\.[^.]*)?$/i
+                        .exec(leafName);
     // Return an empty string instead of undefined if no extension is found.
     return [base, ext || ""];
-  }
+  },
 };
--- a/toolkit/components/jsdownloads/src/moz.build
+++ b/toolkit/components/jsdownloads/src/moz.build
@@ -12,16 +12,17 @@ EXTRA_COMPONENTS += [
     'DownloadLegacy.js',
     'Downloads.manifest',
 ]
 
 EXTRA_JS_MODULES += [
     'DownloadCore.jsm',
     'DownloadIntegration.jsm',
     'DownloadList.jsm',
+    'DownloadPaths.jsm',
     'Downloads.jsm',
     'DownloadStore.jsm',
     'DownloadUIHelper.jsm',
 ]
 
 if CONFIG['MOZ_PLACES']:
     EXTRA_JS_MODULES += [
         'DownloadHistory.jsm',
rename from toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js
rename to toolkit/components/jsdownloads/test/unit/test_DownloadPaths.js
--- a/toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js
+++ b/toolkit/components/jsdownloads/test/unit/test_DownloadPaths.js
@@ -1,28 +1,15 @@
-/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
-/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
-/* ***** BEGIN LICENSE BLOCK *****
- *
- * Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/
- *
- * ***** END LICENSE BLOCK ***** */
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 /**
  * Tests for the "DownloadPaths.jsm" JavaScript module.
  */
 
-var Cc = Components.classes;
-var Ci = Components.interfaces;
-var Cu = Components.utils;
-var Cr = Components.results;
-
-Cu.import("resource://gre/modules/DownloadPaths.jsm");
-
 /**
  * Provides a temporary save directory.
  *
  * @returns nsIFile pointing to the new or existing directory.
  */
 function createTemporarySaveDirectory() {
   var saveDir = Cc["@mozilla.org/file/directory_service;1"].
                 getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
@@ -49,17 +36,17 @@ function testSplitBaseNameAndExtension(a
   do_check_eq(ext, aExt);
 }
 
 function testCreateNiceUniqueFile(aTempFile, aExpectedLeafName) {
   var createdFile = DownloadPaths.createNiceUniqueFile(aTempFile);
   do_check_eq(createdFile.leafName, aExpectedLeafName);
 }
 
-function run_test() {
+add_task(async function test_splitBaseNameAndExtension() {
   // Usual file names.
   testSplitBaseNameAndExtension("base", ["base", ""]);
   testSplitBaseNameAndExtension("base.ext", ["base", ".ext"]);
   testSplitBaseNameAndExtension("base.application", ["base", ".application"]);
   testSplitBaseNameAndExtension("base.x.Z", ["base", ".x.Z"]);
   testSplitBaseNameAndExtension("base.ext.Z", ["base", ".ext.Z"]);
   testSplitBaseNameAndExtension("base.ext.gz", ["base", ".ext.gz"]);
   testSplitBaseNameAndExtension("base.ext.Bz2", ["base", ".ext.Bz2"]);
@@ -83,17 +70,19 @@ function run_test() {
   testSplitBaseNameAndExtension("base ", ["base ", ""]);
   testSplitBaseNameAndExtension("", ["", ""]);
   testSplitBaseNameAndExtension(" ", [" ", ""]);
   testSplitBaseNameAndExtension(" . ", [" ", ". "]);
   testSplitBaseNameAndExtension(" .. ", [" .", ". "]);
   testSplitBaseNameAndExtension(" .ext", [" ", ".ext"]);
   testSplitBaseNameAndExtension(" .ext. ", [" .ext", ". "]);
   testSplitBaseNameAndExtension(" .ext.gz ", [" .ext", ".gz "]);
+});
 
+add_task(async function test_createNiceUniqueFile() {
   var destDir = createTemporarySaveDirectory();
   try {
     // Single extension.
     var tempFile = destDir.clone();
     tempFile.append("test.txt");
     testCreateNiceUniqueFile(tempFile, "test.txt");
     testCreateNiceUniqueFile(tempFile, "test(1).txt");
     testCreateNiceUniqueFile(tempFile, "test(2).txt");
@@ -119,9 +108,9 @@ function run_test() {
       do_throw("Exception expected with a long parent directory name.")
     } catch (e) {
       // An exception is expected, but we don't know which one exactly.
     }
   } finally {
     // Clean up the temporary directory.
     destDir.remove(true);
   }
-}
+});
--- a/toolkit/components/jsdownloads/test/unit/xpcshell.ini
+++ b/toolkit/components/jsdownloads/test/unit/xpcshell.ini
@@ -7,13 +7,14 @@ skip-if = toolkit == 'android'
 support-files =
   common_test_Download.js
 
 [test_DownloadCore.js]
 [test_DownloadHistory.js]
 [test_DownloadIntegration.js]
 [test_DownloadLegacy.js]
 [test_DownloadList.js]
+[test_DownloadPaths.js]
 [test_Downloads.js]
 [test_DownloadStore.js]
 [test_PrivateTemp.js]
 # coverage flag is for bug 1336730
 skip-if = (os != 'linux' || coverage)
--- a/toolkit/mozapps/downloads/moz.build
+++ b/toolkit/mozapps/downloads/moz.build
@@ -11,13 +11,12 @@ TEST_DIRS += ['tests']
 
 EXTRA_COMPONENTS += [
     'nsHelperAppDlg.js',
     'nsHelperAppDlg.manifest',
 ]
 
 EXTRA_JS_MODULES += [
     'DownloadLastDir.jsm',
-    'DownloadPaths.jsm',
     'DownloadUtils.jsm',
 ]
 
 JAR_MANIFESTS += ['jar.mn']
--- a/toolkit/mozapps/downloads/tests/unit/xpcshell.ini
+++ b/toolkit/mozapps/downloads/tests/unit/xpcshell.ini
@@ -1,8 +1,7 @@
 [DEFAULT]
 head = head_downloads.js
 
-[test_DownloadPaths.js]
 [test_DownloadUtils.js]
 [test_lowMinutes.js]
 [test_syncedDownloadUtils.js]
 [test_unspecified_arguments.js]