本文整理汇总了Golang中github.com/juju/cmd/cmdtesting.Context函数的典型用法代码示例。如果您正苦于以下问题:Golang Context函数的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Context函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestMainSuccess
func (s *CmdSuite) TestMainSuccess(c *gc.C) {
ctx := cmdtesting.Context(c)
result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "success!"})
c.Assert(result, gc.Equals, 0)
c.Assert(bufferString(ctx.Stdout), gc.Equals, "success!\n")
c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
}
示例2: TestError
func (s *removeSuite) TestError(c *gc.C) {
s.setFailure("failed!")
ctx := cmdtesting.Context(c)
err := s.subcommand.Run(ctx)
c.Check(errors.Cause(err), gc.ErrorMatches, "failed!")
}
示例3: TestMainRunSilentError
func (s *CmdSuite) TestMainRunSilentError(c *gc.C) {
ctx := cmdtesting.Context(c)
result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "silent-error"})
c.Assert(result, gc.Equals, 1)
c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
}
示例4: TestRegisterSuperAliasHelp
func (s *HelpCommandSuite) TestRegisterSuperAliasHelp(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "jujutest",
})
sub := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "bar",
UsagePrefix: "jujutest",
Purpose: "bar functions",
})
jc.Register(sub)
sub.Register(&simple{name: "foo"})
jc.RegisterSuperAlias("bar-foo", "bar", "foo", nil)
for _, test := range []struct {
args []string
}{
{
args: []string{"bar", "foo", "--help"},
}, {
args: []string{"bar", "help", "foo"},
}, {
args: []string{"help", "bar-foo"},
}, {
args: []string{"bar-foo", "--help"},
},
} {
c.Logf("args: %v", test.args)
ctx := cmdtesting.Context(c)
code := cmd.Main(jc, ctx, test.args)
c.Check(code, gc.Equals, 0)
help := "Usage: jujutest bar foo\n\nSummary:\nto be simple\n"
c.Check(cmdtesting.Stdout(ctx), gc.Equals, help)
}
}
示例5: TestNotifyRun
func (s *SuperCommandSuite) TestNotifyRun(c *gc.C) {
notifyTests := []struct {
usagePrefix string
name string
expectName string
}{
{"juju", "juju", "juju"},
{"something", "else", "something else"},
{"", "juju", "juju"},
{"", "myapp", "myapp"},
}
for i, test := range notifyTests {
c.Logf("test %d. %q %q", i, test.usagePrefix, test.name)
notifyName := ""
sc := cmd.NewSuperCommand(cmd.SuperCommandParams{
UsagePrefix: test.usagePrefix,
Name: test.name,
NotifyRun: func(name string) {
notifyName = name
},
})
sc.Register(&TestCommand{Name: "blah"})
ctx := cmdtesting.Context(c)
code := cmd.Main(sc, ctx, []string{"blah", "--option", "error"})
c.Assert(bufferString(ctx.Stderr), gc.Matches, "")
c.Assert(code, gc.Equals, 1)
c.Assert(notifyName, gc.Equals, test.expectName)
}
}
示例6: TestFileMissing
func (s *uploadSuite) TestFileMissing(c *gc.C) {
s.setSuccess()
ctx := cmdtesting.Context(c)
err := s.subcommand.Run(ctx)
c.Check(os.IsNotExist(errors.Cause(err)), jc.IsTrue)
}
示例7: TestUnknownOutputFormat
func (s *CmdSuite) TestUnknownOutputFormat(c *gc.C) {
ctx := cmdtesting.Context(c)
result := cmd.Main(&OutputCommand{}, ctx, []string{"--format", "cuneiform"})
c.Check(result, gc.Equals, 2)
c.Check(bufferString(ctx.Stdout), gc.Equals, "")
c.Check(bufferString(ctx.Stderr), gc.Matches, ".*: unknown format \"cuneiform\"\n")
}
示例8: TestDescription
func (s *SuperCommandSuite) TestDescription(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Purpose: "blow up the death star"})
jc.Register(&TestCommand{Name: "blah"})
ctx := cmdtesting.Context(c)
code := cmd.Main(jc, ctx, []string{"blah", "--description"})
c.Assert(code, gc.Equals, 0)
c.Assert(bufferString(ctx.Stdout), gc.Equals, "blow up the death star\n")
}
示例9: TestStderr
func (s *LogSuite) TestStderr(c *gc.C) {
l := &cmd.Log{ShowLog: true, Config: "<root>=INFO"}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
logger.Infof("hello")
c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.* INFO .* hello\n`)
}
示例10: TestContextSetenv
func (s *CmdSuite) TestContextSetenv(c *gc.C) {
ctx := cmdtesting.Context(c)
before := ctx.Env["foo"]
ctx.Setenv("foo", "bar")
after := ctx.Env["foo"]
c.Check(before, gc.Equals, "")
c.Check(after, gc.Equals, "bar")
}
示例11: TestHelpWithPrefix
func (s *SuperCommandSuite) TestHelpWithPrefix(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", UsagePrefix: "juju"})
jc.Register(&TestCommand{Name: "blah"})
ctx := cmdtesting.Context(c)
code := cmd.Main(jc, ctx, []string{"blah", "--help"})
c.Assert(code, gc.Equals, 0)
stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1)
c.Assert(stripped, gc.Matches, ".*usage: juju jujutest blah.*blah-doc.*")
}
示例12: TestAlias
func (s *HelpCommandSuite) TestAlias(c *gc.C) {
super := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "super"})
super.Register(&TestCommand{Name: "blah", Aliases: []string{"alias"}})
ctx := cmdtesting.Context(c)
code := cmd.Main(super, ctx, []string{"help", "alias"})
c.Assert(code, gc.Equals, 0)
stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1)
c.Assert(stripped, gc.Matches, "Usage: super blah .*Aliases: alias")
}
示例13: TestOkay
func (s *infoSuite) TestOkay(c *gc.C) {
s.setSuccess()
ctx := cmdtesting.Context(c)
err := s.subcommand.Run(ctx)
c.Check(err, jc.ErrorIsNil)
out := MetaResultString
s.checkStd(c, ctx, out, "")
}
示例14: TestOkay
func (s *listSuite) TestOkay(c *gc.C) {
s.setSuccess()
ctx := cmdtesting.Context(c)
ctx, err := testing.RunCommand(c, s.subcommand, []string{"--verbose"}...)
c.Assert(err, jc.ErrorIsNil)
out := MetaResultString
s.checkStd(c, ctx, out, "")
}
示例15: TestStdin
func (s *CmdSuite) TestStdin(c *gc.C) {
const phrase = "Do you, Juju?"
ctx := cmdtesting.Context(c)
ctx.Stdin = bytes.NewBuffer([]byte(phrase))
result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "echo"})
c.Assert(result, gc.Equals, 0)
c.Assert(bufferString(ctx.Stdout), gc.Equals, phrase)
c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
}