Files
devvm/internal/shell/shell_test.go
T
w3cjandClaude Opus 5.5 82566e9010 refactor: shell quoting, the VM-name grammar and value patterns each in one place
internal/shell, a stdlib leaf: Quote, QuoteJoin and EnvName. They
replace the 4 shell quoters (lima's shq and shqJoin, docker's shq, vm's
and devcontainer's shellQuote) and the 4 variable-name regexes (docker's
envKey, vmssh's envName, vm's envVarName, devcontainer's envName).
lima's quoting tests move there as TestQuote and FuzzQuote.

internal/vmname, a stdlib leaf below project: Prefix, HashLen, IsVM and
SlugOf. project's vmName regex, IsVM and Prefix go (callers use vmname),
lima's projectVM, docker's hostname parsing and cli's export-name regex
use it. TestImageKey (lima) pinned imageKey before the change;
TestParseExportName covers the export names.

Value patterns exported once: from.IsDigest (lima's idPattern),
image.IsKey (build's keyLike and the keyPattern copies in lima, docker
and smolvm), image.ToolVersion (tools' versionLike) and image.ToolName,
with TestIsDigest and TestValuePatterns. state's featureName stays:
internal/state may not import internal/image.

netpolicy.PolicyFiles names a user layer's policy file only for a part
image.LayerName takes, so an image-name part such as ../x never names a
file outside the user's layers; TestPolicyFiles's ../x cases failed
without the check.

The structure test's owners check gets literals: a non-test string
literal holding '\'' outside internal/shell, or [0-9a-f]{14} outside
internal/vmname, fails, naming the owner. TestRulesFail has cases that
break and keep both. shell and vmname join the leaves' layer, and
devcontainer may import shell. Coverage baselines: shell and vmname
added at 100.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-02 16:07:18 -07:00

80 lines
2.1 KiB
Go

package shell
import (
"os/exec"
"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)
}
}
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)
}
}
}