Skip to content

Commit

Permalink
Take usagePrefix into account in log message.
Browse files Browse the repository at this point in the history
  • Loading branch information
Casey Marshall committed May 20, 2014
1 parent 50ab979 commit d2aeef6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
6 changes: 5 additions & 1 deletion cmd/supercommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ func (c *SuperCommand) Run(ctx *Context) error {
return err
}
}
logger.Infof("running %s-%s [%s]", c.Name, version.Current, runtime.Compiler)
if c.usagePrefix == "" || c.usagePrefix == c.Name {
logger.Infof("running %s-%s [%s]", c.Name, version.Current, runtime.Compiler)
} else {
logger.Infof("running %s %s-%s [%s]", c.usagePrefix, c.Name, version.Current, runtime.Compiler)
}
err := c.subcmd.Run(ctx)
if err != nil && err != ErrSilent {
logger.Errorf("%v", err)
Expand Down
27 changes: 22 additions & 5 deletions cmd/supercommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,33 @@ func (s *SuperCommandSuite) TestVersionFlag(c *gc.C) {
c.Assert(testVersionFlagCommand.version, gc.Equals, "abc.123")
}

var loggingTests = []struct {
usagePrefix, name string
pattern string
}{
{"juju", "juju", `^.* running juju-.*
.* ERROR .* BAM!
`},
{"something", "else", `^.* running something else-.*
.* ERROR .* BAM!
`},
{"", "juju", `^.* running juju-.*
.* ERROR .* BAM!
`},
}

func (s *SuperCommandSuite) TestLogging(c *gc.C) {
for _, name := range []string{"juju", "jujutest", "zaphod"} {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: name, Log: &cmd.Log{}})
for _, test := range loggingTests {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
UsagePrefix: test.usagePrefix,
Name: test.name,
Log: &cmd.Log{},
})
jc.Register(&TestCommand{Name: "blah"})
ctx := testing.Context(c)
code := cmd.Main(jc, ctx, []string{"blah", "--option", "error", "--debug"})
c.Assert(code, gc.Equals, 1)
c.Assert(bufferString(ctx.Stderr), gc.Matches, fmt.Sprintf(`^.* running %s-.*
.* ERROR .* BAM!
`, name))
c.Assert(bufferString(ctx.Stderr), gc.Matches, test.pattern)
}
}

Expand Down

0 comments on commit d2aeef6

Please sign in to comment.