-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_test.go
138 lines (126 loc) · 3.41 KB
/
run_test.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// This file is part of run.
//
// Copyright (C) 2020-2021 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//go:build linux || darwin
// +build linux darwin
package run
import (
"bytes"
"context"
"errors"
"os/exec"
"strings"
"testing"
"time"
)
func TestRun(t *testing.T) {
out, err := CMD("echo", "hello", "world").CombinedOutput()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
if string(out) != "hello world\n" {
t.Errorf("wrong output: %s\n", out)
}
t.Run("STDOutOutput", func(t *testing.T) {
out, err := CMD("echo", "hello", "world").STDOutOutput()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
if string(out) != "hello world\n" {
t.Errorf("wrong output: %s\n", out)
}
})
t.Run("STDOutOutput without stderr", func(t *testing.T) {
out, err := CMD("ls", "x").STDOutOutput()
if err == nil {
t.Errorf("Unexpected pass: %s\n", err)
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
errOut := string(exitErr.Stderr)
if errOut != "" {
t.Errorf("wrong stderr output: %s\n", errOut)
}
if exitErr.ExitCode() == 0 {
t.Errorf("wrong exit code: %d\n", exitErr.ExitCode())
}
}
if string(out) != "" {
t.Errorf("wrong output: %s\n", out)
}
})
t.Run("STDOutOutput with stderr", func(t *testing.T) {
out, err := CMD("ls", "x").SaveErr().STDOutOutput()
if err == nil {
t.Errorf("Unexpected pass: %s\n", err)
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
errOut := string(exitErr.Stderr)
if !strings.Contains(errOut, "No such file") {
t.Errorf("wrong stderr output: %s\n", errOut)
}
if exitErr.ExitCode() == 0 {
t.Errorf("wrong exit code: %d\n", exitErr.ExitCode())
}
}
if string(out) != "" {
t.Errorf("wrong output: %s\n", out)
}
})
t.Run("STDOutOutput print stderr", func(t *testing.T) {
var b bytes.Buffer
osStderr = &b
out, err := CMD("ls", "x").PrintErr().STDOutOutput()
if err == nil {
t.Errorf("Unexpected pass: %s\n", err)
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
errOut := string(exitErr.Stderr)
if errOut != "" {
t.Errorf("wrong stderr output: %s\n", errOut)
}
if exitErr.ExitCode() == 0 {
t.Errorf("wrong exit code: %d\n", exitErr.ExitCode())
}
}
if string(out) != "" {
t.Errorf("wrong output: %s\n", out)
}
if !strings.Contains(b.String(), "No such file") {
t.Errorf("wrong osStderr output: %s\n", b.String())
}
})
out, err = CMD("echo", "-n", "hello", "world").CombinedOutput()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
if string(out) != "hello world" {
t.Errorf("wrong output: %s\n", out)
}
out, err = CMD("ls", "./run").Dir("..").CombinedOutput()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
if !strings.Contains(string(out), "run.go") {
t.Errorf("wrong output: %s\n", out)
}
out, err = CMD("cat").In([]byte("hello world")).CombinedOutput()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
if !strings.Contains(string(out), "hello world") {
t.Errorf("wrong output: %s\n", out)
}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
_, err = CMD("sleep", "1").Ctx(ctx).CombinedOutput()
if err == nil {
t.Errorf("Unexpected pass: %s\n", err)
}
}