Skip to content

Commit

Permalink
feat: Use relative paths in appmaps
Browse files Browse the repository at this point in the history
Fixes #100
  • Loading branch information
dividedmind committed Jan 31, 2024
1 parent b0b9a6e commit 5a1949f
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 120 deletions.
12 changes: 9 additions & 3 deletions src/__tests__/classMap.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert";
import AppMap from "../AppMap";
import { makeClassMap } from "../classMap";
import config from "../config";
import { FunctionInfo, SourceLocation } from "../registry";

describe(makeClassMap, () => {
Expand Down Expand Up @@ -41,12 +42,17 @@ describe(makeClassMap, () => {
);
});

it("only creates a package for a file if absolutely required", () => {
it("uses the app name as the top level package if required", () => {
jest.replaceProperty(config, "appName", "testApp");
// if a path is just a file, the class would have been
// toplevel which is prohibited by the spec
check([f("fun", "util.js:42", "util"), f("other", "src/other.js:42", "other")], {
util: { c_util: ["fun@util.js:42"] },
src: { c_other: ["other@src/other.js:42"] },
testApp: {
c_util: ["fun@util.js:42"],
src: {
c_other: ["other@src/other.js:42"],
},
},
});
});
});
Expand Down
7 changes: 4 additions & 3 deletions src/classMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert";

import type AppMap from "./AppMap";
import config from "./config";
import type { FunctionInfo, SourceLocation } from "./registry";

type FNode = [FTree, Record<string, FunctionInfo[]>];
Expand All @@ -13,9 +14,9 @@ export function makeClassMap(funs: Iterable<FunctionInfo>): AppMap.ClassMap {
for (const fun of sortFunctions(funs)) {
if (!fun.location) continue;
// fun.location can contain "/" as separator even in Windows
const pkgs = fun.location.path.split(/[/\\]/).reverse();
if (pkgs.length > 1) pkgs.shift(); // remove the file name (e.g. "foo.js")
else pkgs.push(pkgs.pop()!.replace(/\.[^.]*$/, "")); // remove the suffix
const pkgs = fun.location.path.split(/[/\\]/).reverse().slice(1);
// add app name as fallback top level package
pkgs.push(config.appName);

let [tree, classes]: FNode = [root, {}];
while (pkgs.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/__tests__/instrument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe(instrument.transform, () => {
return arg + 1;
}
`,
{ loc: true, source: "test.js" },
{ loc: true, source: "/test/test.js" },
);
expect(stripLocations(instrument.transform(program))).toStrictEqual(
parse(`
Expand Down Expand Up @@ -79,7 +79,7 @@ describe(instrument.transform, () => {
}
}
`,
{ loc: true, source: "test.js" },
{ loc: true, source: "/test/test.js" },
);

expect(stripLocations(instrument.transform(program))).toStrictEqual(
Expand Down
17 changes: 14 additions & 3 deletions src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { basename } from "node:path";
import { basename, relative } from "node:path";

import type { ESTree } from "meriyah";

import config from "./config";

export interface SourceLocation {
path: string;
lineno: number;
Expand Down Expand Up @@ -30,7 +32,7 @@ export function createFunctionInfo(
generator,
id: id.name,
params: params.map(stripLocation),
location,
location: relativeLocation(location),
klassOrFile: pkgOfPath(location.path),
static: true,
};
Expand All @@ -53,7 +55,7 @@ export function createMethodInfo(
params: params.map(stripLocation),
static: method.static,
klassOrFile: klass.id.name,
location,
location: relativeLocation(location),
};
return info;
}
Expand All @@ -69,3 +71,12 @@ function pkgOfPath(path: string): string {
if (base.includes(".")) return base.split(".").slice(0, -1).join(".");
else return base;
}

// return location relative to config.root
function relativeLocation(location: SourceLocation | undefined): SourceLocation | undefined {
if (!location) return undefined;
return {
path: relative(config.root, location.path),
lineno: location.lineno,
};
}
24 changes: 12 additions & 12 deletions test/__snapshots__/httpClient.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports[`mapping a Jest test 1`] = `
"type": "class",
},
],
"name": "test",
"name": "http-client-appmap-node-test",
"type": "package",
},
],
Expand Down Expand Up @@ -73,7 +73,7 @@ exports[`mapping http client requests (ESM) 1`] = `
{
"children": [
{
"location": "./esm.mjs:6",
"location": "esm.mjs:6",
"name": "makeRequest",
"static": true,
"type": "function",
Expand All @@ -83,7 +83,7 @@ exports[`mapping http client requests (ESM) 1`] = `
"type": "class",
},
],
"name": "httpClient",
"name": "http-client-appmap-node-test",
"type": "package",
},
],
Expand All @@ -109,7 +109,7 @@ exports[`mapping http client requests (ESM) 1`] = `
"lineno": 6,
"method_id": "makeRequest",
"parameters": [],
"path": "./esm.mjs",
"path": "esm.mjs",
"static": true,
"thread_id": 0,
},
Expand Down Expand Up @@ -182,7 +182,7 @@ exports[`mapping http client requests 1`] = `
{
"children": [
{
"location": "./index.js:18",
"location": "index.js:18",
"name": "makeRequests",
"static": true,
"type": "function",
Expand All @@ -192,7 +192,7 @@ exports[`mapping http client requests 1`] = `
"type": "class",
},
],
"name": "httpClient",
"name": "http-client-appmap-node-test",
"type": "package",
},
],
Expand All @@ -218,7 +218,7 @@ exports[`mapping http client requests 1`] = `
"lineno": 18,
"method_id": "makeRequests",
"parameters": [],
"path": "./index.js",
"path": "index.js",
"static": true,
"thread_id": 0,
},
Expand Down Expand Up @@ -344,13 +344,13 @@ exports[`mapping mocked http client requests 1`] = `
{
"children": [
{
"location": "./index.js:18",
"location": "index.js:18",
"name": "makeRequests",
"static": true,
"type": "function",
},
{
"location": "./index.js:38",
"location": "index.js:38",
"name": "mocked",
"static": true,
"type": "function",
Expand All @@ -360,7 +360,7 @@ exports[`mapping mocked http client requests 1`] = `
"type": "class",
},
],
"name": "httpClient",
"name": "http-client-appmap-node-test",
"type": "package",
},
],
Expand All @@ -386,7 +386,7 @@ exports[`mapping mocked http client requests 1`] = `
"lineno": 38,
"method_id": "mocked",
"parameters": [],
"path": "./index.js",
"path": "index.js",
"static": true,
"thread_id": 0,
},
Expand All @@ -397,7 +397,7 @@ exports[`mapping mocked http client requests 1`] = `
"lineno": 18,
"method_id": "makeRequests",
"parameters": [],
"path": "./index.js",
"path": "index.js",
"static": true,
"thread_id": 0,
},
Expand Down
32 changes: 16 additions & 16 deletions test/__snapshots__/httpServer.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[`mapping Express.js requests 1`] = `
{
"children": [
{
"location": "./express.js:21",
"location": "express.js:21",
"name": "helloWorld",
"static": true,
"type": "function",
Expand All @@ -19,7 +19,7 @@ exports[`mapping Express.js requests 1`] = `
"type": "class",
},
],
"name": "httpServer",
"name": "http-server-appmap-node-test",
"type": "package",
},
],
Expand Down Expand Up @@ -61,7 +61,7 @@ exports[`mapping Express.js requests 1`] = `
"value": "[Function: next]",
},
],
"path": "./express.js",
"path": "express.js",
"static": true,
"thread_id": 0,
},
Expand Down Expand Up @@ -169,7 +169,7 @@ exports[`mapping Express.js requests 1`] = `
{
"children": [
{
"location": "./express.js:25",
"location": "express.js:25",
"name": "api",
"static": true,
"type": "function",
Expand All @@ -179,7 +179,7 @@ exports[`mapping Express.js requests 1`] = `
"type": "class",
},
],
"name": "httpServer",
"name": "http-server-appmap-node-test",
"type": "package",
},
],
Expand Down Expand Up @@ -266,7 +266,7 @@ exports[`mapping Express.js requests 1`] = `
"value": "{ param1: '3', param2: '4' }",
},
],
"path": "./express.js",
"path": "express.js",
"static": true,
"thread_id": 0,
},
Expand Down Expand Up @@ -339,7 +339,7 @@ exports[`mapping Express.js requests 1`] = `
{
"children": [
{
"location": "./express.js:29",
"location": "express.js:29",
"name": "postApi",
"static": true,
"type": "function",
Expand All @@ -349,7 +349,7 @@ exports[`mapping Express.js requests 1`] = `
"type": "class",
},
],
"name": "httpServer",
"name": "http-server-appmap-node-test",
"type": "package",
},
],
Expand Down Expand Up @@ -500,7 +500,7 @@ exports[`mapping Express.js requests 1`] = `
}",
},
],
"path": "./express.js",
"path": "express.js",
"static": true,
"thread_id": 0,
},
Expand Down Expand Up @@ -608,19 +608,19 @@ exports[`mapping Express.js requests with remote recording 1`] = `
{
"children": [
{
"location": "./express.js:21",
"location": "express.js:21",
"name": "helloWorld",
"static": true,
"type": "function",
},
{
"location": "./express.js:25",
"location": "express.js:25",
"name": "api",
"static": true,
"type": "function",
},
{
"location": "./express.js:29",
"location": "express.js:29",
"name": "postApi",
"static": true,
"type": "function",
Expand All @@ -630,7 +630,7 @@ exports[`mapping Express.js requests with remote recording 1`] = `
"type": "class",
},
],
"name": "httpServer",
"name": "http-server-appmap-node-test",
"type": "package",
},
],
Expand Down Expand Up @@ -775,7 +775,7 @@ exports[`mapping Express.js requests with remote recording 1`] = `
"value": "[Function: next]",
},
],
"path": "./express.js",
"path": "express.js",
"static": true,
"thread_id": 0,
},
Expand Down Expand Up @@ -880,7 +880,7 @@ exports[`mapping Express.js requests with remote recording 1`] = `
"value": "{ param1: '3', param2: '4' }",
},
],
"path": "./express.js",
"path": "express.js",
"static": true,
"thread_id": 0,
},
Expand Down Expand Up @@ -999,7 +999,7 @@ exports[`mapping Express.js requests with remote recording 1`] = `
}",
},
],
"path": "./express.js",
"path": "express.js",
"static": true,
"thread_id": 0,
},
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/jest.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports[`mapping Jest tests 1`] = `
"type": "class",
},
],
"name": "test",
"name": "jest-appmap-node-test",
"type": "package",
},
],
Expand Down Expand Up @@ -92,7 +92,7 @@ exports[`mapping Jest tests 1`] = `
"type": "class",
},
],
"name": "test",
"name": "jest-appmap-node-test",
"type": "package",
},
],
Expand Down Expand Up @@ -170,7 +170,7 @@ Add a timeout value to this test to increase the timeout, if this is a long-runn
"type": "class",
},
],
"name": "calc",
"name": "jest-appmap-node-test",
"type": "package",
},
],
Expand Down Expand Up @@ -254,7 +254,7 @@ Received: 3",
"type": "class",
},
],
"name": "calc",
"name": "jest-appmap-node-test",
"type": "package",
},
],
Expand Down
Loading

0 comments on commit 5a1949f

Please sign in to comment.