diff --git a/property_tests/arbitraries/canister_arb.ts b/property_tests/arbitraries/canister_arb.ts index 193f698476..2f2ccd0ab3 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 ?? [], @@ -126,3 +137,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 +}