Add NoLog option on testcli.Runner (#2183)

## 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
This commit is contained in:
Denis Bilenko 2025-01-20 09:57:48 +01:00 committed by GitHub
parent 26f527ef64
commit 64fc1c8fe7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 9 deletions

View File

@ -39,6 +39,8 @@ type Runner struct {
StderrLines <-chan string StderrLines <-chan string
errch <-chan error errch <-chan error
Verbose bool
} }
func consumeLines(ctx context.Context, wg *sync.WaitGroup, r io.Reader) <-chan string { func consumeLines(ctx context.Context, wg *sync.WaitGroup, r io.Reader) <-chan string {
@ -139,8 +141,10 @@ func (r *Runner) RunBackground() {
go func() { go func() {
err := root.Execute(ctx, cli) err := root.Execute(ctx, cli)
if err != nil { if err != nil {
if r.Verbose {
r.Logf("Error running command: %s", err) r.Logf("Error running command: %s", err)
} }
}
// Close pipes to signal EOF. // Close pipes to signal EOF.
stdoutW.Close() stdoutW.Close()
@ -154,17 +158,21 @@ func (r *Runner) RunBackground() {
// Make a copy of the buffer such that it remains "unread". // Make a copy of the buffer such that it remains "unread".
scanner := bufio.NewScanner(bytes.NewBuffer(r.stdout.Bytes())) scanner := bufio.NewScanner(bytes.NewBuffer(r.stdout.Bytes()))
for scanner.Scan() { for scanner.Scan() {
if r.Verbose {
r.Logf("[databricks stdout]: %s", scanner.Text()) r.Logf("[databricks stdout]: %s", scanner.Text())
} }
} }
}
if r.stderr.Len() > 0 { if r.stderr.Len() > 0 {
// Make a copy of the buffer such that it remains "unread". // Make a copy of the buffer such that it remains "unread".
scanner := bufio.NewScanner(bytes.NewBuffer(r.stderr.Bytes())) scanner := bufio.NewScanner(bytes.NewBuffer(r.stderr.Bytes()))
for scanner.Scan() { for scanner.Scan() {
if r.Verbose {
r.Logf("[databricks stderr]: %s", scanner.Text()) r.Logf("[databricks stderr]: %s", scanner.Text())
} }
} }
}
// Make caller aware of error. // Make caller aware of error.
errch <- err errch <- err
@ -196,28 +204,36 @@ func (r *Runner) Run() (bytes.Buffer, bytes.Buffer, error) {
cli.SetErr(&stderr) cli.SetErr(&stderr)
cli.SetArgs(r.args) cli.SetArgs(r.args)
if r.Verbose {
r.Logf(" args: %s", strings.Join(r.args, ", ")) r.Logf(" args: %s", strings.Join(r.args, ", "))
}
err := root.Execute(ctx, cli) err := root.Execute(ctx, cli)
if err != nil { if err != nil {
if r.Verbose {
r.Logf(" error: %s", err) r.Logf(" error: %s", err)
} }
}
if stdout.Len() > 0 { if stdout.Len() > 0 {
// Make a copy of the buffer such that it remains "unread". // Make a copy of the buffer such that it remains "unread".
scanner := bufio.NewScanner(bytes.NewBuffer(stdout.Bytes())) scanner := bufio.NewScanner(bytes.NewBuffer(stdout.Bytes()))
for scanner.Scan() { for scanner.Scan() {
if r.Verbose {
r.Logf("stdout: %s", scanner.Text()) r.Logf("stdout: %s", scanner.Text())
} }
} }
}
if stderr.Len() > 0 { if stderr.Len() > 0 {
// Make a copy of the buffer such that it remains "unread". // Make a copy of the buffer such that it remains "unread".
scanner := bufio.NewScanner(bytes.NewBuffer(stderr.Bytes())) scanner := bufio.NewScanner(bytes.NewBuffer(stderr.Bytes()))
for scanner.Scan() { for scanner.Scan() {
if r.Verbose {
r.Logf("stderr: %s", scanner.Text()) r.Logf("stderr: %s", scanner.Text())
} }
} }
}
return stdout, stderr, err return stdout, stderr, err
} }
@ -277,6 +293,7 @@ func NewRunner(t testutil.TestingT, ctx context.Context, args ...string) *Runner
ctx: ctx, ctx: ctx,
args: args, args: args,
Verbose: true,
} }
} }