Files
w3cjandClaude Opus 5.5 d55476d3e2 feat(images): the prelude is one line, so build scripts keep their line numbers
Every script a build runs with bash, devvm's own and the user's layers,
now gets the prelude as one line, eval $'…', in place of its #! line, so
bash's errors and $LINENO give the script's own line numbers. A script
without a #! line is one line further down.

- shell.QuoteLine: ANSI-C quoting for bash, on one line of printable
  ASCII; every other byte as \xHH, so a multibyte locale's lead byte
  (Shift-JIS, GBK, GB18030, Big5) can't swallow the backslash escaping a
  quote, and a NUL panics as a caller's bug. image's layer rule allows
  internal/shell, a leaf.
- image.WithPrelude, the one place the prelude and a script are joined
  (OwnScript calls it), used by base.sh, apt.sh, the built-in layers and
  features, the user's layers, shared-tools/build.sh and oci/build.sh.
  Every devvm script has a #! line. A leading UTF-8 BOM goes before the
  #! check.
- A build's exports for a company proxy (lima's proxyExports) and the
  user's CAs (trust's exports) end in "; ", not a newline, so they share
  the first line and move no line either; backend.Trust.Env says so.
- The stub logs a bash -s script's second line, which is the script's
  own second line with or without the prelude.
- Keys: every build script's bytes change, so base, every image and
  every shared tool image are built again once; the CHANGELOG's After
  you upgrade says so with git-lfs, and what VMs say until devvm update,
  and Breaking has its own heading.
- Tests: TestQuoteLine (every byte but NUL, a break-out word, in
  /bin/bash 3.2 and PATH's bash, in C and each of ja_JP.SJIS, zh_CN.GBK,
  zh_CN.GB18030 and zh_TW.Big5 that locale -a has), TestQuoteLineNUL,
  TestWithPrelude's shebang and BOM cases, TestWithPreludeRuns (the real
  prelude's line gives the prelude back; a layer under set -euo pipefail
  gets the names, keeps its function and its line numbers, with and
  without exports before it).
- e2e nix: the layer copy no longer adds the prelude itself.
- Docs: images.md's prelude section, Customizing's layer bullet,
  structure.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 17:46:30 -07:00

162 lines
4.6 KiB
Go

package shell
import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)
var nasty = []string{
"", "plain", "two words", "it's", `"double"`, `back\slash`, "$HOME", "$(touch /tmp/x)", "`id`",
"a;b", "a&&b", "a|b", "*", "~", "!x", "new\nline", "tab\there", "'", "''", `'\''`, "-n", "--", "é ü 漢",
"\x01\x7f",
}
// sh runs script with sh -c and returns its output.
func sh(t *testing.T, script string) (string, error) {
t.Helper()
out, err := exec.CommandContext(t.Context(), "sh", "-c", script).Output() //nolint:gosec // quoting is what's tested
return string(out), err
}
func TestQuoteText(t *testing.T) {
for in, want := range map[string]string{
"": "''",
"plain": "'plain'",
"it's": `'it'\''s'`,
"'": `''\'''`,
} {
if got := Quote(in); got != want {
t.Errorf("Quote(%q) = %s, want %s", in, got, want)
}
}
if got := QuoteJoin(nil); got != "" {
t.Errorf("QuoteJoin(nil) = %q", got)
}
if got := QuoteJoin([]string{"a b", "it's"}); got != `'a b' 'it'\''s'` {
t.Errorf("QuoteJoin = %s", got)
}
}
// What Quote quotes reaches the shell's command as it was.
func TestQuote(t *testing.T) {
for _, s := range nasty {
if out, err := sh(t, "printf %s "+Quote(s)); err != nil || out != s {
t.Errorf("Quote(%q): the shell got %q, %v", s, out, err)
}
}
out, err := sh(t, `printf '%s\0' `+QuoteJoin(nasty))
if err != nil || out != strings.Join(nasty, "\x00")+"\x00" {
t.Errorf("QuoteJoin: the shell got %q, %v", out, err)
}
}
// bashes are the bash programs here: macOS's /bin/bash is 3.2, the oldest
// devvm's scripts run with, and PATH's may be newer.
func bashes(t *testing.T) []string {
t.Helper()
var found []string
for _, b := range []string{"/bin/bash", "bash"} {
if p, err := exec.LookPath(b); err == nil {
found = append(found, p)
}
}
if len(found) == 0 {
t.Skip("no bash")
}
return found
}
// locales are the C locale and those of the multibyte locales this
// machine has, whose characters can end in a backslash's or a quote's
// byte, so a lead byte at or above 0x80 could swallow the backslash that
// escapes a quote.
func locales(t *testing.T) []string {
t.Helper()
found := []string{"C"}
out, _ := exec.CommandContext(t.Context(), "locale", "-a").Output()
for _, l := range []string{"ja_JP.SJIS", "zh_CN.GBK", "zh_CN.GB18030", "zh_TW.Big5"} {
if slices.ContainsFunc(strings.Fields(string(out)), func(have string) bool { return strings.EqualFold(have, l) }) {
found = append(found, l)
}
}
return found
}
// What QuoteLine quotes is one line of ASCII, and each bash reads it back
// as it was in each locale: every byte but NUL, the nasty words, and a
// word that breaks out where a multibyte lead byte goes through raw.
func TestQuoteLine(t *testing.T) {
every := make([]byte, 0, 255)
for c := 1; c < 256; c++ {
every = append(every, byte(c))
}
ran := filepath.Join(t.TempDir(), "ran")
out := "\x95\\'; touch " + ran + "; echo '\x95\\"
words := append([]string{string(every), "a\\'b\\\\'\n'\n", out}, nasty...)
for _, s := range words {
q := QuoteLine(s)
for i := range len(q) {
if q[i] < 0x20 || q[i] >= 0x7f {
t.Errorf("QuoteLine(%q) = %q: byte %#x isn't printable ASCII", s, q, q[i])
break
}
}
}
for _, bash := range bashes(t) {
for _, loc := range locales(t) {
for _, s := range words {
cmd := exec.CommandContext(t.Context(), bash, "-c", "printf %s "+QuoteLine(s)) //nolint:gosec // quoting is what's tested
cmd.Env = append(os.Environ(), "LC_ALL="+loc)
got, err := cmd.Output()
if err != nil || string(got) != s {
t.Errorf("%s, %s: QuoteLine(%q) gave %q, %v", bash, loc, s, got, err)
}
}
if _, err := os.Stat(ran); err == nil {
t.Fatalf("%s, %s: a quoted word ran a command", bash, loc)
}
}
}
}
// A NUL can't be in a bash string: QuoteLine panics rather than drop it.
func TestQuoteLineNUL(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("QuoteLine with a NUL didn't panic")
}
}()
QuoteLine("a\x00b")
}
func FuzzQuote(f *testing.F) {
for _, s := range nasty {
f.Add(s)
}
f.Fuzz(func(t *testing.T, s string) {
if strings.ContainsRune(s, 0) {
return // no argument holds one
}
if out, err := sh(t, "printf %s "+Quote(s)); err != nil || out != s {
t.Errorf("Quote(%q): the shell got %q, %v", s, out, err)
}
})
}
func TestEnvName(t *testing.T) {
for _, ok := range []string{"A", "_", "HOME", "http_proxy", "_X1", "a9_b"} {
if !EnvName(ok) {
t.Errorf("EnvName(%q) = false", ok)
}
}
for _, bad := range []string{"", "1X", "9", "A-B", "A B", "A=B", "É", "a.b", "$A", "A\n", "-"} {
if EnvName(bad) {
t.Errorf("EnvName(%q) = true", bad)
}
}
}