Skip to content

Commit 55166e5

Browse files
committed
fix stdout/stderr printing and related tests
1 parent cc6d603 commit 55166e5

File tree

6 files changed

+69
-18
lines changed

6 files changed

+69
-18
lines changed

cmd/cmd.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ var inputFile *string
3333
func init() {
3434
outputFormat = rootCmd.PersistentFlags().StringP("output", "o", "yaml", "output format: yaml or json")
3535
inputFile = rootCmd.Flags().StringP("file", "f", "-", "file path to neat, or - to read from stdin")
36+
rootCmd.SetOut(os.Stdout)
37+
rootCmd.SetErr(os.Stderr)
3638
rootCmd.MarkFlagFilename("file")
3739
rootCmd.AddCommand(getCmd)
3840
}
3941

4042
// Execute is the entry point for the command package
4143
func Execute() {
4244
if err := rootCmd.Execute(); err != nil {
43-
fmt.Println(err)
45+
rootCmd.PrintErr(err) // can't use PrintErrln or PrintErrf due to a bug https://github.com/spf13/cobra/pull/894
4446
os.Exit(1)
4547
}
4648
}

test/bats-workaround.bash

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# run2 reimplements the "run" helper function from bats in order to make it handle stdout and stderr seperately
2+
# issue tracked: https://github.com/bats-core/bats-core/issues/47
3+
# original function: https://github.com/bats-core/bats-core/blob/90ce85884ca89b48960194b3d3bf6b816285e053/lib/bats-core/test_functions.bash#L32:L45
4+
function run2() {
5+
local origFlags="$-"
6+
set +eETx
7+
local origIFS="$IFS"
8+
# 'output', 'status', 'lines' are global variables available to tests.
9+
local tmperr=$(mktemp)
10+
stdout="$("$@" 2>"$tmperr")"
11+
# shellcheck disable=SC2034
12+
status="$?"
13+
# shellcheck disable=SC2034
14+
stderr="$(cat <"$tmperr")"
15+
rm "$tmperr"
16+
# shellcheck disable=SC2034,SC2206
17+
IFS=$'\n' stdoutLines=($stdout)
18+
# shellcheck disable=SC2034,SC2206
19+
IFS=$'\n' stderrLines=($stderr)
20+
IFS="$origIFS"
21+
set "-$origFlags"
22+
}

test/bats-workaround_test.bats

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bats
2+
3+
load bats-workaround.bash
4+
5+
function exercise_run2() {
6+
echo "out1"
7+
echo "err1" >&2
8+
echo "out2"
9+
echo "err2" >&2
10+
return 42
11+
}
12+
13+
@test "run2" {
14+
run2 exercise_run2
15+
[[ "$status" -eq 42 ]]
16+
[[ "$stdout" == "out1
17+
out2" ]]
18+
[[ "$stderr" == "err1
19+
err2" ]]
20+
[[ "${stdoutLines[1]}" == "out2" ]]
21+
[[ "${stderrLines[1]}" == "err2" ]]
22+
}

test/e2e-cli.bats

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
#!/usr/bin/env bats
2+
load bats-workaround
23
runtime_os=$(uname -s | tr '[:upper:]' '[:lower:]')
3-
exe="./kubectl-neat_${runtime_os}"
4+
exe="dist/kubectl-neat_${runtime_os}"
45
rootDir="./test/fixtures"
56

67
@test "invalid args 1" {
7-
run "$exe" -foo
8+
echo $exe >&3
9+
run2 "$exe" --foo
810
[ $status -eq 1 ]
9-
[[ "$output" == "Error"* ]]
11+
[[ "$stderr" == "unknown flag: --foo" ]]
1012
}
1113

1214
@test "invalid args 2" {
13-
run "$exe" get -foo
15+
run2 "$exe" get --foo
1416
[ $status -eq 1 ]
15-
[[ "$output" == "Error"* ]]
17+
[[ "$stderr" == "Error invoking kubectl"* ]]
1618
}
1719

1820
@test "invalid args 3" {
19-
run "$exe" foo
21+
run2 "$exe" foo
2022
[ $status -eq 1 ]
21-
[[ "$output" == "Error"* ]]
23+
[[ "$stderr" == 'unknown command "foo" for "kubectl-neat"' ]]
2224
}
2325

2426
@test "local file" {
25-
run "$exe" -f - <"$rootDir/pod1-raw.yaml"
27+
run2 "$exe" -f - <"$rootDir/pod1-raw.yaml"
2628
[ $status -eq 0 ]
27-
[[ "$output" == "apiVersion"* ]]
29+
[[ "$stdout" == "apiVersion"* ]]
2830
}

test/e2e-krew.bats

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bats
2+
load bats-workaround
23

34
function setup() {
45
dir="dist"
@@ -13,7 +14,7 @@ function teardown() {
1314
}
1415

1516
@test "krew install" {
16-
run kubectl "$plugin" get svc kubernetes -oyaml
17-
[ "$status" -eq 0 ]
18-
[ "${lines[1]}" = "kind: Service" ]
17+
run2 kubectl "$plugin" get svc kubernetes -n default
18+
[ "$status" -eq 0 ]
19+
[ "${stdoutLines[1]}" = "kind: Service" ]
1920
}

test/e2e-kubectl.bats

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/usr/bin/env bats
2+
load bats-workaround
23

34
function setup() {
45
runtime_os=$(uname -s | tr '[:upper:]' '[:lower:]')
56
tmpdir=$(mktemp -d)
67
# rename the plugin to avoid conflicts with existing installation
78
plugin_name="neat2"
89
plugin="$tmpdir"/kubectl-"$plugin_name"
9-
ln -s "$(pwd)/kubectl-neat_${runtime_os}" "$plugin"
10+
exe="$PWD/$(find dist -path \*dist/kubectl-neat_$runtime_os\*/kubectl-neat)"
11+
ln -s "$exe" "$plugin"
1012
# PATH modification here has no external affect since bats runs in a subshell
1113
PATH="$PATH":"$tmpdir"
1214
}
@@ -16,13 +18,13 @@ function teardown() {
1618
}
1719

1820
@test "plugin - json" {
19-
run kubectl "$plugin_name" get svc kubernetes -o json
21+
run2 kubectl "$plugin_name" get -o json svc kubernetes -n default
2022
[ "$status" -eq 0 ]
21-
[[ $output == "{"* ]]
23+
[[ $stdout == "{"* ]]
2224
}
2325

2426
@test "plugin - yaml" {
25-
run kubectl "$plugin_name" get svc kubernetes -o yaml
27+
run2 kubectl "$plugin_name" get svc kubernetes -n default -o yaml
2628
[ "$status" -eq 0 ]
27-
[[ $output == "apiVersion"* ]]
29+
[[ $stdout == "apiVersion"* ]]
2830
}

0 commit comments

Comments
 (0)