Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #800 from ochameau/mapping-priority
Browse files Browse the repository at this point in the history
Bug 842486 - Fix priority of modules matched by mapping file. r=@Gozala
  • Loading branch information
ochameau committed Feb 28, 2013
2 parents e20fb10 + 9db0339 commit 7d4b7d3
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 15 deletions.
41 changes: 26 additions & 15 deletions python-lib/cuddlefish/manifest.py
Expand Up @@ -474,18 +474,6 @@ def BAD(msg):
# non-relative import. Might be a short name (requiring a search
# through "library" packages), or a fully-qualified one.

# Search for a module in new layout.
# First normalize require argument in order to easily find a mapping
normalized = reqname
if normalized.endswith(".js"):
normalized = normalized[:-len(".js")]
if normalized.startswith("addon-kit/"):
normalized = normalized[len("addon-kit/"):]
if normalized.startswith("api-utils/"):
normalized = normalized[len("api-utils/"):]
if normalized in NEW_LAYOUT_MAPPING:
reqname = NEW_LAYOUT_MAPPING[normalized]

if "/" in reqname:
# 2: PKG/MOD: find PKG, look inside for MOD
bits = reqname.split("/")
Expand All @@ -507,9 +495,32 @@ def BAD(msg):
# their own package first, then the list of packages defined by their
# .dependencies list
from_pkg = from_module.package.name
return self._search_packages_for_module(from_pkg,
lookfor_sections, reqname,
looked_in)
mi = self._search_packages_for_module(from_pkg,
lookfor_sections, reqname,
looked_in)
if mi:
return mi

# Only after we look for module in the addon itself, search for a module
# in new layout.
# First normalize require argument in order to easily find a mapping
normalized = reqname
if normalized.endswith(".js"):
normalized = normalized[:-len(".js")]
if normalized.startswith("addon-kit/"):
normalized = normalized[len("addon-kit/"):]
if normalized.startswith("api-utils/"):
normalized = normalized[len("api-utils/"):]
if normalized in NEW_LAYOUT_MAPPING:
# get the new absolute path for this module
reqname = NEW_LAYOUT_MAPPING[normalized]
from_pkg = from_module.package.name
return self._search_packages_for_module(from_pkg,
lookfor_sections, reqname,
looked_in)
else:
# We weren't able to find this module, really.
return None

def _handle_module(self, mi):
if not mi:
Expand Down
54 changes: 54 additions & 0 deletions test/addons/require/main.js
@@ -0,0 +1,54 @@
/* 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/. */
"use strict";

exports["test local vs sdk module"] = function (assert) {
assert.notEqual(require("memory"),
require("sdk/deprecated/memory"),
"Local module takes the priority over sdk modules");
assert.ok(require("memory").local,
"this module is really the local one");
}

exports["test 3rd party vs sdk module"] = function (assert) {
// We are testing with a 3rd party package called `panel` with 3 modules
// main, page-mod and third-party

// the only way to require 3rd party package modules are to use absolute paths
// require("panel/main"), require("panel/page-mod"),
// require("panel/third-party") and also require("panel") which will refer
// to panel's main package module.

// So require(page-mod) shouldn't map the 3rd party
assert.equal(require("page-mod"),
require("sdk/page-mod"),
"Third party modules don't overload sdk modules");
assert.ok(require("page-mod").PageMod,
"page-mod module is really the sdk one");

assert.equal(require("panel/page-mod").id, "page-mod",
"panel/page-mod is the 3rd party");

// But require(panel) will map to 3rd party main module
// *and* overload the sdk module
// and also any local module with the same name
assert.equal(require("panel").id, "panel-main",
"Third party main module overload sdk modules");
assert.equal(require("panel"),
require("panel/main"),
"require(panel) maps to require(panel/main)");
// So that you have to use relative path to ensure getting the local module
assert.equal(require("./panel").id,
"local-panel",
"require(./panel) maps to the local module");

// It should still be possible to require sdk module with absolute path
assert.ok(require("sdk/panel").Panel,
"We can bypass this overloading with absolute path to sdk modules");
assert.equal(require("sdk/panel"),
require("addon-kit/panel"),
"Old and new layout both work");
}

require("sdk/test/runner").runTestsFromModule(module);
5 changes: 5 additions & 0 deletions test/addons/require/memory.js
@@ -0,0 +1,5 @@
/* 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/. */

exports.local = true;
4 changes: 4 additions & 0 deletions test/addons/require/package.json
@@ -0,0 +1,4 @@
{
"id": "test-require",
"packages": "packages"
}
5 changes: 5 additions & 0 deletions test/addons/require/packages/panel/main.js
@@ -0,0 +1,5 @@
/* 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/. */

exports.id = "panel-main";
3 changes: 3 additions & 0 deletions test/addons/require/packages/panel/package.json
@@ -0,0 +1,3 @@
{
"id": "test-panel"
}
5 changes: 5 additions & 0 deletions test/addons/require/packages/panel/page-mod.js
@@ -0,0 +1,5 @@
/* 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/. */

exports.id = "page-mod";
5 changes: 5 additions & 0 deletions test/addons/require/panel.js
@@ -0,0 +1,5 @@
/* 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/. */

exports.id = "local-panel";

0 comments on commit 7d4b7d3

Please sign in to comment.