Skip to content

Commit b282aa0

Browse files
authored
Merge branch 'master' into add-file-assertion
2 parents 781486a + 75391aa commit b282aa0

10 files changed

Lines changed: 116 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# v2.4.0
22

33
- Add ability to test suite from a url
4+
- Add ability to test suite from stdin
45
- Add `file` assertion to `stdout` and `stderr`
56

67
# v2.3.0

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,13 @@ $ ./commander test
202202
$ ./commander test /tmp/test.yaml
203203

204204
# Execute a single test
205-
$ ./commander test /tmp/test.yaml "my test"
205+
$ ./commander test /tmp/test.yaml --filter "my test"
206+
207+
# Execute suite from stdin
208+
$ cat /tmp/test.yaml | ./commander test -
206209

207210
# Execute suite from url
208-
$ ./commander test https://raw.githubusercontent.com/commander-cli/commander/master/integration/unix/commander_test.yaml
211+
$ ./commander test https://your-url/commander_test.yaml
209212

210213
# Execute suites within a test directory
211214
$ ./commander test --dir /tmp

cmd/commander/commander.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,23 @@ Tests are always executed in alphabetical order.
5353
5454
Examples:
5555
56+
Directory test:
57+
commander test --dir /your/dir/
58+
59+
Stdin test:
60+
cat commander.yaml | commander test -
61+
62+
HTTP test:
63+
commander test https://your-url/commander_test.yaml
64+
5665
Filtering tests:
57-
test commander.yaml --filter="my test"
66+
commander test commander.yaml --filter="my test"
5867
5968
Multiple filters:
60-
test commander.yaml --filter=filter1 --filter=filter2
69+
commander test commander.yaml --filter=filter1 --filter=filter2
6170
6271
Regex filters:
63-
test commander.yaml --filter="^filter1$"
72+
commander test commander.yaml --filter="^filter1$"
6473
`,
6574
ArgsUsage: "[file] [--filter]",
6675
Flags: []cli.Flag{

commander_linux.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ tests:
3737
stdout:
3838
contains:
3939
- Use --dir to test directories with multiple test files
40-
exit-code: 1
40+
exit-code: 1

commander_unix.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,15 @@ tests:
128128
config:
129129
env:
130130
USER: from_parent
131-
command: ./commander test https://raw.githubusercontent.com/commander-cli/commander/master/integration/unix/commander_test.yaml
131+
command: ./commander test https://raw.githubusercontent.com/commander-cli/commander/master/integration/unix/remote_http.yaml
132132
stdout:
133133
contains:
134-
- ✓ [local] it should exit with error code
135-
- "- [local] it should skip, was skipped"
136-
line-count: 17
134+
- ✓ [local] hello world
135+
exit-code: 0
136+
137+
test stdin input:
138+
command: cat integration/unix/stdin.yaml | ./commander test -
139+
stdout:
140+
contains:
141+
- ✓ [local] hello world
137142
exit-code: 0

commander_windows.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ tests:
6363
contains:
6464
- ✗ [local] echo hello, retries 3
6565
exit-code: 1
66+
67+
test stdin input:
68+
command: type integration\windows\stdin.yaml | commander.exe test -
69+
stdout:
70+
contains:
71+
- ✓ [local] hello world
72+
exit-code: 0

integration/unix/stdin.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tests:
2+
hello world:
3+
command: echo hello world
4+
stdout: hello world
5+
exit-code: 0

integration/windows/stdin.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tests:
2+
hello world:
3+
command: echo hello world
4+
stdout: hello world
5+
exit-code: 0

pkg/app/test_command.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app
22

33
import (
4+
"bufio"
45
"fmt"
56
"io/ioutil"
67
"log"
@@ -38,6 +39,10 @@ func TestCommand(testPath string, ctx TestCommandContext) error {
3839
fmt.Println("Starting test against directory: " + testPath + "...")
3940
fmt.Println("")
4041
result, err = testDir(testPath, ctx.Filters)
42+
case testPath == "-":
43+
fmt.Println("Starting test from stdin...")
44+
fmt.Println("")
45+
result, err = testStdin(ctx.Filters)
4146
case isURL(testPath):
4247
fmt.Println("Starting test from " + testPath + "...")
4348
fmt.Println("")
@@ -121,6 +126,23 @@ func convergeResults(result runtime.Result, new runtime.Result) runtime.Result {
121126
return result
122127
}
123128

129+
func testStdin(filters runtime.Filters) (runtime.Result, error) {
130+
f, err := os.Stdin.Stat()
131+
if err != nil {
132+
return runtime.Result{}, err
133+
}
134+
135+
if (f.Mode() & os.ModeCharDevice) != 0 {
136+
return runtime.Result{}, fmt.Errorf("Error: when testing from stdin the command is intended to work with pipes")
137+
}
138+
139+
r := bufio.NewReader(os.Stdin)
140+
content, err := ioutil.ReadAll(r)
141+
s := suite.ParseYAML(content, "")
142+
143+
return execute(s, filters)
144+
}
145+
124146
func execute(s suite.Suite, filters runtime.Filters) (runtime.Result, error) {
125147
tests := s.GetTests()
126148
if len(filters) != 0 {

pkg/app/test_command_test.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
"bytes"
55
"io"
6+
"io/ioutil"
67
"log"
78
"os"
89
"runtime"
@@ -101,11 +102,55 @@ tests:
101102
func Test_TestCommand_Http_Err(t *testing.T) {
102103
err := TestCommand("http://error/not/a/url", TestCommandContext{Dir: false})
103104

104-
if runtime.GOOS == "windows" {
105-
assert.NotNil(t, err)
106-
} else {
107-
assert.NotNil(t, err)
105+
assert.NotNil(t, err)
106+
}
107+
108+
func Test_TestCommand_StdIn(t *testing.T) {
109+
tmpfile, err := ioutil.TempFile("", "test")
110+
if err != nil {
111+
log.Fatal(err)
112+
}
113+
defer os.Remove(tmpfile.Name()) //clean up
114+
115+
content := []byte(`
116+
tests:
117+
echo hello:
118+
exit-code: 0
119+
`)
120+
121+
if _, err := tmpfile.Write(content); err != nil {
122+
log.Fatal(err)
123+
}
124+
125+
if _, err := tmpfile.Seek(0, 0); err != nil {
126+
log.Fatal(err)
108127
}
128+
129+
// set stdin to tempfile
130+
oldStdin := os.Stdin
131+
defer func() { os.Stdin = oldStdin }() // Restore original Stdin
132+
os.Stdin = tmpfile
133+
134+
out := captureOutput(func() {
135+
TestCommand("-", TestCommandContext{Verbose: false})
136+
})
137+
138+
if err := tmpfile.Close(); err != nil {
139+
log.Fatal(err)
140+
}
141+
142+
assert.Contains(t, out, "✓ [local] echo hello")
143+
}
144+
145+
func Test_TestCommand_StdIn_Err(t *testing.T) {
146+
// set stdin to nil
147+
oldStdin := os.Stdin
148+
defer func() { os.Stdin = oldStdin }() // Restore original Stdin
149+
os.Stdin = nil
150+
151+
err := TestCommand("-", TestCommandContext{Verbose: false})
152+
153+
assert.NotNil(t, err)
109154
}
110155

111156
func Test_ConvergeResults(t *testing.T) {

0 commit comments

Comments
 (0)