summaryrefslogtreecommitdiff
path: root/src/pkg/exec/exec_test.go
blob: af86b55a028d40e9540ad079879c70a4df6d22e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package exec

import (
	"io";
	"testing";
)

func TestRunCat(t *testing.T) {
	cmd, err := Run("/bin/cat", []string{"cat"}, nil,
		Pipe, Pipe, DevNull);
	if err != nil {
		t.Fatalf("opencmd /bin/cat: %v", err)
	}
	io.WriteString(cmd.Stdin, "hello, world\n");
	cmd.Stdin.Close();
	buf, err := io.ReadAll(cmd.Stdout);
	if err != nil {
		t.Fatalf("reading from /bin/cat: %v", err)
	}
	if string(buf) != "hello, world\n" {
		t.Fatalf("reading from /bin/cat: got %q", buf)
	}
	if err = cmd.Close(); err != nil {
		t.Fatalf("closing /bin/cat: %v", err)
	}
}

func TestRunEcho(t *testing.T) {
	cmd, err := Run("/bin/echo", []string{"echo", "hello", "world"}, nil,
		DevNull, Pipe, DevNull);
	if err != nil {
		t.Fatalf("opencmd /bin/echo: %v", err)
	}
	buf, err := io.ReadAll(cmd.Stdout);
	if err != nil {
		t.Fatalf("reading from /bin/echo: %v", err)
	}
	if string(buf) != "hello world\n" {
		t.Fatalf("reading from /bin/echo: got %q", buf)
	}
	if err = cmd.Close(); err != nil {
		t.Fatalf("closing /bin/echo: %v", err)
	}
}