piotrmurach/tty-command

Calls to `run`/`run!` ignore `printer` option

Thomascountz opened this issue · 0 comments

Description

When calling Command#run/Command#run!, the printer option is passed to the new Cmd object, but is ignored when creating a new DryRunner or ProcessRunner. Instead, the current Command object's @printer is used.

def command(*args)
cmd = Cmd.new(*args)
cmd.update(@cmd_options)
cmd
end

def execute_command(cmd, &block)
dry_run = @dry_run || cmd.options[:dry_run] || false
@runner = select_runner(dry_run).new(cmd, @printer, &block)
@runner.run!
end

Steps to reproduce:

> cmd = TTY::Command.new
# => #<TTY::Command:0x000000011e8753d8

> cmd.run!("exit 1")
# [dbd10d8c] Running exit 1
# [dbd10d8c] Finished in 0.025 seconds with exit status 1 (failed)
# => #<TTY::Command::Result:0x0000000121277910 @err="", @out="", @runtime=0.02479, @status=1>

> cmd.run!("exit 1", printer: :null)
# [21e90172] Running exit 1
# [21e90172] Finished in 0.020 seconds with exit status 1 (failed)
# => #<TTY::Command::Result:0x000000012121c9e8 @err="", @out="", @runtime=0.020013, @status=1>

Expected behavior:

The printer option should be taken from the new Cmd object, allowing each call to run or run! to specify its own printer. The behavior of cmd.run!(command, printer: :null) should behave the same as if the original Command object was created with a Command::Printers::Null printer.

> cmd.run!("exit 1", printer: :null)
# => #<TTY::Command::Result:0x00000001034511a0 @err="", @out="", @runtime=0.023029, @status=1>```

Why?

This is useful for calls to run!, that allow the handling failures without raising, i.e., where non-zero exit codes are handled and don't want to be treated as a "failure". (Side note: it's especially especially useful for calls to tools like diff, which return non-zero exit codes to indicate things other than "failures").