diff --git a/README.md b/README.md index 01acfb3..c757af3 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/lib/report_builder/builder.rb b/lib/report_builder/builder.rb index f378be8..2f4b472 100644 --- a/lib/report_builder/builder.rb +++ b/lib/report_builder/builder.rb @@ -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' @@ -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 @@ -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)} @@ -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) diff --git a/testing/rspec/spec/report_builder_spec.rb b/testing/rspec/spec/report_builder_spec.rb index f807674..4f5a3a9 100644 --- a/testing/rspec/spec/report_builder_spec.rb +++ b/testing/rspec/spec/report_builder_spec.rb @@ -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])