Bug 1307905 - Ignore irrelevant properties in stub generation; r=linclark draft
authorNicolas Chevobbe <chevobbe.nicolas@gmail.com>
Tue, 25 Oct 2016 20:16:50 +0200
changeset 435327 7e52d01393d84b3fef637682faae668179093a0a
parent 435182 f13e90d496cf1bc6dfc4fd398da33e4afe785bde
child 536279 313fb2f159e7486086d5db05e91c4a8e9d990d9c
push id35003
push userchevobbe.nicolas@gmail.com
push dateTue, 08 Nov 2016 12:39:47 +0000
reviewerslinclark
bugs1307905
milestone52.0a1
Bug 1307905 - Ignore irrelevant properties in stub generation; r=linclark This can be tested by inserting a new stub in first position, in order to change the actor id on the existing stubs, and then check in the resulting diff that actor id, timestamp and timer properties are not modified. MozReview-Commit-ID: K5enQ5cF7RR
devtools/client/webconsole/new-console-output/test/fixtures/moz.build
devtools/client/webconsole/new-console-output/test/fixtures/stub-generators/head.js
devtools/client/webconsole/new-console-output/test/fixtures/stubs/moz.build
--- a/devtools/client/webconsole/new-console-output/test/fixtures/moz.build
+++ b/devtools/client/webconsole/new-console-output/test/fixtures/moz.build
@@ -1,8 +1,9 @@
 # vim: set filetype=python:
 # 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/.
 
 DIRS += [
-    'stub-generators'
+    'stub-generators',
+    'stubs'
 ]
--- a/devtools/client/webconsole/new-console-output/test/fixtures/stub-generators/head.js
+++ b/devtools/client/webconsole/new-console-output/test/fixtures/stub-generators/head.js
@@ -13,28 +13,130 @@ Services.scriptloader.loadSubScript(
   this);
 
 Services.prefs.setBoolPref("devtools.webconsole.new-frontend-enabled", true);
 registerCleanupFunction(() => {
   Services.prefs.clearUserPref("devtools.webconsole.new-frontend-enabled");
 });
 
 const { prepareMessage } = require("devtools/client/webconsole/new-console-output/utils/messages");
+const { stubPackets } = require("devtools/client/webconsole/new-console-output/test/fixtures/stubs/index.js");
 
 const BASE_PATH = "../../../../devtools/client/webconsole/new-console-output/test/fixtures";
 const TEMP_FILE_PATH = OS.Path.join(`${BASE_PATH}/stub-generators`, "test-tempfile.js");
 
+let cachedPackets = {};
+
+function getCleanedPacket(key, packet) {
+  if(Object.keys(cachedPackets).includes(key)) {
+    return cachedPackets[key];
+  }
+
+  // Strip escaped characters.
+  let safeKey = key
+    .replace(/\\n/g, "\n")
+    .replace(/\\r/g, "\r")
+    .replace(/\\\"/g, `\"`)
+    .replace(/\\\'/g, `\'`);
+
+  // If the stub already exist, we want to ignore irrelevant properties
+  // (actor, timeStamp, timer, ...) that might changed and "pollute"
+  // the diff resulting from this stub generation.
+  let res;
+  if(stubPackets.has(safeKey)) {
+
+    let existingPacket = stubPackets.get(safeKey);
+    res = Object.assign({}, packet, {
+      from: existingPacket.from
+    });
+
+    // Clean root timestamp.
+    if(res.timestamp) {
+      res.timestamp = existingPacket.timestamp;
+    }
+
+    if (res.message) {
+      // Clean timeStamp on the message prop.
+      res.message.timeStamp = existingPacket.message.timeStamp;
+      if (res.message.timer) {
+        // Clean timer properties on the message.
+        // Those properties are found on console.time and console.timeEnd calls,
+        // and those time can vary, which is why we need to clean them.
+        if (res.message.timer.started) {
+          res.message.timer.started = existingPacket.message.timer.started;
+        }
+        if (res.message.timer.duration) {
+          res.message.timer.duration = existingPacket.message.timer.duration;
+        }
+      }
+
+      if(Array.isArray(res.message.arguments)) {
+        // Clean actor ids on each message.arguments item.
+        res.message.arguments.forEach((argument, i) => {
+          if (argument && argument.actor) {
+            argument.actor = existingPacket.message.arguments[i].actor;
+          }
+        });
+      }
+    }
+
+    if (res.result) {
+      // Clean actor ids on evaluation result messages.
+      res.result.actor = existingPacket.result.actor;
+      if (res.result.preview) {
+        if(res.result.preview.timestamp) {
+          // Clean timestamp there too.
+          res.result.preview.timestamp = existingPacket.result.preview.timestamp;
+        }
+      }
+    }
+
+    if (res.exception) {
+      // Clean actor ids on exception messages.
+      res.exception.actor = existingPacket.exception.actor;
+      if (res.exception.preview) {
+        if(res.exception.preview.timestamp) {
+          // Clean timestamp there too.
+          res.exception.preview.timestamp = existingPacket.exception.preview.timestamp;
+        }
+      }
+    }
+
+    if (res.eventActor) {
+      // Clean actor ids, timeStamp and startedDateTime on network messages.
+      res.eventActor.actor = existingPacket.eventActor.actor;
+      res.eventActor.startedDateTime = existingPacket.eventActor.startedDateTime;
+      res.eventActor.timeStamp = existingPacket.eventActor.timeStamp;
+    }
+
+    if (res.pageError) {
+      // Clean timeStamp on pageError messages.
+      res.pageError.timeStamp = existingPacket.pageError.timeStamp;
+    }
+
+  } else {
+    res = packet;
+  }
+
+  cachedPackets[key] = res;
+  return res;
+}
+
 function formatPacket(key, packet) {
   return `
-stubPackets.set("${key}", ${JSON.stringify(packet, null, "\t")});
+stubPackets.set("${key}", ${JSON.stringify(getCleanedPacket(key, packet), null, "\t")});
 `;
 }
 
-function formatStub(key, message) {
-  let prepared = prepareMessage(message, {getNextId: () => "1"});
+function formatStub(key, packet) {
+  let prepared = prepareMessage(
+    getCleanedPacket(key, packet),
+    {getNextId: () => "1"}
+  );
+
   return `
 stubPreparedMessages.set("${key}", new ConsoleMessage(${JSON.stringify(prepared, null, "\t")}));
 `;
 }
 
 function formatNetworkStub(key, packet) {
   let actor = packet.eventActor;
   let networkInfo = {
new file mode 100644
--- /dev/null
+++ b/devtools/client/webconsole/new-console-output/test/fixtures/stubs/moz.build
@@ -0,0 +1,11 @@
+# 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/.
+
+DevToolsModules(
+  'consoleApi.js',
+  'evaluationResult.js',
+  'index.js',
+  'networkEvent.js',
+  'pageError.js',
+)