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
48 changes: 48 additions & 0 deletions pkg/shell/cmds/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
75 changes: 54 additions & 21 deletions pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"

"github.com/aakarim/go-openlore/internal/analytics"
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -892,32 +880,77 @@ 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 {
if _, ok := part.(*parser.Lit); ok {
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 {
Expand Down
Loading