Skip to content

Commit

Permalink
cmd/cmdtesting: rename from cmd/testing
Browse files Browse the repository at this point in the history
Also move the command testing helpers from
the top level testing package.
  • Loading branch information
rogpeppe committed Apr 7, 2017
1 parent ffa4089 commit dadf74c
Show file tree
Hide file tree
Showing 226 changed files with 1,552 additions and 1,475 deletions.
3 changes: 2 additions & 1 deletion apiserver/common/modelwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
apiservertesting "github.com/juju/juju/apiserver/testing"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/environs/config"
Expand Down Expand Up @@ -101,7 +102,7 @@ func (*environWatcherSuite) TestModelConfigFetchError(c *gc.C) {

func testingEnvConfig(c *gc.C) *config.Config {
env, err := bootstrap.Prepare(
modelcmd.BootstrapContext(testing.Context(c)),
modelcmd.BootstrapContext(cmdtesting.Context(c)),
jujuclient.NewMemStore(),
bootstrap.PrepareParams{
ControllerConfig: testing.FakeControllerConfig(),
Expand Down
2 changes: 1 addition & 1 deletion testing/cmd.go → cmd/cmdtesting/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package testing
package cmdtesting

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package testing_test
package cmdtesting_test

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion cmd/testing/prompt.go → cmd/cmdtesting/prompt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package testing
package cmdtesting

import (
"io"
Expand Down
4 changes: 2 additions & 2 deletions cmd/testing/prompt_test.go → cmd/cmdtesting/prompt_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package testing_test
package cmdtesting_test

import (
"fmt"
Expand All @@ -14,7 +14,7 @@ import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

cmdtesting "github.com/juju/juju/cmd/testing"
"github.com/juju/juju/cmd/cmdtesting"
)

type prompterSuite struct {
Expand Down
29 changes: 4 additions & 25 deletions cmd/testing/testing.go → cmd/cmdtesting/testing.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package testing
package cmdtesting

import (
"bytes"
"flag"
"fmt"
"io"
"os"
"os/exec"

"github.com/juju/cmd"
"github.com/juju/gnuflag"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

"github.com/juju/juju/juju/osenv"
"github.com/juju/juju/provider/dummy"
coretesting "github.com/juju/juju/testing"
)

// FlagRunMain is used to indicate that the -run-main flag was used.
var FlagRunMain = flag.Bool("run-main", false, "Run the application's main function for recursive testing")

// BadRun is used to run a command, check the exit code, and return the output.
func BadRun(c *gc.C, exit int, args ...string) string {
localArgs := append([]string{"-test.run", "TestRunMain", "-run-main", "--"}, args...)
ps := exec.Command(os.Args[0], localArgs...)
ps.Env = append(os.Environ(), osenv.JujuXDGDataHomeEnvKey+"="+osenv.JujuXDGDataHome())
output, err := ps.CombinedOutput()
c.Logf("command output: %q", output)
if exit != 0 {
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", exit))
}
return string(output)
}

// HelpText returns a command's formatted help text.
func HelpText(command cmd.Command, name string) string {
buff := &bytes.Buffer{}
Expand Down Expand Up @@ -73,9 +52,9 @@ func NullContext(c *gc.C) *cmd.Context {
return ctx
}

// RunCommand runs the command and returns channels holding the
// RunCommandWithDummyProvider runs the command and returns channels holding the
// command's operations and errors.
func RunCommand(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) {
func RunCommandWithDummyProvider(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) {
if ctx == nil {
panic("ctx == nil")
}
Expand All @@ -92,7 +71,7 @@ func RunCommand(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dum
close(opc)
}()

if err := coretesting.InitCommand(com, args); err != nil {
if err := InitCommand(com, args); err != nil {
errc <- err
return
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/juju/action/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"gopkg.in/juju/names.v2"

"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/cmd/juju/action"
"github.com/juju/juju/state"
"github.com/juju/juju/testing"
)

type ListSuite struct {
Expand Down Expand Up @@ -74,7 +74,7 @@ func (s *ListSuite) TestInit(c *gc.C) {
t.should, strings.Join(t.args, " "))
s.wrappedCommand, s.command = action.NewListCommandForTest(s.store)
args := append([]string{modelFlag, "admin"}, t.args...)
err := testing.InitCommand(s.wrappedCommand, args)
err := cmdtesting.InitCommand(s.wrappedCommand, args)
if t.expectedErr == "" {
c.Check(err, jc.ErrorIsNil)
c.Check(s.command.ApplicationTag(), gc.Equals, t.expectedSvc)
Expand Down Expand Up @@ -140,7 +140,7 @@ snapshot Take a snapshot of the database.

args := append([]string{modelFlag, "admin"}, t.withArgs...)
s.wrappedCommand, s.command = action.NewListCommandForTest(s.store)
ctx, err := testing.RunCommand(c, s.wrappedCommand, args...)
ctx, err := cmdtesting.RunCommand(c, s.wrappedCommand, args...)

if t.expectedErr != "" || t.withAPIErr != "" {
c.Check(err, gc.ErrorMatches, t.expectedErr)
Expand All @@ -150,9 +150,9 @@ snapshot Take a snapshot of the database.
if t.expectFullSchema {
checkFullSchema(c, t.withCharmActions, result)
} else if t.expectNoResults {
c.Check(testing.Stderr(ctx), gc.Matches, t.expectMessage)
c.Check(cmdtesting.Stderr(ctx), gc.Matches, t.expectMessage)
} else {
c.Check(testing.Stdout(ctx), gc.Equals, simpleOutput)
c.Check(cmdtesting.Stdout(ctx), gc.Equals, simpleOutput)
}
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/juju/action/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
gc "gopkg.in/check.v1"

"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/cmd/juju/action"
"github.com/juju/juju/jujuclient"
coretesting "github.com/juju/juju/testing"
Expand Down Expand Up @@ -109,7 +110,7 @@ func tagsForIdPrefix(prefix string, tags ...string) params.FindTagsResults {
// setupValueFile creates a file containing one value for testing.
// cf. cmd/juju/set_test.go
func setupValueFile(c *gc.C, dir, filename, value string) string {
ctx := coretesting.ContextForDir(c, dir)
ctx := cmdtesting.ContextForDir(c, dir)
path := ctx.AbsPath(filename)
content := []byte(value)
err := ioutil.WriteFile(path, content, 0666)
Expand Down
6 changes: 3 additions & 3 deletions cmd/juju/action/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/cmd/juju/action"
"github.com/juju/juju/testing"
)

var (
Expand Down Expand Up @@ -166,7 +166,7 @@ func (s *RunSuite) TestInit(c *gc.C) {
c.Logf("test %d: should %s:\n$ juju run-action %s\n", i,
t.should, strings.Join(t.args, " "))
args := append([]string{modelFlag, "admin"}, t.args...)
err := testing.InitCommand(wrappedCommand, args)
err := cmdtesting.InitCommand(wrappedCommand, args)
if t.expectError == "" {
c.Check(command.UnitTag(), gc.Equals, t.expectUnit)
c.Check(command.ActionName(), gc.Equals, t.expectAction)
Expand Down Expand Up @@ -367,7 +367,7 @@ func (s *RunSuite) TestRun(c *gc.C) {

wrappedCommand, _ := action.NewRunCommandForTest(s.store)
args := append([]string{modelFlag, "admin"}, t.withArgs...)
ctx, err := testing.RunCommand(c, wrappedCommand, args...)
ctx, err := cmdtesting.RunCommand(c, wrappedCommand, args...)

if t.expectedErr != "" || t.withAPIErr != "" {
c.Check(err, gc.ErrorMatches, t.expectedErr)
Expand Down
6 changes: 3 additions & 3 deletions cmd/juju/action/showoutput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/cmd/juju/action"
"github.com/juju/juju/testing"
)

type ShowOutputSuite struct {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (s *ShowOutputSuite) TestInit(c *gc.C) {
t.should, strings.Join(t.args, " "))
cmd, _ := action.NewShowOutputCommandForTest(s.store)
args := append([]string{modelFlag, "admin"}, t.args...)
err := testing.InitCommand(cmd, args)
err := cmdtesting.InitCommand(cmd, args)
if t.expectError != "" {
c.Check(err, gc.ErrorMatches, t.expectError)
}
Expand Down Expand Up @@ -298,7 +298,7 @@ func testRunHelper(c *gc.C, s *ShowOutputSuite, client *fakeAPIClient, expectedE
args = append(args, "--wait", wait)
}
cmd, _ := action.NewShowOutputCommandForTest(s.store)
ctx, err := testing.RunCommand(c, cmd, args...)
ctx, err := cmdtesting.RunCommand(c, cmd, args...)
if expectedErr != "" {
c.Check(err, gc.ErrorMatches, expectedErr)
} else {
Expand Down
4 changes: 2 additions & 2 deletions cmd/juju/action/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
gc "gopkg.in/check.v1"

"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/cmd/juju/action"
"github.com/juju/juju/testing"
)

type StatusSuite struct {
Expand Down Expand Up @@ -106,7 +106,7 @@ func (s *StatusSuite) runTestCase(c *gc.C, tc statusTestCase) {

s.subcommand, _ = action.NewStatusCommandForTest(s.store)
args := append([]string{modelFlag, "admin"}, tc.args...)
ctx, err := testing.RunCommand(c, s.subcommand, args...)
ctx, err := cmdtesting.RunCommand(c, s.subcommand, args...)
if tc.expectError == "" {
c.Assert(err, jc.ErrorIsNil)
} else {
Expand Down
11 changes: 6 additions & 5 deletions cmd/juju/application/addrelation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/cmd/cmdtesting"
jtesting "github.com/juju/juju/testing"
)

type AddRelationSuite struct {
Expand All @@ -34,7 +35,7 @@ func (s *AddRelationSuite) SetUpTest(c *gc.C) {
var _ = gc.Suite(&AddRelationSuite{})

func (s *AddRelationSuite) runAddRelation(c *gc.C, args ...string) error {
_, err := coretesting.RunCommand(c, NewAddRelationCommandForTest(s.mockAPI), args...)
_, err := cmdtesting.RunCommand(c, NewAddRelationCommandForTest(s.mockAPI), args...)
return err
}

Expand Down Expand Up @@ -71,7 +72,7 @@ func (s *AddRelationSuite) TestAddRelationFail(c *gc.C) {
func (s *AddRelationSuite) TestAddRelationBlocked(c *gc.C) {
s.mockAPI.SetErrors(common.OperationBlockedError("TestBlockAddRelation"))
err := s.runAddRelation(c, "application1", "application2")
coretesting.AssertOperationWasBlocked(c, err, ".*TestBlockAddRelation.*")
jtesting.AssertOperationWasBlocked(c, err, ".*TestBlockAddRelation.*")
s.mockAPI.CheckCall(c, 0, "AddRelation", []string{"application1", "application2"})
s.mockAPI.CheckCall(c, 1, "Close")
}
Expand All @@ -81,8 +82,8 @@ func (s *AddRelationSuite) TestAddRelationUnauthorizedMentionsJujuGrant(c *gc.C)
Message: "permission denied",
Code: params.CodeUnauthorized,
})
ctx, _ := coretesting.RunCommand(c, NewAddRelationCommandForTest(s.mockAPI), "application1", "application2")
errString := strings.Replace(coretesting.Stderr(ctx), "\n", " ", -1)
ctx, _ := cmdtesting.RunCommand(c, NewAddRelationCommandForTest(s.mockAPI), "application1", "application2")
errString := strings.Replace(cmdtesting.Stderr(ctx), "\n", " ", -1)
c.Assert(errString, gc.Matches, `.*juju grant.*`)
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/juju/application/addremoterelation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
gc "gopkg.in/check.v1"

"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/feature"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/testing"
)

const endpointSeparator = ":"
Expand Down Expand Up @@ -69,7 +69,7 @@ func (s *AddRemoteRelationSuiteNewAPI) TestAddRelationClientRetrievalFailure(c *
return nil, errors.New(msg)
}

_, err := testing.RunCommand(c, modelcmd.Wrap(addRelationCmd), "othermodel.applicationname2", "applicationname")
_, err := cmdtesting.RunCommand(c, modelcmd.Wrap(addRelationCmd), "othermodel.applicationname2", "applicationname")
c.Assert(err, gc.ErrorMatches, msg)
}

Expand Down Expand Up @@ -166,7 +166,7 @@ func (s *baseAddRemoteRelationSuite) runAddRelation(c *gc.C, args ...string) err
addRelationCmd.newAPIFunc = func() (ApplicationAddRelationAPI, error) {
return s.mockAPI, nil
}
_, err := testing.RunCommand(c, modelcmd.Wrap(addRelationCmd), args...)
_, err := cmdtesting.RunCommand(c, modelcmd.Wrap(addRelationCmd), args...)
return err
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/juju/application/addunit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/cmd/juju/application"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
Expand Down Expand Up @@ -90,13 +91,13 @@ var initAddUnitErrorTests = []struct {
func (s *AddUnitSuite) TestInitErrors(c *gc.C) {
for i, t := range initAddUnitErrorTests {
c.Logf("test %d", i)
err := testing.InitCommand(application.NewAddUnitCommandForTest(s.fake), t.args)
err := cmdtesting.InitCommand(application.NewAddUnitCommandForTest(s.fake), t.args)
c.Check(err, gc.ErrorMatches, t.err)
}
}

func (s *AddUnitSuite) runAddUnit(c *gc.C, args ...string) error {
_, err := testing.RunCommand(c, application.NewAddUnitCommandForTest(s.fake), args...)
_, err := cmdtesting.RunCommand(c, application.NewAddUnitCommandForTest(s.fake), args...)
return err
}

Expand Down Expand Up @@ -141,8 +142,8 @@ func (s *AddUnitSuite) TestUnauthorizedMentionsJujuGrant(c *gc.C) {
Message: "permission denied",
Code: params.CodeUnauthorized,
}
ctx, _ := testing.RunCommand(c, application.NewAddUnitCommandForTest(s.fake), "some-application-name")
errString := strings.Replace(testing.Stderr(ctx), "\n", " ", -1)
ctx, _ := cmdtesting.RunCommand(c, application.NewAddUnitCommandForTest(s.fake), "some-application-name")
errString := strings.Replace(cmdtesting.Stderr(ctx), "\n", " ", -1)
c.Assert(errString, gc.Matches, `.*juju grant.*`)
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/juju/application/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"gopkg.in/juju/charmrepo.v2-unstable/csclient"

"github.com/juju/juju/api"
"github.com/juju/juju/cmd/cmdtesting"
"github.com/juju/juju/constraints"
"github.com/juju/juju/state"
"github.com/juju/juju/state/multiwatcher"
Expand All @@ -34,8 +35,8 @@ import (
// charm or bundle. The deployment output and error are returned.
func runDeployCommand(c *gc.C, id string, args ...string) (string, error) {
args = append([]string{id}, args...)
ctx, err := coretesting.RunCommand(c, NewDefaultDeployCommand(), args...)
return strings.Trim(coretesting.Stderr(ctx), "\n"), err
ctx, err := cmdtesting.RunCommand(c, NewDefaultDeployCommand(), args...)
return strings.Trim(cmdtesting.Stderr(ctx), "\n"), err
}

func (s *BundleDeployCharmStoreSuite) TestDeployBundleNotFoundCharmStore(c *gc.C) {
Expand Down
Loading

0 comments on commit dadf74c

Please sign in to comment.