Skip to content

Commit

Permalink
Attach os.Stdin to stdin of program started by disto run command (#108)
Browse files Browse the repository at this point in the history
Fixes #106
  • Loading branch information
nmiyake authored Apr 19, 2017
1 parent 6866587 commit 44d078b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/distgo/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"go/token"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
Expand Down Expand Up @@ -48,6 +49,7 @@ func DoRun(buildSpec params.ProductBuildSpec, runArgs []string, stdout, stderr i

cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Stdin = os.Stdin

fmt.Fprintln(stdout, strings.Join(args, " "))
if err := cmd.Run(); err != nil {
Expand Down
80 changes: 80 additions & 0 deletions apps/distgo/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package integration_test

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -173,6 +174,85 @@ products:
}
}

func TestRunWithStdin(t *testing.T) {
cli, err := products.Bin("distgo")
require.NoError(t, err)

wd, err := os.Getwd()
require.NoError(t, err)

tmpDir, cleanup, err := dirs.TempDir(wd, "")
defer cleanup()
require.NoError(t, err)

currCaseTmpDir, err := ioutil.TempDir(tmpDir, "")
require.NoError(t, err)

filesToCreate := []gofiles.GoFileSpec{
{
RelPath: "main.go",
Src: `package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
fmt.Printf("read: %q", text)
}
`,
},
}
config := `
products:
hello:
build:
main-pkg: .
`
runArgs := []string{
"--product",
"hello",
}

stdInContent := "output passed to stdin\n"

_, err = gofiles.Write(currCaseTmpDir, filesToCreate)
require.NoError(t, err)

configFile := path.Join(currCaseTmpDir, "config.yml")
err = ioutil.WriteFile(configFile, []byte(config), 0644)
require.NoError(t, err)

var output []byte
func() {
err := os.Chdir(currCaseTmpDir)
defer func() {
err := os.Chdir(wd)
require.NoError(t, err)
}()
require.NoError(t, err)

args := []string{"--config", configFile, "run"}
args = append(args, runArgs...)
cmd := exec.Command(cli, args...)

stdinPipe, err := cmd.StdinPipe()
require.NoError(t, err)
_, err = stdinPipe.Write([]byte(stdInContent))
require.NoError(t, err)

output, err = cmd.CombinedOutput()
require.NoError(t, err, "Output: %s", string(output))
}()

content := string(output)[strings.Index(string(output), "\n")+1:]
assert.Equal(t, fmt.Sprintf("read: %q", stdInContent), content)
}

func TestProjectVersion(t *testing.T) {
cli, err := products.Bin("distgo")
require.NoError(t, err)
Expand Down

0 comments on commit 44d078b

Please sign in to comment.