diff --git a/docs/internal/KNOWN-PITFALLS.md b/docs/internal/KNOWN-PITFALLS.md index 83e3bd9f2..bf1f29b9f 100644 --- a/docs/internal/KNOWN-PITFALLS.md +++ b/docs/internal/KNOWN-PITFALLS.md @@ -333,7 +333,11 @@ single line, read when the install is already over, whose last percentage is 100. `runInstallerProcess` therefore spawns with `lineEnd: "carriage-return-too"`, which ends a line at a bare `\r` as well. Anything else that reads an installer's progress must do the same, or it -reports nothing until the end. +reports nothing until the end. `xcodebuild` also formats its numbers in the +user's locale, so in a region with a decimal comma it prints `9,7%`; the +parser accepts a dot or a comma before the decimals (#260). A locale whose +digits are not ASCII (Arabic `٩٫٧٪`) still matches nothing, so that download +reports no progress at all rather than wrong progress. ## An iOS runtime download outlives the runtime, and only Xcode can reclaim it (#79) diff --git a/src/drivers/installer-process.test.ts b/src/drivers/installer-process.test.ts index 8a0b3928f..0f8def9d6 100644 --- a/src/drivers/installer-process.test.ts +++ b/src/drivers/installer-process.test.ts @@ -58,6 +58,36 @@ describe("runInstallerProcess", () => { expect(progress.map((report) => report.percent)).toEqual([10, 20, 30]); }); + it("reads a percentage written with a decimal comma, as xcodebuild prints it in a region that uses one", async () => { + // Byte for byte as `xcodebuild -downloadPlatform iOS -buildVersion 18.4` wrote it under + // language English, region Poland: one `\n`, then `\r`-separated redraws, a line with only + // the title, a "Preparing" line, comma decimals in the percentage and the sizes, and a + // redraw padded with trailing spaces. + const title = "Downloading iOS 18.4 Universal Simulator (22E238):"; + const runner = new ScriptedProcessRunner([ + { + match: { args: ["install"], command }, + stderrLines: [ + [ + "Finding content...\n", + `${title} `, + `${title} Preparing to download...`, + `${title} 0,0% (73 kB of 8,86 GB)`, + `${title} 0,4% (39,4 MB of 8,86 GB)`, + `${title} 2,7% (235 MB of 8,86 GB) `, + `${title} 9,7% (857,8 MB of 8,86 GB)`, + ].join("\r"), + ], + }, + ]); + const clock = new FakeClock(); + + const { outcome, progress } = run(runner, clock); + await outcome; + + expect(progress.map((report) => report.percent)).toEqual([0, 0.4, 2.7, 9.7]); + }); + it("reports each percentage while the installer is still running, before it exits", async () => { const runner = new ScriptedProcessRunner([ { diff --git a/src/drivers/installer-process.ts b/src/drivers/installer-process.ts index 50f2d6c76..7de6295f2 100644 --- a/src/drivers/installer-process.ts +++ b/src/drivers/installer-process.ts @@ -69,12 +69,16 @@ export async function runInstallerProcess( } } -/** The last percentage printed on a line, e.g. `12.5` from `Downloading iOS 18.0: 12.5%`. */ +/** + * The last percentage printed on a line, e.g. `12.5` from `Downloading iOS 18.0: 12.5%`. + * `xcodebuild` formats it in the user's locale, so the decimal separator may be a comma + * (`12,5%`); a percentage never reaches a thousand, so no grouping separator comes before `%`. + */ function parseProgressPercent(line: string): number | undefined { - const matches = [...line.matchAll(/(\d{1,3}(?:\.\d+)?)\s*%/g)]; + const matches = [...line.matchAll(/(\d{1,3}(?:[.,]\d+)?)\s*%/g)]; const last = matches.at(-1)?.[1]; if (last === undefined) return undefined; - const percent = Number.parseFloat(last); + const percent = Number.parseFloat(last.replace(",", ".")); return percent >= 0 && percent <= 100 ? percent : undefined; }