diff options
-rw-r--r-- | lib/console_helper.rb | 17 | ||||
-rw-r--r-- | spec/console_helper_spec.rb | 38 |
2 files changed, 55 insertions, 0 deletions
diff --git a/lib/console_helper.rb b/lib/console_helper.rb new file mode 100644 index 0000000..5c8bb83 --- /dev/null +++ b/lib/console_helper.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module ConsoleHelper + LINE_PREFACE = '> GitLab:' + + def write_stderr(messages) + format_for_stderr(messages).each do |message| + $stderr.puts(message) + end + end + + def format_for_stderr(messages) + Array(messages).each_with_object([]) do |message, all| + all << "#{LINE_PREFACE} #{message}" unless message.empty? + end + end +end diff --git a/spec/console_helper_spec.rb b/spec/console_helper_spec.rb new file mode 100644 index 0000000..cafc7cb --- /dev/null +++ b/spec/console_helper_spec.rb @@ -0,0 +1,38 @@ +require_relative 'spec_helper' +require_relative '../lib/console_helper' + +describe ConsoleHelper do + using RSpec::Parameterized::TableSyntax + + class DummyClass + include ConsoleHelper + end + + subject { DummyClass.new } + + describe '#write_stderr' do + where(:messages, :stderr_output) do + 'test' | "> GitLab: test\n" + %w{test1 test2} | "> GitLab: test1\n> GitLab: test2\n" + end + + with_them do + it 'puts to $stderr, prefaced with > GitLab:' do + expect { subject.write_stderr(messages) }.to output(stderr_output).to_stderr + end + end + end + + describe '#format_for_stderr' do + where(:messages, :result) do + 'test' | ['> GitLab: test'] + %w{test1 test2} | ['> GitLab: test1', '> GitLab: test2'] + end + + with_them do + it 'returns message(s), prefaced with > GitLab:' do + expect(subject.format_for_stderr(messages)).to eq(result) + end + end + end +end |