diff --git a/internal/cmd/preprocessor/cmd/sanitisers.go b/internal/cmd/preprocessor/cmd/sanitisers.go index ca0d7da96..f293d9e02 100644 --- a/internal/cmd/preprocessor/cmd/sanitisers.go +++ b/internal/cmd/preprocessor/cmd/sanitisers.go @@ -104,27 +104,19 @@ 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) @@ -132,6 +124,35 @@ func (m *matchSpec) matches(cmd *commandStmt) (bool, error) { 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 { diff --git a/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar b/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar index e4fb98986..77e70e87e 100644 --- a/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar +++ b/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar @@ -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"}}} @@ -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"}}} @@ -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 @@ -220,7 +223,7 @@ package site "in-subdir": "J43PmA4U4WvVNMgjwuRuxRYV01FPIWNFmKMuvNPDah4=" } multi_step: { - "8OE47KRJN899MV3TUJQ0JF5DR4IV4E6K09S3J26JI2QFPPH0A1EG====": [{ + "CJTPSGKT1S5VQHE852UV8UR3GU4QGPUVRF75R4F6G64J90U51250====": [{ doc: """ # script doc comment #scripttag @@ -246,6 +249,11 @@ package site hello """ + }, { + doc: "" + cmd: "export MYVAR=value" + exitCode: 0 + output: "" }, { doc: "" cmd: "cue version"