Skip to content

Commit 80bd865

Browse files
committed
Testing 'scw help'
1 parent 3fe3d61 commit 80bd865

File tree

9 files changed

+218
-3
lines changed

9 files changed

+218
-3
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ script:
3737
- make travis_run
3838
- make cover
3939
- goveralls -service=travis-ci -v -coverprofile=profile.out
40+
- make integration-cli

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,14 @@ party:
152152
convey:
153153
go get github.com/smartystreets/goconvey
154154
goconvey -cover -port=9042 -workDir="$(realpath .)/pkg" -depth=-1
155+
156+
157+
.PHONY: integration-cli
158+
integration-cli:
159+
go test -v ./integration-cli
160+
161+
162+
.PHONY: integration-cli
163+
convey-integration-cli:
164+
go get github.com/smartystreets/goconvey
165+
goconvey -cover -port=9043 -workDir="$(realpath .)/integration-cli" -depth=-1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package integrationcli
2+
3+
import "fmt"
4+
5+
func shouldFitInTerminal(actual interface{}, expected ...interface{}) string {
6+
if len(actual.(string)) < 80 {
7+
return ""
8+
}
9+
return fmt.Sprintf("len(%q)\n -> %d chars (> 80 chars)", actual, len(actual.(string)))
10+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package integrationcli
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"strings"
7+
"testing"
8+
9+
. "github.com/scaleway/scaleway-cli/vendor/github.com/smartystreets/goconvey/convey"
10+
)
11+
12+
func TestHelp(t *testing.T) {
13+
Convey("Testing 'scw help'", t, func() {
14+
Convey("scw help", func() {
15+
cmd := exec.Command(scwcli, "help")
16+
out, ec, err := runCommandWithOutput(cmd)
17+
So(ec, ShouldEqual, 0)
18+
So(err, ShouldBeNil)
19+
20+
// headers & footers
21+
So(out, ShouldContainSubstring, "Usage: scw [OPTIONS] COMMAND [arg...]")
22+
So(out, ShouldContainSubstring, "Interact with Scaleway from the command line.")
23+
So(out, ShouldContainSubstring, "Run 'scw COMMAND --help' for more information on a command.")
24+
25+
// options
26+
So(out, ShouldContainSubstring, "Options:")
27+
for _, option := range publicOptions {
28+
So(out, ShouldContainSubstring, " "+option)
29+
}
30+
31+
// public commands
32+
So(out, ShouldContainSubstring, "Commands:")
33+
for _, command := range publicCommands {
34+
So(out, ShouldContainSubstring, " "+command)
35+
}
36+
37+
// secret commands
38+
for _, command := range secretCommands {
39+
So(out, ShouldNotContainSubstring, " "+command)
40+
}
41+
42+
// :lipstick:
43+
for _, line := range strings.Split(out, "\n") {
44+
if false { // FIXME: disabled for now
45+
So(line, shouldFitInTerminal)
46+
}
47+
}
48+
49+
// FIXME: count amount of options/commands, and panic if amount is different
50+
})
51+
52+
commands := append(publicCommands, secretCommands...)
53+
for _, command := range commands {
54+
if command == "help" {
55+
// FIXME: this should not be a special case
56+
continue
57+
}
58+
Convey(fmt.Sprintf("scw --help %s", command), func() {
59+
cmd := exec.Command(scwcli, command, "--help")
60+
_, ec, err := runCommandWithOutput(cmd)
61+
62+
// exit code
63+
So(ec, ShouldEqual, 1)
64+
So(fmt.Sprintf("%s", err), ShouldEqual, "exit status 1")
65+
66+
// FIXME: disabled for now because it depends of 'scw login'
67+
/*
68+
// Header
69+
So(out, ShouldContainSubstring, fmt.Sprintf("Usage: scw %s", command))
70+
// FIXME: test description
71+
// FIXME: test parameters
72+
73+
// Options
74+
So(out, ShouldContainSubstring, "Options:")
75+
So(out, ShouldContainSubstring, " -h, --help=false")
76+
// FIXME: test other options
77+
78+
// Examples
79+
// FIXME: todo
80+
//So(out, ShouldContainSubstring, "Examples:")
81+
82+
secondCmd := exec.Command(scwcli, "help", command)
83+
secondOut, secondEc, secondErr := runCommandWithOutput(secondCmd)
84+
So(out, ShouldEqual, secondOut)
85+
So(ec, ShouldEqual, secondEc)
86+
So(fmt.Sprintf("%v", err), ShouldEqual, fmt.Sprintf("%v", secondErr))
87+
*/
88+
})
89+
}
90+
Convey("Unknown command", func() {
91+
cmd := exec.Command(scwcli, "boogie")
92+
out, ec, err := runCommandWithOutput(cmd)
93+
So(out, ShouldContainSubstring, "scw: unknown subcommand boogie")
94+
So(ec, ShouldEqual, 1)
95+
So(fmt.Sprintf("%s", err), ShouldEqual, "exit status 1")
96+
})
97+
})
98+
}

integration-cli/utils_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package integrationcli
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"syscall"
7+
)
8+
9+
func getExitCode(err error) (int, error) {
10+
exitCode := 0
11+
if exiterr, ok := err.(*exec.ExitError); ok {
12+
if procExit, ok := exiterr.Sys().(syscall.WaitStatus); ok {
13+
return procExit.ExitStatus(), nil
14+
}
15+
}
16+
return exitCode, fmt.Errorf("failed to get exit code")
17+
}
18+
19+
func processExitCode(err error) (exitCode int) {
20+
if err != nil {
21+
var exiterr error
22+
if exitCode, exiterr = getExitCode(err); exiterr != nil {
23+
// TODO: Fix this so we check the error's text.
24+
// we've failed to retrieve exit code, so we set it to 127
25+
exitCode = 127
26+
}
27+
}
28+
return
29+
}
30+
31+
func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
32+
exitCode = 0
33+
out, err := cmd.CombinedOutput()
34+
exitCode = processExitCode(err)
35+
output = string(out)
36+
return
37+
}

integration-cli/vars_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package integrationcli
2+
3+
var (
4+
scwcli string = "../scw"
5+
publicCommands []string = []string{
6+
"help", "attach", "commit", "cp", "create",
7+
"events", "exec", "history", "images", "info",
8+
"inspect", "kill", "login", "logout", "logs",
9+
"port", "ps", "rename", "restart", "rm", "rmi",
10+
"run", "search", "start", "stop", "tag", "top",
11+
"version", "wait",
12+
}
13+
secretCommands []string = []string{
14+
"_patch", "_completion", "_flush-cache",
15+
}
16+
publicOptions []string = []string{
17+
"-h, --help=false",
18+
"-D, --debug=false",
19+
"-V, --verbose=false",
20+
"-q, --quiet=false",
21+
"--api-endpoint=APIEndPoint",
22+
"--sensitive=false",
23+
"-v, --version=false",
24+
}
25+
)

vendor/github.com/docker/docker/pkg/archive/archive.go

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/docker/docker/pkg/archive/archive_unix.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/docker/docker/pkg/archive/archive_windows.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)