From 6d28b7b28ad52596e953594a42bfbf329ba05640 Mon Sep 17 00:00:00 2001 From: Erin Call Date: Thu, 26 Dec 2019 11:29:33 -0800 Subject: [PATCH] Return an error on unknown commands [#15] I'm probably overthinking this--explicitly calling help is a strange and unusual case--but it doesn't really hurt, so I'm going for it. --- internal/helm/plan.go | 1 + internal/helm/plan_test.go | 8 +------- internal/run/config.go | 1 + internal/run/help.go | 11 +++++++++-- internal/run/help_test.go | 32 +++++++++++++++++++++++++++----- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/internal/helm/plan.go b/internal/helm/plan.go index d5c8835..0f32c1e 100644 --- a/internal/helm/plan.go +++ b/internal/helm/plan.go @@ -26,6 +26,7 @@ func NewPlan(cfg Config) (*Plan, error) { p := Plan{ cfg: cfg, runCfg: run.Config{ + HelmCommand: string(cfg.Command), Debug: cfg.Debug, KubeConfig: cfg.KubeConfig, Values: cfg.Values, diff --git a/internal/helm/plan_test.go b/internal/helm/plan_test.go index eebf51b..cf3871d 100644 --- a/internal/helm/plan_test.go +++ b/internal/helm/plan_test.go @@ -40,6 +40,7 @@ func (suite *PlanTestSuite) TestNewPlan() { } runCfg := run.Config{ + HelmCommand: "help", Debug: false, KubeConfig: "/branch/.sfere/profig", Values: "steadfastness,forthrightness", @@ -255,10 +256,3 @@ func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() { stepsMaker := determineSteps(cfg) suite.Same(&help, stepsMaker) } - -func (suite *PlanTestSuite) TestDeterminePlanHelpOnUnknown() { - cfg := Config{} - - stepsMaker := determineSteps(cfg) - suite.Same(&help, stepsMaker) -} diff --git a/internal/run/config.go b/internal/run/config.go index 09b9642..3d5b3f9 100644 --- a/internal/run/config.go +++ b/internal/run/config.go @@ -6,6 +6,7 @@ import ( // Config contains configuration applicable to all helm commands type Config struct { + HelmCommand string Debug bool KubeConfig string Values string diff --git a/internal/run/help.go b/internal/run/help.go index 8597815..a4a116e 100644 --- a/internal/run/help.go +++ b/internal/run/help.go @@ -10,8 +10,15 @@ type Help struct { } // Execute executes the `helm help` command. -func (h *Help) Execute(_ Config) error { - return h.cmd.Run() +func (h *Help) Execute(cfg Config) error { + if err := h.cmd.Run(); err != nil { + return fmt.Errorf("while running '%s': %w", h.cmd.String(), err) + } + + if cfg.HelmCommand == "help" { + return nil + } + return fmt.Errorf("unknown command '%s'", cfg.HelmCommand) } // Prepare gets the Help ready to execute. diff --git a/internal/run/help_test.go b/internal/run/help_test.go index 1824578..ecca2bb 100644 --- a/internal/run/help_test.go +++ b/internal/run/help_test.go @@ -38,9 +38,6 @@ func (suite *HelpTestSuite) TestPrepare() { Stdout(&stdout) mCmd.EXPECT(). Stderr(&stderr) - mCmd.EXPECT(). - Run(). - Times(1) cfg := Config{ Stdout: &stdout, @@ -49,8 +46,33 @@ func (suite *HelpTestSuite) TestPrepare() { h := Help{} err := h.Prepare(cfg) - suite.Require().Nil(err) - h.Execute(cfg) + suite.NoError(err) +} + +func (suite *HelpTestSuite) TestExecute() { + ctrl := gomock.NewController(suite.T()) + defer ctrl.Finish() + mCmd := NewMockcmd(ctrl) + originalCommand := command + command = func(_ string, _ ...string) cmd { + return mCmd + } + defer func() { command = originalCommand }() + + mCmd.EXPECT(). + Run(). + Times(2) + + cfg := Config{ + HelmCommand: "help", + } + help := Help{ + cmd: mCmd, + } + suite.NoError(help.Execute(cfg)) + + cfg.HelmCommand = "get down on friday" + suite.EqualError(help.Execute(cfg), "unknown command 'get down on friday'") } func (suite *HelpTestSuite) TestPrepareDebugFlag() {