Skip to content

Commit 6a7dca8

Browse files
committed
improved json parsing
1 parent a61b6f9 commit 6a7dca8

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

packages/b2c-cli/test/functional/e2e/ods-lifecycle.test.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,32 @@ const __dirname = path.dirname(__filename);
1515
/**
1616
* Helper function to parse JSON response from CLI
1717
*/
18+
function extractJsonFromText(text: string): null | string {
19+
const firstBrace = text.indexOf('{');
20+
const lastBrace = text.lastIndexOf('}');
21+
if (firstBrace !== -1 && lastBrace !== -1 && lastBrace > firstBrace) {
22+
return text.slice(firstBrace, lastBrace + 1);
23+
}
24+
25+
const firstBracket = text.indexOf('[');
26+
const lastBracket = text.lastIndexOf(']');
27+
if (firstBracket !== -1 && lastBracket !== -1 && lastBracket > firstBracket) {
28+
return text.slice(firstBracket, lastBracket + 1);
29+
}
30+
31+
return null;
32+
}
33+
1834
function parseJson(output: string): Record<string, unknown> {
1935
try {
20-
// Try to parse the entire output as JSON first
2136
return JSON.parse(output);
2237
} catch {
23-
// If that fails, look for JSON in the output
24-
const lines = output.split('\n');
25-
for (const line of lines) {
26-
const trimmed = line.trim();
27-
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
28-
try {
29-
return JSON.parse(trimmed);
30-
} catch {}
38+
const jsonString = extractJsonFromText(output);
39+
if (jsonString) {
40+
try {
41+
return JSON.parse(jsonString);
42+
} catch {
43+
// fallthrough to throw below
3144
}
3245
}
3346
throw new Error(`No valid JSON found in output: ${output}`);
@@ -219,7 +232,7 @@ describe('ODS Lifecycle E2E Tests', function () {
219232
expect(result.exitCode).to.equal(0, `Start command failed: ${result.stderr}`);
220233
const state = await getSandboxState(sandboxId);
221234
if (state) {
222-
expect(['started']).to.include(state);
235+
expect(['started', 'starting']).to.include(state);
223236
}
224237
});
225238
});

0 commit comments

Comments
 (0)