Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Verbose option on testcli.Runner #2183

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions internal/testcli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Runner struct {
StderrLines <-chan string

errch <-chan error

Verbose bool
}

func consumeLines(ctx context.Context, wg *sync.WaitGroup, r io.Reader) <-chan string {
Expand Down Expand Up @@ -139,7 +141,9 @@ func (r *Runner) RunBackground() {
go func() {
err := root.Execute(ctx, cli)
if err != nil {
r.Logf("Error running command: %s", err)
if r.Verbose {
r.Logf("Error running command: %s", err)
}
}

// Close pipes to signal EOF.
Expand All @@ -154,15 +158,19 @@ func (r *Runner) RunBackground() {
// Make a copy of the buffer such that it remains "unread".
scanner := bufio.NewScanner(bytes.NewBuffer(r.stdout.Bytes()))
for scanner.Scan() {
r.Logf("[databricks stdout]: %s", scanner.Text())
if r.Verbose {
r.Logf("[databricks stdout]: %s", scanner.Text())
}
}
}

if r.stderr.Len() > 0 {
// Make a copy of the buffer such that it remains "unread".
scanner := bufio.NewScanner(bytes.NewBuffer(r.stderr.Bytes()))
for scanner.Scan() {
r.Logf("[databricks stderr]: %s", scanner.Text())
if r.Verbose {
r.Logf("[databricks stderr]: %s", scanner.Text())
}
}
}

Expand Down Expand Up @@ -196,26 +204,34 @@ func (r *Runner) Run() (bytes.Buffer, bytes.Buffer, error) {
cli.SetErr(&stderr)
cli.SetArgs(r.args)

r.Logf(" args: %s", strings.Join(r.args, ", "))
if r.Verbose {
r.Logf(" args: %s", strings.Join(r.args, ", "))
}

err := root.Execute(ctx, cli)
if err != nil {
r.Logf(" error: %s", err)
if r.Verbose {
r.Logf(" error: %s", err)
}
}

if stdout.Len() > 0 {
// Make a copy of the buffer such that it remains "unread".
scanner := bufio.NewScanner(bytes.NewBuffer(stdout.Bytes()))
for scanner.Scan() {
r.Logf("stdout: %s", scanner.Text())
if r.Verbose {
r.Logf("stdout: %s", scanner.Text())
}
}
}

if stderr.Len() > 0 {
// Make a copy of the buffer such that it remains "unread".
scanner := bufio.NewScanner(bytes.NewBuffer(stderr.Bytes()))
for scanner.Scan() {
r.Logf("stderr: %s", scanner.Text())
if r.Verbose {
r.Logf("stderr: %s", scanner.Text())
}
}
}

Expand Down Expand Up @@ -275,8 +291,9 @@ func NewRunner(t testutil.TestingT, ctx context.Context, args ...string) *Runner
return &Runner{
TestingT: t,

ctx: ctx,
args: args,
ctx: ctx,
args: args,
Verbose: true,
}
}

Expand Down
Loading