Ejemplo n.º 1
0
// 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)
}
Ejemplo n.º 2
0
// 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())
}
Ejemplo n.º 3
0
// 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") })
}
Ejemplo n.º 4
0
// Tests BuildGoPkg's handling of the -o flag.
func TestBuildGoPkg(t *testing.T) {
	if testing.Short() {
		t.Skip()
	}
	sh := gosh.NewShell(t)
	defer sh.Cleanup()

	// Set -o to an absolute name.
	relName := "hw"
	absName := filepath.Join(sh.MakeTempDir(), relName)
	eq(t, gosh.BuildGoPkg(sh, "", helloWorldPkg, "-o", absName), absName)
	c := sh.Cmd(absName)
	eq(t, c.Stdout(), helloWorldStr)

	// Set -o to a relative name with no path separators.
	binDir := sh.MakeTempDir()
	absName = filepath.Join(binDir, relName)
	eq(t, gosh.BuildGoPkg(sh, binDir, helloWorldPkg, "-o", relName), absName)
	c = sh.Cmd(absName)
	eq(t, c.Stdout(), helloWorldStr)

	// Set -o to a relative name that contains a path separator.
	relNameWithSlash := filepath.Join("subdir", relName)
	absName = filepath.Join(binDir, relNameWithSlash)
	eq(t, gosh.BuildGoPkg(sh, binDir, helloWorldPkg, "-o", relNameWithSlash), absName)
	c = sh.Cmd(absName)
	eq(t, c.Stdout(), helloWorldStr)

	// Missing location after -o.
	setsErr(t, sh, func() { gosh.BuildGoPkg(sh, "", helloWorldPkg, "-o") })

	// Multiple -o.
	absName = filepath.Join(sh.MakeTempDir(), relName)
	gosh.BuildGoPkg(sh, "", helloWorldPkg, "-o", relName, "-o", absName)
	c = sh.Cmd(absName)
	eq(t, c.Stdout(), helloWorldStr)

	// Use --o instead of -o.
	absName = filepath.Join(sh.MakeTempDir(), relName)
	gosh.BuildGoPkg(sh, "", helloWorldPkg, "--o", absName)
	c = sh.Cmd(absName)
	eq(t, c.Stdout(), helloWorldStr)
}