本文整理汇总了Golang中github.com/asadovsky/gosh.NewShell函数的典型用法代码示例。如果您正苦于以下问题:Golang NewShell函数的具体用法?Golang NewShell怎么用?Golang NewShell使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewShell函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestPipelineDifferentShells
func TestPipelineDifferentShells(t *testing.T) {
sh1 := gosh.NewShell(t)
defer sh1.Cleanup()
sh2 := gosh.NewShell(t)
defer sh2.Cleanup()
setsErr(t, sh1, func() { gosh.NewPipeline(sh1.FuncCmd(echoFunc), sh2.FuncCmd(catFunc)) })
setsErr(t, sh2, func() { gosh.NewPipeline(sh2.FuncCmd(echoFunc), sh1.FuncCmd(catFunc)) })
p := gosh.NewPipeline(sh1.FuncCmd(echoFunc))
setsErr(t, sh1, func() { p.PipeStdout(sh2.FuncCmd(catFunc)) })
p = gosh.NewPipeline(sh1.FuncCmd(echoFunc))
setsErr(t, sh1, func() { p.PipeStderr(sh2.FuncCmd(catFunc)) })
p = gosh.NewPipeline(sh1.FuncCmd(echoFunc))
setsErr(t, sh1, func() { p.PipeCombinedOutput(sh2.FuncCmd(catFunc)) })
}
开发者ID:asadovsky,项目名称:gosh,代码行数:15,代码来源:pipeline_test.go
示例2: TestStdoutStderr
func TestStdoutStderr(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
// Write to stdout only.
c := sh.FuncCmd(writeFunc, true, false)
stdoutPipe, stderrPipe := c.StdoutPipe(), c.StderrPipe()
stdout, stderr := c.StdoutStderr()
eq(t, stdout, "AA")
eq(t, stderr, "")
eq(t, toString(t, stdoutPipe), "AA")
eq(t, toString(t, stderrPipe), "")
// Write to stderr only.
c = sh.FuncCmd(writeFunc, false, true)
stdoutPipe, stderrPipe = c.StdoutPipe(), c.StderrPipe()
stdout, stderr = c.StdoutStderr()
eq(t, stdout, "")
eq(t, stderr, "BB")
eq(t, toString(t, stdoutPipe), "")
eq(t, toString(t, stderrPipe), "BB")
// Write to both stdout and stderr.
c = sh.FuncCmd(writeFunc, true, true)
stdoutPipe, stderrPipe = c.StdoutPipe(), c.StderrPipe()
stdout, stderr = c.StdoutStderr()
eq(t, stdout, "AA")
eq(t, stderr, "BB")
eq(t, toString(t, stdoutPipe), "AA")
eq(t, toString(t, stderrPipe), "BB")
}
开发者ID:asadovsky,项目名称:gosh,代码行数:31,代码来源:shell_test.go
示例3: TestPipelineTerminate
func TestPipelineTerminate(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
for _, d := range []time.Duration{0, time.Hour} {
for _, s := range []os.Signal{os.Interrupt, os.Kill} {
fmt.Println(d, s)
p := gosh.NewPipeline(sh.FuncCmd(sleepFunc, d, 0), sh.FuncCmd(sleepFunc, d, 0))
p.Start()
p.Cmds()[0].AwaitVars("ready")
p.Cmds()[1].AwaitVars("ready")
// Wait for a bit to allow the zero-sleep commands to exit.
time.Sleep(100 * time.Millisecond)
// Terminate should succeed regardless of the exit code, and regardless of
// whether the signal arrived or the processes had already exited.
p.Terminate(s)
}
}
// Terminate should fail if Wait has been called.
z := time.Duration(0)
p := gosh.NewPipeline(sh.FuncCmd(sleepFunc, z, 0), sh.FuncCmd(sleepFunc, z, 0))
p.Run()
setsErr(t, sh, func() { p.Terminate(os.Interrupt) })
}
开发者ID:asadovsky,项目名称:gosh,代码行数:25,代码来源:pipeline_test.go
示例4: TestSignal
func TestSignal(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
for _, d := range []time.Duration{0, time.Hour} {
for _, s := range []os.Signal{os.Interrupt, os.Kill} {
fmt.Println(d, s)
c := sh.FuncCmd(sleepFunc, d, 0)
c.Start()
c.AwaitVars("ready")
// Wait for a bit to allow the zero-sleep commands to exit.
time.Sleep(100 * time.Millisecond)
c.Signal(s)
switch {
case s == os.Interrupt:
// Wait should succeed as long as the exit code was 0, regardless of
// whether the signal arrived or the process had already exited.
c.Wait()
case d != 0:
// Note: We don't call Wait in the {d: 0, s: os.Kill} case because doing
// so makes the test flaky on slow systems.
setsErr(t, sh, func() { c.Wait() })
}
}
}
// Signal should fail if Wait has been called.
c := sh.FuncCmd(sleepFunc, time.Duration(0), 0)
c.Run()
setsErr(t, sh, func() { c.Signal(os.Interrupt) })
}
开发者ID:asadovsky,项目名称:gosh,代码行数:31,代码来源:shell_test.go
示例5: TestCmdFailureLoggingDisabled
// Tests that we don't log command failures when ExitErrorIsOk or
// ContinueOnError is set.
func TestCmdFailureLoggingDisabled(t *testing.T) {
tb := &customTB{t: t, buf: &bytes.Buffer{}}
sh := gosh.NewShell(tb)
defer sh.Cleanup()
// If ExitErrorIsOk is set and the command fails, we shouldn't log anything.
tb.Reset()
c := sh.FuncCmd(exitFunc, 1)
c.ExitErrorIsOk = true
c.Run()
eq(t, tb.calledFailNow, false)
eq(t, tb.buf.String(), "")
// If ContinueOnError is set and the command fails, we should log the exit
// status but not the command stderr.
tb.Reset()
c = sh.FuncCmd(exitFunc, 1)
sh.ContinueOnError = true
c.Run()
eq(t, tb.calledFailNow, false)
got := tb.buf.String()
if !strings.Contains(got, "exit status 1") {
t.Fatalf("missing error: %s", got)
}
if strings.Contains(got, "STDERR") {
t.Fatalf("should not log stderr: %s", got)
}
}
开发者ID:asadovsky,项目名称:gosh,代码行数:30,代码来源:shell_test.go
示例6: TestPushdPopd
func TestPushdPopd(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
startDir, err := os.Getwd()
ok(t, err)
parentDir := filepath.Dir(startDir)
neq(t, startDir, parentDir)
sh.Pushd(parentDir)
cwd, err := os.Getwd()
ok(t, err)
eq(t, cwd, parentDir)
sh.Pushd(startDir)
cwd, err = os.Getwd()
ok(t, err)
eq(t, cwd, startDir)
sh.Popd()
cwd, err = os.Getwd()
ok(t, err)
eq(t, cwd, parentDir)
sh.Popd()
cwd, err = os.Getwd()
ok(t, err)
eq(t, cwd, startDir)
// The next sh.Popd() will fail.
setsErr(t, sh, func() { sh.Popd() })
}
开发者ID:asadovsky,项目名称:gosh,代码行数:27,代码来源:shell_test.go
示例7: TestAddStdoutStderrWriter
// Tests that it's safe to add os.Stdout and os.Stderr as writers.
func TestAddStdoutStderrWriter(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
stdout, stderr := sh.FuncCmd(writeMoreFunc).StdoutStderr()
eq(t, stdout, "AA stdout done")
eq(t, stderr, "BB stderr done")
}
开发者ID:asadovsky,项目名称:gosh,代码行数:9,代码来源:shell_test.go
示例8: TestAwaitVarsProcessExit
// Tests that AwaitVars returns immediately when the process exits.
func TestAwaitVarsProcessExit(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
c := sh.FuncCmd(exitFunc, 0)
c.Start()
setsErr(t, sh, func() { c.AwaitVars("foo") })
}
开发者ID:asadovsky,项目名称:gosh,代码行数:9,代码来源:shell_test.go
示例9: TestMakeTempFile
func TestMakeTempFile(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
file := sh.MakeTempFile()
fi, err := file.Stat()
ok(t, err)
eq(t, fi.Mode().IsRegular(), true)
}
开发者ID:asadovsky,项目名称:gosh,代码行数:9,代码来源:shell_test.go
示例10: TestMakeTempDir
func TestMakeTempDir(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
name := sh.MakeTempDir()
fi, err := os.Stat(name)
ok(t, err)
eq(t, fi.Mode().IsDir(), true)
}
开发者ID:asadovsky,项目名称:gosh,代码行数:9,代码来源:shell_test.go
示例11: TestCustomTB
func TestCustomTB(t *testing.T) {
tb := &customTB{t: t}
sh := gosh.NewShell(tb)
defer sh.Cleanup()
sh.HandleError(fakeError)
// Note, our deferred sh.Cleanup() should succeed despite this error.
nok(t, sh.Err)
eq(t, tb.calledFailNow, true)
}
开发者ID:asadovsky,项目名称:gosh,代码行数:10,代码来源:shell_test.go
示例12: TestPushdNoPopdCleanup
func TestPushdNoPopdCleanup(t *testing.T) {
startDir := getwdEvalSymlinks(t)
sh := gosh.NewShell(t)
tmpDir := sh.MakeTempDir()
sh.Pushd(tmpDir)
eq(t, getwdEvalSymlinks(t), evalSymlinks(t, tmpDir))
// There is no matching popd; the cwd is tmpDir, which is deleted by Cleanup.
// Cleanup needs to put us back in startDir, otherwise all subsequent Pushd
// calls will fail.
sh.Cleanup()
eq(t, getwdEvalSymlinks(t), startDir)
}
开发者ID:asadovsky,项目名称:gosh,代码行数:12,代码来源:shell_test.go
示例13: TestHandleErrorPanics
// Tests that Shell.HandleError panics under various conditions.
func TestHandleErrorPanics(t *testing.T) {
func() { // errDidNotCallNewShell
sh := gosh.Shell{}
defer func() { neq(t, recover(), nil) }()
sh.HandleError(fakeError)
}()
func() { // errShellErrIsNotNil
sh := gosh.NewShell(t)
sh.ContinueOnError = true
defer sh.Cleanup()
sh.Err = fakeError
defer func() { neq(t, recover(), nil) }()
sh.HandleError(fakeError)
}()
func() { // errAlreadyCalledCleanup
sh := gosh.NewShell(t)
sh.ContinueOnError = true
sh.Cleanup()
defer func() { neq(t, recover(), nil) }()
sh.HandleError(fakeError)
}()
}
开发者ID:asadovsky,项目名称:gosh,代码行数:23,代码来源:shell_test.go
示例14: TestFuncCmd
// Mirrors ExampleFuncCmd in internal/gosh_example/main.go.
func TestFuncCmd(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
// Start server.
c := sh.FuncCmd(serveFunc)
c.Start()
addr := c.AwaitVars("addr")["addr"]
neq(t, addr, "")
// Run client.
c = sh.FuncCmd(getFunc, addr)
eq(t, c.Stdout(), helloWorldStr)
}
开发者ID:asadovsky,项目名称:gosh,代码行数:15,代码来源:shell_test.go
示例15: ExampleFuncCmd
// Mirrors TestFuncCmd in shell_test.go.
func ExampleFuncCmd() {
sh := gosh.NewShell(nil)
defer sh.Cleanup()
// Start server.
c := sh.FuncCmd(serveFunc)
c.Start()
addr := c.AwaitVars("addr")["addr"]
fmt.Println(addr)
// Run client.
c = sh.FuncCmd(getFunc, addr)
fmt.Print(c.Stdout())
}
开发者ID:asadovsky,项目名称:gosh,代码行数:15,代码来源:main.go
示例16: TestLookPath
// Tests that Shell.Cmd uses Shell.Vars["PATH"] to locate executables with
// relative names.
func TestLookPath(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
binDir := sh.MakeTempDir()
sh.Vars["PATH"] = binDir + ":" + sh.Vars["PATH"]
relName := "hw"
absName := filepath.Join(binDir, relName)
gosh.BuildGoPkg(sh, "", helloWorldPkg, "-o", absName)
c := sh.Cmd(relName)
eq(t, c.Stdout(), helloWorldStr)
// Test the case where we cannot find the executable.
sh.Vars["PATH"] = ""
setsErr(t, sh, func() { sh.Cmd("yes") })
}
开发者ID:asadovsky,项目名称:gosh,代码行数:18,代码来源:shell_test.go
示例17: TestPipelineClosedPipe
func TestPipelineClosedPipe(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
writeLoop, readLine := sh.FuncCmd(writeLoopFunc), sh.FuncCmd(readFunc)
// WriteLoop finishes because it gets a closed pipe write error after readLine
// finishes. Note that the closed pipe error is ignored.
p := gosh.NewPipeline(writeLoop, readLine)
eq(t, p.Stdout(), "")
ok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
p = p.Clone()
eq(t, p.Stdout(), "")
ok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
}
开发者ID:asadovsky,项目名称:gosh,代码行数:16,代码来源:pipeline_test.go
示例18: TestCombinedOutput
func TestCombinedOutput(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
c := sh.FuncCmd(writeFunc, true, true)
buf := &bytes.Buffer{}
c.AddStdoutWriter(buf)
c.AddStderrWriter(buf)
output := c.CombinedOutput()
// Note, we can't assume any particular ordering of stdout and stderr, so we
// simply check the length of the combined output.
eq(t, len(output), 4)
// The ordering must be the same, regardless of how we captured the combined
// output.
eq(t, output, buf.String())
}
开发者ID:asadovsky,项目名称:gosh,代码行数:16,代码来源:shell_test.go
示例19: ExampleCmd
// Mirrors TestCmd in shell_test.go.
func ExampleCmd() {
sh := gosh.NewShell(nil)
defer sh.Cleanup()
// Start server.
binDir := sh.MakeTempDir()
binPath := gosh.BuildGoPkg(sh, binDir, "github.com/asadovsky/gosh/internal/gosh_example_server")
c := sh.Cmd(binPath)
c.Start()
addr := c.AwaitVars("addr")["addr"]
fmt.Println(addr)
// Run client.
binPath = gosh.BuildGoPkg(sh, binDir, "github.com/asadovsky/gosh/internal/gosh_example_client")
c = sh.Cmd(binPath, "-addr="+addr)
fmt.Print(c.Stdout())
}
开发者ID:asadovsky,项目名称:gosh,代码行数:18,代码来源:main.go
示例20: TestCmd
// Mirrors ExampleCmd in internal/gosh_example/main.go.
func TestCmd(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
// Start server.
binDir := sh.MakeTempDir()
binPath := gosh.BuildGoPkg(sh, binDir, "github.com/asadovsky/gosh/internal/gosh_example_server")
c := sh.Cmd(binPath)
c.Start()
addr := c.AwaitVars("addr")["addr"]
neq(t, addr, "")
// Run client.
binPath = gosh.BuildGoPkg(sh, binDir, "github.com/asadovsky/gosh/internal/gosh_example_client")
c = sh.Cmd(binPath, "-addr="+addr)
eq(t, c.Stdout(), helloWorldStr)
}
开发者ID:asadovsky,项目名称:gosh,代码行数:18,代码来源:shell_test.go
注:本文中的github.com/asadovsky/gosh.NewShell函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论