Skip to content

Commit

Permalink
Add CleanedOutput class
Browse files Browse the repository at this point in the history
This class is responsible for taking the output from an external command
and cleaning it up ready to be included in a GitHub comment.
  • Loading branch information
chrismytton committed Feb 8, 2017
1 parent fcf7527 commit c98c8ff
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/cleaned_output.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'colorize'

class CleanedOutput
def initialize(output:)
@output = output
end

def to_s
cleaned_output[-64_000..-1] || cleaned_output
end

private

def cleaned_output
output.gsub(morph_api_key, 'REDACTED').uncolorize
end

def morph_api_key
ERB::Util.url_encode(ENV['MORPH_API_KEY'])
end

attr_reader :output
end
45 changes: 45 additions & 0 deletions test/cleaned_output_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true
require 'test_helper'

describe 'CleanedOutput' do
describe 'basic output' do
subject { CleanedOutput.new(output: 'Hello, world') }

it 'returns it untouched' do
subject.to_s.must_equal 'Hello, world'
end
end

describe 'output containing Morph API key' do
before do
@old_key = ENV['MORPH_API_KEY']
ENV['MORPH_API_KEY'] = 'test-morph-api-key'
end

after { ENV['MORPH_API_KEY'] = @old_key }

subject { CleanedOutput.new(output: 'Key: test-morph-api-key') }

it 'removes the key from the output' do
subject.to_s.must_equal 'Key: REDACTED'
end
end

describe 'with color escape codes in output' do
let(:output) { 'Test' }
subject { CleanedOutput.new(output: output) }

it 'strips escape codes' do
subject.to_s.must_equal 'Test'
end
end

describe 'large output' do
let(:output) { 'x' * 100_000 }
subject { CleanedOutput.new(output: output) }

it 'only returns that last 64k' do
subject.to_s.size.must_equal 64_000
end
end
end

0 comments on commit c98c8ff

Please sign in to comment.