Skip to content

Commit

Permalink
Optionally use an input string to load report data
Browse files Browse the repository at this point in the history
Allow loading report data from a provided JSON string instead of loading
from a path on the filesystem.
  • Loading branch information
Bob Farrell committed Sep 12, 2020
1 parent 6fb63c3 commit 88a2419
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +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 |
| 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
15 changes: 8 additions & 7 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 Down Expand Up @@ -71,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 @@ -116,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
15 changes: 15 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,21 @@
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 = {
Expand Down

0 comments on commit 88a2419

Please sign in to comment.