Skip to content

Commit

Permalink
assert probes in proberunner
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapacan committed Jan 14, 2024
1 parent 9a18a14 commit d3c0214
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ dist
temp.js
temp/
.vscode/
.idea/
15 changes: 14 additions & 1 deletion src/ProbeRunner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Import Native Dependencies
import assert from "node:assert";

// Import all the probes
import isUnsafeCallee from "./probes/isUnsafeCallee.js";
import isLiteral from "./probes/isLiteral.js";
Expand Down Expand Up @@ -71,7 +74,17 @@ export class ProbeRunner {
) {
this.sourceFile = sourceFile;

// TODO: Assert/validate all probes? (Good first issue)
for (const probe of probes) {
assert(
typeof probe.validateNode === "function" || Array.isArray(probe.validateNode),
`Invalid probe ${probe.name}: validateNode must be a function or an array of functions`
);
assert(
typeof probe.main === "function",
`Invalid probe ${probe.name}: main must be a function`
);
}

this.probes = probes;
}

Expand Down
53 changes: 49 additions & 4 deletions test/ProbeRunner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,57 @@ describe("ProbeRunner", () => {
assert.strictEqual(pr.probes, ProbeRunner.Defaults);
});

it("should use provided probes", () => {
const probes = [{}];
it("should use provided probes with validate node as func", () => {
const fakeProbe = [
{
validateNode: (node) => [node.type === "CallExpression"],
main: mock.fn(),
teardown: mock.fn()
}
];

const pr = new ProbeRunner(null, probes);
const pr = new ProbeRunner(null, fakeProbe);
assert.strictEqual(pr.sourceFile, null);
assert.strictEqual(pr.probes, probes);
assert.strictEqual(pr.probes, fakeProbe);
});

it("should use provided probe with validate node as Array", () => {
const fakeProbe = [
{
validateNode: [],
main: mock.fn(),
teardown: mock.fn()
}];

const pr = new ProbeRunner(null, fakeProbe);
assert.strictEqual(pr.sourceFile, null);
assert.strictEqual(pr.probes, fakeProbe);
});

it("should fail if main not present", () => {
const fakeProbe = {
validateNode: (node) => [node.type === "CallExpression"],
teardown: mock.fn()
};

function instantiateProbeRunner() {
return new ProbeRunner(null, [fakeProbe]);
}

assert.throws(instantiateProbeRunner, Error, "Invalid probe");
});

it("should fail if validate not present", () => {
const fakeProbe = {
main: mock.fn(),
teardown: mock.fn()
};

function instantiateProbeRunner() {
return new ProbeRunner(null, [fakeProbe]);
}

assert.throws(instantiateProbeRunner, Error, "Invalid probe");
});
});

Expand Down

0 comments on commit d3c0214

Please sign in to comment.