Skip to content

Commit

Permalink
Fix command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyworld committed May 20, 2022
1 parent b1f9ce1 commit 5d9f9ae
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apiserver/facades/client/payloads/facade_test.go
Expand Up @@ -11,9 +11,9 @@ import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

api "github.com/juju/juju/api/client/payloads"
"github.com/juju/juju/apiserver/facades/client/payloads"
"github.com/juju/juju/payload"
"github.com/juju/juju/payload/api"
"github.com/juju/juju/rpc/params"
)

Expand Down
86 changes: 86 additions & 0 deletions cmd/juju/commands/commands_test.go
@@ -0,0 +1,86 @@
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package commands

import (
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/testing"

jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/modelcmd"
)

type stubCommand struct {
modelcmd.ModelCommandBase
stub *testing.Stub
info *cmd.Info
envName string
}

func (c *stubCommand) Info() *cmd.Info {
c.stub.AddCall("Info")
c.stub.NextErr() // pop one off

if c.info == nil {
return &cmd.Info{
Name: "some-command",
}
}
return jujucmd.Info(c.info)
}

func (c *stubCommand) Run(ctx *cmd.Context) error {
c.stub.AddCall("Run", ctx)
if err := c.stub.NextErr(); err != nil {
return errors.Trace(err)
}
return nil
}

func (c *stubCommand) SetModelName(name string, allowDefault bool) error {
c.stub.AddCall("SetModelName", name)
c.envName = name
return c.stub.NextErr()
}

func (c *stubCommand) ModelName() (string, error) {
c.stub.AddCall("ModelName")
c.stub.NextErr() // pop one off

return c.envName, nil
}

type stubRegistry struct {
stub *testing.Stub

names []string
}

func (r *stubRegistry) Register(subcmd cmd.Command) {
r.stub.AddCall("Register", subcmd)
r.stub.NextErr() // pop one off

r.names = append(r.names, subcmd.Info().Name)
for _, name := range subcmd.Info().Aliases {
r.names = append(r.names, name)
}
}

func (r *stubRegistry) RegisterSuperAlias(name, super, forName string, check cmd.DeprecationCheck) {
r.stub.AddCall("RegisterSuperAlias", name, super, forName)
r.stub.NextErr() // pop one off

r.names = append(r.names, name)
}

func (r *stubRegistry) RegisterDeprecated(subcmd cmd.Command, check cmd.DeprecationCheck) {
r.stub.AddCall("RegisterDeprecated", subcmd, check)
r.stub.NextErr() // pop one off

r.names = append(r.names, subcmd.Info().Name)
for _, name := range subcmd.Info().Aliases {
r.names = append(r.names, name)
}
}
13 changes: 0 additions & 13 deletions cmd/juju/commands/main_test.go
Expand Up @@ -597,18 +597,6 @@ command\.(.|\n)*`)

func (s *MainSuite) TestRegisterCommands(c *gc.C) {
stub := &jujutesting.Stub{}
extraNames := []string{"cmd-a", "cmd-b"}
for i := range extraNames {
name := extraNames[i]
RegisterCommand(func() cmd.Command {
return &stubCommand{
stub: stub,
info: &cmd.Info{
Name: name,
},
}
})
}

registry := &stubRegistry{stub: stub}
registry.names = append(registry.names, "help") // implicit
Expand All @@ -617,7 +605,6 @@ func (s *MainSuite) TestRegisterCommands(c *gc.C) {

expected := make([]string, len(commandNames))
copy(expected, commandNames)
expected = append(expected, extraNames...)
if !featureflag.Enabled(feature.ActionsV2) {
expected = append(expected, "cancel-action", "run-action", "show-action-status", "show-action-output")
}
Expand Down

0 comments on commit 5d9f9ae

Please sign in to comment.