Skip to content

Commit

Permalink
Split apart and add unit tests for getTelemetryFor. (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored and Chris Garrett committed Jun 20, 2019
1 parent 008e0e7 commit 29c6014
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
10 changes: 9 additions & 1 deletion .eslintrc.js
Expand Up @@ -11,5 +11,13 @@ module.exports = {
plugins: ["prettier"],
rules: {
"prettier/prettier": "error"
}
},
overrides: [
{
files: ['**/*.test.js'],
env: {
jest: true
}
}
]
};
2 changes: 1 addition & 1 deletion transforms/helpers/parse-helper.js
Expand Up @@ -6,7 +6,7 @@ const {
getOptions,
startsWithUpperCaseLetter
} = require("./util");
const getTelemetryFor = require("./util/get-telemetry-for");
const { getTelemetryFor } = require("./util/get-telemetry-for");
const {
hasValidProps,
isFileOfType,
Expand Down
35 changes: 26 additions & 9 deletions transforms/helpers/util/get-telemetry-for.js
Expand Up @@ -6,7 +6,7 @@ const cache = require("../../../lib/cache");
const telemetry = cache.has("telemetry")
? JSON.parse(cache.get("telemetry").value)
: {};
const addonFilePaths = {};
const ADDON_PATHS = {};

let packagePaths = walkSync("./", {
globs: ["**/package.json"],
Expand All @@ -16,34 +16,51 @@ let packagePaths = walkSync("./", {
for (let packagePath of packagePaths) {
let { name } = fs.readJsonSync(packagePath);

addonFilePaths[path.dirname(path.resolve(".", packagePath))] = name;
ADDON_PATHS[path.dirname(path.resolve(".", packagePath))] = name;
}

/**
* Get the runtime data for the file being transformed
* Transforms a literal "on disk" path to a "module path".
*
* @param {String} filePath Absolute path of the file to read data from
* @returns {Object} Runtime configuration object
* @param {String} filePath the path on disk (from current working directory)
* @returns {String} The in-browser module path for the specified filePath
*/
module.exports = function getTelemetryData(filePath) {
function getModulePathFor(filePath, addonPaths = ADDON_PATHS) {
let fileSegments = filePath.split("/");
let addonSegments = [];

while (fileSegments.length > 0) {
addonSegments.push(fileSegments.shift());

if (addonFilePaths[addonSegments.join("/")]) {
if (addonPaths[addonSegments.join("/")]) {
break;
}
}

let addonFilePath = addonSegments.join("/");
let addonName = addonFilePaths[addonFilePath];
let addonName = addonPaths[addonFilePath];

let relativeFilePath = fileSegments
.join("/")
.replace(/^(addon|app)\//, "")
.replace(/\.[^/.]+$/, "");

return telemetry[`${addonName}/${relativeFilePath}`];
return `${addonName}/${relativeFilePath}`;
}

/**
* Get the runtime data for the file being transformed
*
* @param {String} filePath Absolute path of the file to read data from
* @returns {Object} Runtime configuration object
*/
function getTelemetryFor(filePath) {
let modulePath = getModulePathFor(filePath);

return telemetry[modulePath];
}

module.exports = {
getTelemetryFor,
getModulePathFor
};
17 changes: 17 additions & 0 deletions transforms/helpers/util/get-telemetry-for.test.js
@@ -0,0 +1,17 @@
const { getModulePathFor } = require("./get-telemetry-for");

describe("getModulePathFor", () => {
test("accesses telemetry data for the specified app module", () => {
const addonPaths = {
"/User/whomever/voyager-web/lib/invitation-platform":
"invitation-platform"
};

expect(
getModulePathFor(
"/User/whomever/voyager-web/lib/invitation-platform/addon/components/fuse-limit-alert",
addonPaths
)
).toEqual("invitation-platform/components/fuse-limit-alert");
});
});

0 comments on commit 29c6014

Please sign in to comment.