diff options
author | Ash McKenzie <amckenzie@gitlab.com> | 2019-04-02 17:09:25 +1100 |
---|---|---|
committer | Ash McKenzie <amckenzie@gitlab.com> | 2019-04-04 13:32:30 +1100 |
commit | 3a79ac9d615e492af0d3f1e9545262e2ecf162aa (patch) | |
tree | 446c94dd0e9405ffd3090c4f840a4c7ebc3bebfb | |
parent | a3bdb3ef8ccbb0601c79882480b48bde85611509 (diff) | |
download | gitlab-shell-3a79ac9d615e492af0d3f1e9545262e2ecf162aa.tar.gz |
New ConsoleHelper module
.write_stderr
.format_for_stderr
-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 |