当前位置: 首页>>代码示例>>Golang>>正文


Golang io.CaptureOutput函数代码示例

本文整理汇总了Golang中github.com/cloudfoundry/cli/testhelpers/io.CaptureOutput函数的典型用法代码示例。如果您正苦于以下问题:Golang CaptureOutput函数的具体用法?Golang CaptureOutput怎么用?Golang CaptureOutput使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了CaptureOutput函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1:

	. "github.com/onsi/gomega"
)

var _ = Describe("CallCliCmd", func() {
	Describe(".Run", func() {
		var fakeCliConnection *pluginfakes.FakeCliConnection
		var callCliCommandPlugin *CliCmd

		BeforeEach(func() {
			fakeCliConnection = &pluginfakes.FakeCliConnection{}
			callCliCommandPlugin = &CliCmd{}
		})

		It("calls the cli command that is passed as an argument", func() {
			io_helpers.CaptureOutput(func() {
				callCliCommandPlugin.Run(fakeCliConnection, []string{"cli-command", "plugins", "arg1"})
			})

			Expect(fakeCliConnection.CliCommandArgsForCall(0)[0]).To(Equal("plugins"))
			Expect(fakeCliConnection.CliCommandArgsForCall(0)[1]).To(Equal("arg1"))
		})

		It("ouputs the text returned by the cli command", func() {
			fakeCliConnection.CliCommandReturns([]string{"Hi", "Mom"}, nil)
			output := io_helpers.CaptureOutput(func() {
				callCliCommandPlugin.Run(fakeCliConnection, []string{"cli-command", "plugins", "arg1"})
			})

			Expect(output[1]).To(Equal("---------- Command output from the plugin ----------"))
			Expect(output[2]).To(Equal("# 0  value:  Hi"))
			Expect(output[3]).To(Equal("# 1  value:  Mom"))
开发者ID:Reejoshi,项目名称:cli,代码行数:31,代码来源:call_cli_cmd_test.go

示例2:

var _ = Describe("UI", func() {
	var fakeLogger *tracefakes.FakePrinter
	BeforeEach(func() {
		fakeLogger = new(tracefakes.FakePrinter)
	})

	Describe("Printing message to stdout with PrintCapturingNoOutput", func() {
		It("prints strings without using the TeePrinter", func() {
			bucket := gbytes.NewBuffer()

			printer := NewTeePrinter(os.Stdout)
			printer.SetOutputBucket(bucket)

			io_helpers.SimulateStdin("", func(reader io.Reader) {
				output := io_helpers.CaptureOutput(func() {
					ui := NewUI(reader, os.Stdout, printer, fakeLogger)
					ui.PrintCapturingNoOutput("Hello")
				})

				Expect("Hello").To(Equal(strings.Join(output, "")))
				Expect(bucket.Contents()).To(HaveLen(0))
			})
		})
	})

	Describe("Printing message to stdout with Say", func() {
		It("prints strings", func() {
			io_helpers.SimulateStdin("", func(reader io.Reader) {
				output := io_helpers.CaptureOutput(func() {
					ui := NewUI(reader, os.Stdout, NewTeePrinter(os.Stdout), fakeLogger)
					ui.Say("Hello")
				})
开发者ID:yingkitw,项目名称:cli,代码行数:32,代码来源:ui_test.go

示例3:

	)
	Context("When running wildcard-apps", func() {
		BeforeEach(func() {
			appsList = make([]plugin_models.GetAppsModel, 0)
			appsList = append(appsList,
				plugin_models.GetAppsModel{"spring-music", "", "", 0, 0, 0, 0, nil},
				plugin_models.GetAppsModel{"app321", "", "", 0, 0, 0, 0, nil},
			)
			fakeCliConnection = &fakes.FakeCliConnection{}
			wildcardPlugin = &Wildcard{}
		})
		Describe("When there are matching apps", func() {
			It("prints a table containing only those apps", func() {
				fakeCliConnection.GetAppsReturns(appsList, nil)
				output := io_helpers.CaptureOutput(func() {
					wildcardPlugin.Run(fakeCliConnection, []string{"wildcard-apps", "app*"})
				})

				Expect(output).To(ContainSubstrings(
					[]string{"app321"},
				))
				Expect(output).ToNot(ContainSubstrings(
					[]string{"spring-music"},
				))
			})
		})
		Describe("When there are matching apps", func() {
			It("prints a table containing only those apps", func() {
				fakeCliConnection.GetAppsReturns(appsList, nil)
				output := io_helpers.CaptureOutput(func() {
					wildcardPlugin.Run(fakeCliConnection, []string{"wildcard-apps", "app*"})
开发者ID:lemaral,项目名称:Wildcard_Plugin,代码行数:31,代码来源:wildcard_plugin_test.go

示例4:

				outputCapture := &fakes.FakeOutputCapture{}
				rpcService, err = NewRpcService(app, outputCapture, nil, nil)
				Expect(err).ToNot(HaveOccurred())

				err := rpcService.Start()
				Expect(err).ToNot(HaveOccurred())

				pingCli(rpcService.Port())
			})

			It("returns false in success if the command cannot be found", func() {
				io_helpers.CaptureOutput(func() {
					client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port())
					Expect(err).ToNot(HaveOccurred())

					var success bool
					err = client.Call("CliRpcCmd.CallCoreCommand", []string{"not_a_cmd"}, &success)
					Expect(success).To(BeFalse())
					Expect(err).ToNot(HaveOccurred())
				})
			})

			It("returns an error if a command cannot parse provided flags", func() {
				io_helpers.CaptureOutput(func() {
					client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port())
					Expect(err).ToNot(HaveOccurred())

					var success bool
					err = client.Call("CliRpcCmd.CallCoreCommand", []string{"test_cmd", "-invalid_flag"}, &success)

					Expect(err).To(HaveOccurred())
开发者ID:swisscom,项目名称:cf-statistics-plugin,代码行数:31,代码来源:cli_rpc_server_test.go

示例5:

		AfterEach(func() {
			fakeFirehose.Close()
		})

		Context("when invoked via 'app-nozzle'", func() {
			Context("when app name is not recognized", func() {
				BeforeEach(func() {
					fakeCliConnection.GetAppReturns(plugin_models.GetAppModel{}, errors.New("App not found"))
				})
				It("returns error message", func(done Done) {
					defer close(done)
					outputChan := make(chan []string)
					go func() {
						output := io_helpers.CaptureOutput(func() {
							nozzlerCmd.Run(fakeCliConnection, []string{"app-nozzle", "IDontExist"})
						})
						outputChan <- output
					}()

					var output []string
					Eventually(outputChan, 2).Should(Receive(&output))
					outputString := strings.Join(output, "|")

					Expect(outputString).To(ContainSubstring("App not found"))
				}, 3)

			})
			Context("when app name is valid", func() {
				BeforeEach(func() {
					fakeFirehose.AppMode = true
开发者ID:cloudfoundry,项目名称:firehose-plugin,代码行数:30,代码来源:nozzle_plugin_test.go

示例6:

			Ω(output).Should(Equal([]string{"SOME", "OUTPUT", "COMMAND"}))
		})

		It("Return Service Credentials From Appplication Environment", func() {
			_, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("VCAP_SERVICES", []string{""})
			Ω(err).Should(MatchError("missing service credentials for application"))

			service_creds := []string{"{\"VCAP_SERVICES\":{\"service\": [ { \"credentials\": {} } ]}}"}
			b, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("VCAP_SERVICES", service_creds)
			Ω(err).ShouldNot(HaveOccurred())
			Ω(string(b[:])).Should(Equal("{\"service\":[{\"credentials\":{}}]}"))
		})

		It("Print Service Credentials As Shell Variable", func() {
			output := io_helpers.CaptureOutput(func() {
				callCopyEnvCommandPlugin.ExportCredsAsShellVar("VCAP_SERVICES", "testing")
			})
			Ω(output[0]).Should(Equal("export VCAP_SERVICES='testing';"))
		})

		It("Silently uninstalls", func() {
			callCopyEnvCommandPlugin.Run(fakeCliConnection, []string{"CLI-MESSAGE-UNINSTALL"})
			Ω(fakeCliConnection.CliCommandWithoutTerminalOutputCallCount()).Should(Equal(0))
		})

		Context("when called with --all", func() {
			It("Extracts VCAP_APPLICATION and VCAP_SERVICE", func() {
				services := "{\"VCAP_SERVICES\":[\"services\"]}"
				application := "{\"VCAP_APPLICATION\":[\"application\"]}"
				fakeCliConnection.CliCommandWithoutTerminalOutputReturns([]string{
					services, application, "OTHER"}, nil)
开发者ID:Samze,项目名称:copyenv,代码行数:31,代码来源:copyenv_test.go

示例7: createCommandFactory

	"github.com/codegangsta/cli"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Help", func() {
	It("shows help for all commands", func() {
		commandFactory := createCommandFactory()

		dummyTemplate := `
{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
{{.Name}}
{{end}}{{end}}{{end}}
`
		output := io_helpers.CaptureOutput(func() {
			app.ShowHelp(dummyTemplate, createApp(commandFactory))
		})

		for _, metadata := range commandFactory.CommandMetadatas() {
			Expect(commandInOutput(metadata.Name, output)).To(BeTrue(), metadata.Name+" not in help")
		}
	})
})

func createCommandFactory() command_factory.Factory {
	fakeUI := &testterm.FakeUI{}
	configRepo := testconfig.NewRepository()
	manifestRepo := manifest.NewManifestDiskRepository()
	apiRepoLocator := api.NewRepositoryLocator(configRepo, map[string]net.Gateway{
		"auth":             net.NewUAAGateway(configRepo),
		"cloud-controller": net.NewCloudControllerGateway(configRepo, time.Now),
开发者ID:Jack1996,项目名称:cli,代码行数:31,代码来源:help_test.go

示例8:

var _ = Describe("TeePrinter", func() {
	var (
		output  []string
		printer *TeePrinter
	)

	Describe(".Print", func() {
		var bucket *gbytes.Buffer

		BeforeEach(func() {
			bucket = gbytes.NewBuffer()

			output = io_helpers.CaptureOutput(func() {
				printer = NewTeePrinter(os.Stdout)
				printer.SetOutputBucket(bucket)
				printer.Print("Hello ")
				printer.Print("Mom!")
			})
		})

		It("should delegate to fmt.Print", func() {
			Expect(output[0]).To(Equal("Hello Mom!"))
		})

		It("should save the output to the slice", func() {
			Expect(bucket).To(gbytes.Say("Hello "))
			Expect(bucket).To(gbytes.Say("Mom!"))
		})

		It("should decolorize text", func() {
			io_helpers.CaptureOutput(func() {
开发者ID:jasonkeene,项目名称:cli,代码行数:31,代码来源:tee_printer_test.go

示例9:

			Ω(md.Commands[0].HelpText).NotTo(BeNil())
		})
	})

	Describe("Run", func() {
		var fakeDroplet *fake_droplet.FakeDroplet

		BeforeEach(func() {
			fakeDroplet = &fake_droplet.FakeDroplet{}
			downloadDropletPlugin.Drop = fakeDroplet
		})

		Context("Messages", func() {
			It("prints an informative message when downloading the droplet", func() {
				output := io_helpers.CaptureOutput(func() {
					downloadDropletPlugin.Run(fakeCliConnection, goodArgs)
				})
				Ω(output[0]).To(Equal("Saving theApp's droplet to /tmp"))
			})
		})

		Context("initializer complication", func() {
			It("Should call the initializer during run", func() {
				downloadDropletPlugin.Run(fakeCliConnection, goodArgs)
				cmd, cli := fakeInitiliazer.InitializePluginArgsForCall(0)
				Ω(cmd).To(Equal(downloadDropletPlugin))
				Ω(cli).To(Equal(fakeCliConnection))
				Ω(fakeInitiliazer.InitializePluginCallCount()).Should(Equal(1))
			})

			It("Should handle errors from the initiliazers.", func() {
开发者ID:krujos,项目名称:download_droplet_plugin,代码行数:31,代码来源:download_droplet_cmd_test.go

示例10:

var _ = Describe("TeePrinter", func() {
	var (
		output  []string
		printer *TeePrinter
	)

	Describe(".Print", func() {
		var bucket *[]string

		BeforeEach(func() {
			bucket = &[]string{}

			output = io_helpers.CaptureOutput(func() {
				printer = NewTeePrinter()
				printer.SetOutputBucket(bucket)
				printer.Print("Hello ")
				printer.Print("Mom!")
			})
		})

		It("should delegate to fmt.Print", func() {
			Expect(output[0]).To(Equal("Hello Mom!"))
		})

		It("should save the output to the slice", func() {
			Expect((*bucket)[0]).To(Equal("Hello "))
			Expect((*bucket)[1]).To(Equal("Mom!"))
		})

		It("should decolorize text", func() {
			bucket = &[]string{}
开发者ID:vframbach,项目名称:cli,代码行数:31,代码来源:tee_printer_test.go

示例11:

	io_helpers "github.com/cloudfoundry/cli/testhelpers/io"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Help", func() {
	It("shows help for all commands", func() {
		dummyTemplate := `
{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
{{.Name}}
{{end}}{{end}}{{end}}
`
		output := io_helpers.CaptureOutput(func() {
			help.ShowHelp(dummyTemplate)
		})

		for _, metadata := range command_registry.Commands.Metadatas() {
			Expect(commandInOutput(metadata.Name, output)).To(BeTrue(), metadata.Name+" not in help")
		}
	})

	It("shows help for all installed plugin's commands", func() {
		config_helpers.PluginRepoDir = func() string {
			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
		}

		dummyTemplate := `
{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
{{.Name}}
开发者ID:ralfcam,项目名称:cli,代码行数:30,代码来源:help_test.go

示例12:

	testassert "github.com/cloudfoundry/cli/testhelpers/assert"
	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
	io_helpers "github.com/cloudfoundry/cli/testhelpers/io"

	. "github.com/cloudfoundry/cli/cf/terminal"
	. "github.com/cloudfoundry/cli/testhelpers/matchers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("UI", func() {
	Describe("Printing message to stdout with Say", func() {
		It("prints strings", func() {
			io_helpers.SimulateStdin("", func(reader io.Reader) {
				output := io_helpers.CaptureOutput(func() {
					ui := NewUI(reader)
					ui.Say("Hello")
				})

				Expect("Hello").To(Equal(strings.Join(output, "")))
			})
		})

		It("prints formatted strings", func() {
			io_helpers.SimulateStdin("", func(reader io.Reader) {
				output := io_helpers.CaptureOutput(func() {
					ui := NewUI(reader)
					ui.Say("Hello %s", "World!")
				})

				Expect("Hello World!").To(Equal(strings.Join(output, "")))
			})
开发者ID:herchu,项目名称:cli,代码行数:32,代码来源:ui_test.go

示例13:

		Expect(buffer.Contents()).To(ContainSubstring("test2_cmd2"))
		Expect(buffer.Contents()).To(ContainSubstring("test2_really_long_really_long_really_long_command_name"))
	})

	It("adjusts the output format to the longest length of plugin command name", func() {
		confighelpers.PluginRepoDir = func() string {
			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
		}

		dummyTemplate := `
{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
{{.Name}}%%%{{.Description}}
{{end}}{{end}}{{end}}
`
		output := io.CaptureOutput(func() {
			help.ShowHelp(os.Stdout, dummyTemplate)
		})

		cmdNameLen := len(strings.Split(output[2], "%%%")[0])

		for _, line := range output {
			if strings.TrimSpace(line) == "" {
				continue
			}

			expectedLen := len(strings.Split(line, "%%%")[0])
			Expect(cmdNameLen).To(Equal(expectedLen))
		}

	})
开发者ID:Reejoshi,项目名称:cli,代码行数:30,代码来源:help_test.go

示例14:

var _ = Describe("appenv", func() {
	var fakeCliConnection *fakes.FakeCliConnection
	var appenv *AppEnv

	BeforeEach(func() {
		fakeCliConnection = &fakes.FakeCliConnection{}
		fakeCliConnection.IsLoggedInStub = func() (bool, error) { return true, nil }

		appenv = &AppEnv{}
	})

	Context("when uninstalled", func() {
		It("does nothing", func() {
			output := ioStub.CaptureOutput(func() {
				appenv.Run(fakeCliConnection, []string{"CLI-MESSAGE-UNINSTALL"})
			})

			Expect(output).To(ConsistOf([]string{""}))
		})
	})

	Context("when not logged in", func() {
		BeforeEach(func() {
			fakeCliConnection.IsLoggedInStub = func() (bool, error) { return false, nil }
		})

		It("returns an error message", func() {
			_, err := appenv.GetEnvs(fakeCliConnection, []string{"app_name"})
			Expect(err).To(MatchError("You must login first!"))
		})
开发者ID:Samze,项目名称:appenvs,代码行数:30,代码来源:appenv_test.go

示例15:

		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("help").SetDependency(deps, pluginCall))
	}

	BeforeEach(func() {
		ui = &testterm.FakeUI{}
		requirementsFactory = &testreq.FakeReqFactory{}
		config = &testconfig.FakePluginConfiguration{}
	})

	runCommand := func(args ...string) bool {
		return testcmd.RunCliCommand("help", args, requirementsFactory, updateCommandDependency, false)
	}

	Context("when no argument is provided", func() {
		It("prints the main help menu of the 'cf' app", func() {
			outputs := io_helpers.CaptureOutput(func() { runCommand() })

			Eventually(outputs).Should(ContainSubstrings([]string{"A command line tool to interact with Cloud Foundry"}))
			Eventually(outputs).Should(ContainSubstrings([]string{"CF_TRACE=true"}))
		})
	})

	Context("when a command name is provided as an argument", func() {
		Context("When the command exists", func() {
			It("prints the usage help for the command", func() {
				runCommand("target")

				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"target - Set or view the targeted org or space"}))
			})
		})
开发者ID:vframbach,项目名称:cli,代码行数:30,代码来源:help_test.go


注:本文中的github.com/cloudfoundry/cli/testhelpers/io.CaptureOutput函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。