From 7db9f5149ac7f56613a32895b2674ddea960260d Mon Sep 17 00:00:00 2001 From: Andrejs Date: Wed, 24 Jan 2024 22:24:32 +0100 Subject: [PATCH] Add ability to set custom report name in final allure report (#633) * Allow to set custom report name * Set custom test report name * Correctly set report name with space * Improve option description --- .github/workflows/test.yml | 1 + README.md | 1 + .../commands/upload.rb | 5 ++- .../lib/report_generator.rb | 11 ++++-- .../lib/uploaders/_uploader.rb | 7 ++-- .../lib/report_generator_spec.rb | 35 ++++++++++++++----- .../lib/uploaders/common_uploader.rb | 4 ++- .../lib/uploaders/gcs_spec.rb | 2 +- .../lib/uploaders/s3_spec.rb | 24 +++++++++---- 9 files changed, 68 insertions(+), 22 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2b7e670d..5c8e6e64 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -121,6 +121,7 @@ jobs: --summary="behaviors" \ --summary-table-type="${{ matrix.table }}" \ --report-title="Test Report" \ + --report-name="Test Report" \ --copy-latest \ --color \ --debug diff --git a/README.md b/README.md index 8fb44db8..aa756312 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Options: --prefix=VALUE # Optional prefix for report path. Required: false --update-pr=VALUE # Add report url to PR via comment or description update. Required: false: (comment/description/actions) --report-title=VALUE # Title for url section in PR comment/description. Required: false, default: "Allure Report" + --report-name=VALUE # Custom report name in final Allure report. Required: false --summary=VALUE # Additionally add summary table to PR comment or description. Required: false: (behaviors/suites/packages/total), default: "total" --summary-table-type=VALUE # Summary table type. Required: false: (ascii/markdown), default: "ascii" --base-url=VALUE # Use custom base url instead of default cloud provider one. Required: false diff --git a/lib/allure_report_publisher/commands/upload.rb b/lib/allure_report_publisher/commands/upload.rb index 8657062a..325da894 100644 --- a/lib/allure_report_publisher/commands/upload.rb +++ b/lib/allure_report_publisher/commands/upload.rb @@ -29,6 +29,9 @@ class Upload < Dry::CLI::Command type: :string, default: "Allure Report", desc: "Title for url section in PR comment/description. Required: false" + option :report_name, + type: :string, + desc: "Custom report name in final Allure report. Required: false" option :summary, type: :string, desc: "Additionally add summary table to PR comment or description. Required: false", @@ -109,7 +112,7 @@ def call(**args) def uploader @uploader ||= uploaders(args[:type]).new( result_paths: @result_paths, - **args.slice(:bucket, :prefix, :base_url, :copy_latest) + **args.slice(:bucket, :prefix, :base_url, :copy_latest, :report_name) ) end diff --git a/lib/allure_report_publisher/lib/report_generator.rb b/lib/allure_report_publisher/lib/report_generator.rb index ed481c38..c5ffd712 100644 --- a/lib/allure_report_publisher/lib/report_generator.rb +++ b/lib/allure_report_publisher/lib/report_generator.rb @@ -10,8 +10,9 @@ class NoAllureResultsError < StandardError; end class ReportGenerator include Helpers - def initialize(result_paths) + def initialize(result_paths, report_name) @result_paths = result_paths.join(" ") + @report_name = report_name end # Generate allure report @@ -44,14 +45,18 @@ def report_path # @return [String] result paths string attr_reader :result_paths + # @return [String] custom report name + attr_reader :report_name # Generate allure report # # @return [void] def generate_report log_debug("Generating allure report") - cmd = "allure generate --clean --output #{report_path} #{common_info_path} #{result_paths}" - out = execute_shell(cmd) + cmd = ["allure generate --clean"] + cmd << "--report-name '#{report_name}'" if report_name + cmd << "--output #{report_path} #{common_info_path} #{result_paths}" + out = execute_shell(cmd.join(" ")) log_debug("Generated allure report. #{out}".strip) deduplicate_executors diff --git a/lib/allure_report_publisher/lib/uploaders/_uploader.rb b/lib/allure_report_publisher/lib/uploaders/_uploader.rb index d5858ef4..d5844979 100644 --- a/lib/allure_report_publisher/lib/uploaders/_uploader.rb +++ b/lib/allure_report_publisher/lib/uploaders/_uploader.rb @@ -30,12 +30,14 @@ class Uploader # @option args [String] :prefix # @option args [String] :base_url # @option args [String] :copy_latest + # @option args [String] :report_name def initialize(**args) @result_paths = args[:result_paths] @bucket_name = args[:bucket] @prefix = args[:prefix] @base_url = args[:base_url] @copy_latest = ci_info && args[:copy_latest] # copy latest for ci only + @report_name = args[:report_name] end # Execute allure report generation and upload @@ -94,7 +96,8 @@ def report_url :bucket_name, :prefix, :base_url, - :copy_latest + :copy_latest, + :report_name def_delegator :report_generator, :common_info_path @@ -155,7 +158,7 @@ def upload_latest_copy # # @return [Publisher::ReportGenerator] def report_generator - @report_generator ||= ReportGenerator.new(result_paths) + @report_generator ||= ReportGenerator.new(result_paths, report_name) end # Report path prefix diff --git a/spec/allure_report_publisher/lib/report_generator_spec.rb b/spec/allure_report_publisher/lib/report_generator_spec.rb index 86e79353..9fcdcc7c 100644 --- a/spec/allure_report_publisher/lib/report_generator_spec.rb +++ b/spec/allure_report_publisher/lib/report_generator_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Publisher::ReportGenerator, epic: "generator" do include ActiveSupport::Testing::TimeHelpers - subject(:report_generator) { described_class.new(result_paths) } + subject(:report_generator) { described_class.new(result_paths, report_name) } include_context "with mock helper" @@ -15,6 +15,7 @@ let(:report_dir) { File.join(tmpdir, "allure-report-#{Time.now.to_i}") } let(:status) { true } let(:tmpdir) { "/tmp/dir" } + let(:report_name) { nil } before do allow(Dir).to receive(:mktmpdir).with("allure-results") { common_info_dir } @@ -31,14 +32,32 @@ allow(File).to receive(:write).with("#{report_dir}/widgets/executors.json", deduped_executors) end - it "generates allure report" do - freeze_time do - report_generator.generate + context "without custom report name" do + it "generates allure report" do + freeze_time do + report_generator.generate + + expect(Open3).to have_received(:capture3).with( + "allure generate --clean --output #{report_dir} #{common_info_dir} #{result_paths.join(' ')}" + ) + expect(File).to have_received(:write).with("#{report_dir}/widgets/executors.json", deduped_executors) + end + end + end + + context "with custom report name" do + let(:report_name) { "custom_report_name" } + + it "generates allure report with custom name" do + freeze_time do + report_generator.generate - expect(Open3).to have_received(:capture3).with( - "allure generate --clean --output #{report_dir} #{common_info_dir} #{result_paths.join(' ')}" - ) - expect(File).to have_received(:write).with("#{report_dir}/widgets/executors.json", deduped_executors) + expect(Open3).to have_received(:capture3).with( + "allure generate --clean --report-name '#{report_name}' " \ + "--output #{report_dir} #{common_info_dir} #{result_paths.join(' ')}" + ) + expect(File).to have_received(:write).with("#{report_dir}/widgets/executors.json", deduped_executors) + end end end end diff --git a/spec/allure_report_publisher/lib/uploaders/common_uploader.rb b/spec/allure_report_publisher/lib/uploaders/common_uploader.rb index ad2bab76..67b1ba34 100644 --- a/spec/allure_report_publisher/lib/uploaders/common_uploader.rb +++ b/spec/allure_report_publisher/lib/uploaders/common_uploader.rb @@ -16,6 +16,7 @@ let(:base_url) { nil } let(:ci_provider) { nil } let(:run_id) { "123" } + let(:report_name) { nil } let(:history_files) do [ @@ -33,7 +34,8 @@ bucket: bucket_name, prefix: prefix, base_url: base_url, - copy_latest: false + copy_latest: false, + report_name: report_name } end diff --git a/spec/allure_report_publisher/lib/uploaders/gcs_spec.rb b/spec/allure_report_publisher/lib/uploaders/gcs_spec.rb index a44fd01a..3ca981ab 100644 --- a/spec/allure_report_publisher/lib/uploaders/gcs_spec.rb +++ b/spec/allure_report_publisher/lib/uploaders/gcs_spec.rb @@ -42,7 +42,7 @@ described_class.new(**args).execute aggregate_failures do - expect(Publisher::ReportGenerator).to have_received(:new).with(result_paths) + expect(Publisher::ReportGenerator).to have_received(:new).with(result_paths, report_name) expect(report_generator).to have_received(:generate) end end diff --git a/spec/allure_report_publisher/lib/uploaders/s3_spec.rb b/spec/allure_report_publisher/lib/uploaders/s3_spec.rb index b0a93cec..c85ff40f 100644 --- a/spec/allure_report_publisher/lib/uploaders/s3_spec.rb +++ b/spec/allure_report_publisher/lib/uploaders/s3_spec.rb @@ -52,6 +52,17 @@ allow(File).to receive(:new).with(Pathname.new(report_latest[:body].path)) { report_latest[:body] } end + shared_examples "report generator" do + it "generates allure report" do + described_class.new(**args).execute + + aggregate_failures do + expect(Publisher::ReportGenerator).to have_received(:new).with(result_paths, report_name) + expect(report_generator).to have_received(:generate) + end + end + end + context "with missing aws credentials" do let(:err_msg) do Pastel.new(enabled: true).decorate(<<~MSG.strip, :red) @@ -79,13 +90,14 @@ ClimateControl.modify(GITHUB_WORKFLOW: nil, GITLAB_CI: nil) { example.run } end - it "generates allure report" do - described_class.new(**args).execute + context "without custom report name" do + it_behaves_like "report generator" + end - aggregate_failures do - expect(Publisher::ReportGenerator).to have_received(:new).with(result_paths) - expect(report_generator).to have_received(:generate) - end + context "with custom report name" do + let(:report_name) { "custom_report_name" } + + it_behaves_like "report generator" end it "uploads allure report to s3" do