This is an automated email from the ASF dual-hosted git repository. sjanc pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git
commit 96ea54d77fceb138c525bd96876a641e2d679f03 Author: Szymon Janc <[email protected]> AuthorDate: Mon Oct 19 17:45:43 2026 +0200 newt: Exit with error on unknown command or flag cmd.Execute() result was ignored, so e.g. "newt bogus" or "newt target show --nosuchflag" printed an error but exited with status 0. Cobra already prints the error, so just exit with status 1. Assisted-by: Claude:claude-opus-5-5 --- newt/error_reporting_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++ newt/newt.go | 5 +++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/newt/error_reporting_test.go b/newt/error_reporting_test.go new file mode 100644 index 00000000..bed2f4d2 --- /dev/null +++ b/newt/error_reporting_test.go @@ -0,0 +1,65 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package main + +import ( + "strings" + "testing" +) + +// errorReportingExpectFail runs newt in dir and fails the test if newt +// succeeds or if its output does not contain all of msgs. It returns the +// combined output. +func errorReportingExpectFail(t *testing.T, dir string, + msgs []string, args ...string) string { + + t.Helper() + + out, err := newtCombinedOutput(dir, nil, args...) + if err == nil { + t.Errorf("newt %s succeeded\n%s", strings.Join(args, " "), out) + } + + for _, m := range msgs { + if !strings.Contains(out, m) { + t.Errorf("newt %s: missing %q in output\n%s", + strings.Join(args, " "), m, out) + } + } + + return out +} + +// Unknown commands and flags must make newt exit with an error. +func TestErrorReportingUnknownCommand(t *testing.T) { + dir := t.TempDir() + + t.Run("command", func(t *testing.T) { + errorReportingExpectFail(t, dir, + []string{`unknown command "bogus"`}, "bogus") + }) + + t.Run("flag", func(t *testing.T) { + errorReportingExpectFail(t, dir, + []string{"unknown flag: --nosuchflag"}, + "target", "show", "--nosuchflag") + }) +} diff --git a/newt/newt.go b/newt/newt.go index 5a443624..adfe0ce7 100644 --- a/newt/newt.go +++ b/newt/newt.go @@ -187,5 +187,8 @@ func main() { cmd.SilenceUsage = false } - cmd.Execute() + // Cobra has already printed the error (e.g., unknown command or flag). + if err := cmd.Execute(); err != nil { + os.Exit(1) + } }
