Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(237)

Unified Diff: cmd/logging_test.go

Issue 6100050: SuperCommand now just handles subcommand selection
Patch Set: Created 12 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cmd/logging.go ('k') | cmd/supercommand.go » ('j') | cmd/supercommand.go » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cmd/logging_test.go
=== added file 'cmd/logging_test.go'
--- cmd/logging_test.go 1970-01-01 00:00:00 +0000
+++ cmd/logging_test.go 2012-04-20 13:32:56 +0000
@@ -0,0 +1,121 @@
+package cmd_test
+
+import (
+ "bytes"
+ "io/ioutil"
+ "launchpad.net/gnuflag"
+ . "launchpad.net/gocheck"
+ "launchpad.net/juju/go/cmd"
+ "launchpad.net/juju/go/log"
+ "path/filepath"
+)
+
+type LoggingSuite struct{}
+
+var _ = Suite(&LoggingSuite{})
+
+type LogCommand struct {
+ *cmd.FlagCommand
+}
+
+func (c *LogCommand) Info() *cmd.Info {
+ return &cmd.Info{"testlog", "", "", "", true}
+}
+
+func (c *LogCommand) InitFlagSet(f *gnuflag.FlagSet) {}
+
+func (c *LogCommand) Run(ctx *cmd.Context) error {
+ log.Printf("hello log")
+ log.Debugf("hello debug")
+ return nil
+}
+
+var (
+ logre = `.* JUJU hello log\n`
+ debugre = `.* JUJU:DEBUG hello debug\n`
+)
+
+func RunLogCmd(c *C, args ...string) *cmd.Context {
+ ctx := &cmd.Context{c.MkDir(), &bytes.Buffer{}, &bytes.Buffer{}}
+ lsc := cmd.NewLoggingSuperCommand("do", "")
+ lsc.Register(&LogCommand{})
+ code := cmd.Main(lsc, ctx, args)
+ c.Assert(code, Equals, 0)
+ return ctx
+}
+
+func (s *LoggingSuite) TestNothing(c *C) {
+ ctx := RunLogCmd(c, "testlog")
+ c.Assert(str(ctx.Stderr), Equals, "")
+}
+
+func (s *LoggingSuite) TestVerbose(c *C) {
+ for _, args := range [][]string{
+ {"testlog", "--verbose"},
+ {"testlog", "-v"},
+ {"--verbose", "testlog"},
+ {"-v", "testlog"},
+ } {
+ ctx := RunLogCmd(c, args...)
+ c.Assert(str(ctx.Stderr), Matches, logre)
+ }
+}
+
+func (s *LoggingSuite) TestDebug(c *C) {
+ for _, args := range [][]string{
+ {"testlog", "--debug"},
+ {"--debug", "testlog"},
+ } {
+ ctx := RunLogCmd(c, args...)
+ c.Assert(str(ctx.Stderr), Matches, logre+debugre)
+ }
+}
+
+func (s *LoggingSuite) TestAbsLogFile(c *C) {
+ dir := c.MkDir()
+ abs := filepath.Join(dir, "logfile")
+ ctx := RunLogCmd(c, "testlog", "--log-file", abs)
+ content, err := ioutil.ReadFile(abs)
+ c.Assert(err, IsNil)
+ c.Assert(string(content), Matches, logre)
+ c.Assert(str(ctx.Stderr), Equals, "")
+
+ dir = c.MkDir()
+ abs = filepath.Join(dir, "logfile")
+ ctx = RunLogCmd(c, "--log-file", abs, "testlog", "--verbose")
+ content, err = ioutil.ReadFile(abs)
+ c.Assert(err, IsNil)
+ c.Assert(string(content), Matches, logre)
+ c.Assert(str(ctx.Stderr), Equals, "")
+
+ dir = c.MkDir()
+ abs = filepath.Join(dir, "logfile")
+ ctx = RunLogCmd(c, "--log-file", abs, "testlog", "--debug")
+ content, err = ioutil.ReadFile(abs)
+ c.Assert(err, IsNil)
+ c.Assert(string(content), Matches, logre+debugre)
+ c.Assert(str(ctx.Stderr), Equals, "")
+}
+
+func (s *LoggingSuite) TestRelLogFile(c *C) {
+ ctx := RunLogCmd(c, "testlog", "--log-file", "log")
+ rel := filepath.Join(ctx.Dir, "log")
+ content, err := ioutil.ReadFile(rel)
+ c.Assert(err, IsNil)
+ c.Assert(string(content), Matches, logre)
+ c.Assert(str(ctx.Stderr), Equals, "")
+
+ ctx = RunLogCmd(c, "--log-file", "log", "testlog", "--verbose")
+ rel = filepath.Join(ctx.Dir, "log")
+ content, err = ioutil.ReadFile(rel)
+ c.Assert(err, IsNil)
+ c.Assert(string(content), Matches, logre)
+ c.Assert(str(ctx.Stderr), Equals, "")
+
+ ctx = RunLogCmd(c, "--log-file", "log", "testlog", "--debug")
+ rel = filepath.Join(ctx.Dir, "log")
+ content, err = ioutil.ReadFile(rel)
+ c.Assert(err, IsNil)
+ c.Assert(string(content), Matches, logre+debugre)
+ c.Assert(str(ctx.Stderr), Equals, "")
+}
« no previous file with comments | « cmd/logging.go ('k') | cmd/supercommand.go » ('j') | cmd/supercommand.go » ('J')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b