From a1362ceca2ab74a380ea479786c5f7ce0b59cfb7 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Tue, 12 Dec 2023 13:02:47 -0700 Subject: [PATCH] Escape candid strings for bash --- property_tests/arbitraries/canister_arb.ts | 32 ++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/property_tests/arbitraries/canister_arb.ts b/property_tests/arbitraries/canister_arb.ts index dbea963343..496776b9d9 100644 --- a/property_tests/arbitraries/canister_arb.ts +++ b/property_tests/arbitraries/canister_arb.ts @@ -41,11 +41,22 @@ export function CanisterArb< ...(config.updateMethods ?? []) ]; - const initArgs = config.initMethod?.params.map(({ el: { value } }) => { - return value.candidTypeObject - .getIdl([]) - .valueToString(value.agentArgumentValue); - }); + const initArgs = config.initMethod?.params.map( + ({ + el: { + src: { candidTypeAnnotation }, + value + } + }) => { + const paramCandidString = value.candidTypeObject + .getIdl([]) + .valueToString(value.agentArgumentValue); + + return candidTypeAnnotation === 'text' + ? escapeCandidStringForBash(paramCandidString) + : paramCandidString; + } + ); const sourceCode = generateSourceCode( config.globalDeclarations ?? [], @@ -129,3 +140,14 @@ function generateSourceCode( }); `; } + +function escapeCandidStringForBash(input: string) { + return `"${escapeForBash(input.slice(1, -1))}"`; +} + +function escapeForBash(input: string) { + return input + .replace(/\\/g, '\\\\') // Escape backslashes + .replace(/'/g, "'\\''") // Escape single quotes + .replace(/"/g, '\\"'); // Escape double quotes +}