From e4efde6ffc7451503606b91112e325d4196dd6a9 Mon Sep 17 00:00:00 2001 From: Sergey Sergeev Date: Mon, 22 Jan 2024 10:43:09 -0800 Subject: [PATCH] prevent TemplateProcessor context from overwritting --- src/CliCore.ts | 2 +- src/test/StatedREPL.test.js | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/CliCore.ts b/src/CliCore.ts index 00165764..3c8e89b5 100644 --- a/src/CliCore.ts +++ b/src/CliCore.ts @@ -127,7 +127,7 @@ export default class CliCore { } else { // if we are re-initializing, we need to reset the tagSet and options, if provided this.templateProcessor.tagSet = new Set(); this.templateProcessor.options = options; - if (contextData) { + if (contextData && Object.keys(contextData).length > 0) { this.templateProcessor.setupContext(contextData); } } diff --git a/src/test/StatedREPL.test.js b/src/test/StatedREPL.test.js index ee9a0011..557e96c9 100644 --- a/src/test/StatedREPL.test.js +++ b/src/test/StatedREPL.test.js @@ -13,6 +13,7 @@ // limitations under the License. import StatedREPL from "../../dist/src/StatedREPL.js"; +import TemplateProcessor from "../../dist/src/TemplateProcessor.js"; test("test stringify", async () => { expect(StatedREPL.stringify({a: 1, b: 2})).toBe( @@ -60,6 +61,20 @@ test("test onInit", async () => { } }); - +// This test validates a bug when running an init command in StatedREPL overwrites context of provided TemplateProcessor +test("TemplateProcessor keeps context on init", async () => { + const nozzle = (something) => "nozzle got some " + something; + const context = {"nozzle": nozzle, "ZOINK": "ZOINK"} + const tp = new TemplateProcessor({ + "a": "${$nozzle($ZOINK)}" + }, context); + const repl = new StatedREPL(tp); + await repl.cliCore.init('-f "example/contextFunc.json"'); + expect(tp.output).toEqual( + { + "a": "nozzle got some ZOINK", + } + ); +});