Skip to content

Commit

Permalink
set correct exit code when running a script with an invalid option
Browse files Browse the repository at this point in the history
  • Loading branch information
ariasmn committed Jun 7, 2024
1 parent 0db029a commit 5265061
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,21 @@ func TestEventSystemError(t *testing.T) {
},
expExitCode: exitcodes.ScriptException,
},
{
name: "invalid",
script: `
export const options = {
vus: 'this is an invalid type',
}
export default function () {}
`,
expLog: []string{
"got event Exit with data '&{Error:could not initialize '-': could not load JS test 'file:///-': strconv.ParseInt: parsing \"this is an invalid type\": invalid syntax}'",
"could not initialize '-': could not load JS test 'file:///-': strconv.ParseInt: parsing \"this is an invalid type\": invalid syntax",
},
expExitCode: exitcodes.InvalidConfig,
},
{
name: "throw",
script: `
Expand Down
7 changes: 6 additions & 1 deletion js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/sirupsen/logrus"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/event"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/compiler"
Expand Down Expand Up @@ -208,7 +210,10 @@ func (b *Bundle) populateExports(updateOptions bool, exports *goja.Object) error
dec.DisallowUnknownFields()
if err := dec.Decode(&b.Options); err != nil {
if uerr := json.Unmarshal(data, &b.Options); uerr != nil {
return uerr
return errext.WithAbortReasonIfNone(
errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig),
errext.AbortedByScriptError,
)
}
b.preInitState.Logger.WithError(err).Warn("There were unknown fields in the options exported in the script")
}
Expand Down

0 comments on commit 5265061

Please sign in to comment.