Skip to content

Commit

Permalink
Add NoLog option on testcli.Runner (#2183)
Browse files Browse the repository at this point in the history
## Changes
Setting Verbose=false on testcli.Runner disables all logging related to
running process (stdout, stderr, error, args).

I'm using this in #2184 where I'm using testcli runner to run acceptance
tests and seeing all output is not useful.

## Tests
Manually inspecting test output in #2184
  • Loading branch information
denik authored Jan 20, 2025
1 parent 26f527e commit 64fc1c8
Showing 1 changed file with 26 additions and 9 deletions.
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

0 comments on commit 64fc1c8

Please sign in to comment.