0
Fork 0
mirror of https://github.com/oblien/openship.git synced 2026-11-18 06:55:56 +00:00
openship/packages/core/test/shell-split.test.ts
shuvamk e4940828fe fix(core): keep single-quoted backslashes and empty quoted args in shellSplitWords
`shellSplitWords` is what turns a compose `command` string into argv (#332), but
it diverges from docker compose's own tokenizer in two ways, so a service can be
started with different arguments than `docker compose up` would use.

Verified against `docker compose config --format json` (Docker 29.6.1, Compose
5.2.0) as the oracle:

  command: awk '{ gsub(/\r/, ""); print }' /data/in
    compose   ["awk", "{ gsub(/\\r/, \"\"); print }", "/data/in"]
    openship  ["awk", "{ gsub(/r/, \"\"); print }",   "/data/in"]

  command: node app.js --base-href ""
    compose   ["node", "app.js", "--base-href", ""]
    openship  ["node", "app.js", "--base-href"]

Cause: the `\\` branch ran before the `inSingle` check, so a backslash inside
single quotes was consumed as an escape — an awk/sed/grep program loses every
`\r`, `\d`, `\.` and silently means something else. And a word was only pushed
when `current` was non-empty, so an explicitly quoted empty argument was dropped
instead of becoming an empty argv element, shifting every argument after it.

Fix: skip escape handling while `inSingle`, and track whether the current word
had a quote so an empty one is still emitted. `""` as the whole command still
yields `[]` (clear the image CMD) because `trim()` leaves no characters to scan.

Over 16 differential cases against the compose oracle, matches go from 6/16 to
14/16. The two that still differ are out of scope here: ANSI-C escapes outside
single quotes (`printf "a\tb\n"`) are expanded by compose and not by us, and
compose's shlex truncates `a && b` at the operator while this module documents
and tests operators as literal argv.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 17:50:28 +05:30

60 lines
2.4 KiB