cli/test/helpers.go

147 lines
3.1 KiB
Go
Raw Normal View History

package test
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"testing"
2022-09-19 20:23:45 +00:00
"gitlab.com/gitlab-org/cli/internal/run"
Isolate commands into separate sub-packages (#229) * refactor: organize commands into separate subfolders * Format code with gofmt This commit fixes the style issues introduced in d1f70d5 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/1ce57d1c-4f1e-48c2-a4d8-00c1bce7fb49/ * 🔥 sub-packaging * Format code with gofmt This commit fixes the style issues introduced in ca0bff4 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/11379011-98c6-43ea-8baa-4fefa9b775e4/ * 🔥 sub-packaging * Format code with gofmt This commit fixes the style issues introduced in 00e5936 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/c912f945-acf3-48d2-a550-0895c0153df0/ * Done with sub-packaging 🔥 * 🔥 tests * Format code with gofmt This commit fixes the style issues introduced in 56a8ff4 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/24498865-4980-479f-90c7-7ddd228c2bb4/ * fix bug risks * fix tests * fix tests * Format code with gofmt This commit fixes the style issues introduced in c947d02 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/cb2ac46a-f857-40d5-8508-eacc253b3614/ * Format code with gofmt This commit fixes the style issues introduced in 902ff7b according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/f60ba0d6-94d3-47c0-ae27-568a5cc5d9e5/ * fix tests * fix tests * 🔥 gookit/color * Format code with gofmt This commit fixes the style issues introduced in 5a129ea according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/8f1c8327-f5c6-4ff5-97d2-3c8a23f0a1e2/ * Autofix issues in 1 files Resolved issues in the following files via DeepSource Autofix: 1. commands/cmdtest/helper.go * add tests for MR * Format code with gofmt This commit fixes the style issues introduced in 758af28 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/d55a4867-aa23-4202-b2cc-a7ceafbaa53e/ * migrate old config to new config * fix bug risks * Format code with gofmt This commit fixes the style issues introduced in 0ea8eb2 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/f90cf664-8396-4b78-814d-3167ce898164/ * refactoring Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
2020-09-29 05:55:34 +00:00
"github.com/pkg/errors"
)
// TODO copypasta from command package
type CmdOut struct {
OutBuf, ErrBuf *bytes.Buffer
}
func (c CmdOut) String() string {
return c.OutBuf.String()
}
func (c CmdOut) Stderr() string {
return c.ErrBuf.String()
}
// OutputStub implements a simple utils.Runnable
type OutputStub struct {
Out []byte
Error error
}
func (s OutputStub) Output() ([]byte, error) {
if s.Error != nil {
return s.Out, s.Error
}
return s.Out, nil
}
func (s OutputStub) Run() error {
if s.Error != nil {
return s.Error
}
return nil
}
type CmdStubber struct {
Stubs []*OutputStub
Count int
Calls []*exec.Cmd
}
func InitCmdStubber() (*CmdStubber, func()) {
cs := CmdStubber{}
teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs))
return &cs, teardown
}
func (cs *CmdStubber) Stub(desiredOutput string) {
// TODO maybe have some kind of command mapping but going simple for now
cs.Stubs = append(cs.Stubs, &OutputStub{[]byte(desiredOutput), nil})
}
func (cs *CmdStubber) StubError(errText string) {
// TODO support error types beyond CmdError
stderrBuff := bytes.NewBufferString(errText)
args := []string{"stub"} // TODO make more real?
err := errors.New(errText)
cs.Stubs = append(cs.Stubs, &OutputStub{Error: &run.CmdError{
Stderr: stderrBuff,
Args: args,
Err: err,
}})
}
func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {
return func(cmd *exec.Cmd) run.Runnable {
cs.Calls = append(cs.Calls, cmd)
call := cs.Count
cs.Count += 1
if call >= len(cs.Stubs) {
panic(fmt.Sprintf("more execs than stubs. most recent call: %v", cmd))
}
// fmt.Printf("Called stub for `%v`\n", cmd) // Helpful for debugging
return cs.Stubs[call]
}
}
type T interface {
Helper()
Errorf(string, ...interface{})
}
func ExpectLines(t T, output string, lines ...string) {
t.Helper()
var r *regexp.Regexp
for _, l := range lines {
r = regexp.MustCompile(l)
if !r.MatchString(output) {
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
return
}
}
}
func ClearEnvironmentVariables(t *testing.T) {
// prevent using environment variables for test
t.Setenv("GITLAB_TOKEN", "")
t.Setenv("VISUAL", "")
t.Setenv("EDITOR", "")
t.Setenv("GITLAB_ACCESS_TOKEN", "")
t.Setenv("OAUTH_TOKEN", "")
}
func GetHostOrSkip(t testing.TB) string {
t.Helper()
glTestHost := os.Getenv("GITLAB_TEST_HOST")
if glTestHost == "" || os.Getenv("GITLAB_TOKEN") == "" {
if os.Getenv("CI") == "true" {
t.Log("Expected GITLAB_TEST_HOST and GITLAB_TOKEN to be set in CI. Marking as failed.")
t.Fail()
}
t.Skip("Set GITLAB_TEST_HOST and GITLAB_TOKEN to run this integration test")
}
return glTestHost
}
func ReturnBuffer(old *os.File, r *os.File, w *os.File) string {
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
outC <- buf.String()
}()
// back to normal state
w.Close()
os.Stdout = old // restoring the real stdout
out := <-outC
return out
}