Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for outputting HTML report to StringIO #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ gem install report_builder
| Option | Type | Default | Values |
|----------------------|-------------------------|---------------------|------------------------------------------------------------------------------------------|
| json_path/input_path | [String]/[Array]/[Hash] | (current directory) | input json files path / array of json files or path / hash of json files or path |
| json_string | [String] | nil | input json string containing report data (e.g. if report is loaded from a database) |
| report_path | [String] | 'test_report' | reports output file path with file name without extension |
| json_report_path | [String] | (report_path) | json report output file path with file name without extension |
| html_report_path | [String] | (report_path) | html report output file path with file name without extension |
| html_report_file | [StringIO]/[IO]/[File] | nil | html report output file object (can be used to bypass filesystem with StringIO) |
| retry_report_path | [String] | (report_path) | retry report output file path with file name without extension |
| report_types | [Array] | [:html] | :json, :html, :retry (output file types) |
| report_title | [String] | 'Test Results' | report and html title |
Expand Down
23 changes: 14 additions & 9 deletions lib/report_builder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Builder
def build_report
options = ReportBuilder.options

groups = get_groups options[:input_path]
groups = get_groups options[:input_path], options[:input_string]

json_report_path = options[:json_report_path] || options[:report_path]
if options[:report_types].include? 'JSON'
Expand All @@ -37,9 +37,13 @@ def build_report
end

html_report_path = options[:html_report_path] || options[:report_path]
html_report_file = options[:html_report_file]
if options[:report_types].include? 'HTML'
File.open(html_report_path + '.html', 'w') do |file|
file.write get(groups.size > 1 ? 'group_report' : 'report').result(binding)
html_content = get(groups.size > 1 ? 'group_report' : 'report').result(binding)
if html_report_file.nil?
File.open(html_report_path + '.html', 'w') { |file| file.write html_content }
else
html_report_file.write html_content
end
end

Expand Down Expand Up @@ -67,15 +71,16 @@ def get(template)
@erb[template] ||= ERB.new(File.read(File.dirname(__FILE__) + '/../../template/' + template + '.erb'), nil, nil, '_' + template)
end

def get_groups(input_path)
def get_groups(input_path, input_string)
groups = []
if input_path.is_a? Hash
groups << {'features' => get_features(nil, input_string)} unless input_string.nil?
if input_string.nil? && input_path.is_a?(Hash)
input_path.each do |group_name, group_path|
files = get_files group_path
puts "Error:: No file(s) found at #{group_path}" if files.empty?
groups << {'name' => group_name, 'features' => get_features(files)}
end
else
elsif input_string.nil?
files = get_files input_path
raise "Error:: No file(s) found at #{input_path}" if files.empty?
groups << {'features' => get_features(files)}
Expand Down Expand Up @@ -112,9 +117,9 @@ def get_files(path)
end.uniq
end

def get_features(files)
files.each_with_object([]) do |file, features|
data = File.read(file)
def get_features(files, input_string = nil)
(input_string.nil? ? files : [input_string]).each_with_object([]) do |file, features|
data = input_string.nil? ? File.read(file) : file
next if data.empty?
begin
features << JSON.parse(data)
Expand Down
33 changes: 33 additions & 0 deletions testing/rspec/spec/report_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,39 @@
expect(generated_report).to include(additional_css)
end

it 'can be configured to use JSON input string' do
input_string = File.read("#{TEST_FIXTURES_DIRECTORY}/json_reports/report.json")
output_directory = ReportBuilder::FileHelper.create_directory

generic_output_location = "#{output_directory}/report"

ReportBuilder.build_report(report_types: [:json, :html, :retry], input_string: input_string, report_path: generic_output_location)

files_created = Dir.entries(output_directory)
files_created.delete('.')
files_created.delete('..')

expect(files_created).to match_array(["#{File.basename(generic_output_location)}.json", "#{File.basename(generic_output_location)}.html", "#{File.basename(generic_output_location)}.retry"])
end

it 'can be configured to output report to a StringIO instance' do
string_io = StringIO.new
options = {
json_path: "#{TEST_FIXTURES_DIRECTORY}/json_reports",
html_report_file: string_io,
report_types: ['html'],
report_title: 'Test Results',
include_images: false,
additional_info: { Environment: 'POC' }
}

ReportBuilder.build_report options
string_io.rewind

expected_report = File.read("#{TEST_FIXTURES_DIRECTORY}/combined.html")
expect(string_io.read).to eql File.read("#{TEST_FIXTURES_DIRECTORY}/combined.html")
end

describe 'report configuration' do
it 'has a default configuration' do
expect(ReportBuilder.report_types).to eq([:html])
Expand Down