Skip to content

Commit

Permalink
preprocessor: support declatations in matchSpec
Browse files Browse the repository at this point in the history
This allows some bash like the following:

    export BLAH=blah

to be successfully considered by a match spec.

Preprocessor-No-Write-Cache: true
Signed-off-by: Paul Jolly <[email protected]>
Change-Id: Ic9a4443159b3e4d34ea75e124f861250bf8aedd5
  • Loading branch information
myitcv committed Feb 18, 2024
1 parent a79dbad commit 69d7354
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
61 changes: 41 additions & 20 deletions internal/cmd/preprocessor/cmd/sanitisers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,55 @@ func (m *matchSpec) matches(cmd *commandStmt) (bool, error) {
if !ok {
return false, nil
}
if m.CommandPrefix != "" {
if len(got.Args) < len(want.Args) {
return false, nil
}
} else {
if len(got.Args) != len(want.Args) {
return false, nil
}
if matched, err := matchArgs(m, got.Args, want.Args); err != nil || !matched {
return matched, err
}
p := syntax.NewPrinter(syntax.SingleLine(true))
for i := range want.Args {
var gotS, wantS strings.Builder
p.Print(&gotS, got.Args[i])
p.Print(&wantS, want.Args[i])

// TODO: per @mvdan, revisit whether we can/should use
// reflect.DeepEqual or some other mechanism here (with performance
// being the main consideration).
if gotS.String() != wantS.String() {
return false, nil
}
case *syntax.DeclClause:
want, ok := want.Cmd.(*syntax.DeclClause)
if !ok {
return false, nil
}
if got.Variant.Value != want.Variant.Value {
return false, nil
}
if matched, err := matchArgs(m, got.Args, want.Args); err != nil || !matched {
return matched, err
}
default:
return false, fmt.Errorf("don't know how to handle stmt of type %T", got)
}
return true, nil
}

func matchArgs[T syntax.Node](m *matchSpec, got, want []T) (bool, error) {
if m.CommandPrefix != "" {
// When in command prefix match moding, we need got to be >= want
if len(got) < len(want) {
return false, nil
}
} else {
// Else we need to have exactly the same length otherwise it cannot
// possibly be a match
if len(got) != len(want) {
return false, nil
}
}
p := syntax.NewPrinter(syntax.SingleLine(true))
for i := range want {
var gotS, wantS strings.Builder
p.Print(&gotS, got[i])
p.Print(&wantS, want[i])

// TODO: per @mvdan, revisit whether we can/should use
// reflect.DeepEqual or some other mechanism here (with performance
// being the main consideration).
if gotS.String() != wantS.String() {
return false, nil
}
}
return true, nil
}

// patternKindAndCommand defines a common type to be embedded in types that use
// pattern-based matching.
type patternProperties struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ content: dir: page: {
>echo hello
>{{{end}}}
>{{{with script "en" "cue version"}}}
>export MYVAR=value
>cue version
>{{{end}}}
>{{{with script "en" "norun"}}}
Expand Down Expand Up @@ -147,6 +148,7 @@ content: dir: page: {
>echo hello
>{{{end}}}
>{{{with script "en" "cue version"}}}
>export MYVAR=value
>cue version
>{{{end}}}
>{{{with script "en" "norun"}}}
Expand Down Expand Up @@ -190,7 +192,8 @@ map: {
$ echo hello
hello
```
```text { title="TERMINAL" codeToCopy="Y3VlIHZlcnNpb24K" }
```text { title="TERMINAL" codeToCopy="ZXhwb3J0IE1ZVkFSPXZhbHVlCmN1ZSB2ZXJzaW9uCg==" }
$ export MYVAR=value
$ cue version
cue version v0.8.0-alpha.1

Expand Down Expand Up @@ -220,7 +223,7 @@ package site
"in-subdir": "J43PmA4U4WvVNMgjwuRuxRYV01FPIWNFmKMuvNPDah4="
}
multi_step: {
"8OE47KRJN899MV3TUJQ0JF5DR4IV4E6K09S3J26JI2QFPPH0A1EG====": [{
"CJTPSGKT1S5VQHE852UV8UR3GU4QGPUVRF75R4F6G64J90U51250====": [{
doc: """
# script doc comment
#scripttag
Expand All @@ -246,6 +249,11 @@ package site
hello

"""
}, {
doc: ""
cmd: "export MYVAR=value"
exitCode: 0
output: ""
}, {
doc: ""
cmd: "cue version"
Expand Down

0 comments on commit 69d7354

Please sign in to comment.