From d3c02145a662d27aa8fcc34575ce059d5f2fff61 Mon Sep 17 00:00:00 2001 From: tchapacan Date: Sun, 14 Jan 2024 16:22:41 +0100 Subject: [PATCH] assert probes in proberunner --- .gitignore | 1 + src/ProbeRunner.js | 15 +++++++++++- test/ProbeRunner.spec.js | 53 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 7f815f4..b627d17 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,4 @@ dist temp.js temp/ .vscode/ +.idea/ diff --git a/src/ProbeRunner.js b/src/ProbeRunner.js index dc314cf..2debece 100644 --- a/src/ProbeRunner.js +++ b/src/ProbeRunner.js @@ -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"; @@ -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; } diff --git a/test/ProbeRunner.spec.js b/test/ProbeRunner.spec.js index 621c2a6..143671a 100644 --- a/test/ProbeRunner.spec.js +++ b/test/ProbeRunner.spec.js @@ -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"); }); });