fix: tests fail on Windows

This commit is contained in:
tanzhenxin
2025-09-10 16:24:59 +08:00
parent 22dfefc9f1
commit e341e9ae37
4 changed files with 315 additions and 38 deletions

View File

@@ -20,33 +20,36 @@
export function parse(yamlString: string): Record<string, unknown> {
const lines = yamlString
.split('\n')
.map((line) => line.trim())
.filter((line) => line && !line.startsWith('#'));
.filter((line) => line.trim() && !line.trim().startsWith('#'));
const result: Record<string, unknown> = {};
let currentKey = '';
let currentArray: string[] = [];
let currentArray: unknown[] = [];
let inArray = false;
let currentObject: Record<string, unknown> = {};
let inObject = false;
let objectKey = '';
for (const line of lines) {
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Handle array items
if (line.startsWith('- ')) {
if (line.startsWith(' - ')) {
if (!inArray) {
inArray = true;
currentArray = [];
}
currentArray.push(line.substring(2).trim());
const itemRaw = line.substring(4).trim();
currentArray.push(parseValue(itemRaw));
continue;
}
// End of array
if (inArray && !line.startsWith('- ')) {
if (inArray && !line.startsWith(' - ')) {
result[currentKey] = currentArray;
inArray = false;
currentArray = [];
currentKey = '';
}
// Handle nested object items (simple indentation)
@@ -62,6 +65,7 @@ export function parse(yamlString: string): Record<string, unknown> {
result[objectKey] = currentObject;
inObject = false;
currentObject = {};
objectKey = '';
}
// Handle key-value pairs
@@ -72,17 +76,25 @@ export function parse(yamlString: string): Record<string, unknown> {
if (value === '') {
// This might be the start of an object or array
currentKey = key.trim();
// Check if next lines are indented (object) or start with - (array)
continue;
// Look ahead to determine if this is an array or object
if (i + 1 < lines.length) {
const nextLine = lines[i + 1];
if (nextLine.startsWith(' - ')) {
// Next line is an array item, so this will be handled in the next iteration
continue;
} else if (nextLine.startsWith(' ')) {
// Next line is indented, so this is an object
inObject = true;
objectKey = currentKey;
currentObject = {};
currentKey = '';
continue;
}
}
} else {
result[key.trim()] = parseValue(value);
}
} else if (currentKey && !inArray && !inObject) {
// This might be the start of an object
inObject = true;
objectKey = currentKey;
currentObject = {};
currentKey = '';
}
}
@@ -114,7 +126,7 @@ export function stringify(
if (Array.isArray(value)) {
lines.push(`${key}:`);
for (const item of value) {
lines.push(` - ${item}`);
lines.push(` - ${formatValue(item)}`);
}
} else if (typeof value === 'object' && value !== null) {
lines.push(`${key}:`);
@@ -140,6 +152,13 @@ function parseValue(value: string): unknown {
if (value === 'null') return null;
if (value === '') return '';
// Handle quoted strings
if (value.startsWith('"') && value.endsWith('"') && value.length >= 2) {
const unquoted = value.slice(1, -1);
// Unescape quotes and backslashes
return unquoted.replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}
// Try to parse as number
const num = Number(value);
if (!isNaN(num) && isFinite(num)) {
@@ -155,9 +174,16 @@ function parseValue(value: string): unknown {
*/
function formatValue(value: unknown): string {
if (typeof value === 'string') {
// Quote strings that might be ambiguous
if (value.includes(':') || value.includes('#') || value.trim() !== value) {
return `"${value.replace(/"/g, '\\"')}"`;
// Quote strings that might be ambiguous or contain special characters
if (
value.includes(':') ||
value.includes('#') ||
value.includes('"') ||
value.includes('\\') ||
value.trim() !== value
) {
// Escape backslashes THEN quotes
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}
return value;
}