diff --git a/cli/cmd/convert.go b/cli/cmd/convert.go index 1d0734fc..5de4064b 100644 --- a/cli/cmd/convert.go +++ b/cli/cmd/convert.go @@ -43,6 +43,6 @@ func init() { } func runConvert(cmd *cobra.Command, args []string) error { - fmt.Fprintln(cmd.OutOrStdout(), "not yet implemented") - return nil + cmd.SilenceUsage = true + return fmt.Errorf("%s is not yet implemented", cmd.CommandPath()) } diff --git a/cli/cmd/plugin/install.go b/cli/cmd/plugin/install.go index 7a77f235..eebc59f9 100644 --- a/cli/cmd/plugin/install.go +++ b/cli/cmd/plugin/install.go @@ -33,6 +33,6 @@ func init() { } func runPluginInstall(cmd *cobra.Command, args []string) error { - fmt.Fprintln(cmd.OutOrStdout(), "not yet implemented") - return nil + cmd.SilenceUsage = true + return fmt.Errorf("%s is not yet implemented", cmd.CommandPath()) } diff --git a/cli/cmd/plugin/remove.go b/cli/cmd/plugin/remove.go index 38df2259..1f1cfd89 100644 --- a/cli/cmd/plugin/remove.go +++ b/cli/cmd/plugin/remove.go @@ -30,6 +30,6 @@ var removeCmd = &cobra.Command{ } func runPluginRemove(cmd *cobra.Command, args []string) error { - fmt.Fprintln(cmd.OutOrStdout(), "not yet implemented") - return nil + cmd.SilenceUsage = true + return fmt.Errorf("%s is not yet implemented", cmd.CommandPath()) } diff --git a/cli/cmd/stubs_test.go b/cli/cmd/stubs_test.go new file mode 100644 index 00000000..e405a22b --- /dev/null +++ b/cli/cmd/stubs_test.go @@ -0,0 +1,70 @@ +// 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 cmd + +import ( + "bytes" + "strings" + "testing" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func TestUnimplementedCommandsFail(t *testing.T) { + t.Setenv("OSSIE_PLUGIN_DIR", t.TempDir()) + + tests := [][]string{ + {"validate", "model.yaml"}, + {"convert", "--from", "dbt", "--input", "model.yaml"}, + {"plugin", "install", "dbt"}, + {"plugin", "remove", "dbt"}, + } + + for _, args := range tests { + t.Run(strings.Join(args, " "), func(t *testing.T) { + t.Cleanup(func() { clearFlags(rootCmd) }) + + out := new(bytes.Buffer) + rootCmd.SetOut(out) + rootCmd.SetErr(out) + rootCmd.SetArgs(args) + + err := rootCmd.Execute() + if err == nil { + t.Fatalf("Execute(%q) returned nil, want not-implemented error", args) + } + if !strings.Contains(err.Error(), "not yet implemented") { + t.Errorf("Execute(%q) error = %q, want it to mention not yet implemented", args, err) + } + if strings.Contains(out.String(), "Usage:") { + t.Errorf("Execute(%q) printed usage:\n%s", args, out.String()) + } + }) + } +} + +// Flag values set by one Execute call stay on the shared rootCmd tree. +func clearFlags(cmd *cobra.Command) { + cmd.Flags().Visit(func(f *pflag.Flag) { + _ = f.Value.Set(f.DefValue) + f.Changed = false + }) + for _, sub := range cmd.Commands() { + clearFlags(sub) + } +} diff --git a/cli/cmd/validate.go b/cli/cmd/validate.go index 08a3be55..0d096e60 100644 --- a/cli/cmd/validate.go +++ b/cli/cmd/validate.go @@ -35,6 +35,6 @@ func init() { } func runValidate(cmd *cobra.Command, args []string) error { - fmt.Fprintln(cmd.OutOrStdout(), "not yet implemented") - return nil + cmd.SilenceUsage = true + return fmt.Errorf("%s is not yet implemented", cmd.CommandPath()) }