diff --git a/pkg/shell/cmds/shell_test.go b/pkg/shell/cmds/shell_test.go index ecdd7f6..f85fb7a 100644 --- a/pkg/shell/cmds/shell_test.go +++ b/pkg/shell/cmds/shell_test.go @@ -118,6 +118,54 @@ func TestForLoop(t *testing.T) { } } +func TestForLoopGlobExpansion(t *testing.T) { + fs := testFS() + fs.AddFile("/docs/active/alpha.md", "alpha\n") + fs.AddFile("/docs/active/another file.md", "another\n") + fs.AddFile("/docs/active/beta.md", "beta\n") + fs.AddFile("/docs/active/.absent.md", "hidden\n") + fs.AddFile("/docs/active/alpha.txt", "not markdown\n") + + const matches = "[/docs/active/alpha.md]\n[/docs/active/another file.md]\n" + for _, tt := range []struct { + name, words, want string + }{ + {"relative", "active/a*.md", matches}, + {"absolute", "/docs/active/a*.md", matches}, + {"bare", "a*.md", matches}, + {"quoted prefix", `"/docs/active/"a*.md`, matches}, + {"variable pattern", "$PATTERN", matches}, + {"question mark", "active/alph?.md", "[/docs/active/alpha.md]\n"}, + {"no match", "active/missing*.md", "[active/missing*.md]\n"}, + {"single quoted", "'active/a*.md'", "[active/a*.md]\n"}, + {"double quoted", `"active/a*.md"`, "[active/a*.md]\n"}, + {"escaped", `active/a\*.md`, "[active/a*.md]\n"}, + {"quoted variable", `"$PATTERN"`, "[active/a*.md]\n"}, + {"mixed list", "before active/a*.md after", "[before]\n" + matches + "[after]\n"}, + {"multiple variable patterns", "$PATTERNS", matches + "[/docs/active/beta.md]\n"}, + {"multiple command substitution patterns", `$(echo 'active/a*.md active/b*.md')`, matches + "[/docs/active/beta.md]\n"}, + {"split before glob", "$SPACE_PATTERN", "[active/another]\n[/docs/readme.md]\n"}, + {"quoted space prefix", `"active/another "*.md`, "[/docs/active/another file.md]\n"}, + {"split with quoted suffix", `$WORDS" *.md"`, "[one]\n[two *.md]\n"}, + {"split with literal affixes", `pre${WORDS}post`, "[preone]\n[twopost]\n"}, + {"whitespace boundaries", `pre${PADDED}post`, "[pre]\n[one]\n[two]\n[post]\n"}, + {"empty variable", `$UNSET`, ""}, + {"empty quoted variable", `"$UNSET"`, "[]\n"}, + } { + t.Run(tt.name, func(t *testing.T) { + cwd := "/docs" + if tt.name == "bare" { + cwd = "/docs/active" + } + command := `cd ` + cwd + `; PATTERN='active/a*.md'; PATTERNS='active/a*.md active/b*.md'; SPACE_PATTERN='active/another *.md'; WORDS='one two'; PADDED=' one two '; for f in ` + tt.words + `; do echo "[$f]"; done` + out, errOut, code := execCmd(t, fs, command) + if code != 0 || errOut != "" || out != tt.want { + t.Fatalf("got stdout=%q stderr=%q code=%d; want stdout=%q stderr empty code=0", out, errOut, code, tt.want) + } + }) + } +} + func TestIfThenElse(t *testing.T) { out, _, _ := execCmd(t, testFS(), "if true; then echo yes; else echo no; fi") if strings.TrimSpace(out) != "yes" { diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index dd01847..f484d2c 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -11,6 +11,7 @@ import ( "strconv" "strings" "time" + "unicode" "unicode/utf8" "github.com/aakarim/go-openlore/internal/analytics" @@ -637,15 +638,7 @@ func (s *Shell) execCallInner(call *parser.CallExpr, w io.Writer, errW io.Writer args := make([]string, 0, len(call.Args)) for _, word := range call.Args { - expanded, globPattern, hasGlob := s.expandGlobWord(word) - if hasGlob { - matches := s.globExpand(globPattern) - if len(matches) > 0 { - args = append(args, matches...) - continue - } - } - args = append(args, expanded) + args = append(args, s.expandGlobWords(word, false)...) } *command = args[0] @@ -815,12 +808,7 @@ func (s *Shell) execFor(fc *parser.ForClause, w io.Writer, errW io.Writer, stdin varName := fc.Loop.Name.Value var items []string for _, word := range fc.Loop.Items { - expanded := s.expandWord(word) - if parser.ContainsExpansion(word) { - items = append(items, strings.Fields(expanded)...) - } else { - items = append(items, expanded) - } + items = append(items, s.expandGlobWords(word, true)...) } var code int @@ -892,14 +880,37 @@ func (s *Shell) expandWord(word *parser.Word) string { return sb.String() } -// expandGlobWord expands a word while retaining which wildcard characters -// came from unquoted, unescaped parts and are therefore eligible for globbing. -func (s *Shell) expandGlobWord(word *parser.Word) (expanded, pattern string, hasGlob bool) { +// expandGlobWords optionally splits unquoted substitutions into fields before +// globbing. Quoted/escaped characters remain literal, and matched filenames +// are never split. Command arguments retain their existing non-splitting behavior. +func (s *Shell) expandGlobWords(word *parser.Word, split bool) []string { if word == nil { - return "", "", false + return []string{""} } + var words []string var expandedBuilder, patternBuilder strings.Builder + var hasGlob bool + present := !split + flush := func() { + if !present { + return + } + matches := []string(nil) + if hasGlob { + matches = s.globExpand(patternBuilder.String()) + } + if len(matches) > 0 { + words = append(words, matches...) + } else { + words = append(words, expandedBuilder.String()) + } + expandedBuilder.Reset() + patternBuilder.Reset() + hasGlob = false + present = false + } + for i, part := range word.Parts { str := s.expandPart(part) if i == 0 { @@ -907,17 +918,39 @@ func (s *Shell) expandGlobWord(word *parser.Word) (expanded, pattern string, has str = s.expandTilde(str) } } - expandedBuilder.WriteString(str) switch part.(type) { case *parser.SglQuoted, *parser.DblQuoted, *parser.Escaped: + expandedBuilder.WriteString(str) patternBuilder.WriteString(escapeGlobPattern(str)) + present = true default: + if split { + switch part.(type) { + case *parser.ParamExp, *parser.CmdSubst: + for { + boundary := strings.IndexFunc(str, unicode.IsSpace) + if boundary < 0 { + break + } + chunk := str[:boundary] + expandedBuilder.WriteString(chunk) + patternBuilder.WriteString(chunk) + hasGlob = hasGlob || strings.ContainsAny(chunk, "*?") + present = present || chunk != "" + flush() + str = strings.TrimLeftFunc(str[boundary:], unicode.IsSpace) + } + } + } + expandedBuilder.WriteString(str) patternBuilder.WriteString(str) hasGlob = hasGlob || strings.ContainsAny(str, "*?") + present = present || str != "" } } - return expandedBuilder.String(), patternBuilder.String(), hasGlob + flush() + return words } func escapeGlobPattern(value string) string {