Bug 1037292 - Use a date-specific temporary folder name for project editor tests. r=jwalker, a=test-only
authorBrian Grinstead <bgrinstead@mozilla.com>
Fri, 01 Aug 2014 07:52:44 -0500
changeset 217374 fe5ef38bf7799fe630287e9039781f289622d588
parent 217373 7671c6af65a483e39bf312ce826fc38f8838b79b
child 217375 004a4131c54f47e72445f85edf438129b60e8a0a
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwalker, test-only
bugs1037292
milestone33.0a2
Bug 1037292 - Use a date-specific temporary folder name for project editor tests. r=jwalker, a=test-only
browser/devtools/projecteditor/test/browser_projecteditor_tree_selection.js
browser/devtools/projecteditor/test/head.js
--- a/browser/devtools/projecteditor/test/browser_projecteditor_tree_selection.js
+++ b/browser/devtools/projecteditor/test/browser_projecteditor_tree_selection.js
@@ -12,17 +12,17 @@ let test = asyncTest(function*() {
 
   is (getTempFile("").path, TEMP_PATH, "Temp path is set correctly.");
 
   ok (projecteditor.currentEditor, "There is an editor for projecteditor");
   let resources = projecteditor.project.allResources();
 
   is (
     resources.map(r=>r.basename).join("|"),
-    "ProjectEditor|css|styles.css|data|img|icons|128x128.png|16x16.png|32x32.png|vector.svg|fake.png|js|script.js|index.html|LICENSE|README.md",
+    TEMP_FOLDER_NAME + "|css|styles.css|data|img|icons|128x128.png|16x16.png|32x32.png|vector.svg|fake.png|js|script.js|index.html|LICENSE|README.md",
     "Resources came through in proper order"
   );
 
   for (let i = 0; i < resources.length; i++){
     yield selectFileFirstLoad(projecteditor, resources[i]);
   }
   for (let i = 0; i < resources.length; i++){
     yield selectFileSubsequentLoad(projecteditor, resources[i]);
--- a/browser/devtools/projecteditor/test/head.js
+++ b/browser/devtools/projecteditor/test/head.js
@@ -9,31 +9,33 @@ const {console} = Cu.import("resource://
 const promise = devtools.require("sdk/core/promise");
 const {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", {});
 const {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {});
 const ProjectEditor = devtools.require("projecteditor/projecteditor");
 
 const TEST_URL_ROOT = "http://mochi.test:8888/browser/browser/devtools/projecteditor/test/";
 const SAMPLE_WEBAPP_URL = TEST_URL_ROOT + "/helper_homepage.html";
 let TEMP_PATH;
+let TEMP_FOLDER_NAME = "ProjectEditor" + (new Date().getTime());
 
 // All test are asynchronous
 waitForExplicitFinish();
 
 // Uncomment this pref to dump all devtools emitted events to the console.
 // Services.prefs.setBoolPref("devtools.dump.emit", true);
 
 // Set the testing flag on gDevTools and reset it when the test ends
 gDevTools.testing = true;
 registerCleanupFunction(() => gDevTools.testing = false);
 
 // Clear preferences that may be set during the course of tests.
 registerCleanupFunction(() => {
   // Services.prefs.clearUserPref("devtools.dump.emit");
   TEMP_PATH = null;
+  TEMP_FOLDER_NAME = null;
 });
 
 // Auto close the toolbox and close the test tabs when the test ends
 registerCleanupFunction(() => {
   try {
     let target = TargetFactory.forTab(gBrowser.selectedTab);
     gDevTools.closeToolbox(target);
   } catch (ex) {
@@ -85,23 +87,32 @@ function addTab(url) {
  *                 - "../../../commandline/test/helpers.js"
  */
 function loadHelperScript(filePath) {
   let testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
   Services.scriptloader.loadSubScript(testDir + "/" + filePath, this);
 }
 
 function addProjectEditorTabForTempDirectory(opts = {}) {
-  TEMP_PATH = buildTempDirectoryStructure();
+  try {
+    TEMP_PATH = buildTempDirectoryStructure();
+  } catch (e) {
+    // Bug 1037292 - The test servers sometimes are unable to
+    // write to the temporary directory due to locked files
+    // or access denied errors.  Try again if this failed.
+    info ("Project Editor temp directory creation failed.  Trying again.");
+    TEMP_PATH = buildTempDirectoryStructure();
+  }
   let customOpts = {
     name: "Test",
     iconUrl: "chrome://browser/skin/devtools/tool-options.svg",
     projectOverviewURL: SAMPLE_WEBAPP_URL
   };
 
+  info ("Adding a project editor tab for editing at: " + TEMP_PATH);
   return addProjectEditorTab(opts).then((projecteditor) => {
     return projecteditor.setProjectToAppPath(TEMP_PATH, customOpts).then(() => {
       return projecteditor;
     });
   });
 }
 
 function addProjectEditorTab(opts = {}) {
@@ -123,76 +134,79 @@ function addProjectEditorTab(opts = {}) 
 }
 
 /**
  * Build a temporary directory as a workspace for this loader
  * https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O
  */
 function buildTempDirectoryStructure() {
 
+  let dirName = TEMP_FOLDER_NAME;
+  info ("Building a temporary directory at " + dirName);
+
   // First create (and remove) the temp dir to discard any changes
-  let TEMP_DIR = FileUtils.getDir("TmpD", ["ProjectEditor"], true);
+  let TEMP_DIR = FileUtils.getDir("TmpD", [dirName], true);
   TEMP_DIR.remove(true);
 
   // Now rebuild our fake project.
-  TEMP_DIR = FileUtils.getDir("TmpD", ["ProjectEditor"], true);
+  TEMP_DIR = FileUtils.getDir("TmpD", [dirName], true);
 
-  FileUtils.getDir("TmpD", ["ProjectEditor", "css"], true);
-  FileUtils.getDir("TmpD", ["ProjectEditor", "data"], true);
-  FileUtils.getDir("TmpD", ["ProjectEditor", "img", "icons"], true);
-  FileUtils.getDir("TmpD", ["ProjectEditor", "js"], true);
+  FileUtils.getDir("TmpD", [dirName, "css"], true);
+  FileUtils.getDir("TmpD", [dirName, "data"], true);
+  FileUtils.getDir("TmpD", [dirName, "img", "icons"], true);
+  FileUtils.getDir("TmpD", [dirName, "js"], true);
 
-  let htmlFile = FileUtils.getFile("TmpD", ["ProjectEditor", "index.html"]);
+  let htmlFile = FileUtils.getFile("TmpD", [dirName, "index.html"]);
   htmlFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
   writeToFileSync(htmlFile, [
     '<!DOCTYPE html>',
     '<html lang="en">',
     ' <head>',
     '   <meta charset="utf-8" />',
     '   <title>ProjectEditor Temp File</title>',
     '   <link rel="stylesheet" href="style.css" />',
     ' </head>',
     ' <body id="home">',
     '   <p>ProjectEditor Temp File</p>',
     ' </body>',
     '</html>'].join("\n")
   );
 
-  let readmeFile = FileUtils.getFile("TmpD", ["ProjectEditor", "README.md"]);
+  let readmeFile = FileUtils.getFile("TmpD", [dirName, "README.md"]);
   readmeFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
   writeToFileSync(readmeFile, [
     '## Readme'
     ].join("\n")
   );
 
-  let licenseFile = FileUtils.getFile("TmpD", ["ProjectEditor", "LICENSE"]);
+  let licenseFile = FileUtils.getFile("TmpD", [dirName, "LICENSE"]);
   licenseFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
   writeToFileSync(licenseFile, [
    '/* 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/. */'
     ].join("\n")
   );
 
-  let cssFile = FileUtils.getFile("TmpD", ["ProjectEditor", "css", "styles.css"]);
+  let cssFile = FileUtils.getFile("TmpD", [dirName, "css", "styles.css"]);
   cssFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
   writeToFileSync(cssFile, [
     'body {',
     ' background: red;',
     '}'
     ].join("\n")
   );
 
-  FileUtils.getFile("TmpD", ["ProjectEditor", "js", "script.js"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+  FileUtils.getFile("TmpD", [dirName, "js", "script.js"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
 
-  FileUtils.getFile("TmpD", ["ProjectEditor", "img", "fake.png"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
-  FileUtils.getFile("TmpD", ["ProjectEditor", "img", "icons", "16x16.png"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
-  FileUtils.getFile("TmpD", ["ProjectEditor", "img", "icons", "32x32.png"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
-  FileUtils.getFile("TmpD", ["ProjectEditor", "img", "icons", "128x128.png"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
-  FileUtils.getFile("TmpD", ["ProjectEditor", "img", "icons", "vector.svg"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+  FileUtils.getFile("TmpD", [dirName, "img", "fake.png"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+  FileUtils.getFile("TmpD", [dirName, "img", "icons", "16x16.png"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+  FileUtils.getFile("TmpD", [dirName, "img", "icons", "32x32.png"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+  FileUtils.getFile("TmpD", [dirName, "img", "icons", "128x128.png"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+  FileUtils.getFile("TmpD", [dirName, "img", "icons", "vector.svg"]).createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
 
   return TEMP_DIR.path;
 }
 
 // https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O#Writing_to_a_file
 function writeToFile(file, data) {
   if (typeof file === "string") {
     file = new FileUtils.File(file);
@@ -236,17 +250,17 @@ function writeToFileSync(file, data) {
   var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                   createInstance(Components.interfaces.nsIConverterOutputStream);
   converter.init(foStream, "UTF-8", 0, 0);
   converter.writeString(data);
   converter.close(); // this closes foStream
 }
 
 function getTempFile(path) {
-  let parts = ["ProjectEditor"];
+  let parts = [TEMP_FOLDER_NAME];
   parts = parts.concat(path.split("/"));
   return FileUtils.getFile("TmpD", parts);
 }
 
 // https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O#Writing_to_a_file
 function* getFileData(file) {
   if (typeof file === "string") {
     file = new FileUtils.File(file);