From 9971c07c14aa9fc296c911bc47710d2d09df8e69 Mon Sep 17 00:00:00 2001 From: Kaoru Shirai <475350+kaorukobo@users.noreply.github.com> Date: Sat, 13 Sep 2025 14:12:15 +0900 Subject: [PATCH 1/5] Add tag option --- CHANGELOG.md | 5 ++ README.md | 27 +++++-- lib/tty/command.rb | 4 +- lib/tty/command/cmd.rb | 9 ++- lib/tty/command/printers/pretty.rb | 7 +- spec/unit/run_spec.rb | 110 ++++++++++++++++++++++------- 6 files changed, 129 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3feb3e..b98b998 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change log +## [Unreleased] + +### Added +* Add :tag option to add custom tags to command output + ## [v0.10.1] - 2021-02-14 ### Fixed diff --git a/README.md b/README.md index 022717f..e1255b1 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,9 @@ Or install it yourself as: * [2.3. Logging](#23-logging) * [2.3.1. Color](#231-color) * [2.3.2. UUID](#232-uuid) - * [2.3.3. Only output on error](#233-only-output-on-error) - * [2.3.4. Verbose](#234-verbose) + * [2.3.3. Tag](#233-tag) + * [2.3.4. Only output on error](#234-only-output-on-error) + * [2.3.5. Verbose](#235-verbose) * [2.4. Dry run](#24-dry-run) * [2.5. Wait](#25-wait) * [2.6. Test](#26-test) @@ -221,7 +222,25 @@ cmd.run("echo hello", uuid: false) # Finished in 0.003 seconds with exit status 0 (successful) ``` -#### 2.3.3 Only output on error +#### 2.3.3 Tag + +You can add custom tags to command output using the `:tag` option. This is useful for categorizing or identifying a specific instance or run of a command: + +```ruby +cmd = TTY::Command.new(tag: "deploy") +cmd.run("echo hello") + +# or individually per command run: +cmd = TTY::Command.new +cmd.run("echo hello", tag: "deploy") + +# => +# [deploy] Running echo hello +# hello +# [deploy] Finished in 0.003 seconds with exit status 0 (successful) +``` + +#### 2.3.4 Only output on error When using a command that can fail, setting `:only_output_on_error` option to `true` hides the output if the command succeeds: @@ -254,7 +273,7 @@ will also print the output. *Setting this option will cause the output to show at once, at the end of the command.* -#### 2.3.4 Verbose +#### 2.3.5 Verbose By default commands will produce warnings when, for example `pty` option is not supported on a given platform. You can switch off such warnings with `:verbose` option set to `false`. diff --git a/lib/tty/command.rb b/lib/tty/command.rb index 86880d2..798f433 100644 --- a/lib/tty/command.rb +++ b/lib/tty/command.rb @@ -55,14 +55,16 @@ def initialize(**options) @output = options.fetch(:output) { $stdout } @color = options.fetch(:color) { true } @uuid = options.fetch(:uuid) { true } + @tag = options[:tag] @printer_name = options.fetch(:printer) { :pretty } @dry_run = options.fetch(:dry_run) { false } - @printer = use_printer(@printer_name, color: @color, uuid: @uuid) + @printer = use_printer(@printer_name, color: @color, uuid: @uuid, tag: @tag) @cmd_options = {} @cmd_options[:verbose] = options.fetch(:verbose, true) @cmd_options[:pty] = true if options[:pty] @cmd_options[:binmode] = true if options[:binmode] @cmd_options[:timeout] = options[:timeout] if options[:timeout] + @cmd_options[:tag] = @tag if @tag end # Start external executable in a child process diff --git a/lib/tty/command/cmd.rb b/lib/tty/command/cmd.rb index 6b76723..f8cbf88 100644 --- a/lib/tty/command/cmd.rb +++ b/lib/tty/command/cmd.rb @@ -123,6 +123,12 @@ def group(value) def with_clean_env end + # The command tag + # @api public + def tag + @options[:tag] + end + # Assemble full command # # @api public @@ -140,7 +146,8 @@ def to_hash { command: command, argv: argv, - uuid: uuid + uuid: uuid, + tag: @options[:tag] } end diff --git a/lib/tty/command/printers/pretty.rb b/lib/tty/command/printers/pretty.rb index 0e79b7b..8fb474d 100644 --- a/lib/tty/command/printers/pretty.rb +++ b/lib/tty/command/printers/pretty.rb @@ -9,6 +9,7 @@ class Pretty < Abstract def initialize(*) super @uuid = options.fetch(:uuid, true) + @tag = options[:tag] end def print_command_start(cmd, *args) @@ -46,10 +47,10 @@ def print_command_exit(cmd, status, runtime, *args) def write(cmd, message, data = nil) cmd_set_uuid = cmd.options.fetch(:uuid, true) uuid_needed = cmd.options[:uuid].nil? ? @uuid : cmd_set_uuid + prefix = cmd.tag || @tag || (uuid_needed ? cmd.uuid : nil) + out = [] - if uuid_needed - out << "[#{decorate(cmd.uuid, :green)}] " unless cmd.uuid.nil? - end + out << "[#{decorate(prefix, :green)}] " if prefix out << "#{message}\n" target = (cmd.only_output_on_error && !data.nil?) ? data : output target << out.join diff --git a/spec/unit/run_spec.rb b/spec/unit/run_spec.rb index 90593b5..06b0a13 100644 --- a/spec/unit/run_spec.rb +++ b/spec/unit/run_spec.rb @@ -48,36 +48,98 @@ ]) end - it "runs command successfully with logging without uuid set globally" do - output = StringIO.new - command = TTY::Command.new(output: output, uuid: false) + # TODO: + # Move "with xxx option" context to the end of + # the describe block to follow RSpec style guide. + + context "with uuid option" do + it "runs command successfully with logging without uuid set globally" do + output = StringIO.new + command = TTY::Command.new(output: output, uuid: false) + + command.run(:echo, "hello") + output.rewind + + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq([ + "Running \e[33;1mecho hello\e[0m\n", + "\thello\n", + "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n" + ]) + end - command.run(:echo, "hello") - output.rewind + it "runs command successfully with logging without uuid set locally" do + output = StringIO.new + command = TTY::Command.new(output: output) - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq([ - "Running \e[33;1mecho hello\e[0m\n", - "\thello\n", - "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n" - ]) + command.run(:echo, "hello", uuid: false) + output.rewind + + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq([ + "Running \e[33;1mecho hello\e[0m\n", + "\thello\n", + "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n" + ]) + end end - it "runs command successfully with logging without uuid set locally" do - output = StringIO.new - command = TTY::Command.new(output: output) + context "with tag option" do + it "prints the tag set globally" do + output = StringIO.new + tag = "task" + command = TTY::Command.new(output: output, tag: tag) + + command.run(:echo, "hello") + + output.rewind + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq([ + "[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n", + "[\e[32m#{tag}\e[0m] \thello\n", + "[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \ + "(\e[32;1msuccessful\e[0m)\n" + ]) + end - command.run(:echo, "hello", uuid: false) - output.rewind + it "prints the tag set locally" do + output = StringIO.new + tag = "task" + command = TTY::Command.new(output: output) + + command.run(:echo, "hello", tag: tag) + + output.rewind + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq([ + "[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n", + "[\e[32m#{tag}\e[0m] \thello\n", + "[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \ + "(\e[32;1msuccessful\e[0m)\n" + ]) + end - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq([ - "Running \e[33;1mecho hello\e[0m\n", - "\thello\n", - "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n" - ]) + it "prints the tag even if uuid is set to false" do + output = StringIO.new + tag = "task" + command = TTY::Command.new(output: output, tag: tag, uuid: false) + + command.run(:echo, "hello") + + output.rewind + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq([ + "[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n", + "[\e[32m#{tag}\e[0m] \thello\n", + "[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \ + "(\e[32;1msuccessful\e[0m)\n" + ]) + end end it "runs command and fails with logging" do From 8854ff9f254c850c75ba03c93c30518fc312d386 Mon Sep 17 00:00:00 2001 From: Kaoru Shirai <475350+kaorukobo@users.noreply.github.com> Date: Sat, 13 Sep 2025 15:02:05 +0900 Subject: [PATCH 2/5] refactor: DRY up duplicated expected log lines in run_spec.rb --- spec/unit/run_spec.rb | 61 +++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/spec/unit/run_spec.rb b/spec/unit/run_spec.rb index 06b0a13..c4d0caa 100644 --- a/spec/unit/run_spec.rb +++ b/spec/unit/run_spec.rb @@ -62,11 +62,9 @@ lines = output.readlines lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq([ - "Running \e[33;1mecho hello\e[0m\n", - "\thello\n", - "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n" - ]) + expect(lines).to eq( + generic_colored_log_lines(prefix: nil) + ) end it "runs command successfully with logging without uuid set locally" do @@ -78,11 +76,9 @@ lines = output.readlines lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq([ - "Running \e[33;1mecho hello\e[0m\n", - "\thello\n", - "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n" - ]) + expect(lines).to eq( + generic_colored_log_lines(prefix: nil) + ) end end @@ -97,12 +93,9 @@ output.rewind lines = output.readlines lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq([ - "[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n", - "[\e[32m#{tag}\e[0m] \thello\n", - "[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" - ]) + expect(lines).to eq( + generic_colored_log_lines(prefix: tag) + ) end it "prints the tag set locally" do @@ -115,12 +108,9 @@ output.rewind lines = output.readlines lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq([ - "[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n", - "[\e[32m#{tag}\e[0m] \thello\n", - "[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" - ]) + expect(lines).to eq( + generic_colored_log_lines(prefix: tag) + ) end it "prints the tag even if uuid is set to false" do @@ -133,12 +123,9 @@ output.rewind lines = output.readlines lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq([ - "[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n", - "[\e[32m#{tag}\e[0m] \thello\n", - "[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" - ]) + expect(lines).to eq( + generic_colored_log_lines(prefix: tag) + ) end end @@ -246,4 +233,22 @@ lines = output.readlines expect(lines[0]).to include("Running \e[33;1mecho hello\e[0m\n") end + + # Generates the expected log lines in colored mode, with/without `[prefix]` + def generic_colored_log_lines(prefix: nil) + if prefix + [ + "[\e[32m#{prefix}\e[0m] Running \e[33;1mecho hello\e[0m\n", + "[\e[32m#{prefix}\e[0m] \thello\n", + "[\e[32m#{prefix}\e[0m] Finished in x seconds with exit status 0 " \ + "(\e[32;1msuccessful\e[0m)\n" + ] + else + [ + "Running \e[33;1mecho hello\e[0m\n", + "\thello\n", + "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n" + ] + end + end end From fb8899a08719bc94885fb7965fa2c7608b44a9aa Mon Sep 17 00:00:00 2001 From: Kaoru Shirai <475350+kaorukobo@users.noreply.github.com> Date: Sat, 13 Sep 2025 15:08:32 +0900 Subject: [PATCH 3/5] refactor: Rearrange context blocks in run_spec.rb --- spec/unit/run_spec.rb | 168 +++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 86 deletions(-) diff --git a/spec/unit/run_spec.rb b/spec/unit/run_spec.rb index c4d0caa..006b695 100644 --- a/spec/unit/run_spec.rb +++ b/spec/unit/run_spec.rb @@ -26,7 +26,7 @@ "[\e[32m#{uuid}\e[0m] Running \e[33;1mecho hello\e[0m\n", "[\e[32m#{uuid}\e[0m] \thello\n", "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" + "(\e[32;1msuccessful\e[0m)\n" ]) end @@ -48,87 +48,6 @@ ]) end - # TODO: - # Move "with xxx option" context to the end of - # the describe block to follow RSpec style guide. - - context "with uuid option" do - it "runs command successfully with logging without uuid set globally" do - output = StringIO.new - command = TTY::Command.new(output: output, uuid: false) - - command.run(:echo, "hello") - output.rewind - - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq( - generic_colored_log_lines(prefix: nil) - ) - end - - it "runs command successfully with logging without uuid set locally" do - output = StringIO.new - command = TTY::Command.new(output: output) - - command.run(:echo, "hello", uuid: false) - output.rewind - - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq( - generic_colored_log_lines(prefix: nil) - ) - end - end - - context "with tag option" do - it "prints the tag set globally" do - output = StringIO.new - tag = "task" - command = TTY::Command.new(output: output, tag: tag) - - command.run(:echo, "hello") - - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq( - generic_colored_log_lines(prefix: tag) - ) - end - - it "prints the tag set locally" do - output = StringIO.new - tag = "task" - command = TTY::Command.new(output: output) - - command.run(:echo, "hello", tag: tag) - - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq( - generic_colored_log_lines(prefix: tag) - ) - end - - it "prints the tag even if uuid is set to false" do - output = StringIO.new - tag = "task" - command = TTY::Command.new(output: output, tag: tag, uuid: false) - - command.run(:echo, "hello") - - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") - expect(lines).to eq( - generic_colored_log_lines(prefix: tag) - ) - end - end - it "runs command and fails with logging" do non_zero_exit = fixtures_path("non_zero_exit") output = StringIO.new @@ -145,7 +64,7 @@ "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{non_zero_exit}\e[0m\n", "[\e[32m#{uuid}\e[0m] \tnooo\n", "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 1 " \ - "(\e[31;1mfailed\e[0m)\n" + "(\e[31;1mfailed\e[0m)\n" ]) end @@ -209,12 +128,12 @@ "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{phased_output}\e[0m\n", "[\e[32m#{uuid}\e[0m] \t..........\n", "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" + "(\e[32;1msuccessful\e[0m)\n" ]) end it "does not persist environment variables", - unless: RSpec::Support::OS.windows? do + unless: RSpec::Support::OS.windows? do output = StringIO.new command = TTY::Command.new(output: output) @@ -234,6 +153,83 @@ expect(lines[0]).to include("Running \e[33;1mecho hello\e[0m\n") end + context "with uuid option" do + it "runs command successfully with logging without uuid set globally" do + output = StringIO.new + command = TTY::Command.new(output: output, uuid: false) + + command.run(:echo, "hello") + output.rewind + + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq( + generic_colored_log_lines(prefix: nil) + ) + end + + it "runs command successfully with logging without uuid set locally" do + output = StringIO.new + command = TTY::Command.new(output: output) + + command.run(:echo, "hello", uuid: false) + output.rewind + + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq( + generic_colored_log_lines(prefix: nil) + ) + end + end + + context "with tag option" do + it "prints the tag set globally" do + output = StringIO.new + tag = "task" + command = TTY::Command.new(output: output, tag: tag) + + command.run(:echo, "hello") + + output.rewind + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq( + generic_colored_log_lines(prefix: tag) + ) + end + + it "prints the tag set locally" do + output = StringIO.new + tag = "task" + command = TTY::Command.new(output: output) + + command.run(:echo, "hello", tag: tag) + + output.rewind + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq( + generic_colored_log_lines(prefix: tag) + ) + end + + it "prints the tag even if uuid is set to false" do + output = StringIO.new + tag = "task" + command = TTY::Command.new(output: output, tag: tag, uuid: false) + + command.run(:echo, "hello") + + output.rewind + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + expect(lines).to eq( + generic_colored_log_lines(prefix: tag) + ) + end + end + # Generates the expected log lines in colored mode, with/without `[prefix]` def generic_colored_log_lines(prefix: nil) if prefix @@ -241,7 +237,7 @@ def generic_colored_log_lines(prefix: nil) "[\e[32m#{prefix}\e[0m] Running \e[33;1mecho hello\e[0m\n", "[\e[32m#{prefix}\e[0m] \thello\n", "[\e[32m#{prefix}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" + "(\e[32;1msuccessful\e[0m)\n" ] else [ From d3f0d413f0246819f1b7bf3169ac31f465607bc1 Mon Sep 17 00:00:00 2001 From: Kaoru Shirai <475350+kaorukobo@users.noreply.github.com> Date: Sat, 13 Sep 2025 16:10:23 +0900 Subject: [PATCH 4/5] refactor: DRY up command output retrieval logic in run_spec.rb --- spec/unit/run_spec.rb | 104 +++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/spec/unit/run_spec.rb b/spec/unit/run_spec.rb index 006b695..b3165fd 100644 --- a/spec/unit/run_spec.rb +++ b/spec/unit/run_spec.rb @@ -12,16 +12,14 @@ end it "runs command successfully with logging" do - output = StringIO.new uuid = "xxxx" allow(SecureRandom).to receive(:uuid).and_return(uuid) - command = TTY::Command.new(output: output) - command.run(:echo, "hello") + lines = retrieve_log_lines do |output| + command = TTY::Command.new(output: output) + command.run(:echo, "hello") + end - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq([ "[\e[32m#{uuid}\e[0m] Running \e[33;1mecho hello\e[0m\n", "[\e[32m#{uuid}\e[0m] \thello\n", @@ -31,16 +29,14 @@ end it "runs command successfully with logging without color" do - output = StringIO.new uuid = "xxxx" allow(SecureRandom).to receive(:uuid).and_return(uuid) - command = TTY::Command.new(output: output, color: false) - command.run(:echo, "hello") + lines = retrieve_log_lines do |output| + command = TTY::Command.new(output: output, color: false) + command.run(:echo, "hello") + end - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq([ "[#{uuid}] Running echo hello\n", "[#{uuid}] \thello\n", @@ -50,16 +46,14 @@ it "runs command and fails with logging" do non_zero_exit = fixtures_path("non_zero_exit") - output = StringIO.new uuid = "xxxx" allow(SecureRandom).to receive(:uuid).and_return(uuid) - command = TTY::Command.new(output: output) - command.run!("ruby #{non_zero_exit}") + lines = retrieve_log_lines do |output| + command = TTY::Command.new(output: output) + command.run!("ruby #{non_zero_exit}") + end - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq([ "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{non_zero_exit}\e[0m\n", "[\e[32m#{uuid}\e[0m] \tnooo\n", @@ -113,17 +107,16 @@ phased_output = fixtures_path("phased_output") uuid = "xxxx" allow(SecureRandom).to receive(:uuid).and_return(uuid) - output = StringIO.new - cmd = TTY::Command.new(output: output) - out, err = cmd.run("ruby #{phased_output}") + out = err = nil + lines = retrieve_log_lines do |output| + cmd = TTY::Command.new(output: output) + out, err = cmd.run("ruby #{phased_output}") + end expect(out).to eq("." * 10) expect(err).to eq("") - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq([ "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{phased_output}\e[0m\n", "[\e[32m#{uuid}\e[0m] \t..........\n", @@ -155,28 +148,22 @@ context "with uuid option" do it "runs command successfully with logging without uuid set globally" do - output = StringIO.new - command = TTY::Command.new(output: output, uuid: false) + lines = retrieve_log_lines do |output| + command = TTY::Command.new(output: output, uuid: false) + command.run(:echo, "hello") + end - command.run(:echo, "hello") - output.rewind - - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq( generic_colored_log_lines(prefix: nil) ) end it "runs command successfully with logging without uuid set locally" do - output = StringIO.new - command = TTY::Command.new(output: output) + lines = retrieve_log_lines do |output| + command = TTY::Command.new(output: output) + command.run(:echo, "hello", uuid: false) + end - command.run(:echo, "hello", uuid: false) - output.rewind - - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq( generic_colored_log_lines(prefix: nil) ) @@ -185,51 +172,56 @@ context "with tag option" do it "prints the tag set globally" do - output = StringIO.new tag = "task" - command = TTY::Command.new(output: output, tag: tag) - command.run(:echo, "hello") + lines = retrieve_log_lines do |output| + command = TTY::Command.new(output: output, tag: tag) + command.run(:echo, "hello") + end - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq( generic_colored_log_lines(prefix: tag) ) end it "prints the tag set locally" do - output = StringIO.new tag = "task" - command = TTY::Command.new(output: output) - command.run(:echo, "hello", tag: tag) + lines = retrieve_log_lines do |output| + command = TTY::Command.new(output: output) + command.run(:echo, "hello", tag: tag) + end - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq( generic_colored_log_lines(prefix: tag) ) end it "prints the tag even if uuid is set to false" do - output = StringIO.new tag = "task" - command = TTY::Command.new(output: output, tag: tag, uuid: false) - command.run(:echo, "hello") + lines = retrieve_log_lines do |output| + command = TTY::Command.new(output: output, tag: tag, uuid: false) + command.run(:echo, "hello") + end - output.rewind - lines = output.readlines - lines.last.gsub!(/\d+\.\d+/, "x") expect(lines).to eq( generic_colored_log_lines(prefix: tag) ) end end + # Retrieves log lines from the output produced within the given block. + # Also replaces the execution time portion in the output with `x`. + def retrieve_log_lines + output = StringIO.new + yield(output) + output.rewind + lines = output.readlines + lines.last.gsub!(/\d+\.\d+/, "x") + lines + end + # Generates the expected log lines in colored mode, with/without `[prefix]` def generic_colored_log_lines(prefix: nil) if prefix From 4ce2b8c281e2d3268bacfd43676c2a8f65082004 Mon Sep 17 00:00:00 2001 From: Kaoru Shirai <475350+kaorukobo@users.noreply.github.com> Date: Sat, 13 Sep 2025 15:54:51 +0900 Subject: [PATCH 5/5] style: reformat code per houndci-bot suggestions --- lib/tty/command.rb | 4 +++- spec/unit/run_spec.rb | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/tty/command.rb b/lib/tty/command.rb index 798f433..c117b56 100644 --- a/lib/tty/command.rb +++ b/lib/tty/command.rb @@ -58,7 +58,9 @@ def initialize(**options) @tag = options[:tag] @printer_name = options.fetch(:printer) { :pretty } @dry_run = options.fetch(:dry_run) { false } - @printer = use_printer(@printer_name, color: @color, uuid: @uuid, tag: @tag) + @printer = use_printer( + @printer_name, color: @color, uuid: @uuid, tag: @tag + ) @cmd_options = {} @cmd_options[:verbose] = options.fetch(:verbose, true) @cmd_options[:pty] = true if options[:pty] diff --git a/spec/unit/run_spec.rb b/spec/unit/run_spec.rb index b3165fd..d5ad849 100644 --- a/spec/unit/run_spec.rb +++ b/spec/unit/run_spec.rb @@ -24,7 +24,7 @@ "[\e[32m#{uuid}\e[0m] Running \e[33;1mecho hello\e[0m\n", "[\e[32m#{uuid}\e[0m] \thello\n", "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" + "(\e[32;1msuccessful\e[0m)\n" ]) end @@ -58,7 +58,7 @@ "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{non_zero_exit}\e[0m\n", "[\e[32m#{uuid}\e[0m] \tnooo\n", "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 1 " \ - "(\e[31;1mfailed\e[0m)\n" + "(\e[31;1mfailed\e[0m)\n" ]) end @@ -121,12 +121,12 @@ "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{phased_output}\e[0m\n", "[\e[32m#{uuid}\e[0m] \t..........\n", "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" + "(\e[32;1msuccessful\e[0m)\n" ]) end it "does not persist environment variables", - unless: RSpec::Support::OS.windows? do + unless: RSpec::Support::OS.windows? do output = StringIO.new command = TTY::Command.new(output: output) @@ -229,7 +229,7 @@ def generic_colored_log_lines(prefix: nil) "[\e[32m#{prefix}\e[0m] Running \e[33;1mecho hello\e[0m\n", "[\e[32m#{prefix}\e[0m] \thello\n", "[\e[32m#{prefix}\e[0m] Finished in x seconds with exit status 0 " \ - "(\e[32;1msuccessful\e[0m)\n" + "(\e[32;1msuccessful\e[0m)\n" ] else [