etcd/tests/e2e/ctl_v3_completion_test.go
Wei Fu c2f27a0070 tests/e2e: disable testShellCompletion if enable cov flag
The etcdctl and etcdutl built with `-tags cov` mode will show go-test result
after each execution, like

```
...

PASS
coverage: 0.0% of statements in ./...
```

Since the PASS is not real command, the `source completion` command will
fail with command-not-found error. And there is no easy way to disable
the (*testing.M).Run's output. Therefore, this patch uses build tag !cov
to disable cases when enable coverage.

Fixes: #14694

Signed-off-by: Wei Fu <fuweid89@gmail.com>
2022-11-14 17:58:18 +08:00

62 lines
1.8 KiB
Go

// Copyright 2021 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !cov
// +build !cov
package e2e
import (
"bytes"
"fmt"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/require"
"go.etcd.io/etcd/tests/v3/framework/e2e"
)
func TestCtlV3CompletionBash(t *testing.T) {
testShellCompletion(t, e2e.BinPath.Etcdctl, "bash")
}
func TestUtlV3CompletionBash(t *testing.T) {
testShellCompletion(t, e2e.BinPath.Etcdutl, "bash")
}
// testShellCompletion can only run in non-coverage mode. The etcdctl and etcdutl
// built with `-tags cov` mode will show go-test result after each execution, like
//
// PASS
// coverage: 0.0% of statements in ./...
//
// Since the PASS is not real command, the `source completion" fails with
// command-not-found error.
func testShellCompletion(t *testing.T, binPath, shellName string) {
e2e.BeforeTest(t)
stdout := new(bytes.Buffer)
completionCmd := exec.Command(binPath, "completion", shellName)
completionCmd.Stdout = stdout
completionCmd.Stderr = os.Stderr
require.NoError(t, completionCmd.Run())
filename := fmt.Sprintf("etcdctl-%s.completion", shellName)
require.NoError(t, os.WriteFile(filename, stdout.Bytes(), 0644))
shellCmd := exec.Command(shellName, "-c", "source "+filename)
require.NoError(t, shellCmd.Run())
}