diff --git a/lean/commands/report.py b/lean/commands/report.py index 6a5bc91b..e0fae125 100644 --- a/lean/commands/report.py +++ b/lean/commands/report.py @@ -251,7 +251,10 @@ def report(backtest_results: Optional[Path], logger.info(f"Custom HTML template file '{html}' could not be found") if pdf: - run_options["commands"].append(f'cp /tmp/report.pdf "/Output/{report_destination.name.replace(".html", ".pdf")}"') + pdf_destination = report_destination.with_suffix(".pdf") + if report_destination.suffix.lower() == ".pdf": + pdf_destination = report_destination.with_name(report_destination.name + ".pdf") + run_options["commands"].append(f'cp /tmp/report.pdf "/Output/{pdf_destination.name}"') if live_results is not None: run_options["mounts"].append(Mount(target="/Lean/Report/bin/Debug/live-data-source-file.json", source=str(live_results), diff --git a/tests/commands/test_report.py b/tests/commands/test_report.py index b8368dfd..abb01ddc 100644 --- a/tests/commands/test_report.py +++ b/tests/commands/test_report.py @@ -613,3 +613,25 @@ def test_report_runs_custom_image_when_given_as_option() -> None: args, kwargs = docker_manager.run_image.call_args assert args[0] == DockerImage(name="custom/lean", tag="456") + + +@pytest.mark.parametrize("destination, pdf_destination", [ + ("report.html", "report.pdf"), + ("report.HTML", "report.pdf"), + ("my-report", "my-report.pdf"), + ("report.html.backtest.html", "report.html.backtest.pdf"), + ("report.pdf", "report.pdf.pdf"), + ("report.PDF", "report.PDF.pdf"), +]) +def test_report_exports_pdf_to_separate_destination(destination: str, pdf_destination: str) -> None: + docker_manager = mock.Mock() + docker_manager.run_image.side_effect = run_image + initialize_container(docker_manager_to_use=docker_manager) + + result = CliRunner().invoke(lean, ["report", "--pdf", "--report-destination", destination]) + + assert result.exit_code == 0 + commands = docker_manager.run_image.call_args.kwargs["commands"] + assert f'cp /tmp/report.html "/Output/{destination}"' in commands + assert f'cp /tmp/report.pdf "/Output/{pdf_destination}"' in commands + assert destination.lower() != pdf_destination.lower()