Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/internal/KNOWN-PITFALLS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 30 additions & 0 deletions src/drivers/installer-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
10 changes: 7 additions & 3 deletions src/drivers/installer-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading