Skip to content

Commit

Permalink
Escape candid strings for bash
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Dec 18, 2023
1 parent a361bab commit 7ec8bb8
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions property_tests/arbitraries/canister_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [],
Expand Down Expand Up @@ -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
}

0 comments on commit 7ec8bb8

Please sign in to comment.