Skip to content

Commit

Permalink
prevent TemplateProcessor context from overwritting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Sergeev committed Jan 22, 2024
1 parent 14bead3 commit e4efde6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/CliCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/StatedREPL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
}
);
});


0 comments on commit e4efde6

Please sign in to comment.